]>
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
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"
22 #include "wx/protocol/log.h"
25 #include "wx/module.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 wxProtoInfo::wxProtoInfo(const wxChar
*name
, const wxChar
*serv
,
38 const bool need_host1
, wxClassInfo
*info
)
43 m_needhost
= need_host1
;
45 next
= wxURL::ms_protocols
;
46 wxURL::ms_protocols
= this;
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
58 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxSocketClient
)
60 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxObject
)
63 wxProtocol::wxProtocol()
68 m_lastError
= wxPROTO_NOERR
;
70 SetDefaultTimeout(60); // default timeout is 60 seconds
74 bool wxProtocol::Reconnect()
93 void wxProtocol::SetDefaultTimeout(wxUint32 Value
)
95 m_uiDefaultTimeout
= Value
;
97 wxSocketBase::SetTimeout(Value
); // sets it for this socket
101 wxProtocol::~wxProtocol()
106 // ----------------------------------------------------------------------------
107 // Read a line from socket
108 // ----------------------------------------------------------------------------
111 wxProtocolError
wxProtocol::ReadLine(wxSocketBase
*sock
, wxString
& result
)
113 static const int LINE_BUF
= 4095;
117 wxCharBuffer
buf(LINE_BUF
);
118 char *pBuf
= buf
.data();
119 while ( sock
->WaitForRead() )
121 // peek at the socket to see if there is a CRLF
122 sock
->Peek(pBuf
, LINE_BUF
);
124 size_t nRead
= sock
->LastCount();
125 if ( !nRead
&& sock
->Error() )
126 return wxPROTO_NETERR
;
128 // look for "\r\n" paying attention to a special case: "\r\n" could
129 // have been split by buffer boundary, so check also for \r at the end
130 // of the last chunk and \n at the beginning of this one
132 const char *eol
= strchr(pBuf
, '\n');
134 // if we found '\n', is there a '\r' as well?
139 // check for case of "\r\n" being split
140 if ( result
.empty() || result
.Last() != _T('\r') )
142 // ignore the stray '\n'
145 //else: ok, got real EOL
147 // read just this '\n' and restart
150 else // '\n' in the middle of the buffer
152 // in any case, read everything up to and including '\n'
153 nRead
= eol
- pBuf
+ 1;
155 if ( eol
[-1] != '\r' )
157 // as above, simply ignore stray '\n'
163 sock
->Read(pBuf
, nRead
);
164 if ( sock
->LastCount() != nRead
)
165 return wxPROTO_NETERR
;
168 result
+= wxString::FromAscii(pBuf
);
172 // remove trailing "\r\n"
173 result
.RemoveLast(2);
175 return wxPROTO_NOERR
;
179 return wxPROTO_NETERR
;
182 wxProtocolError
wxProtocol::ReadLine(wxString
& result
)
184 return ReadLine(this, result
);
187 #endif // wxUSE_SOCKETS
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 void wxProtocol::SetLog(wxProtocolLog
*log
)
199 void wxProtocol::LogRequest(const wxString
& str
)
202 m_log
->LogRequest(str
);
205 void wxProtocol::LogResponse(const wxString
& str
)
208 m_log
->LogResponse(str
);
211 void wxProtocolLog::DoLogString(const wxString
& str
)
213 wxUnusedVar(str
); // unused if wxLogTrace() is disabled
214 wxLogTrace(m_traceMask
, "%s", str
);
217 #endif // wxUSE_PROTOCOL