]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/protocol.cpp
Further fixes to references
[wxWidgets.git] / src / common / protocol.cpp
... / ...
CommitLineData
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
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "protocol.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#if wxUSE_PROTOCOL
24
25#include "wx/protocol/protocol.h"
26#include "wx/url.h"
27#include "wx/module.h"
28
29#include <stdlib.h>
30
31/////////////////////////////////////////////////////////////////
32// wxProtoInfo
33/////////////////////////////////////////////////////////////////
34
35/*
36 * --------------------------------------------------------------
37 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
38 * --------------------------------------------------------------
39 */
40
41wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
42 const bool need_host1, wxClassInfo *info)
43{
44 m_protoname = name;
45 m_servname = serv;
46 m_cinfo = info;
47 m_needhost = need_host1;
48 next = wxURL::ms_protocols;
49 wxURL::ms_protocols = this;
50}
51
52/////////////////////////////////////////////////////////////////
53// wxProtocol ///////////////////////////////////////////////////
54/////////////////////////////////////////////////////////////////
55
56#if wxUSE_SOCKETS
57IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
58#else
59IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
60#endif
61
62wxProtocol::wxProtocol()
63#if wxUSE_SOCKETS
64 : wxSocketClient()
65#endif
66{
67}
68
69bool wxProtocol::Reconnect()
70{
71 wxIPV4address addr;
72
73 if (!GetPeer(addr))
74 {
75 Close();
76 return FALSE;
77 }
78 if (!Close())
79 return FALSE;
80 if (!Connect(addr))
81 return FALSE;
82
83 return TRUE;
84}
85
86// ----------------------------------------------------------------------------
87// Read a line from socket
88// ----------------------------------------------------------------------------
89
90// TODO ReadLine() should use buffers private to wxProtocol for efficiency!
91
92// static
93wxProtocolError wxProtocol::ReadLine(wxSocketBase *socket, wxString& result)
94{
95 result.Empty();
96 char ch, chLast = '\0';
97 while ( !socket->Read(&ch, sizeof(ch)).Error() )
98 {
99 switch ( ch )
100 {
101 case '\r':
102 // remember it, if the following is '\n', we're done
103 chLast = '\r';
104 break;
105
106 case '\n':
107 // only ends line if the previous character was '\r'
108 if ( chLast == '\r' )
109 {
110 // EOL found
111 return wxPROTO_NOERR;
112 }
113 //else: fall through
114
115 default:
116 // normal char
117 if ( chLast )
118 {
119 result += chLast;
120 chLast = '\0';
121 }
122
123 result += ch;
124 }
125 }
126
127 return wxPROTO_NETERR;
128}
129
130wxProtocolError wxProtocol::ReadLine(wxString& result)
131{
132 return ReadLine(this, result);
133}
134
135// old function which only chops '\n' and not '\r\n'
136wxProtocolError GetLine(wxSocketBase *sock, wxString& result) {
137#define PROTO_BSIZE 2048
138 size_t avail, size;
139 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
140 char *ret;
141 bool found;
142
143 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
144 if (sock->Error() || avail == 0)
145 return wxPROTO_NETERR;
146
147 memcpy(tmp_str, tmp_buf, avail);
148
149// Not implemented on all systems
150// ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
151 found = FALSE;
152 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
153 if (*ret == '\n') {
154 found = TRUE;
155 break;
156 }
157
158 if (!found)
159 return wxPROTO_PROTERR;
160 *ret = 0;
161
162 result = tmp_str;
163 result = result.Left(result.Length()-1);
164
165 size = ret-tmp_str+1;
166 sock->Unread(&tmp_buf[size], avail-size);
167 return wxPROTO_NOERR;
168#undef PROTO_BSIZE
169}
170
171#endif // wxUSE_PROTOCOL
172