1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/protocol.cpp
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/protocol/protocol.h"
24 #include "wx/module.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
36 const bool need_host1, wxClassInfo *info)
41 m_needhost = need_host1;
43 next = wxURL::ms_protocols;
44 wxURL::ms_protocols = this;
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
56 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
58 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
61 wxProtocol::wxProtocol()
66 m_lastError = wxPROTO_NOERR;
67 SetDefaultTimeout(60); // default timeout is 60 seconds
71 bool wxProtocol::Reconnect()
90 void wxProtocol::SetDefaultTimeout(wxUint32 Value)
92 m_uiDefaultTimeout = Value;
94 wxSocketBase::SetTimeout(Value); // sets it for this socket
99 // ----------------------------------------------------------------------------
100 // Read a line from socket
101 // ----------------------------------------------------------------------------
104 wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
106 static const int LINE_BUF = 4095;
110 wxCharBuffer buf(LINE_BUF);
111 char *pBuf = buf.data();
112 while ( sock->WaitForRead() )
114 // peek at the socket to see if there is a CRLF
115 sock->Peek(pBuf, LINE_BUF);
117 size_t nRead = sock->LastCount();
118 if ( !nRead && sock->Error() )
119 return wxPROTO_NETERR;
121 // look for "\r\n" paying attention to a special case: "\r\n" could
122 // have been split by buffer boundary, so check also for \r at the end
123 // of the last chunk and \n at the beginning of this one
125 const char *eol = strchr(pBuf, '\n');
127 // if we found '\n', is there a '\r' as well?
132 // check for case of "\r\n" being split
133 if ( result.empty() || result.Last() != _T('\r') )
135 // ignore the stray '\n'
138 //else: ok, got real EOL
140 // read just this '\n' and restart
143 else // '\n' in the middle of the buffer
145 // in any case, read everything up to and including '\n'
146 nRead = eol - pBuf + 1;
148 if ( eol[-1] != '\r' )
150 // as above, simply ignore stray '\n'
156 sock->Read(pBuf, nRead);
157 if ( sock->LastCount() != nRead )
158 return wxPROTO_NETERR;
161 result += wxString::FromAscii(pBuf);
165 // remove trailing "\r\n"
166 result.RemoveLast(2);
168 return wxPROTO_NOERR;
172 return wxPROTO_NETERR;
175 wxProtocolError wxProtocol::ReadLine(wxString& result)
177 return ReadLine(this, result);
180 #endif // wxUSE_SOCKETS
182 #endif // wxUSE_PROTOCOL