]> git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
remove unused function and out of date comments (we don't use bsearch() any more)
[wxWidgets.git] / src / common / protocol.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/common/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16     #pragma hdrstop
17 #endif
18
19 #if wxUSE_PROTOCOL
20
21 #include "wx/protocol/protocol.h"
22
23 #ifndef WX_PRECOMP
24     #include "wx/module.h"
25 #endif
26
27 #include "wx/url.h"
28
29 #include <stdlib.h>
30
31 // ----------------------------------------------------------------------------
32 // wxProtoInfo
33 // ----------------------------------------------------------------------------
34
35 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
36                          const bool need_host1, wxClassInfo *info)
37            : m_protoname(name),
38              m_servname(serv)
39 {
40     m_cinfo = info;
41     m_needhost = need_host1;
42 #if wxUSE_URL
43     next = wxURL::ms_protocols;
44     wxURL::ms_protocols = this;
45 #else
46     next = NULL;
47 #endif
48 }
49
50
51 // ----------------------------------------------------------------------------
52 // wxProtocol
53 // ----------------------------------------------------------------------------
54
55 #if wxUSE_SOCKETS
56 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
57 #else
58 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
59 #endif
60
61 wxProtocol::wxProtocol()
62 #if wxUSE_SOCKETS
63  : wxSocketClient()
64 #endif
65 {
66     m_lastError = wxPROTO_NOERR;
67     SetDefaultTimeout(60);      // default timeout is 60 seconds
68 }
69
70 #if wxUSE_SOCKETS
71 bool wxProtocol::Reconnect()
72 {
73     wxIPV4address addr;
74
75     if (!GetPeer(addr))
76     {
77         Close();
78         return false;
79     }
80
81     if (!Close())
82         return false;
83
84     if (!Connect(addr))
85         return false;
86
87     return true;
88 }
89
90 void 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
99 // ----------------------------------------------------------------------------
100 // Read a line from socket
101 // ----------------------------------------------------------------------------
102
103 /* static */
104 wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
105 {
106     static const int LINE_BUF = 4095;
107
108     result.clear();
109
110     wxCharBuffer buf(LINE_BUF);
111     char *pBuf = buf.data();
112     while ( sock->WaitForRead() )
113     {
114         // peek at the socket to see if there is a CRLF
115         sock->Peek(pBuf, LINE_BUF);
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
124         pBuf[nRead] = '\0';
125         const char *eol = strchr(pBuf, '\n');
126
127         // if we found '\n', is there a '\r' as well?
128         if ( eol )
129         {
130             if ( eol == pBuf )
131             {
132                 // check for case of "\r\n" being split
133                 if ( result.empty() || result.Last() != _T('\r') )
134                 {
135                     // ignore the stray '\n'
136                     eol = NULL;
137                 }
138                 //else: ok, got real EOL
139
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'
146                 nRead = eol - pBuf + 1;
147
148                 if ( eol[-1] != '\r' )
149                 {
150                     // as above, simply ignore stray '\n'
151                     eol = NULL;
152                 }
153             }
154         }
155
156         sock->Read(pBuf, nRead);
157         if ( sock->LastCount() != nRead )
158             return wxPROTO_NETERR;
159
160         pBuf[nRead] = '\0';
161         result += wxString::FromAscii(pBuf);
162
163         if ( eol )
164         {
165             // remove trailing "\r\n"
166             result.RemoveLast(2);
167
168             return wxPROTO_NOERR;
169         }
170     }
171
172     return wxPROTO_NETERR;
173 }
174
175 wxProtocolError wxProtocol::ReadLine(wxString& result)
176 {
177     return ReadLine(this, result);
178 }
179
180 #endif // wxUSE_SOCKETS
181
182 #endif // wxUSE_PROTOCOL