Rune system fix, dk crash fix
[getmangos.git] / dep / include / sockets / SocketHandler.h
blobc7e20dfde8286a7dc5ed78bd7deae52ba19090be
1 /** \file SocketHandler.h
2 ** \date 2004-02-13
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifndef _SOCKETS_SocketHandler_H
31 #define _SOCKETS_SocketHandler_H
33 #include "sockets-config.h"
34 #include <map>
35 #include <list>
37 #include "socket_include.h"
38 #include "ISocketHandler.h"
40 #ifdef SOCKETS_NAMESPACE
41 namespace SOCKETS_NAMESPACE {
42 #endif
45 class Socket;
46 #ifdef ENABLE_RESOLVER
47 class ResolvServer;
48 #endif
49 class Mutex;
51 /** Socket container class, event generator.
52 \ingroup basic */
53 class SocketHandler : public ISocketHandler
55 protected:
56 /** Map type for holding file descriptors/socket object pointers. */
57 typedef std::map<SOCKET,Socket *> socket_m;
59 public:
60 /** SocketHandler constructor.
61 \param log Optional log class pointer */
62 SocketHandler(StdLog *log = NULL);
64 /** SocketHandler threadsafe constructor.
65 \param mutex Externally declared mutex variable
66 \param log Optional log class pointer */
67 SocketHandler(Mutex& mutex,StdLog *log = NULL);
69 ~SocketHandler();
71 /** Get mutex reference for threadsafe operations. */
72 Mutex& GetMutex() const;
74 /** Register StdLog object for error callback.
75 \param log Pointer to log class */
76 void RegStdLog(StdLog *log);
78 /** Log error to log class for print out / storage. */
79 void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING);
81 /** Add socket instance to socket map. Removal is always automatic. */
82 void Add(Socket *);
84 /** Get status of read/write/exception file descriptor set for a socket. */
85 void Get(SOCKET s,bool& r,bool& w,bool& e);
87 /** Set read/write/exception file descriptor sets (fd_set). */
88 void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true);
90 /** Wait for events, generate callbacks. */
91 int Select(long sec,long usec);
93 /** This method will not return until an event has been detected. */
94 int Select();
96 /** Wait for events, generate callbacks. */
97 int Select(struct timeval *tsel);
99 /** Check that a socket really is handled by this socket handler. */
100 bool Valid(Socket *);
102 /** Return number of sockets handled by this handler. */
103 size_t GetCount();
105 /** Override and return false to deny all incoming connections.
106 \param p ListenSocket class pointer (use GetPort to identify which one) */
107 bool OkToAccept(Socket *p);
109 /** Called by Socket when a socket changes state. */
110 void AddList(SOCKET s,list_t which_one,bool add);
112 // Connection pool
113 #ifdef ENABLE_POOL
114 /** Find available open connection (used by connection pool). */
115 ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&);
116 /** Enable connection pool (by default disabled). */
117 void EnablePool(bool x = true);
118 /** Check pool status.
119 \return true if connection pool is enabled */
120 bool PoolEnabled();
121 #endif // ENABLE_POOL
123 // Socks4
124 #ifdef ENABLE_SOCKS4
125 /** Set socks4 server ip that all new tcp sockets should use. */
126 void SetSocks4Host(ipaddr_t);
127 /** Set socks4 server hostname that all new tcp sockets should use. */
128 void SetSocks4Host(const std::string& );
129 /** Set socks4 server port number that all new tcp sockets should use. */
130 void SetSocks4Port(port_t);
131 /** Set optional socks4 userid. */
132 void SetSocks4Userid(const std::string& );
133 /** If connection to socks4 server fails, immediately try direct connection to final host. */
134 void SetSocks4TryDirect(bool x = true);
135 /** Get socks4 server ip.
136 \return socks4 server ip */
137 ipaddr_t GetSocks4Host();
138 /** Get socks4 port number.
139 \return socks4 port number */
140 port_t GetSocks4Port();
141 /** Get socks4 userid (optional).
142 \return socks4 userid */
143 const std::string& GetSocks4Userid();
144 /** Check status of socks4 try direct flag.
145 \return true if direct connection should be tried if connection to socks4 server fails */
146 bool Socks4TryDirect();
147 #endif // ENABLE_SOCKS4
149 // DNS resolve server
150 #ifdef ENABLE_RESOLVER
151 /** Enable asynchronous DNS.
152 \param port Listen port of asynchronous dns server */
153 void EnableResolver(port_t port = 16667);
154 /** Check resolver status.
155 \return true if resolver is enabled */
156 bool ResolverEnabled();
157 /** Queue a dns request.
158 \param host Hostname to be resolved
159 \param port Port number will be echoed in Socket::OnResolved callback */
160 int Resolve(Socket *,const std::string& host,port_t port);
161 #ifdef ENABLE_IPV6
162 int Resolve6(Socket *,const std::string& host,port_t port);
163 #endif
164 /** Do a reverse dns lookup. */
165 int Resolve(Socket *,ipaddr_t a);
166 #ifdef ENABLE_IPV6
167 int Resolve(Socket *,in6_addr& a);
168 #endif
169 /** Get listen port of asynchronous dns server. */
170 port_t GetResolverPort();
171 /** Resolver thread ready for queries. */
172 bool ResolverReady();
173 /** Returns true if the socket is waiting for a resolve event. */
174 bool Resolving(Socket *);
175 #endif // ENABLE_RESOLVER
177 #ifdef ENABLE_TRIGGERS
178 /** Fetch unique trigger id. */
179 int TriggerID(Socket *src);
180 /** Subscribe socket to trigger id. */
181 bool Subscribe(int id, Socket *dst);
182 /** Unsubscribe socket from trigger id. */
183 bool Unsubscribe(int id, Socket *dst);
184 /** Execute OnTrigger for subscribed sockets.
185 \param id Trigger ID
186 \param data Data passed from source to destination
187 \param erase Empty trigger id source and destination maps if 'true',
188 Leave them in place if 'false' - if a trigger should be called many times */
189 void Trigger(int id, Socket::TriggerData& data, bool erase = true);
190 #endif // ENABLE_TRIGGERS
192 #ifdef ENABLE_DETACH
193 /** Indicates that the handler runs under SocketThread. */
194 void SetSlave(bool x = true);
195 /** Indicates that the handler runs under SocketThread. */
196 bool IsSlave();
197 #endif
199 /** Sanity check of those accursed lists. */
200 void CheckSanity();
202 protected:
203 socket_m m_sockets; ///< Active sockets map
204 socket_m m_add; ///< Sockets to be added to sockets map
205 std::list<Socket *> m_delete; ///< Sockets to be deleted (failed when Add)
207 protected:
208 StdLog *m_stdlog; ///< Registered log class, or NULL
209 Mutex& m_mutex; ///< Thread safety mutex
210 bool m_b_use_mutex; ///< Mutex correctly initialized
212 private:
213 void CheckList(socket_v&,const std::string&); ///< Used by CheckSanity
214 /** Remove socket from socket map, used by Socket class. */
215 void Remove(Socket *);
216 SOCKET m_maxsock; ///< Highest file descriptor + 1 in active sockets list
217 fd_set m_rfds; ///< file descriptor set monitored for read events
218 fd_set m_wfds; ///< file descriptor set monitored for write events
219 fd_set m_efds; ///< file descriptor set monitored for exceptions
220 int m_preverror; ///< debug select() error
221 int m_errcnt; ///< debug select() error
222 time_t m_tlast; ///< timeout control
224 // state lists
225 socket_v m_fds; ///< Active file descriptor list
226 socket_v m_fds_erase; ///< File descriptors that are to be erased from m_sockets
227 socket_v m_fds_callonconnect; ///< checklist CallOnConnect
228 #ifdef ENABLE_DETACH
229 socket_v m_fds_detach; ///< checklist Detach
230 #endif
231 socket_v m_fds_timeout; ///< checklist timeout
232 socket_v m_fds_retry; ///< checklist retry client connect
233 socket_v m_fds_close; ///< checklist close and delete
235 #ifdef ENABLE_SOCKS4
236 ipaddr_t m_socks4_host; ///< Socks4 server host ip
237 port_t m_socks4_port; ///< Socks4 server port number
238 std::string m_socks4_userid; ///< Socks4 userid
239 bool m_bTryDirect; ///< Try direct connection if socks4 server fails
240 #endif
241 #ifdef ENABLE_RESOLVER
242 int m_resolv_id; ///< Resolver id counter
243 ResolvServer *m_resolver; ///< Resolver thread pointer
244 port_t m_resolver_port; ///< Resolver listen port
245 std::map<Socket *, bool> m_resolve_q; ///< resolve queue
246 #endif
247 #ifdef ENABLE_POOL
248 bool m_b_enable_pool; ///< Connection pool enabled if true
249 #endif
250 #ifdef ENABLE_TRIGGERS
251 int m_next_trigger_id; ///< Unique trigger id counter
252 std::map<int, Socket *> m_trigger_src; ///< mapping trigger id to source socket
253 std::map<int, std::map<Socket *, bool> > m_trigger_dst; ///< mapping trigger id to destination sockets
254 #endif
255 #ifdef ENABLE_DETACH
256 bool m_slave; ///< Indicates that this is a ISocketHandler run in SocketThread
257 #endif
261 #ifdef SOCKETS_NAMESPACE
263 #endif
265 #endif // _SOCKETS_SocketHandler_H