]> git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
Beginings of wxX11 support. Much more to do but I have no more time
[wxWidgets.git] / src / common / protocol.cpp
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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 : m_protoname(name),
44 m_servname(serv)
45 {
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
80 if (!Close())
81 return FALSE;
82
83 if (!Connect(addr))
84 return FALSE;
85
86 return TRUE;
87 }
88
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 {
122 result += wxString::FromAscii( chLast );
123 chLast = '\0';
124 }
125
126 result += wxString::FromAscii( ch );
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'
139 wxProtocolError GetLine(wxSocketBase *sock, wxString& result)
140 {
141 #define PROTO_BSIZE 2048
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 }
162
163 if (!found)
164 return wxPROTO_PROTERR;
165
166 *ret = 0;
167
168 result = wxString::FromAscii( tmp_str );
169 result = result.Left(result.Length()-1);
170
171 size = ret-tmp_str+1;
172 sock->Unread(&tmp_buf[size], avail-size);
173
174 return wxPROTO_NOERR;
175 #undef PROTO_BSIZE
176 }
177 #endif // wxUSE_SOCKETS
178
179 #endif // wxUSE_PROTOCOL
180