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_SocketHandler_H
00031 #define _SOCKETS_SocketHandler_H
00032
00033 #include "sockets-config.h"
00034 #include <map>
00035 #include <list>
00036
00037 #include "socket_include.h"
00038 #include "ISocketHandler.h"
00039
00040 #ifdef SOCKETS_NAMESPACE
00041 namespace SOCKETS_NAMESPACE {
00042 #endif
00043
00044
00045 class Socket;
00046 #ifdef ENABLE_RESOLVER
00047 class ResolvServer;
00048 #endif
00049 class IMutex;
00050
00053 class SocketHandler : public ISocketHandler
00054 {
00055 protected:
00057 typedef std::map<SOCKET,Socket *> socket_m;
00058
00059 public:
00062 SocketHandler(StdLog *log = NULL);
00063
00067 SocketHandler(IMutex& mutex,StdLog *log = NULL);
00068
00069 ~SocketHandler();
00070
00072 IMutex& GetMutex() const;
00073
00076 void RegStdLog(StdLog *log);
00077
00079 void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING);
00080
00082 void Add(Socket *);
00083
00085 void Get(SOCKET s,bool& r,bool& w,bool& e);
00086
00088 void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true);
00089
00091 int Select(long sec,long usec);
00092
00094 int Select();
00095
00097 int Select(struct timeval *tsel);
00098
00100 bool Valid(Socket *);
00101
00103 size_t GetCount();
00104
00107 bool OkToAccept(Socket *p);
00108
00110 void AddList(SOCKET s,list_t which_one,bool add);
00111
00112
00113 #ifdef ENABLE_POOL
00114
00115 ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&);
00117 void EnablePool(bool x = true);
00120 bool PoolEnabled();
00121 #endif // ENABLE_POOL
00122
00123
00124 #ifdef ENABLE_SOCKS4
00125
00126 void SetSocks4Host(ipaddr_t);
00128 void SetSocks4Host(const std::string& );
00130 void SetSocks4Port(port_t);
00132 void SetSocks4Userid(const std::string& );
00134 void SetSocks4TryDirect(bool x = true);
00137 ipaddr_t GetSocks4Host();
00140 port_t GetSocks4Port();
00143 const std::string& GetSocks4Userid();
00146 bool Socks4TryDirect();
00147 #endif // ENABLE_SOCKS4
00148
00149
00150 #ifdef ENABLE_RESOLVER
00151
00153 void EnableResolver(port_t port = 16667);
00156 bool ResolverEnabled();
00160 int Resolve(Socket *,const std::string& host,port_t port);
00161 #ifdef ENABLE_IPV6
00162 int Resolve6(Socket *,const std::string& host,port_t port);
00163 #endif
00164
00165 int Resolve(Socket *,ipaddr_t a);
00166 #ifdef ENABLE_IPV6
00167 int Resolve(Socket *,in6_addr& a);
00168 #endif
00169
00170 port_t GetResolverPort();
00172 bool ResolverReady();
00174 bool Resolving(Socket *);
00175 #endif // ENABLE_RESOLVER
00176
00177 #ifdef ENABLE_TRIGGERS
00178
00179 int TriggerID(Socket *src);
00181 bool Subscribe(int id, Socket *dst);
00183 bool Unsubscribe(int id, Socket *dst);
00189 void Trigger(int id, Socket::TriggerData& data, bool erase = true);
00190 #endif // ENABLE_TRIGGERS
00191
00192 #ifdef ENABLE_DETACH
00193
00194 void SetSlave(bool x = true);
00196 bool IsSlave();
00197 #endif
00198
00200 void CheckSanity();
00201
00202 protected:
00203 socket_m m_sockets;
00204 socket_m m_add;
00205 std::list<Socket *> m_delete;
00206
00207 protected:
00209 void Remove(Socket *);
00210 StdLog *m_stdlog;
00211 IMutex& m_mutex;
00212 bool m_b_use_mutex;
00213
00214 private:
00215 void CheckList(socket_v&,const std::string&);
00216 SOCKET m_maxsock;
00217 fd_set m_rfds;
00218 fd_set m_wfds;
00219 fd_set m_efds;
00220 int m_preverror;
00221 int m_errcnt;
00222 time_t m_tlast;
00223
00224
00225 socket_v m_fds;
00226 socket_v m_fds_erase;
00227 socket_v m_fds_callonconnect;
00228 #ifdef ENABLE_DETACH
00229 socket_v m_fds_detach;
00230 #endif
00231 socket_v m_fds_timeout;
00232 socket_v m_fds_retry;
00233 socket_v m_fds_close;
00234
00235 #ifdef ENABLE_SOCKS4
00236 ipaddr_t m_socks4_host;
00237 port_t m_socks4_port;
00238 std::string m_socks4_userid;
00239 bool m_bTryDirect;
00240 #endif
00241 #ifdef ENABLE_RESOLVER
00242 int m_resolv_id;
00243 ResolvServer *m_resolver;
00244 port_t m_resolver_port;
00245 std::map<Socket *, bool> m_resolve_q;
00246 #endif
00247 #ifdef ENABLE_POOL
00248 bool m_b_enable_pool;
00249 #endif
00250 #ifdef ENABLE_TRIGGERS
00251 int m_next_trigger_id;
00252 std::map<int, Socket *> m_trigger_src;
00253 std::map<int, std::map<Socket *, bool> > m_trigger_dst;
00254 #endif
00255 #ifdef ENABLE_DETACH
00256 bool m_slave;
00257 #endif
00258 };
00259
00260
00261 #ifdef SOCKETS_NAMESPACE
00262 }
00263 #endif
00264
00265 #endif // _SOCKETS_SocketHandler_H
00266