]>
git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
2dfb4b40ab04eac1b0653f478772c96aa7e49bfa
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"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 wxProtoInfo::wxProtoInfo(const wxChar
*name
, const wxChar
*serv
,
37 const bool need_host1
, wxClassInfo
*info
)
42 m_needhost
= need_host1
;
44 next
= wxURL::ms_protocols
;
45 wxURL::ms_protocols
= this;
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
57 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxSocketClient
)
59 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxObject
)
62 wxProtocol::wxProtocol()
67 m_lastError
= wxPROTO_NOERR
;
69 SetDefaultTimeout(60); // default timeout is 60 seconds
73 bool wxProtocol::Reconnect()
92 void wxProtocol::SetDefaultTimeout(wxUint32 Value
)
94 m_uiDefaultTimeout
= Value
;
96 wxSocketBase::SetTimeout(Value
); // sets it for this socket
100 wxProtocol::~wxProtocol()
105 // ----------------------------------------------------------------------------
106 // Read a line from socket
107 // ----------------------------------------------------------------------------
110 wxProtocolError
wxProtocol::ReadLine(wxSocketBase
*sock
, wxString
& result
)
112 static const int LINE_BUF
= 4095;
116 wxCharBuffer
buf(LINE_BUF
);
117 char *pBuf
= buf
.data();
118 while ( sock
->WaitForRead() )
120 // peek at the socket to see if there is a CRLF
121 sock
->Peek(pBuf
, LINE_BUF
);
123 size_t nRead
= sock
->LastCount();
124 if ( !nRead
&& sock
->Error() )
125 return wxPROTO_NETERR
;
127 // look for "\r\n" paying attention to a special case: "\r\n" could
128 // have been split by buffer boundary, so check also for \r at the end
129 // of the last chunk and \n at the beginning of this one
131 const char *eol
= strchr(pBuf
, '\n');
133 // if we found '\n', is there a '\r' as well?
138 // check for case of "\r\n" being split
139 if ( result
.empty() || result
.Last() != _T('\r') )
141 // ignore the stray '\n'
144 //else: ok, got real EOL
146 // read just this '\n' and restart
149 else // '\n' in the middle of the buffer
151 // in any case, read everything up to and including '\n'
152 nRead
= eol
- pBuf
+ 1;
154 if ( eol
[-1] != '\r' )
156 // as above, simply ignore stray '\n'
162 sock
->Read(pBuf
, nRead
);
163 if ( sock
->LastCount() != nRead
)
164 return wxPROTO_NETERR
;
167 result
+= wxString::FromAscii(pBuf
);
171 // remove trailing "\r\n"
172 result
.RemoveLast(2);
174 return wxPROTO_NOERR
;
178 return wxPROTO_NETERR
;
181 wxProtocolError
wxProtocol::ReadLine(wxString
& result
)
183 return ReadLine(this, result
);
186 #endif // wxUSE_SOCKETS
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 void wxProtocol::SetLog(wxProtocolLog
*log
)
198 void wxProtocol::LogRequest(const wxString
& str
)
201 m_log
->LogRequest(str
);
204 void wxProtocol::LogResponse(const wxString
& str
)
207 m_log
->LogResponse(str
);
210 void wxProtocolLog::DoLogString(const wxString
& WXUNUSED_UNLESS_DEBUG(str
))
212 wxLogTrace(m_traceMask
, "%s", str
);
215 #endif // wxUSE_PROTOCOL