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