PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/include/sockets/ISocketHandler.h

https://bitbucket.org/oregon/oregoncore/
C Header | 233 lines | 104 code | 30 blank | 99 comment | 0 complexity | 9304fbd32e9e0e428d1186c984b808be MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0, BSD-2-Clause
  1. /** \file ISocketHandler.h
  2. ** \date 2004-02-13
  3. ** \author grymse@alhem.net
  4. **/
  5. /*
  6. Copyright (C) 2004-2007 Anders Hedstrom
  7. This library is made available under the terms of the GNU GPL.
  8. If you would like to use this library in a closed-source application,
  9. a separate license agreement is available. For information about
  10. the closed-source license agreement for the C++ sockets library,
  11. please visit http://www.alhem.net/Sockets/license.html and/or
  12. email license@alhem.net.
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or (at your option) any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #ifndef _SOCKETS_ISocketHandler_H
  26. #define _SOCKETS_ISocketHandler_H
  27. #include "sockets-config.h"
  28. #include <list>
  29. #include "socket_include.h"
  30. #include "Socket.h"
  31. #include "StdLog.h"
  32. #ifdef SOCKETS_NAMESPACE
  33. namespace SOCKETS_NAMESPACE {
  34. #endif
  35. typedef enum {
  36. LIST_CALLONCONNECT = 0,
  37. #ifdef ENABLE_DETACH
  38. LIST_DETACH,
  39. #endif
  40. LIST_TIMEOUT,
  41. LIST_RETRY,
  42. LIST_CLOSE
  43. } list_t;
  44. class SocketAddress;
  45. class Mutex;
  46. /** Socket container class, event generator.
  47. \ingroup basic */
  48. class ISocketHandler
  49. {
  50. friend class Socket;
  51. public:
  52. /** Connection pool class for internal use by the ISocketHandler.
  53. \ingroup internal */
  54. #ifdef ENABLE_POOL
  55. class PoolSocket : public Socket
  56. {
  57. public:
  58. PoolSocket(ISocketHandler& h,Socket *src) : Socket(h) {
  59. CopyConnection( src );
  60. SetIsClient();
  61. }
  62. void OnRead() {
  63. Handler().LogError(this, "OnRead", 0, "data on hibernating socket", LOG_LEVEL_FATAL);
  64. SetCloseAndDelete();
  65. }
  66. void OnOptions(int,int,int,SOCKET) {}
  67. };
  68. #endif
  69. public:
  70. virtual ~ISocketHandler() {}
  71. /** Get mutex reference for threadsafe operations. */
  72. virtual Mutex& GetMutex() const = 0;
  73. /** Register StdLog object for error callback.
  74. \param log Pointer to log class */
  75. virtual void RegStdLog(StdLog *log) = 0;
  76. /** Log error to log class for print out / storage. */
  77. virtual void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING) = 0;
  78. // -------------------------------------------------------------------------
  79. // Socket stuff
  80. // -------------------------------------------------------------------------
  81. /** Add socket instance to socket map. Removal is always automatic. */
  82. virtual void Add(Socket *) = 0;
  83. private:
  84. /** Remove socket from socket map, used by Socket class. */
  85. virtual void Remove(Socket *) = 0;
  86. public:
  87. /** Get status of read/write/exception file descriptor set for a socket. */
  88. virtual void Get(SOCKET s,bool& r,bool& w,bool& e) = 0;
  89. /** Set read/write/exception file descriptor sets (fd_set). */
  90. virtual void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true) = 0;
  91. /** Wait for events, generate callbacks. */
  92. virtual int Select(long sec,long usec) = 0;
  93. /** This method will not return until an event has been detected. */
  94. virtual int Select() = 0;
  95. /** Wait for events, generate callbacks. */
  96. virtual int Select(struct timeval *tsel) = 0;
  97. /** Check that a socket really is handled by this socket handler. */
  98. virtual bool Valid(Socket *) = 0;
  99. /** Return number of sockets handled by this handler. */
  100. virtual size_t GetCount() = 0;
  101. /** Override and return false to deny all incoming connections.
  102. \param p ListenSocket class pointer (use GetPort to identify which one) */
  103. virtual bool OkToAccept(Socket *p) = 0;
  104. /** Called by Socket when a socket changes state. */
  105. virtual void AddList(SOCKET s,list_t which_one,bool add) = 0;
  106. // -------------------------------------------------------------------------
  107. // Connection pool
  108. // -------------------------------------------------------------------------
  109. #ifdef ENABLE_POOL
  110. /** Find available open connection (used by connection pool). */
  111. virtual ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&) = 0;
  112. /** Enable connection pool (by default disabled). */
  113. virtual void EnablePool(bool = true) = 0;
  114. /** Check pool status.
  115. \return true if connection pool is enabled */
  116. virtual bool PoolEnabled() = 0;
  117. #endif // ENABLE_POOL
  118. // -------------------------------------------------------------------------
  119. // Socks4
  120. // -------------------------------------------------------------------------
  121. #ifdef ENABLE_SOCKS4
  122. /** Set socks4 server ip that all new tcp sockets should use. */
  123. virtual void SetSocks4Host(ipaddr_t) = 0;
  124. /** Set socks4 server hostname that all new tcp sockets should use. */
  125. virtual void SetSocks4Host(const std::string& ) = 0;
  126. /** Set socks4 server port number that all new tcp sockets should use. */
  127. virtual void SetSocks4Port(port_t) = 0;
  128. /** Set optional socks4 userid. */
  129. virtual void SetSocks4Userid(const std::string& ) = 0;
  130. /** If connection to socks4 server fails, immediately try direct connection to final host. */
  131. virtual void SetSocks4TryDirect(bool = true) = 0;
  132. /** Get socks4 server ip.
  133. \return socks4 server ip */
  134. virtual ipaddr_t GetSocks4Host() = 0;
  135. /** Get socks4 port number.
  136. \return socks4 port number */
  137. virtual port_t GetSocks4Port() = 0;
  138. /** Get socks4 userid (optional).
  139. \return socks4 userid */
  140. virtual const std::string& GetSocks4Userid() = 0;
  141. /** Check status of socks4 try direct flag.
  142. \return true if direct connection should be tried if connection to socks4 server fails */
  143. virtual bool Socks4TryDirect() = 0;
  144. #endif // ENABLE_SOCKS4
  145. // -------------------------------------------------------------------------
  146. // DNS resolve server
  147. // -------------------------------------------------------------------------
  148. #ifdef ENABLE_RESOLVER
  149. /** Enable asynchronous DNS.
  150. \param port Listen port of asynchronous dns server */
  151. virtual void EnableResolver(port_t = 16667) = 0;
  152. /** Check resolver status.
  153. \return true if resolver is enabled */
  154. virtual bool ResolverEnabled() = 0;
  155. /** Queue a dns request.
  156. \param host Hostname to be resolved
  157. \param port Port number will be echoed in Socket::OnResolved callback */
  158. virtual int Resolve(Socket *,const std::string& host,port_t port) = 0;
  159. #ifdef ENABLE_IPV6
  160. virtual int Resolve6(Socket *,const std::string& host,port_t port) = 0;
  161. #endif
  162. /** Do a reverse dns lookup. */
  163. virtual int Resolve(Socket *,ipaddr_t a) = 0;
  164. #ifdef ENABLE_IPV6
  165. virtual int Resolve(Socket *,in6_addr& a) = 0;
  166. #endif
  167. /** Get listen port of asynchronous dns server. */
  168. virtual port_t GetResolverPort() = 0;
  169. /** Resolver thread ready for queries. */
  170. virtual bool ResolverReady() = 0;
  171. /** Returns true if socket waiting for a resolve event. */
  172. virtual bool Resolving(Socket *) = 0;
  173. #endif // ENABLE_RESOLVER
  174. #ifdef ENABLE_TRIGGERS
  175. /** Fetch unique trigger id. */
  176. virtual int TriggerID(Socket *src) = 0;
  177. /** Subscribe socket to trigger id. */
  178. virtual bool Subscribe(int id, Socket *dst) = 0;
  179. /** Unsubscribe socket from trigger id. */
  180. virtual bool Unsubscribe(int id, Socket *dst) = 0;
  181. /** Execute OnTrigger for subscribed sockets.
  182. \param id Trigger ID
  183. \param data Data passed from source to destination
  184. \param erase Empty trigger id source and destination maps if 'true',
  185. Leave them in place if 'false' - if a trigger should be called many times */
  186. virtual void Trigger(int id, Socket::TriggerData& data, bool erase = true) = 0;
  187. #endif // ENABLE_TRIGGERS
  188. #ifdef ENABLE_DETACH
  189. /** Indicates that the handler runs under SocketThread. */
  190. virtual void SetSlave(bool x = true) = 0;
  191. /** Indicates that the handler runs under SocketThread. */
  192. virtual bool IsSlave() = 0;
  193. #endif // ENABLE_DETACH
  194. };
  195. #ifdef SOCKETS_NAMESPACE
  196. }
  197. #endif
  198. #endif // _SOCKETS_ISocketHandler_H