Unicode interface (the communication itself is still in ASCII, I hope).
[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 // 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_SOCKETS
24
25 #ifndef WX_PRECOMP
26 #include <wx/wx.h>
27 #endif
28
29 #include "wx/protocol/protocol.h"
30 #include "wx/url.h"
31 #include "wx/module.h"
32
33 /////////////////////////////////////////////////////////////////
34 // wxProtoInfo
35 /////////////////////////////////////////////////////////////////
36
37 /*
38 * --------------------------------------------------------------
39 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
40 * --------------------------------------------------------------
41 */
42
43 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
44 const bool need_host1, wxClassInfo *info)
45 {
46 m_protoname = name;
47 m_servname = serv;
48 m_cinfo = info;
49 m_needhost = need_host1;
50 next = wxURL::g_protocols;
51 wxURL::g_protocols = this;
52 }
53
54 /////////////////////////////////////////////////////////////////
55 // wxProtocol ///////////////////////////////////////////////////
56 /////////////////////////////////////////////////////////////////
57
58 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
59
60 wxProtocol::wxProtocol()
61 : wxSocketClient()
62 {
63 }
64
65 bool wxProtocol::Reconnect()
66 {
67 wxIPV4address addr;
68
69 if (!GetPeer(addr)) {
70 Close();
71 return FALSE;
72 }
73 if (!Close())
74 return FALSE;
75 if (!Connect(addr))
76 return FALSE;
77 return TRUE;
78 }
79
80 wxProtocolError GetLine(wxSocketBase *sock, wxString& result) {
81 #define PROTO_BSIZE 2048
82 size_t avail, size;
83 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
84 char *ret;
85 bool found;
86
87 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
88 if (sock->LastError() != 0 || avail == 0)
89 return wxPROTO_NETERR;
90
91 memcpy(tmp_str, tmp_buf, avail);
92
93 // Not implemented on all systems
94 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
95 found = FALSE;
96 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
97 if (*ret == '\n') {
98 found = TRUE;
99 break;
100 }
101
102 if (!found)
103 return wxPROTO_PROTERR;
104 *ret = 0;
105
106 result = tmp_str;
107 result = result.Left(result.Length()-1);
108
109 size = ret-tmp_str+1;
110 sock->CreatePushbackBefore(&tmp_buf[size], avail-size);
111 return wxPROTO_NOERR;
112 #undef PROTO_BSIZE
113 }
114
115 // ----------------------------------------------------------------------
116 // Module
117 // ----------------------------------------------------------------------
118
119 class wxProtocolModule: public wxModule {
120 DECLARE_DYNAMIC_CLASS(wxProtocolModule)
121 public:
122 wxProtocolModule() {}
123 bool OnInit();
124 void OnExit();
125 };
126
127 #if !USE_SHARED_LIBRARY
128 IMPLEMENT_DYNAMIC_CLASS(wxProtocolModule, wxModule)
129 #endif
130
131 bool wxProtocolModule::OnInit()
132 {
133 wxURL::g_proxy = new wxHTTP();
134 return TRUE;
135 }
136
137 void wxProtocolModule::OnExit()
138 {
139 delete wxURL::g_proxy;
140 wxURL::g_proxy = NULL;
141 }
142
143 #endif
144 // wxUSE_SOCKETS