]>
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" |
02761f6c WS |
22 | |
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/module.h" | |
25 | #endif | |
26 | ||
f4ada568 GL |
27 | #include "wx/url.h" |
28 | ||
f61815af GL |
29 | #include <stdlib.h> |
30 | ||
f4ada568 GL |
31 | ///////////////////////////////////////////////////////////////// |
32 | // wxProtoInfo | |
33 | ///////////////////////////////////////////////////////////////// | |
34 | ||
35 | /* | |
36 | * -------------------------------------------------------------- | |
37 | * --------- wxProtoInfo CONSTRUCTOR ---------------------------- | |
38 | * -------------------------------------------------------------- | |
39 | */ | |
40 | ||
4846abaf | 41 | wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv, |
f4ada568 | 42 | const bool need_host1, wxClassInfo *info) |
69268d6d VZ |
43 | : m_protoname(name), |
44 | m_servname(serv) | |
f4ada568 | 45 | { |
2b5f62a0 VZ |
46 | m_cinfo = info; |
47 | m_needhost = need_host1; | |
34e0d9f8 | 48 | #if wxUSE_URL |
2b5f62a0 VZ |
49 | next = wxURL::ms_protocols; |
50 | wxURL::ms_protocols = this; | |
34e0d9f8 JS |
51 | #else |
52 | next = NULL; | |
53 | #endif | |
f4ada568 GL |
54 | } |
55 | ||
56 | ///////////////////////////////////////////////////////////////// | |
57 | // wxProtocol /////////////////////////////////////////////////// | |
58 | ///////////////////////////////////////////////////////////////// | |
59 | ||
8a4df159 | 60 | #if wxUSE_SOCKETS |
f4ada568 | 61 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient) |
8a4df159 RR |
62 | #else |
63 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject) | |
64 | #endif | |
f4ada568 GL |
65 | |
66 | wxProtocol::wxProtocol() | |
8a4df159 | 67 | #if wxUSE_SOCKETS |
f4ada568 | 68 | : wxSocketClient() |
8a4df159 | 69 | #endif |
f4ada568 GL |
70 | { |
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 | ||
8e907a13 VZ |
93 | // ---------------------------------------------------------------------------- |
94 | // Read a line from socket | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
db7c5931 | 97 | /* static */ |
6adbb0bf | 98 | wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result) |
8e907a13 | 99 | { |
db7c5931 VZ |
100 | static const int LINE_BUF = 4095; |
101 | ||
102 | result.clear(); | |
103 | ||
104 | wxCharBuffer buf(LINE_BUF); | |
684dface | 105 | char *pBuf = buf.data(); |
db7c5931 | 106 | while ( sock->WaitForRead() ) |
8e907a13 | 107 | { |
db7c5931 | 108 | // peek at the socket to see if there is a CRLF |
684dface | 109 | sock->Peek(pBuf, LINE_BUF); |
db7c5931 VZ |
110 | |
111 | size_t nRead = sock->LastCount(); | |
112 | if ( !nRead && sock->Error() ) | |
113 | return wxPROTO_NETERR; | |
114 | ||
115 | // look for "\r\n" paying attention to a special case: "\r\n" could | |
116 | // have been split by buffer boundary, so check also for \r at the end | |
117 | // of the last chunk and \n at the beginning of this one | |
684dface VZ |
118 | pBuf[nRead] = '\0'; |
119 | const char *eol = strchr(pBuf, '\n'); | |
db7c5931 VZ |
120 | |
121 | // if we found '\n', is there a '\r' as well? | |
122 | if ( eol ) | |
8e907a13 | 123 | { |
684dface | 124 | if ( eol == pBuf ) |
db7c5931 VZ |
125 | { |
126 | // check for case of "\r\n" being split | |
127 | if ( result.empty() || result.Last() != _T('\r') ) | |
8e907a13 | 128 | { |
db7c5931 VZ |
129 | // ignore the stray '\n' |
130 | eol = NULL; | |
8e907a13 | 131 | } |
db7c5931 | 132 | //else: ok, got real EOL |
8e907a13 | 133 | |
db7c5931 VZ |
134 | // read just this '\n' and restart |
135 | nRead = 1; | |
136 | } | |
137 | else // '\n' in the middle of the buffer | |
138 | { | |
139 | // in any case, read everything up to and including '\n' | |
684dface | 140 | nRead = eol - pBuf + 1; |
db7c5931 VZ |
141 | |
142 | if ( eol[-1] != '\r' ) | |
8e907a13 | 143 | { |
db7c5931 VZ |
144 | // as above, simply ignore stray '\n' |
145 | eol = NULL; | |
8e907a13 | 146 | } |
db7c5931 VZ |
147 | } |
148 | } | |
149 | ||
684dface | 150 | sock->Read(pBuf, nRead); |
db7c5931 VZ |
151 | if ( sock->LastCount() != nRead ) |
152 | return wxPROTO_NETERR; | |
153 | ||
684dface VZ |
154 | pBuf[nRead] = '\0'; |
155 | result += wxString::FromAscii(pBuf); | |
db7c5931 VZ |
156 | |
157 | if ( eol ) | |
158 | { | |
159 | // remove trailing "\r\n" | |
160 | result.RemoveLast(2); | |
8e907a13 | 161 | |
db7c5931 | 162 | return wxPROTO_NOERR; |
8e907a13 VZ |
163 | } |
164 | } | |
165 | ||
166 | return wxPROTO_NETERR; | |
167 | } | |
168 | ||
169 | wxProtocolError wxProtocol::ReadLine(wxString& result) | |
170 | { | |
171 | return ReadLine(this, result); | |
172 | } | |
173 | ||
a9cb577a | 174 | #endif // wxUSE_SOCKETS |
a5d46b73 VZ |
175 | |
176 | #endif // wxUSE_PROTOCOL |