]>
Commit | Line | Data |
---|---|---|
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 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #if wxUSE_PROTOCOL | |
24 | ||
25 | #include "wx/protocol/protocol.h" | |
26 | #include "wx/url.h" | |
27 | #include "wx/module.h" | |
28 | ||
29 | #include <stdlib.h> | |
30 | ||
31 | ///////////////////////////////////////////////////////////////// | |
32 | // wxProtoInfo | |
33 | ///////////////////////////////////////////////////////////////// | |
34 | ||
35 | /* | |
36 | * -------------------------------------------------------------- | |
37 | * --------- wxProtoInfo CONSTRUCTOR ---------------------------- | |
38 | * -------------------------------------------------------------- | |
39 | */ | |
40 | ||
41 | wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv, | |
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; | |
48 | next = wxURL::ms_protocols; | |
49 | wxURL::ms_protocols = this; | |
50 | } | |
51 | ||
52 | ///////////////////////////////////////////////////////////////// | |
53 | // wxProtocol /////////////////////////////////////////////////// | |
54 | ///////////////////////////////////////////////////////////////// | |
55 | ||
56 | #if wxUSE_SOCKETS | |
57 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient) | |
58 | #else | |
59 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject) | |
60 | #endif | |
61 | ||
62 | wxProtocol::wxProtocol() | |
63 | #if wxUSE_SOCKETS | |
64 | : wxSocketClient() | |
65 | #endif | |
66 | { | |
67 | } | |
68 | ||
69 | #if wxUSE_SOCKETS | |
70 | bool wxProtocol::Reconnect() | |
71 | { | |
72 | wxIPV4address addr; | |
73 | ||
74 | if (!GetPeer(addr)) | |
75 | { | |
76 | Close(); | |
77 | return FALSE; | |
78 | } | |
79 | if (!Close()) | |
80 | return FALSE; | |
81 | if (!Connect(addr)) | |
82 | return FALSE; | |
83 | ||
84 | return TRUE; | |
85 | } | |
86 | ||
87 | // ---------------------------------------------------------------------------- | |
88 | // Read a line from socket | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
91 | // TODO ReadLine() should use buffers private to wxProtocol for efficiency! | |
92 | ||
93 | // static | |
94 | wxProtocolError wxProtocol::ReadLine(wxSocketBase *socket, wxString& result) | |
95 | { | |
96 | result.Empty(); | |
97 | char ch, chLast = '\0'; | |
98 | while ( !socket->Read(&ch, sizeof(ch)).Error() ) | |
99 | { | |
100 | switch ( ch ) | |
101 | { | |
102 | case '\r': | |
103 | // remember it, if the following is '\n', we're done | |
104 | chLast = '\r'; | |
105 | break; | |
106 | ||
107 | case '\n': | |
108 | // only ends line if the previous character was '\r' | |
109 | if ( chLast == '\r' ) | |
110 | { | |
111 | // EOL found | |
112 | return wxPROTO_NOERR; | |
113 | } | |
114 | //else: fall through | |
115 | ||
116 | default: | |
117 | // normal char | |
118 | if ( chLast ) | |
119 | { | |
120 | result += chLast; | |
121 | chLast = '\0'; | |
122 | } | |
123 | ||
124 | result += ch; | |
125 | } | |
126 | } | |
127 | ||
128 | return wxPROTO_NETERR; | |
129 | } | |
130 | ||
131 | wxProtocolError wxProtocol::ReadLine(wxString& result) | |
132 | { | |
133 | return ReadLine(this, result); | |
134 | } | |
135 | ||
136 | // old function which only chops '\n' and not '\r\n' | |
137 | wxProtocolError GetLine(wxSocketBase *sock, wxString& result) { | |
138 | #define PROTO_BSIZE 2048 | |
139 | size_t avail, size; | |
140 | char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE]; | |
141 | char *ret; | |
142 | bool found; | |
143 | ||
144 | avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount(); | |
145 | if (sock->Error() || avail == 0) | |
146 | return wxPROTO_NETERR; | |
147 | ||
148 | memcpy(tmp_str, tmp_buf, avail); | |
149 | ||
150 | // Not implemented on all systems | |
151 | // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail); | |
152 | found = FALSE; | |
153 | for (ret=tmp_str;ret < (tmp_str+avail); ret++) | |
154 | if (*ret == '\n') { | |
155 | found = TRUE; | |
156 | break; | |
157 | } | |
158 | ||
159 | if (!found) | |
160 | return wxPROTO_PROTERR; | |
161 | *ret = 0; | |
162 | ||
163 | result = tmp_str; | |
164 | result = result.Left(result.Length()-1); | |
165 | ||
166 | size = ret-tmp_str+1; | |
167 | sock->Unread(&tmp_buf[size], avail-size); | |
168 | return wxPROTO_NOERR; | |
169 | #undef PROTO_BSIZE | |
170 | } | |
171 | #endif // wxUSE_SOCKETS | |
172 | ||
173 | #endif // wxUSE_PROTOCOL | |
174 |