]> git.saurik.com Git - wxWidgets.git/blame - src/common/protocol.cpp
add extra space for top border when wxDIALOG_UNIT_COMPATIBILITY == 1 too
[wxWidgets.git] / src / common / protocol.cpp
CommitLineData
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
65571936 9// Licence: wxWindows licence
f4ada568
GL
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f4ada568
GL
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 41wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
f4ada568 42 const bool need_host1, wxClassInfo *info)
69268d6d
VZ
43 : m_protoname(name),
44 m_servname(serv)
f4ada568 45{
2b5f62a0
VZ
46 m_cinfo = info;
47 m_needhost = need_host1;
34e0d9f8 48#if wxUSE_URL
2b5f62a0
VZ
49 next = wxURL::ms_protocols;
50 wxURL::ms_protocols = this;
34e0d9f8
JS
51#else
52 next = NULL;
53#endif
f4ada568
GL
54}
55
56/////////////////////////////////////////////////////////////////
57// wxProtocol ///////////////////////////////////////////////////
58/////////////////////////////////////////////////////////////////
59
8a4df159 60#if wxUSE_SOCKETS
f4ada568 61IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
8a4df159
RR
62#else
63IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
64#endif
f4ada568
GL
65
66wxProtocol::wxProtocol()
8a4df159 67#if wxUSE_SOCKETS
f4ada568 68 : wxSocketClient()
8a4df159 69#endif
f4ada568
GL
70{
71}
72
a9cb577a 73#if wxUSE_SOCKETS
f4ada568
GL
74bool wxProtocol::Reconnect()
75{
2b5f62a0
VZ
76 wxIPV4address addr;
77
78 if (!GetPeer(addr))
79 {
80 Close();
7e548f6b 81 return false;
2b5f62a0
VZ
82 }
83
84 if (!Close())
7e548f6b
WS
85 return false;
86
2b5f62a0 87 if (!Connect(addr))
7e548f6b 88 return false;
2b5f62a0 89
7e548f6b 90 return true;
f4ada568
GL
91}
92
8e907a13
VZ
93// ----------------------------------------------------------------------------
94// Read a line from socket
95// ----------------------------------------------------------------------------
96
db7c5931 97/* static */
6adbb0bf 98wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
8e907a13 99{
db7c5931
VZ
100 static const int LINE_BUF = 4095;
101
102 result.clear();
103
104 wxCharBuffer buf(LINE_BUF);
684dface 105 char *pBuf = buf.data();
db7c5931 106 while ( sock->WaitForRead() )
8e907a13 107 {
db7c5931 108 // peek at the socket to see if there is a CRLF
684dface 109 sock->Peek(pBuf, LINE_BUF);
db7c5931
VZ
110
111 size_t nRead = sock->LastCount();
112 if ( !nRead && sock->Error() )
113 return wxPROTO_NETERR;
114
115 // look for "\r\n" paying attention to a special case: "\r\n" could
116 // have been split by buffer boundary, so check also for \r at the end
117 // of the last chunk and \n at the beginning of this one
684dface
VZ
118 pBuf[nRead] = '\0';
119 const char *eol = strchr(pBuf, '\n');
db7c5931
VZ
120
121 // if we found '\n', is there a '\r' as well?
122 if ( eol )
8e907a13 123 {
684dface 124 if ( eol == pBuf )
db7c5931
VZ
125 {
126 // check for case of "\r\n" being split
127 if ( result.empty() || result.Last() != _T('\r') )
8e907a13 128 {
db7c5931
VZ
129 // ignore the stray '\n'
130 eol = NULL;
8e907a13 131 }
db7c5931 132 //else: ok, got real EOL
8e907a13 133
db7c5931
VZ
134 // read just this '\n' and restart
135 nRead = 1;
136 }
137 else // '\n' in the middle of the buffer
138 {
139 // in any case, read everything up to and including '\n'
684dface 140 nRead = eol - pBuf + 1;
db7c5931
VZ
141
142 if ( eol[-1] != '\r' )
8e907a13 143 {
db7c5931
VZ
144 // as above, simply ignore stray '\n'
145 eol = NULL;
8e907a13 146 }
db7c5931
VZ
147 }
148 }
149
684dface 150 sock->Read(pBuf, nRead);
db7c5931
VZ
151 if ( sock->LastCount() != nRead )
152 return wxPROTO_NETERR;
153
684dface
VZ
154 pBuf[nRead] = '\0';
155 result += wxString::FromAscii(pBuf);
db7c5931
VZ
156
157 if ( eol )
158 {
159 // remove trailing "\r\n"
160 result.RemoveLast(2);
8e907a13 161
db7c5931 162 return wxPROTO_NOERR;
8e907a13
VZ
163 }
164 }
165
166 return wxPROTO_NETERR;
167}
168
169wxProtocolError wxProtocol::ReadLine(wxString& result)
170{
171 return ReadLine(this, result);
172}
173
174// old function which only chops '\n' and not '\r\n'
2b5f62a0
VZ
175wxProtocolError GetLine(wxSocketBase *sock, wxString& result)
176{
f4ada568 177#define PROTO_BSIZE 2048
2b5f62a0
VZ
178 size_t avail, size;
179 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
180 char *ret;
181 bool found;
182
183 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
184 if (sock->Error() || avail == 0)
185 return wxPROTO_NETERR;
186
187 memcpy(tmp_str, tmp_buf, avail);
188
189 // Not implemented on all systems
190 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
7e548f6b 191 found = false;
2b5f62a0 192 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
7e548f6b 193 if (*ret == '\n')
2b5f62a0 194 {
7e548f6b 195 found = true;
2b5f62a0
VZ
196 break;
197 }
f4ada568 198
2b5f62a0
VZ
199 if (!found)
200 return wxPROTO_PROTERR;
7e548f6b 201
2b5f62a0 202 *ret = 0;
f4ada568 203
2b5f62a0
VZ
204 result = wxString::FromAscii( tmp_str );
205 result = result.Left(result.Length()-1);
f4ada568 206
2b5f62a0
VZ
207 size = ret-tmp_str+1;
208 sock->Unread(&tmp_buf[size], avail-size);
7e548f6b 209
2b5f62a0 210 return wxPROTO_NOERR;
f4ada568
GL
211#undef PROTO_BSIZE
212}
a9cb577a 213#endif // wxUSE_SOCKETS
a5d46b73
VZ
214
215#endif // wxUSE_PROTOCOL
216