]>
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 | { | |
44 | m_protoname = name; | |
45 | m_servname = serv; | |
46 | m_cinfo = info; | |
47 | m_needhost = need_host1; | |
b2b35524 VZ |
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 | ||
69 | bool wxProtocol::Reconnect() | |
70 | { | |
71 | wxIPV4address addr; | |
72 | ||
c0eba78b GRG |
73 | if (!GetPeer(addr)) |
74 | { | |
f4ada568 GL |
75 | Close(); |
76 | return FALSE; | |
77 | } | |
78 | if (!Close()) | |
79 | return FALSE; | |
80 | if (!Connect(addr)) | |
81 | return FALSE; | |
c0eba78b | 82 | |
f4ada568 GL |
83 | return TRUE; |
84 | } | |
85 | ||
8e907a13 VZ |
86 | // ---------------------------------------------------------------------------- |
87 | // Read a line from socket | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | // TODO ReadLine() should use buffers private to wxProtocol for efficiency! | |
91 | ||
92 | // static | |
93 | wxProtocolError wxProtocol::ReadLine(wxSocketBase *socket, wxString& result) | |
94 | { | |
95 | result.Empty(); | |
96 | char ch, chLast = '\0'; | |
97 | while ( !socket->Read(&ch, sizeof(ch)).Error() ) | |
98 | { | |
99 | switch ( ch ) | |
100 | { | |
101 | case '\r': | |
102 | // remember it, if the following is '\n', we're done | |
103 | chLast = '\r'; | |
104 | break; | |
105 | ||
106 | case '\n': | |
107 | // only ends line if the previous character was '\r' | |
108 | if ( chLast == '\r' ) | |
109 | { | |
110 | // EOL found | |
111 | return wxPROTO_NOERR; | |
112 | } | |
113 | //else: fall through | |
114 | ||
115 | default: | |
116 | // normal char | |
117 | if ( chLast ) | |
118 | { | |
119 | result += chLast; | |
120 | chLast = '\0'; | |
121 | } | |
122 | ||
123 | result += ch; | |
124 | } | |
125 | } | |
126 | ||
127 | return wxPROTO_NETERR; | |
128 | } | |
129 | ||
130 | wxProtocolError wxProtocol::ReadLine(wxString& result) | |
131 | { | |
132 | return ReadLine(this, result); | |
133 | } | |
134 | ||
135 | // old function which only chops '\n' and not '\r\n' | |
f4ada568 GL |
136 | wxProtocolError GetLine(wxSocketBase *sock, wxString& result) { |
137 | #define PROTO_BSIZE 2048 | |
138 | size_t avail, size; | |
139 | char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE]; | |
140 | char *ret; | |
141 | bool found; | |
142 | ||
143 | avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount(); | |
c0eba78b | 144 | if (sock->Error() || avail == 0) |
f4ada568 GL |
145 | return wxPROTO_NETERR; |
146 | ||
147 | memcpy(tmp_str, tmp_buf, avail); | |
148 | ||
149 | // Not implemented on all systems | |
150 | // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail); | |
151 | found = FALSE; | |
152 | for (ret=tmp_str;ret < (tmp_str+avail); ret++) | |
153 | if (*ret == '\n') { | |
154 | found = TRUE; | |
155 | break; | |
156 | } | |
157 | ||
158 | if (!found) | |
159 | return wxPROTO_PROTERR; | |
160 | *ret = 0; | |
161 | ||
162 | result = tmp_str; | |
163 | result = result.Left(result.Length()-1); | |
164 | ||
165 | size = ret-tmp_str+1; | |
5bb0807e | 166 | sock->Unread(&tmp_buf[size], avail-size); |
f4ada568 GL |
167 | return wxPROTO_NOERR; |
168 | #undef PROTO_BSIZE | |
169 | } | |
a5d46b73 VZ |
170 | |
171 | #endif // wxUSE_PROTOCOL | |
172 |