00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _SOCKETS_ISocketHandler_H
00031 #define _SOCKETS_ISocketHandler_H
00032 #include "sockets-config.h"
00033
00034 #include <map>
00035 #include <list>
00036
00037 #include "socket_include.h"
00038 #include "StdLog.h"
00039 #include "Mutex.h"
00040 #include "Socket.h"
00041
00042 #ifdef SOCKETS_NAMESPACE
00043 namespace SOCKETS_NAMESPACE {
00044 #endif
00045
00046 typedef enum {
00047 LIST_CALLONCONNECT = 0,
00048 #ifdef ENABLE_DETACH
00049 LIST_DETACH,
00050 #endif
00051 LIST_CONNECTING,
00052 LIST_RETRY,
00053 LIST_CLOSE
00054 } list_t;
00055
00056 class SocketAddress;
00057
00058
00061 class ISocketHandler
00062 {
00063 friend class Socket;
00064
00065 public:
00068 #ifdef ENABLE_POOL
00069 class PoolSocket : public Socket
00070 {
00071 public:
00072 PoolSocket(ISocketHandler& h,Socket *src) : Socket(h) {
00073 CopyConnection( src );
00074 SetIsClient();
00075 }
00076
00077 void OnRead() {
00078 Handler().LogError(this, "OnRead", 0, "data on hibernating socket", LOG_LEVEL_FATAL);
00079 SetCloseAndDelete();
00080 }
00081 void OnOptions(int,int,int,SOCKET) {}
00082
00083 };
00084 #endif
00085
00086 public:
00089 ISocketHandler(StdLog *log);
00093 ISocketHandler(Mutex& mutex,StdLog *log);
00094 virtual ~ISocketHandler();
00095
00097 Mutex& GetMutex() const;
00098
00099 #ifdef ENABLE_DETACH
00100
00101 void SetSlave(bool x = true);
00103 bool IsSlave();
00104 #endif
00105
00108 void RegStdLog(StdLog *log);
00110 void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING);
00111
00112
00113
00114
00116 virtual void Add(Socket *) = 0;
00117 private:
00119 virtual void Remove(Socket *) = 0;
00120 public:
00122 virtual void Get(SOCKET s,bool& r,bool& w,bool& e) = 0;
00124 virtual void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true) = 0;
00125
00127 virtual int Select(long sec,long usec) = 0;
00129 virtual int Select() = 0;
00131 virtual int Select(struct timeval *tsel) = 0;
00132
00134 virtual bool Valid(Socket *) = 0;
00136 virtual size_t GetCount() = 0;
00137
00140 virtual bool OkToAccept(Socket *p) = 0;
00141
00143 virtual void AddList(SOCKET s,list_t which_one,bool add) = 0;
00144
00145
00146
00147
00148 #ifdef ENABLE_POOL
00149
00150 virtual ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&) {
00151 return NULL;
00152 }
00154 virtual void EnablePool(bool = true) {}
00157 virtual bool PoolEnabled() {
00158 return false;
00159 }
00160 #endif // ENABLE_POOL
00161
00162
00163
00164
00165 #ifdef ENABLE_SOCKS4
00166
00167 virtual void SetSocks4Host(ipaddr_t) {}
00169 virtual void SetSocks4Host(const std::string& ) {}
00171 virtual void SetSocks4Port(port_t) {};
00173 virtual void SetSocks4Userid(const std::string& ) {}
00175 virtual void SetSocks4TryDirect(bool = true) {}
00178 virtual ipaddr_t GetSocks4Host() {
00179 return (ipaddr_t)0;
00180 }
00183 virtual port_t GetSocks4Port() {
00184 return 0;
00185 }
00188 virtual const std::string& GetSocks4Userid() = 0;
00191 virtual bool Socks4TryDirect() {
00192 return false;
00193 }
00194 #endif // ENABLE_SOCKS4
00195
00196
00197
00198
00199 #ifdef ENABLE_RESOLVER
00200
00202 virtual void EnableResolver(port_t = 16667) {}
00205 virtual bool ResolverEnabled() {
00206 return false;
00207 }
00211 virtual int Resolve(Socket *,const std::string& host,port_t port) {
00212 return -1;
00213 }
00214 #ifdef ENABLE_IPV6
00215 virtual int Resolve6(Socket *,const std::string& host,port_t port) {
00216 return -1;
00217 }
00218 #endif
00219
00220 virtual int Resolve(Socket *,ipaddr_t a) {
00221 return -1;
00222 }
00223 #ifdef ENABLE_IPV6
00224 virtual int Resolve(Socket *,in6_addr& a) {
00225 return -1;
00226 }
00227 #endif
00228
00229 virtual port_t GetResolverPort() {
00230 return 0;
00231 }
00233 virtual bool ResolverReady() {
00234 return false;
00235 }
00236 #endif
00237
00238 protected:
00239 StdLog *m_stdlog;
00240 #ifdef ENABLE_DETACH
00241 bool m_slave;
00242 #endif
00243 Mutex& m_mutex;
00244 bool m_b_use_mutex;
00245 };
00246
00247
00248 #ifdef SOCKETS_NAMESPACE
00249 }
00250 #endif
00251
00252 #endif // _SOCKETS_ISocketHandler_H