PageRenderTime 52ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/include/sockets/SocketHandler.h

https://bitbucket.org/oregon/oregoncore/
C Header | 267 lines | 133 code | 38 blank | 96 comment | 0 complexity | f9f635aba62b77f3be764045851c84f7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0, BSD-2-Clause
  1. /** \file SocketHandler.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_SocketHandler_H
  26. #define _SOCKETS_SocketHandler_H
  27. #include "sockets-config.h"
  28. #include <map>
  29. #include <list>
  30. #include "socket_include.h"
  31. #include "ISocketHandler.h"
  32. #ifdef SOCKETS_NAMESPACE
  33. namespace SOCKETS_NAMESPACE {
  34. #endif
  35. class Socket;
  36. #ifdef ENABLE_RESOLVER
  37. class ResolvServer;
  38. #endif
  39. class Mutex;
  40. /** Socket container class, event generator.
  41. \ingroup basic */
  42. class SocketHandler : public ISocketHandler
  43. {
  44. protected:
  45. /** Map type for holding file descriptors/socket object pointers. */
  46. typedef std::map<SOCKET,Socket *> socket_m;
  47. public:
  48. /** SocketHandler constructor.
  49. \param log Optional log class pointer */
  50. SocketHandler(StdLog *log = NULL);
  51. /** SocketHandler threadsafe constructor.
  52. \param mutex Externally declared mutex variable
  53. \param log Optional log class pointer */
  54. SocketHandler(Mutex& mutex,StdLog *log = NULL);
  55. ~SocketHandler();
  56. /** Get mutex reference for threadsafe operations. */
  57. Mutex& GetMutex() const;
  58. /** Register StdLog object for error callback.
  59. \param log Pointer to log class */
  60. void RegStdLog(StdLog *log);
  61. /** Log error to log class for print out / storage. */
  62. void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING);
  63. /** Add socket instance to socket map. Removal is always automatic. */
  64. void Add(Socket *);
  65. /** Get status of read/write/exception file descriptor set for a socket. */
  66. void Get(SOCKET s,bool& r,bool& w,bool& e);
  67. /** Set read/write/exception file descriptor sets (fd_set). */
  68. void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true);
  69. /** Wait for events, generate callbacks. */
  70. int Select(long sec,long usec);
  71. /** This method will not return until an event has been detected. */
  72. int Select();
  73. /** Wait for events, generate callbacks. */
  74. int Select(struct timeval *tsel);
  75. /** Check that a socket really is handled by this socket handler. */
  76. bool Valid(Socket *);
  77. /** Return number of sockets handled by this handler. */
  78. size_t GetCount();
  79. /** Override and return false to deny all incoming connections.
  80. \param p ListenSocket class pointer (use GetPort to identify which one) */
  81. bool OkToAccept(Socket *p);
  82. /** Called by Socket when a socket changes state. */
  83. void AddList(SOCKET s,list_t which_one,bool add);
  84. // Connection pool
  85. #ifdef ENABLE_POOL
  86. /** Find available open connection (used by connection pool). */
  87. ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&);
  88. /** Enable connection pool (by default disabled). */
  89. void EnablePool(bool x = true);
  90. /** Check pool status.
  91. \return true if connection pool is enabled */
  92. bool PoolEnabled();
  93. #endif // ENABLE_POOL
  94. // Socks4
  95. #ifdef ENABLE_SOCKS4
  96. /** Set socks4 server ip that all new tcp sockets should use. */
  97. void SetSocks4Host(ipaddr_t);
  98. /** Set socks4 server hostname that all new tcp sockets should use. */
  99. void SetSocks4Host(const std::string& );
  100. /** Set socks4 server port number that all new tcp sockets should use. */
  101. void SetSocks4Port(port_t);
  102. /** Set optional socks4 userid. */
  103. void SetSocks4Userid(const std::string& );
  104. /** If connection to socks4 server fails, immediately try direct connection to final host. */
  105. void SetSocks4TryDirect(bool x = true);
  106. /** Get socks4 server ip.
  107. \return socks4 server ip */
  108. ipaddr_t GetSocks4Host();
  109. /** Get socks4 port number.
  110. \return socks4 port number */
  111. port_t GetSocks4Port();
  112. /** Get socks4 userid (optional).
  113. \return socks4 userid */
  114. const std::string& GetSocks4Userid();
  115. /** Check status of socks4 try direct flag.
  116. \return true if direct connection should be tried if connection to socks4 server fails */
  117. bool Socks4TryDirect();
  118. #endif // ENABLE_SOCKS4
  119. // DNS resolve server
  120. #ifdef ENABLE_RESOLVER
  121. /** Enable asynchronous DNS.
  122. \param port Listen port of asynchronous dns server */
  123. void EnableResolver(port_t port = 16667);
  124. /** Check resolver status.
  125. \return true if resolver is enabled */
  126. bool ResolverEnabled();
  127. /** Queue a dns request.
  128. \param host Hostname to be resolved
  129. \param port Port number will be echoed in Socket::OnResolved callback */
  130. int Resolve(Socket *,const std::string& host,port_t port);
  131. #ifdef ENABLE_IPV6
  132. int Resolve6(Socket *,const std::string& host,port_t port);
  133. #endif
  134. /** Do a reverse dns lookup. */
  135. int Resolve(Socket *,ipaddr_t a);
  136. #ifdef ENABLE_IPV6
  137. int Resolve(Socket *,in6_addr& a);
  138. #endif
  139. /** Get listen port of asynchronous dns server. */
  140. port_t GetResolverPort();
  141. /** Resolver thread ready for queries. */
  142. bool ResolverReady();
  143. /** Returns true if the socket is waiting for a resolve event. */
  144. bool Resolving(Socket *);
  145. #endif // ENABLE_RESOLVER
  146. #ifdef ENABLE_TRIGGERS
  147. /** Fetch unique trigger id. */
  148. int TriggerID(Socket *src);
  149. /** Subscribe socket to trigger id. */
  150. bool Subscribe(int id, Socket *dst);
  151. /** Unsubscribe socket from trigger id. */
  152. bool Unsubscribe(int id, Socket *dst);
  153. /** Execute OnTrigger for subscribed sockets.
  154. \param id Trigger ID
  155. \param data Data passed from source to destination
  156. \param erase Empty trigger id source and destination maps if 'true',
  157. Leave them in place if 'false' - if a trigger should be called many times */
  158. void Trigger(int id, Socket::TriggerData& data, bool erase = true);
  159. #endif // ENABLE_TRIGGERS
  160. #ifdef ENABLE_DETACH
  161. /** Indicates that the handler runs under SocketThread. */
  162. void SetSlave(bool x = true);
  163. /** Indicates that the handler runs under SocketThread. */
  164. bool IsSlave();
  165. #endif
  166. /** Sanity check of those accursed lists. */
  167. void CheckSanity();
  168. protected:
  169. socket_m m_sockets; ///< Active sockets map
  170. socket_m m_add; ///< Sockets to be added to sockets map
  171. std::list<Socket *> m_delete; ///< Sockets to be deleted (failed when Add)
  172. protected:
  173. StdLog *m_stdlog; ///< Registered log class, or NULL
  174. Mutex& m_mutex; ///< Thread safety mutex
  175. bool m_b_use_mutex; ///< Mutex correctly initialized
  176. private:
  177. void CheckList(socket_v&,const std::string&); ///< Used by CheckSanity
  178. /** Remove socket from socket map, used by Socket class. */
  179. void Remove(Socket *);
  180. SOCKET m_maxsock; ///< Highest file descriptor + 1 in active sockets list
  181. fd_set m_rfds; ///< file descriptor set monitored for read events
  182. fd_set m_wfds; ///< file descriptor set monitored for write events
  183. fd_set m_efds; ///< file descriptor set monitored for exceptions
  184. int m_preverror; ///< debug select() error
  185. int m_errcnt; ///< debug select() error
  186. time_t m_tlast; ///< timeout control
  187. // state lists
  188. socket_v m_fds; ///< Active file descriptor list
  189. socket_v m_fds_erase; ///< File descriptors that are to be erased from m_sockets
  190. socket_v m_fds_callonconnect; ///< checklist CallOnConnect
  191. #ifdef ENABLE_DETACH
  192. socket_v m_fds_detach; ///< checklist Detach
  193. #endif
  194. socket_v m_fds_timeout; ///< checklist timeout
  195. socket_v m_fds_retry; ///< checklist retry client connect
  196. socket_v m_fds_close; ///< checklist close and delete
  197. #ifdef ENABLE_SOCKS4
  198. ipaddr_t m_socks4_host; ///< Socks4 server host ip
  199. port_t m_socks4_port; ///< Socks4 server port number
  200. std::string m_socks4_userid; ///< Socks4 userid
  201. bool m_bTryDirect; ///< Try direct connection if socks4 server fails
  202. #endif
  203. #ifdef ENABLE_RESOLVER
  204. int m_resolv_id; ///< Resolver id counter
  205. ResolvServer *m_resolver; ///< Resolver thread pointer
  206. port_t m_resolver_port; ///< Resolver listen port
  207. std::map<Socket *, bool> m_resolve_q; ///< resolve queue
  208. #endif
  209. #ifdef ENABLE_POOL
  210. bool m_b_enable_pool; ///< Connection pool enabled if true
  211. #endif
  212. #ifdef ENABLE_TRIGGERS
  213. int m_next_trigger_id; ///< Unique trigger id counter
  214. std::map<int, Socket *> m_trigger_src; ///< mapping trigger id to source socket
  215. std::map<int, std::map<Socket *, bool> > m_trigger_dst; ///< mapping trigger id to destination sockets
  216. #endif
  217. #ifdef ENABLE_DETACH
  218. bool m_slave; ///< Indicates that this is a ISocketHandler run in SocketThread
  219. #endif
  220. };
  221. #ifdef SOCKETS_NAMESPACE
  222. }
  223. #endif
  224. #endif // _SOCKETS_SocketHandler_H