]> git.saurik.com Git - wxWidgets.git/blame - src/common/protocol.cpp
Forgot this
[wxWidgets.git] / src / common / protocol.cpp
CommitLineData
f4ada568 1/////////////////////////////////////////////////////////////////////////////
02761f6c 2// Name: src/common/protocol.cpp
f4ada568
GL
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
fcc6dddd
JS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
02761f6c 16 #pragma hdrstop
fcc6dddd
JS
17#endif
18
a5d46b73
VZ
19#if wxUSE_PROTOCOL
20
f4ada568 21#include "wx/protocol/protocol.h"
02761f6c
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/module.h"
25#endif
26
f4ada568
GL
27#include "wx/url.h"
28
f61815af
GL
29#include <stdlib.h>
30
730b772b 31// ----------------------------------------------------------------------------
f4ada568 32// wxProtoInfo
730b772b 33// ----------------------------------------------------------------------------
f4ada568 34
4846abaf 35wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
f4ada568 36 const bool need_host1, wxClassInfo *info)
69268d6d
VZ
37 : m_protoname(name),
38 m_servname(serv)
f4ada568 39{
2b5f62a0
VZ
40 m_cinfo = info;
41 m_needhost = need_host1;
34e0d9f8 42#if wxUSE_URL
2b5f62a0
VZ
43 next = wxURL::ms_protocols;
44 wxURL::ms_protocols = this;
34e0d9f8
JS
45#else
46 next = NULL;
47#endif
f4ada568
GL
48}
49
730b772b
FM
50
51// ----------------------------------------------------------------------------
52// wxProtocol
53// ----------------------------------------------------------------------------
f4ada568 54
8a4df159 55#if wxUSE_SOCKETS
f4ada568 56IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
8a4df159
RR
57#else
58IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
59#endif
f4ada568
GL
60
61wxProtocol::wxProtocol()
8a4df159 62#if wxUSE_SOCKETS
f4ada568 63 : wxSocketClient()
8a4df159 64#endif
f4ada568 65{
730b772b
FM
66 m_lastError = wxPROTO_NOERR;
67 SetDefaultTimeout(60); // default timeout is 60 seconds
f4ada568
GL
68}
69
a9cb577a 70#if wxUSE_SOCKETS
f4ada568
GL
71bool wxProtocol::Reconnect()
72{
2b5f62a0
VZ
73 wxIPV4address addr;
74
75 if (!GetPeer(addr))
76 {
77 Close();
7e548f6b 78 return false;
2b5f62a0
VZ
79 }
80
81 if (!Close())
7e548f6b
WS
82 return false;
83
2b5f62a0 84 if (!Connect(addr))
7e548f6b 85 return false;
2b5f62a0 86
7e548f6b 87 return true;
f4ada568
GL
88}
89
730b772b
FM
90void wxProtocol::SetDefaultTimeout(wxUint32 Value)
91{
92 m_uiDefaultTimeout = Value;
93#if wxUSE_SOCKETS
94 wxSocketBase::SetTimeout(Value); // sets it for this socket
95#endif
96}
97
98
8e907a13
VZ
99// ----------------------------------------------------------------------------
100// Read a line from socket
101// ----------------------------------------------------------------------------
102
db7c5931 103/* static */
6adbb0bf 104wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
8e907a13 105{
db7c5931
VZ
106 static const int LINE_BUF = 4095;
107
108 result.clear();
109
110 wxCharBuffer buf(LINE_BUF);
684dface 111 char *pBuf = buf.data();
db7c5931 112 while ( sock->WaitForRead() )
8e907a13 113 {
db7c5931 114 // peek at the socket to see if there is a CRLF
684dface 115 sock->Peek(pBuf, LINE_BUF);
db7c5931
VZ
116
117 size_t nRead = sock->LastCount();
118 if ( !nRead && sock->Error() )
119 return wxPROTO_NETERR;
120
121 // look for "\r\n" paying attention to a special case: "\r\n" could
122 // have been split by buffer boundary, so check also for \r at the end
123 // of the last chunk and \n at the beginning of this one
684dface
VZ
124 pBuf[nRead] = '\0';
125 const char *eol = strchr(pBuf, '\n');
db7c5931
VZ
126
127 // if we found '\n', is there a '\r' as well?
128 if ( eol )
8e907a13 129 {
684dface 130 if ( eol == pBuf )
db7c5931
VZ
131 {
132 // check for case of "\r\n" being split
133 if ( result.empty() || result.Last() != _T('\r') )
8e907a13 134 {
db7c5931
VZ
135 // ignore the stray '\n'
136 eol = NULL;
8e907a13 137 }
db7c5931 138 //else: ok, got real EOL
8e907a13 139
db7c5931
VZ
140 // read just this '\n' and restart
141 nRead = 1;
142 }
143 else // '\n' in the middle of the buffer
144 {
145 // in any case, read everything up to and including '\n'
684dface 146 nRead = eol - pBuf + 1;
db7c5931
VZ
147
148 if ( eol[-1] != '\r' )
8e907a13 149 {
db7c5931
VZ
150 // as above, simply ignore stray '\n'
151 eol = NULL;
8e907a13 152 }
db7c5931
VZ
153 }
154 }
155
684dface 156 sock->Read(pBuf, nRead);
db7c5931
VZ
157 if ( sock->LastCount() != nRead )
158 return wxPROTO_NETERR;
159
684dface
VZ
160 pBuf[nRead] = '\0';
161 result += wxString::FromAscii(pBuf);
db7c5931
VZ
162
163 if ( eol )
164 {
165 // remove trailing "\r\n"
166 result.RemoveLast(2);
8e907a13 167
db7c5931 168 return wxPROTO_NOERR;
8e907a13
VZ
169 }
170 }
171
172 return wxPROTO_NETERR;
173}
174
175wxProtocolError wxProtocol::ReadLine(wxString& result)
176{
177 return ReadLine(this, result);
178}
179
a9cb577a 180#endif // wxUSE_SOCKETS
a5d46b73
VZ
181
182#endif // wxUSE_PROTOCOL