]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: protocol.cpp | |
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 | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "protocol.h" | |
14 | #endif | |
15 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
2df7be7f | 20 | #pragma hdrstop |
fcc6dddd JS |
21 | #endif |
22 | ||
a5d46b73 VZ |
23 | #if wxUSE_PROTOCOL |
24 | ||
f4ada568 GL |
25 | #include "wx/protocol/protocol.h" |
26 | #include "wx/url.h" | |
3b4183d8 | 27 | #include "wx/module.h" |
f4ada568 | 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 GL |
42 | const bool need_host1, wxClassInfo *info) |
43 | { | |
2b5f62a0 VZ |
44 | m_protoname = name; |
45 | m_servname = serv; | |
46 | m_cinfo = info; | |
47 | m_needhost = need_host1; | |
48 | next = wxURL::ms_protocols; | |
49 | wxURL::ms_protocols = this; | |
f4ada568 GL |
50 | } |
51 | ||
52 | ///////////////////////////////////////////////////////////////// | |
53 | // wxProtocol /////////////////////////////////////////////////// | |
54 | ///////////////////////////////////////////////////////////////// | |
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 GL |
66 | { |
67 | } | |
68 | ||
a9cb577a | 69 | #if wxUSE_SOCKETS |
f4ada568 GL |
70 | bool wxProtocol::Reconnect() |
71 | { | |
2b5f62a0 VZ |
72 | wxIPV4address addr; |
73 | ||
74 | if (!GetPeer(addr)) | |
75 | { | |
76 | Close(); | |
77 | return FALSE; | |
78 | } | |
79 | ||
80 | if (!Close()) | |
81 | return FALSE; | |
82 | ||
83 | if (!Connect(addr)) | |
84 | return FALSE; | |
85 | ||
86 | return TRUE; | |
f4ada568 GL |
87 | } |
88 | ||
8e907a13 VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // Read a line from socket | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | // TODO ReadLine() should use buffers private to wxProtocol for efficiency! | |
94 | ||
95 | // static | |
96 | wxProtocolError wxProtocol::ReadLine(wxSocketBase *socket, wxString& result) | |
97 | { | |
98 | result.Empty(); | |
99 | char ch, chLast = '\0'; | |
100 | while ( !socket->Read(&ch, sizeof(ch)).Error() ) | |
101 | { | |
102 | switch ( ch ) | |
103 | { | |
104 | case '\r': | |
105 | // remember it, if the following is '\n', we're done | |
106 | chLast = '\r'; | |
107 | break; | |
108 | ||
109 | case '\n': | |
110 | // only ends line if the previous character was '\r' | |
111 | if ( chLast == '\r' ) | |
112 | { | |
113 | // EOL found | |
114 | return wxPROTO_NOERR; | |
115 | } | |
116 | //else: fall through | |
117 | ||
118 | default: | |
119 | // normal char | |
120 | if ( chLast ) | |
121 | { | |
2b5f62a0 | 122 | result += wxString::FromAscii( chLast ); |
8e907a13 VZ |
123 | chLast = '\0'; |
124 | } | |
125 | ||
2b5f62a0 | 126 | result += wxString::FromAscii( ch ); |
8e907a13 VZ |
127 | } |
128 | } | |
129 | ||
130 | return wxPROTO_NETERR; | |
131 | } | |
132 | ||
133 | wxProtocolError wxProtocol::ReadLine(wxString& result) | |
134 | { | |
135 | return ReadLine(this, result); | |
136 | } | |
137 | ||
138 | // old function which only chops '\n' and not '\r\n' | |
2b5f62a0 VZ |
139 | wxProtocolError GetLine(wxSocketBase *sock, wxString& result) |
140 | { | |
f4ada568 | 141 | #define PROTO_BSIZE 2048 |
2b5f62a0 VZ |
142 | size_t avail, size; |
143 | char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE]; | |
144 | char *ret; | |
145 | bool found; | |
146 | ||
147 | avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount(); | |
148 | if (sock->Error() || avail == 0) | |
149 | return wxPROTO_NETERR; | |
150 | ||
151 | memcpy(tmp_str, tmp_buf, avail); | |
152 | ||
153 | // Not implemented on all systems | |
154 | // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail); | |
155 | found = FALSE; | |
156 | for (ret=tmp_str;ret < (tmp_str+avail); ret++) | |
157 | if (*ret == '\n') | |
158 | { | |
159 | found = TRUE; | |
160 | break; | |
161 | } | |
f4ada568 | 162 | |
2b5f62a0 VZ |
163 | if (!found) |
164 | return wxPROTO_PROTERR; | |
165 | ||
166 | *ret = 0; | |
f4ada568 | 167 | |
2b5f62a0 VZ |
168 | result = wxString::FromAscii( tmp_str ); |
169 | result = result.Left(result.Length()-1); | |
f4ada568 | 170 | |
2b5f62a0 VZ |
171 | size = ret-tmp_str+1; |
172 | sock->Unread(&tmp_buf[size], avail-size); | |
173 | ||
174 | return wxPROTO_NOERR; | |
f4ada568 GL |
175 | #undef PROTO_BSIZE |
176 | } | |
a9cb577a | 177 | #endif // wxUSE_SOCKETS |
a5d46b73 VZ |
178 | |
179 | #endif // wxUSE_PROTOCOL | |
180 |