1 | /** \file ISocketHandler.h |
---|
2 | ** \date 2004-02-13 |
---|
3 | ** \author grymse@alhem.net |
---|
4 | **/ |
---|
5 | /* |
---|
6 | Copyright (C) 2004-2007 Anders Hedstrom |
---|
7 | |
---|
8 | This library is made available under the terms of the GNU GPL. |
---|
9 | |
---|
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. |
---|
15 | |
---|
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. |
---|
20 | |
---|
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. |
---|
25 | |
---|
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. |
---|
29 | */ |
---|
30 | #ifndef _SOCKETS_ISocketHandler_H |
---|
31 | #define _SOCKETS_ISocketHandler_H |
---|
32 | #include "sockets-config.h" |
---|
33 | |
---|
34 | #include <list> |
---|
35 | |
---|
36 | #include "socket_include.h" |
---|
37 | #include "Socket.h" |
---|
38 | #include "StdLog.h" |
---|
39 | |
---|
40 | #ifdef SOCKETS_NAMESPACE |
---|
41 | namespace SOCKETS_NAMESPACE { |
---|
42 | #endif |
---|
43 | |
---|
44 | typedef enum { |
---|
45 | LIST_CALLONCONNECT = 0, |
---|
46 | #ifdef ENABLE_DETACH |
---|
47 | LIST_DETACH, |
---|
48 | #endif |
---|
49 | LIST_TIMEOUT, |
---|
50 | LIST_RETRY, |
---|
51 | LIST_CLOSE |
---|
52 | } list_t; |
---|
53 | |
---|
54 | class SocketAddress; |
---|
55 | class Mutex; |
---|
56 | |
---|
57 | |
---|
58 | /** Socket container class, event generator. |
---|
59 | \ingroup basic */ |
---|
60 | class ISocketHandler |
---|
61 | { |
---|
62 | friend class Socket; |
---|
63 | |
---|
64 | public: |
---|
65 | /** Connection pool class for internal use by the ISocketHandler. |
---|
66 | \ingroup internal */ |
---|
67 | #ifdef ENABLE_POOL |
---|
68 | class PoolSocket : public Socket |
---|
69 | { |
---|
70 | public: |
---|
71 | PoolSocket(ISocketHandler& h,Socket *src) : Socket(h) { |
---|
72 | CopyConnection( src ); |
---|
73 | SetIsClient(); |
---|
74 | } |
---|
75 | |
---|
76 | void OnRead() { |
---|
77 | Handler().LogError(this, "OnRead", 0, "data on hibernating socket", LOG_LEVEL_FATAL); |
---|
78 | SetCloseAndDelete(); |
---|
79 | } |
---|
80 | void OnOptions(int,int,int,SOCKET) {} |
---|
81 | |
---|
82 | }; |
---|
83 | #endif |
---|
84 | |
---|
85 | public: |
---|
86 | virtual ~ISocketHandler() {} |
---|
87 | |
---|
88 | /** Get mutex reference for threadsafe operations. */ |
---|
89 | virtual Mutex& GetMutex() const = 0; |
---|
90 | |
---|
91 | /** Register StdLog object for error callback. |
---|
92 | \param log Pointer to log class */ |
---|
93 | virtual void RegStdLog(StdLog *log) = 0; |
---|
94 | |
---|
95 | /** Log error to log class for print out / storage. */ |
---|
96 | virtual void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING) = 0; |
---|
97 | |
---|
98 | // ------------------------------------------------------------------------- |
---|
99 | // Socket stuff |
---|
100 | // ------------------------------------------------------------------------- |
---|
101 | /** Add socket instance to socket map. Removal is always automatic. */ |
---|
102 | virtual void Add(Socket *) = 0; |
---|
103 | private: |
---|
104 | /** Remove socket from socket map, used by Socket class. */ |
---|
105 | virtual void Remove(Socket *) = 0; |
---|
106 | public: |
---|
107 | /** Get status of read/write/exception file descriptor set for a socket. */ |
---|
108 | virtual void Get(SOCKET s,bool& r,bool& w,bool& e) = 0; |
---|
109 | /** Set read/write/exception file descriptor sets (fd_set). */ |
---|
110 | virtual void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true) = 0; |
---|
111 | |
---|
112 | /** Wait for events, generate callbacks. */ |
---|
113 | virtual int Select(long sec,long usec) = 0; |
---|
114 | /** This method will not return until an event has been detected. */ |
---|
115 | virtual int Select() = 0; |
---|
116 | /** Wait for events, generate callbacks. */ |
---|
117 | virtual int Select(struct timeval *tsel) = 0; |
---|
118 | |
---|
119 | /** Check that a socket really is handled by this socket handler. */ |
---|
120 | virtual bool Valid(Socket *) = 0; |
---|
121 | /** Return number of sockets handled by this handler. */ |
---|
122 | virtual size_t GetCount() = 0; |
---|
123 | |
---|
124 | /** Override and return false to deny all incoming connections. |
---|
125 | \param p ListenSocket class pointer (use GetPort to identify which one) */ |
---|
126 | virtual bool OkToAccept(Socket *p) = 0; |
---|
127 | |
---|
128 | /** Called by Socket when a socket changes state. */ |
---|
129 | virtual void AddList(SOCKET s,list_t which_one,bool add) = 0; |
---|
130 | |
---|
131 | // ------------------------------------------------------------------------- |
---|
132 | // Connection pool |
---|
133 | // ------------------------------------------------------------------------- |
---|
134 | #ifdef ENABLE_POOL |
---|
135 | /** Find available open connection (used by connection pool). */ |
---|
136 | virtual ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&) = 0; |
---|
137 | /** Enable connection pool (by default disabled). */ |
---|
138 | virtual void EnablePool(bool = true) = 0; |
---|
139 | /** Check pool status. |
---|
140 | \return true if connection pool is enabled */ |
---|
141 | virtual bool PoolEnabled() = 0; |
---|
142 | #endif // ENABLE_POOL |
---|
143 | |
---|
144 | // ------------------------------------------------------------------------- |
---|
145 | // Socks4 |
---|
146 | // ------------------------------------------------------------------------- |
---|
147 | #ifdef ENABLE_SOCKS4 |
---|
148 | /** Set socks4 server ip that all new tcp sockets should use. */ |
---|
149 | virtual void SetSocks4Host(ipaddr_t) = 0; |
---|
150 | /** Set socks4 server hostname that all new tcp sockets should use. */ |
---|
151 | virtual void SetSocks4Host(const std::string& ) = 0; |
---|
152 | /** Set socks4 server port number that all new tcp sockets should use. */ |
---|
153 | virtual void SetSocks4Port(port_t) = 0; |
---|
154 | /** Set optional socks4 userid. */ |
---|
155 | virtual void SetSocks4Userid(const std::string& ) = 0; |
---|
156 | /** If connection to socks4 server fails, immediately try direct connection to final host. */ |
---|
157 | virtual void SetSocks4TryDirect(bool = true) = 0; |
---|
158 | /** Get socks4 server ip. |
---|
159 | \return socks4 server ip */ |
---|
160 | virtual ipaddr_t GetSocks4Host() = 0; |
---|
161 | /** Get socks4 port number. |
---|
162 | \return socks4 port number */ |
---|
163 | virtual port_t GetSocks4Port() = 0; |
---|
164 | /** Get socks4 userid (optional). |
---|
165 | \return socks4 userid */ |
---|
166 | virtual const std::string& GetSocks4Userid() = 0; |
---|
167 | /** Check status of socks4 try direct flag. |
---|
168 | \return true if direct connection should be tried if connection to socks4 server fails */ |
---|
169 | virtual bool Socks4TryDirect() = 0; |
---|
170 | #endif // ENABLE_SOCKS4 |
---|
171 | |
---|
172 | // ------------------------------------------------------------------------- |
---|
173 | // DNS resolve server |
---|
174 | // ------------------------------------------------------------------------- |
---|
175 | #ifdef ENABLE_RESOLVER |
---|
176 | /** Enable asynchronous DNS. |
---|
177 | \param port Listen port of asynchronous dns server */ |
---|
178 | virtual void EnableResolver(port_t = 16667) = 0; |
---|
179 | /** Check resolver status. |
---|
180 | \return true if resolver is enabled */ |
---|
181 | virtual bool ResolverEnabled() = 0; |
---|
182 | /** Queue a dns request. |
---|
183 | \param host Hostname to be resolved |
---|
184 | \param port Port number will be echoed in Socket::OnResolved callback */ |
---|
185 | virtual int Resolve(Socket *,const std::string& host,port_t port) = 0; |
---|
186 | #ifdef ENABLE_IPV6 |
---|
187 | virtual int Resolve6(Socket *,const std::string& host,port_t port) = 0; |
---|
188 | #endif |
---|
189 | /** Do a reverse dns lookup. */ |
---|
190 | virtual int Resolve(Socket *,ipaddr_t a) = 0; |
---|
191 | #ifdef ENABLE_IPV6 |
---|
192 | virtual int Resolve(Socket *,in6_addr& a) = 0; |
---|
193 | #endif |
---|
194 | /** Get listen port of asynchronous dns server. */ |
---|
195 | virtual port_t GetResolverPort() = 0; |
---|
196 | /** Resolver thread ready for queries. */ |
---|
197 | virtual bool ResolverReady() = 0; |
---|
198 | /** Returns true if socket waiting for a resolve event. */ |
---|
199 | virtual bool Resolving(Socket *) = 0; |
---|
200 | #endif // ENABLE_RESOLVER |
---|
201 | |
---|
202 | #ifdef ENABLE_TRIGGERS |
---|
203 | /** Fetch unique trigger id. */ |
---|
204 | virtual int TriggerID(Socket *src) = 0; |
---|
205 | /** Subscribe socket to trigger id. */ |
---|
206 | virtual bool Subscribe(int id, Socket *dst) = 0; |
---|
207 | /** Unsubscribe socket from trigger id. */ |
---|
208 | virtual bool Unsubscribe(int id, Socket *dst) = 0; |
---|
209 | /** Execute OnTrigger for subscribed sockets. |
---|
210 | \param id Trigger ID |
---|
211 | \param data Data passed from source to destination |
---|
212 | \param erase Empty trigger id source and destination maps if 'true', |
---|
213 | Leave them in place if 'false' - if a trigger should be called many times */ |
---|
214 | virtual void Trigger(int id, Socket::TriggerData& data, bool erase = true) = 0; |
---|
215 | #endif // ENABLE_TRIGGERS |
---|
216 | |
---|
217 | #ifdef ENABLE_DETACH |
---|
218 | /** Indicates that the handler runs under SocketThread. */ |
---|
219 | virtual void SetSlave(bool x = true) = 0; |
---|
220 | /** Indicates that the handler runs under SocketThread. */ |
---|
221 | virtual bool IsSlave() = 0; |
---|
222 | #endif // ENABLE_DETACH |
---|
223 | |
---|
224 | }; |
---|
225 | |
---|
226 | |
---|
227 | #ifdef SOCKETS_NAMESPACE |
---|
228 | } |
---|
229 | #endif |
---|
230 | |
---|
231 | #endif // _SOCKETS_ISocketHandler_H |
---|
232 | |
---|