* Added wxsocket lib and sample (I hope I don't forget some file)
[wxWidgets.git] / src / common / protocol.cpp
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 #ifdef WXPREC
17 #include <wx/wxprec.h>
18 #else
19 #include <wx/wx.h>
20 #endif
21
22 #include "wx/protocol/protocol.h"
23 #include "wx/url.h"
24
25 #ifdef __BORLANDC__
26 #pragma hdrstop
27 #endif
28
29 /////////////////////////////////////////////////////////////////
30 // wxProtoInfo
31 /////////////////////////////////////////////////////////////////
32
33 /*
34 * --------------------------------------------------------------
35 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
36 * --------------------------------------------------------------
37 */
38
39 wxProtoInfo::wxProtoInfo(const char *name, const char *serv,
40 const bool need_host1, wxClassInfo *info)
41 {
42 m_protoname = name;
43 m_servname = serv;
44 m_cinfo = info;
45 m_needhost = need_host1;
46 next = wxURL::g_protocols;
47 wxURL::g_protocols = this;
48 }
49
50 /////////////////////////////////////////////////////////////////
51 // wxProtocol ///////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////
53
54 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
55
56 wxProtocol::wxProtocol()
57 : wxSocketClient()
58 {
59 }
60
61 bool wxProtocol::Reconnect()
62 {
63 wxIPV4address addr;
64
65 if (!GetPeer(addr)) {
66 Close();
67 return FALSE;
68 }
69 if (!Close())
70 return FALSE;
71 if (!Connect(addr))
72 return FALSE;
73 return TRUE;
74 }
75
76 wxProtocolError GetLine(wxSocketBase *sock, wxString& result) {
77 #define PROTO_BSIZE 2048
78 size_t avail, size;
79 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
80 char *ret;
81 bool found;
82
83 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
84 if (sock->LastError() != 0 || avail == 0)
85 return wxPROTO_NETERR;
86
87 memcpy(tmp_str, tmp_buf, avail);
88
89 // Not implemented on all systems
90 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
91 found = FALSE;
92 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
93 if (*ret == '\n') {
94 found = TRUE;
95 break;
96 }
97
98 if (!found)
99 return wxPROTO_PROTERR;
100 *ret = 0;
101
102 result = tmp_str;
103 result = result.Left(result.Length()-1);
104
105 size = ret-tmp_str+1;
106 sock->CreatePushbackBefore(&tmp_buf[size], avail-size);
107 return wxPROTO_NOERR;
108 #undef PROTO_BSIZE
109 }