| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/protocol/protocol.h |
| 3 | // Purpose: Protocol base class |
| 4 | // Author: Guilhem Lavaux |
| 5 | // Modified by: |
| 6 | // Created: 10/07/1997 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1997, 1998 Guilhem Lavaux |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_PROTOCOL_PROTOCOL_H |
| 13 | #define _WX_PROTOCOL_PROTOCOL_H |
| 14 | |
| 15 | #include "wx/defs.h" |
| 16 | |
| 17 | #if wxUSE_PROTOCOL |
| 18 | |
| 19 | #include "wx/object.h" |
| 20 | #include "wx/string.h" |
| 21 | #include "wx/stream.h" |
| 22 | |
| 23 | #if wxUSE_SOCKETS |
| 24 | #include "wx/socket.h" |
| 25 | #endif |
| 26 | |
| 27 | class WXDLLIMPEXP_FWD_NET wxProtocolLog; |
| 28 | |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | // constants |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | typedef enum |
| 34 | { |
| 35 | wxPROTO_NOERR = 0, |
| 36 | wxPROTO_NETERR, |
| 37 | wxPROTO_PROTERR, |
| 38 | wxPROTO_CONNERR, |
| 39 | wxPROTO_INVVAL, |
| 40 | wxPROTO_NOHNDLR, |
| 41 | wxPROTO_NOFILE, |
| 42 | wxPROTO_ABRT, |
| 43 | wxPROTO_RCNCT, |
| 44 | wxPROTO_STREAMING |
| 45 | } wxProtocolError; |
| 46 | |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | // wxProtocol: abstract base class for all protocols |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | |
| 51 | class WXDLLIMPEXP_NET wxProtocol |
| 52 | #if wxUSE_SOCKETS |
| 53 | : public wxSocketClient |
| 54 | #else |
| 55 | : public wxObject |
| 56 | #endif |
| 57 | { |
| 58 | public: |
| 59 | wxProtocol(); |
| 60 | virtual ~wxProtocol(); |
| 61 | |
| 62 | #if wxUSE_SOCKETS |
| 63 | bool Reconnect(); |
| 64 | virtual bool Connect( const wxString& WXUNUSED(host) ) { return false; } |
| 65 | virtual bool Connect( const wxSockAddress& addr, bool WXUNUSED(wait) = true) |
| 66 | { return wxSocketClient::Connect(addr); } |
| 67 | |
| 68 | // read a '\r\n' terminated line from the given socket and put it in |
| 69 | // result (without the terminators) |
| 70 | static wxProtocolError ReadLine(wxSocketBase *socket, wxString& result); |
| 71 | |
| 72 | // read a line from this socket - this one can be overridden in the |
| 73 | // derived classes if different line termination convention is to be used |
| 74 | virtual wxProtocolError ReadLine(wxString& result); |
| 75 | #endif // wxUSE_SOCKETS |
| 76 | |
| 77 | virtual bool Abort() = 0; |
| 78 | virtual wxInputStream *GetInputStream(const wxString& path) = 0; |
| 79 | virtual wxString GetContentType() const = 0; |
| 80 | |
| 81 | // the error code |
| 82 | virtual wxProtocolError GetError() const { return m_lastError; } |
| 83 | |
| 84 | void SetUser(const wxString& user) { m_username = user; } |
| 85 | void SetPassword(const wxString& passwd) { m_password = passwd; } |
| 86 | |
| 87 | virtual void SetDefaultTimeout(wxUint32 Value); |
| 88 | |
| 89 | // override wxSocketBase::SetTimeout function to avoid that the internal |
| 90 | // m_uiDefaultTimeout goes out-of-sync: |
| 91 | virtual void SetTimeout(long seconds) |
| 92 | { SetDefaultTimeout(seconds); } |
| 93 | |
| 94 | |
| 95 | // logging support: each wxProtocol object may have the associated logger |
| 96 | // (by default there is none) which is used to log network requests and |
| 97 | // responses |
| 98 | |
| 99 | // set the logger, deleting the old one and taking ownership of this one |
| 100 | void SetLog(wxProtocolLog *log); |
| 101 | |
| 102 | // return the current logger, may be NULL |
| 103 | wxProtocolLog *GetLog() const { return m_log; } |
| 104 | |
| 105 | // detach the existing logger without deleting it, the caller is |
| 106 | // responsible for deleting the returned pointer if it's non-NULL |
| 107 | wxProtocolLog *DetachLog() |
| 108 | { |
| 109 | wxProtocolLog * const log = m_log; |
| 110 | m_log = NULL; |
| 111 | return log; |
| 112 | } |
| 113 | |
| 114 | // these functions forward to the same functions with the same names in |
| 115 | // wxProtocolLog if we have a valid logger and do nothing otherwise |
| 116 | void LogRequest(const wxString& str); |
| 117 | void LogResponse(const wxString& str); |
| 118 | |
| 119 | protected: |
| 120 | // the timeout associated with the protocol: |
| 121 | wxUint32 m_uiDefaultTimeout; |
| 122 | |
| 123 | wxString m_username; |
| 124 | wxString m_password; |
| 125 | |
| 126 | // this must be always updated by the derived classes! |
| 127 | wxProtocolError m_lastError; |
| 128 | |
| 129 | private: |
| 130 | wxProtocolLog *m_log; |
| 131 | |
| 132 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxProtocol) |
| 133 | }; |
| 134 | |
| 135 | // ---------------------------------------------------------------------------- |
| 136 | // macros for protocol classes |
| 137 | // ---------------------------------------------------------------------------- |
| 138 | |
| 139 | #define DECLARE_PROTOCOL(class) \ |
| 140 | public: \ |
| 141 | static wxProtoInfo g_proto_##class; |
| 142 | |
| 143 | #define IMPLEMENT_PROTOCOL(class, name, serv, host) \ |
| 144 | wxProtoInfo class::g_proto_##class(name, serv, host, wxCLASSINFO(class)); \ |
| 145 | bool wxProtocolUse##class = true; |
| 146 | |
| 147 | #define USE_PROTOCOL(class) \ |
| 148 | extern bool wxProtocolUse##class ; \ |
| 149 | static struct wxProtocolUserFor##class \ |
| 150 | { \ |
| 151 | wxProtocolUserFor##class() { wxProtocolUse##class = true; } \ |
| 152 | } wxProtocolDoUse##class; |
| 153 | |
| 154 | class WXDLLIMPEXP_NET wxProtoInfo : public wxObject |
| 155 | { |
| 156 | public: |
| 157 | wxProtoInfo(const wxChar *name, |
| 158 | const wxChar *serv_name, |
| 159 | const bool need_host1, |
| 160 | wxClassInfo *info); |
| 161 | |
| 162 | protected: |
| 163 | wxProtoInfo *next; |
| 164 | wxString m_protoname; |
| 165 | wxString prefix; |
| 166 | wxString m_servname; |
| 167 | wxClassInfo *m_cinfo; |
| 168 | bool m_needhost; |
| 169 | |
| 170 | friend class wxURL; |
| 171 | |
| 172 | DECLARE_DYNAMIC_CLASS(wxProtoInfo) |
| 173 | wxDECLARE_NO_COPY_CLASS(wxProtoInfo); |
| 174 | }; |
| 175 | |
| 176 | #endif // wxUSE_PROTOCOL |
| 177 | |
| 178 | #endif // _WX_PROTOCOL_PROTOCOL_H |