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