]>
git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/protocol.cpp
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
7 // Copyright: (c) 1997, 1998 Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/protocol/protocol.h"
21 #include "wx/protocol/log.h"
24 #include "wx/module.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
38 wxProtoInfo::wxProtoInfo(const wxChar
*name
, const wxChar
*serv
,
39 const bool need_host1
, wxClassInfo
*info
)
44 m_needhost
= need_host1
;
46 next
= wxURL::ms_protocols
;
47 wxURL::ms_protocols
= this;
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
59 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxSocketClient
)
61 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxObject
)
64 wxProtocol::wxProtocol()
69 m_lastError
= wxPROTO_NOERR
;
71 SetDefaultTimeout(60); // default timeout is 60 seconds
74 void wxProtocol::SetDefaultTimeout(wxUint32 Value
)
76 m_uiDefaultTimeout
= Value
;
78 wxSocketBase::SetTimeout(Value
); // sets it for this socket
82 wxProtocol::~wxProtocol()
88 bool wxProtocol::Reconnect()
107 // ----------------------------------------------------------------------------
108 // Read a line from socket
109 // ----------------------------------------------------------------------------
112 wxProtocolError
wxProtocol::ReadLine(wxSocketBase
*sock
, wxString
& result
)
114 static const int LINE_BUF
= 4095;
118 wxCharBuffer
buf(LINE_BUF
);
119 char *pBuf
= buf
.data();
120 while ( sock
->WaitForRead() )
122 // peek at the socket to see if there is a CRLF
123 sock
->Peek(pBuf
, LINE_BUF
);
125 size_t nRead
= sock
->LastCount();
126 if ( !nRead
&& sock
->Error() )
127 return wxPROTO_NETERR
;
129 // look for "\r\n" paying attention to a special case: "\r\n" could
130 // have been split by buffer boundary, so check also for \r at the end
131 // of the last chunk and \n at the beginning of this one
133 const char *eol
= strchr(pBuf
, '\n');
135 // if we found '\n', is there a '\r' as well?
140 // check for case of "\r\n" being split
141 if ( result
.empty() || result
.Last() != wxT('\r') )
143 // ignore the stray '\n'
146 //else: ok, got real EOL
148 // read just this '\n' and restart
151 else // '\n' in the middle of the buffer
153 // in any case, read everything up to and including '\n'
154 nRead
= eol
- pBuf
+ 1;
156 if ( eol
[-1] != '\r' )
158 // as above, simply ignore stray '\n'
164 sock
->Read(pBuf
, nRead
);
165 if ( sock
->LastCount() != nRead
)
166 return wxPROTO_NETERR
;
169 result
+= wxString::FromAscii(pBuf
);
173 // remove trailing "\r\n"
174 result
.RemoveLast(2);
176 return wxPROTO_NOERR
;
180 return wxPROTO_NETERR
;
183 wxProtocolError
wxProtocol::ReadLine(wxString
& result
)
185 return ReadLine(this, result
);
188 #endif // wxUSE_SOCKETS
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 void wxProtocol::SetLog(wxProtocolLog
*log
)
200 void wxProtocol::LogRequest(const wxString
& str
)
203 m_log
->LogRequest(str
);
206 void wxProtocol::LogResponse(const wxString
& str
)
209 m_log
->LogResponse(str
);
212 void wxProtocolLog::DoLogString(const wxString
& str
)
214 wxUnusedVar(str
); // unused if wxLogTrace() is disabled
215 wxLogTrace(m_traceMask
, "%s", str
);
218 #endif // wxUSE_PROTOCOL