]> git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
No changes, just use wxRecursionGuard instead of manual boolean flag.
[wxWidgets.git] / src / common / protocol.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/protocol.cpp
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 07/07/1997
7 // Copyright: (c) 1997, 1998 Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_PROTOCOL
19
20 #include "wx/protocol/protocol.h"
21 #include "wx/protocol/log.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/module.h"
25 #endif
26
27 #include "wx/url.h"
28 #include "wx/log.h"
29
30 #include <stdlib.h>
31
32 // ----------------------------------------------------------------------------
33 // wxProtoInfo
34 // ----------------------------------------------------------------------------
35
36 IMPLEMENT_CLASS(wxProtoInfo, wxObject)
37
38 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
39 const bool need_host1, wxClassInfo *info)
40 : m_protoname(name),
41 m_servname(serv)
42 {
43 m_cinfo = info;
44 m_needhost = need_host1;
45 #if wxUSE_URL
46 next = wxURL::ms_protocols;
47 wxURL::ms_protocols = this;
48 #else
49 next = NULL;
50 #endif
51 }
52
53
54 // ----------------------------------------------------------------------------
55 // wxProtocol
56 // ----------------------------------------------------------------------------
57
58 #if wxUSE_SOCKETS
59 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
60 #else
61 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
62 #endif
63
64 wxProtocol::wxProtocol()
65 #if wxUSE_SOCKETS
66 : wxSocketClient()
67 #endif
68 {
69 m_lastError = wxPROTO_NOERR;
70 m_log = NULL;
71 SetDefaultTimeout(60); // default timeout is 60 seconds
72 }
73
74 void wxProtocol::SetDefaultTimeout(wxUint32 Value)
75 {
76 m_uiDefaultTimeout = Value;
77 #if wxUSE_SOCKETS
78 wxSocketBase::SetTimeout(Value); // sets it for this socket
79 #endif
80 }
81
82 wxProtocol::~wxProtocol()
83 {
84 delete m_log;
85 }
86
87 #if wxUSE_SOCKETS
88 bool wxProtocol::Reconnect()
89 {
90 wxIPV4address addr;
91
92 if (!GetPeer(addr))
93 {
94 Close();
95 return false;
96 }
97
98 if (!Close())
99 return false;
100
101 if (!Connect(addr))
102 return false;
103
104 return true;
105 }
106
107 // ----------------------------------------------------------------------------
108 // Read a line from socket
109 // ----------------------------------------------------------------------------
110
111 /* static */
112 wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
113 {
114 static const int LINE_BUF = 4095;
115
116 result.clear();
117
118 wxCharBuffer buf(LINE_BUF);
119 char *pBuf = buf.data();
120 while ( sock->WaitForRead() )
121 {
122 // peek at the socket to see if there is a CRLF
123 sock->Peek(pBuf, LINE_BUF);
124
125 size_t nRead = sock->LastCount();
126 if ( !nRead && sock->Error() )
127 return wxPROTO_NETERR;
128
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
132 pBuf[nRead] = '\0';
133 const char *eol = strchr(pBuf, '\n');
134
135 // if we found '\n', is there a '\r' as well?
136 if ( eol )
137 {
138 if ( eol == pBuf )
139 {
140 // check for case of "\r\n" being split
141 if ( result.empty() || result.Last() != wxT('\r') )
142 {
143 // ignore the stray '\n'
144 eol = NULL;
145 }
146 //else: ok, got real EOL
147
148 // read just this '\n' and restart
149 nRead = 1;
150 }
151 else // '\n' in the middle of the buffer
152 {
153 // in any case, read everything up to and including '\n'
154 nRead = eol - pBuf + 1;
155
156 if ( eol[-1] != '\r' )
157 {
158 // as above, simply ignore stray '\n'
159 eol = NULL;
160 }
161 }
162 }
163
164 sock->Read(pBuf, nRead);
165 if ( sock->LastCount() != nRead )
166 return wxPROTO_NETERR;
167
168 pBuf[nRead] = '\0';
169 result += wxString::FromAscii(pBuf);
170
171 if ( eol )
172 {
173 // remove trailing "\r\n"
174 result.RemoveLast(2);
175
176 return wxPROTO_NOERR;
177 }
178 }
179
180 return wxPROTO_NETERR;
181 }
182
183 wxProtocolError wxProtocol::ReadLine(wxString& result)
184 {
185 return ReadLine(this, result);
186 }
187
188 #endif // wxUSE_SOCKETS
189
190 // ----------------------------------------------------------------------------
191 // logging
192 // ----------------------------------------------------------------------------
193
194 void wxProtocol::SetLog(wxProtocolLog *log)
195 {
196 delete m_log;
197 m_log = log;
198 }
199
200 void wxProtocol::LogRequest(const wxString& str)
201 {
202 if ( m_log )
203 m_log->LogRequest(str);
204 }
205
206 void wxProtocol::LogResponse(const wxString& str)
207 {
208 if ( m_log )
209 m_log->LogResponse(str);
210 }
211
212 void wxProtocolLog::DoLogString(const wxString& str)
213 {
214 wxUnusedVar(str); // unused if wxLogTrace() is disabled
215 wxLogTrace(m_traceMask, "%s", str);
216 }
217
218 #endif // wxUSE_PROTOCOL