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