]>
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 | |
4846abaf | 37 | wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv, |
f4ada568 | 38 | const bool need_host1, wxClassInfo *info) |
69268d6d VZ |
39 | : m_protoname(name), |
40 | m_servname(serv) | |
f4ada568 | 41 | { |
2b5f62a0 VZ |
42 | m_cinfo = info; |
43 | m_needhost = need_host1; | |
34e0d9f8 | 44 | #if wxUSE_URL |
2b5f62a0 VZ |
45 | next = wxURL::ms_protocols; |
46 | wxURL::ms_protocols = this; | |
34e0d9f8 JS |
47 | #else |
48 | next = NULL; | |
49 | #endif | |
f4ada568 GL |
50 | } |
51 | ||
730b772b FM |
52 | |
53 | // ---------------------------------------------------------------------------- | |
54 | // wxProtocol | |
55 | // ---------------------------------------------------------------------------- | |
f4ada568 | 56 | |
8a4df159 | 57 | #if wxUSE_SOCKETS |
f4ada568 | 58 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient) |
8a4df159 RR |
59 | #else |
60 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject) | |
61 | #endif | |
f4ada568 GL |
62 | |
63 | wxProtocol::wxProtocol() | |
8a4df159 | 64 | #if wxUSE_SOCKETS |
f4ada568 | 65 | : wxSocketClient() |
8a4df159 | 66 | #endif |
f4ada568 | 67 | { |
730b772b | 68 | m_lastError = wxPROTO_NOERR; |
0576cd9e | 69 | m_log = NULL; |
730b772b | 70 | SetDefaultTimeout(60); // default timeout is 60 seconds |
f4ada568 GL |
71 | } |
72 | ||
a9cb577a | 73 | #if wxUSE_SOCKETS |
f4ada568 GL |
74 | bool wxProtocol::Reconnect() |
75 | { | |
2b5f62a0 VZ |
76 | wxIPV4address addr; |
77 | ||
78 | if (!GetPeer(addr)) | |
79 | { | |
80 | Close(); | |
7e548f6b | 81 | return false; |
2b5f62a0 VZ |
82 | } |
83 | ||
84 | if (!Close()) | |
7e548f6b WS |
85 | return false; |
86 | ||
2b5f62a0 | 87 | if (!Connect(addr)) |
7e548f6b | 88 | return false; |
2b5f62a0 | 89 | |
7e548f6b | 90 | return true; |
f4ada568 GL |
91 | } |
92 | ||
730b772b FM |
93 | void wxProtocol::SetDefaultTimeout(wxUint32 Value) |
94 | { | |
95 | m_uiDefaultTimeout = Value; | |
96 | #if wxUSE_SOCKETS | |
97 | wxSocketBase::SetTimeout(Value); // sets it for this socket | |
98 | #endif | |
99 | } | |
100 | ||
0576cd9e VZ |
101 | wxProtocol::~wxProtocol() |
102 | { | |
103 | delete m_log; | |
104 | } | |
730b772b | 105 | |
8e907a13 VZ |
106 | // ---------------------------------------------------------------------------- |
107 | // Read a line from socket | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
db7c5931 | 110 | /* static */ |
6adbb0bf | 111 | wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result) |
8e907a13 | 112 | { |
db7c5931 VZ |
113 | static const int LINE_BUF = 4095; |
114 | ||
115 | result.clear(); | |
116 | ||
117 | wxCharBuffer buf(LINE_BUF); | |
684dface | 118 | char *pBuf = buf.data(); |
db7c5931 | 119 | while ( sock->WaitForRead() ) |
8e907a13 | 120 | { |
db7c5931 | 121 | // peek at the socket to see if there is a CRLF |
684dface | 122 | sock->Peek(pBuf, LINE_BUF); |
db7c5931 VZ |
123 | |
124 | size_t nRead = sock->LastCount(); | |
125 | if ( !nRead && sock->Error() ) | |
126 | return wxPROTO_NETERR; | |
127 | ||
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 | |
684dface VZ |
131 | pBuf[nRead] = '\0'; |
132 | const char *eol = strchr(pBuf, '\n'); | |
db7c5931 VZ |
133 | |
134 | // if we found '\n', is there a '\r' as well? | |
135 | if ( eol ) | |
8e907a13 | 136 | { |
684dface | 137 | if ( eol == pBuf ) |
db7c5931 VZ |
138 | { |
139 | // check for case of "\r\n" being split | |
140 | if ( result.empty() || result.Last() != _T('\r') ) | |
8e907a13 | 141 | { |
db7c5931 VZ |
142 | // ignore the stray '\n' |
143 | eol = NULL; | |
8e907a13 | 144 | } |
db7c5931 | 145 | //else: ok, got real EOL |
8e907a13 | 146 | |
db7c5931 VZ |
147 | // read just this '\n' and restart |
148 | nRead = 1; | |
149 | } | |
150 | else // '\n' in the middle of the buffer | |
151 | { | |
152 | // in any case, read everything up to and including '\n' | |
684dface | 153 | nRead = eol - pBuf + 1; |
db7c5931 VZ |
154 | |
155 | if ( eol[-1] != '\r' ) | |
8e907a13 | 156 | { |
db7c5931 VZ |
157 | // as above, simply ignore stray '\n' |
158 | eol = NULL; | |
8e907a13 | 159 | } |
db7c5931 VZ |
160 | } |
161 | } | |
162 | ||
684dface | 163 | sock->Read(pBuf, nRead); |
db7c5931 VZ |
164 | if ( sock->LastCount() != nRead ) |
165 | return wxPROTO_NETERR; | |
166 | ||
684dface VZ |
167 | pBuf[nRead] = '\0'; |
168 | result += wxString::FromAscii(pBuf); | |
db7c5931 VZ |
169 | |
170 | if ( eol ) | |
171 | { | |
172 | // remove trailing "\r\n" | |
173 | result.RemoveLast(2); | |
8e907a13 | 174 | |
db7c5931 | 175 | return wxPROTO_NOERR; |
8e907a13 VZ |
176 | } |
177 | } | |
178 | ||
179 | return wxPROTO_NETERR; | |
180 | } | |
181 | ||
182 | wxProtocolError wxProtocol::ReadLine(wxString& result) | |
183 | { | |
184 | return ReadLine(this, result); | |
185 | } | |
186 | ||
a9cb577a | 187 | #endif // wxUSE_SOCKETS |
a5d46b73 | 188 | |
0576cd9e VZ |
189 | // ---------------------------------------------------------------------------- |
190 | // logging | |
191 | // ---------------------------------------------------------------------------- | |
192 | ||
193 | void wxProtocol::SetLog(wxProtocolLog *log) | |
194 | { | |
195 | delete m_log; | |
196 | m_log = log; | |
197 | } | |
198 | ||
199 | void wxProtocol::LogRequest(const wxString& str) | |
200 | { | |
201 | if ( m_log ) | |
202 | m_log->LogRequest(str); | |
203 | } | |
204 | ||
205 | void wxProtocol::LogResponse(const wxString& str) | |
206 | { | |
207 | if ( m_log ) | |
208 | m_log->LogResponse(str); | |
209 | } | |
210 | ||
ca94b1cc | 211 | void wxProtocolLog::DoLogString(const wxString& str) |
0576cd9e | 212 | { |
657a8a35 | 213 | wxUnusedVar(str); // unused if wxLogTrace() is disabled |
0576cd9e VZ |
214 | wxLogTrace(m_traceMask, "%s", str); |
215 | } | |
216 | ||
a5d46b73 | 217 | #endif // wxUSE_PROTOCOL |