]> git.saurik.com Git - wxWidgets.git/blame - src/common/protocol.cpp
Update version to 2.9.4 in version.bkl too and rebake everything.
[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"
0576cd9e 22#include "wx/protocol/log.h"
02761f6c
WS
23
24#ifndef WX_PRECOMP
25 #include "wx/module.h"
26#endif
27
f4ada568 28#include "wx/url.h"
1ffe9586 29#include "wx/log.h"
f4ada568 30
f61815af
GL
31#include <stdlib.h>
32
730b772b 33// ----------------------------------------------------------------------------
f4ada568 34// wxProtoInfo
730b772b 35// ----------------------------------------------------------------------------
f4ada568 36
ef8f6d95
VZ
37IMPLEMENT_CLASS(wxProtoInfo, wxObject)
38
4846abaf 39wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
f4ada568 40 const bool need_host1, wxClassInfo *info)
69268d6d
VZ
41 : m_protoname(name),
42 m_servname(serv)
f4ada568 43{
2b5f62a0
VZ
44 m_cinfo = info;
45 m_needhost = need_host1;
34e0d9f8 46#if wxUSE_URL
2b5f62a0
VZ
47 next = wxURL::ms_protocols;
48 wxURL::ms_protocols = this;
34e0d9f8
JS
49#else
50 next = NULL;
51#endif
f4ada568
GL
52}
53
730b772b
FM
54
55// ----------------------------------------------------------------------------
56// wxProtocol
57// ----------------------------------------------------------------------------
f4ada568 58
8a4df159 59#if wxUSE_SOCKETS
f4ada568 60IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
8a4df159
RR
61#else
62IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
63#endif
f4ada568
GL
64
65wxProtocol::wxProtocol()
8a4df159 66#if wxUSE_SOCKETS
f4ada568 67 : wxSocketClient()
8a4df159 68#endif
f4ada568 69{
730b772b 70 m_lastError = wxPROTO_NOERR;
0576cd9e 71 m_log = NULL;
730b772b 72 SetDefaultTimeout(60); // default timeout is 60 seconds
f4ada568
GL
73}
74
a9cb577a 75#if wxUSE_SOCKETS
f4ada568
GL
76bool wxProtocol::Reconnect()
77{
2b5f62a0
VZ
78 wxIPV4address addr;
79
80 if (!GetPeer(addr))
81 {
82 Close();
7e548f6b 83 return false;
2b5f62a0
VZ
84 }
85
86 if (!Close())
7e548f6b
WS
87 return false;
88
2b5f62a0 89 if (!Connect(addr))
7e548f6b 90 return false;
2b5f62a0 91
7e548f6b 92 return true;
f4ada568
GL
93}
94
730b772b
FM
95void wxProtocol::SetDefaultTimeout(wxUint32 Value)
96{
97 m_uiDefaultTimeout = Value;
98#if wxUSE_SOCKETS
99 wxSocketBase::SetTimeout(Value); // sets it for this socket
100#endif
101}
102
0576cd9e
VZ
103wxProtocol::~wxProtocol()
104{
105 delete m_log;
106}
730b772b 107
8e907a13
VZ
108// ----------------------------------------------------------------------------
109// Read a line from socket
110// ----------------------------------------------------------------------------
111
db7c5931 112/* static */
6adbb0bf 113wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
8e907a13 114{
db7c5931
VZ
115 static const int LINE_BUF = 4095;
116
117 result.clear();
118
119 wxCharBuffer buf(LINE_BUF);
684dface 120 char *pBuf = buf.data();
db7c5931 121 while ( sock->WaitForRead() )
8e907a13 122 {
db7c5931 123 // peek at the socket to see if there is a CRLF
684dface 124 sock->Peek(pBuf, LINE_BUF);
db7c5931
VZ
125
126 size_t nRead = sock->LastCount();
127 if ( !nRead && sock->Error() )
128 return wxPROTO_NETERR;
129
130 // look for "\r\n" paying attention to a special case: "\r\n" could
131 // have been split by buffer boundary, so check also for \r at the end
132 // of the last chunk and \n at the beginning of this one
684dface
VZ
133 pBuf[nRead] = '\0';
134 const char *eol = strchr(pBuf, '\n');
db7c5931
VZ
135
136 // if we found '\n', is there a '\r' as well?
137 if ( eol )
8e907a13 138 {
684dface 139 if ( eol == pBuf )
db7c5931
VZ
140 {
141 // check for case of "\r\n" being split
9a83f860 142 if ( result.empty() || result.Last() != wxT('\r') )
8e907a13 143 {
db7c5931
VZ
144 // ignore the stray '\n'
145 eol = NULL;
8e907a13 146 }
db7c5931 147 //else: ok, got real EOL
8e907a13 148
db7c5931
VZ
149 // read just this '\n' and restart
150 nRead = 1;
151 }
152 else // '\n' in the middle of the buffer
153 {
154 // in any case, read everything up to and including '\n'
684dface 155 nRead = eol - pBuf + 1;
db7c5931
VZ
156
157 if ( eol[-1] != '\r' )
8e907a13 158 {
db7c5931
VZ
159 // as above, simply ignore stray '\n'
160 eol = NULL;
8e907a13 161 }
db7c5931
VZ
162 }
163 }
164
684dface 165 sock->Read(pBuf, nRead);
db7c5931
VZ
166 if ( sock->LastCount() != nRead )
167 return wxPROTO_NETERR;
168
684dface
VZ
169 pBuf[nRead] = '\0';
170 result += wxString::FromAscii(pBuf);
db7c5931
VZ
171
172 if ( eol )
173 {
174 // remove trailing "\r\n"
175 result.RemoveLast(2);
8e907a13 176
db7c5931 177 return wxPROTO_NOERR;
8e907a13
VZ
178 }
179 }
180
181 return wxPROTO_NETERR;
182}
183
184wxProtocolError wxProtocol::ReadLine(wxString& result)
185{
186 return ReadLine(this, result);
187}
188
a9cb577a 189#endif // wxUSE_SOCKETS
a5d46b73 190
0576cd9e
VZ
191// ----------------------------------------------------------------------------
192// logging
193// ----------------------------------------------------------------------------
194
195void wxProtocol::SetLog(wxProtocolLog *log)
196{
197 delete m_log;
198 m_log = log;
199}
200
201void wxProtocol::LogRequest(const wxString& str)
202{
203 if ( m_log )
204 m_log->LogRequest(str);
205}
206
207void wxProtocol::LogResponse(const wxString& str)
208{
209 if ( m_log )
210 m_log->LogResponse(str);
211}
212
ca94b1cc 213void wxProtocolLog::DoLogString(const wxString& str)
0576cd9e 214{
657a8a35 215 wxUnusedVar(str); // unused if wxLogTrace() is disabled
0576cd9e
VZ
216 wxLogTrace(m_traceMask, "%s", str);
217}
218
a5d46b73 219#endif // wxUSE_PROTOCOL