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