protocol.h 5.1 KB

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