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