| 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 | #include "wx/protocol/protocol.h" |
| 26 | #include "wx/url.h" |
| 27 | #include "wx/module.h" |
| 28 | |
| 29 | ///////////////////////////////////////////////////////////////// |
| 30 | // wxProtoInfo |
| 31 | ///////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | /* |
| 34 | * -------------------------------------------------------------- |
| 35 | * --------- wxProtoInfo CONSTRUCTOR ---------------------------- |
| 36 | * -------------------------------------------------------------- |
| 37 | */ |
| 38 | |
| 39 | wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *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 | } |
| 110 | |
| 111 | // ---------------------------------------------------------------------- |
| 112 | // Module |
| 113 | // ---------------------------------------------------------------------- |
| 114 | |
| 115 | class wxProtocolModule: public wxModule { |
| 116 | DECLARE_DYNAMIC_CLASS(wxProtocolModule) |
| 117 | public: |
| 118 | wxProtocolModule() {} |
| 119 | bool OnInit(); |
| 120 | void OnExit(); |
| 121 | }; |
| 122 | |
| 123 | #if !USE_SHARED_LIBRARY |
| 124 | IMPLEMENT_DYNAMIC_CLASS(wxProtocolModule, wxModule) |
| 125 | #endif |
| 126 | |
| 127 | bool wxProtocolModule::OnInit() |
| 128 | { |
| 129 | wxURL::g_proxy = new wxHTTP(); |
| 130 | return TRUE; |
| 131 | } |
| 132 | |
| 133 | void wxProtocolModule::OnExit() |
| 134 | { |
| 135 | delete wxURL::g_proxy; |
| 136 | wxURL::g_proxy = NULL; |
| 137 | } |
| 138 | |
| 139 | #endif |
| 140 | // wxUSE_SOCKETS |