1 /** \file ISocketHandler.h
3 ** \author grymse@alhem.net
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_ISocketHandler_H
31 #define _SOCKETS_ISocketHandler_H
32 #include "sockets-config.h"
36 #include "socket_include.h"
40 #ifdef SOCKETS_NAMESPACE
41 namespace SOCKETS_NAMESPACE
{
45 LIST_CALLONCONNECT
= 0,
58 /** Socket container class, event generator.
65 /** Connection pool class for internal use by the ISocketHandler.
68 class PoolSocket
: public Socket
71 PoolSocket(ISocketHandler
& h
,Socket
*src
) : Socket(h
) {
72 CopyConnection( src
);
77 Handler().LogError(this, "OnRead", 0, "data on hibernating socket", LOG_LEVEL_FATAL
);
80 void OnOptions(int,int,int,SOCKET
) {}
86 virtual ~ISocketHandler() {}
88 /** Get mutex reference for threadsafe operations. */
89 virtual Mutex
& GetMutex() const = 0;
91 /** Register StdLog object for error callback.
92 \param log Pointer to log class */
93 virtual void RegStdLog(StdLog
*log
) = 0;
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;
98 // -------------------------------------------------------------------------
100 // -------------------------------------------------------------------------
101 /** Add socket instance to socket map. Removal is always automatic. */
102 virtual void Add(Socket
*) = 0;
104 /** Remove socket from socket map, used by Socket class. */
105 virtual void Remove(Socket
*) = 0;
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;
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;
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;
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;
128 /** Called by Socket when a socket changes state. */
129 virtual void AddList(SOCKET s
,list_t which_one
,bool add
) = 0;
131 // -------------------------------------------------------------------------
133 // -------------------------------------------------------------------------
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
144 // -------------------------------------------------------------------------
146 // -------------------------------------------------------------------------
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
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;
187 virtual int Resolve6(Socket
*,const std::string
& host
,port_t port
) = 0;
189 /** Do a reverse dns lookup. */
190 virtual int Resolve(Socket
*,ipaddr_t a
) = 0;
192 virtual int Resolve(Socket
*,in6_addr
& a
) = 0;
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
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.
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
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
227 #ifdef SOCKETS_NAMESPACE
231 #endif // _SOCKETS_ISocketHandler_H