Logo
~Sockets~
~Examples~
~Contact~


SocketHandler.h

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004-2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about 
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 "StdLog.h"
00039 #include "Mutex.h"
00040 #include "ISocketHandler.h"
00041 
00042 #ifdef SOCKETS_NAMESPACE
00043 namespace SOCKETS_NAMESPACE {
00044 #endif
00045 
00046 
00047 class Socket;
00048 #ifdef ENABLE_RESOLVER
00049 class ResolvServer;
00050 #endif
00051 class Mutex;
00052 
00055 class SocketHandler : public ISocketHandler
00056 {
00057 protected:
00059         typedef std::map<SOCKET,Socket *> socket_m;
00060 
00061 public:
00064         SocketHandler(StdLog *log = NULL);
00065 
00069         SocketHandler(Mutex& mutex,StdLog *log = NULL);
00070 
00071         ~SocketHandler();
00072 
00074         Mutex& GetMutex() const;
00075 
00078         void RegStdLog(StdLog *log);
00079 
00081         void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING);
00082 
00084         void Add(Socket *);
00085 
00087         void Get(SOCKET s,bool& r,bool& w,bool& e);
00088 
00090         void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true);
00091 
00093         int Select(long sec,long usec);
00094 
00096         int Select();
00097 
00099         int Select(struct timeval *tsel);
00100 
00102         bool Valid(Socket *);
00103 
00105         size_t GetCount();
00106 
00109         bool OkToAccept(Socket *p);
00110 
00112         void AddList(SOCKET s,list_t which_one,bool add);
00113 
00114         // Connection pool
00115 #ifdef ENABLE_POOL
00116 
00117         ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&);
00119         void EnablePool(bool x = true);
00122         bool PoolEnabled();
00123 #endif // ENABLE_POOL
00124 
00125         // Socks4
00126 #ifdef ENABLE_SOCKS4
00127 
00128         void SetSocks4Host(ipaddr_t);
00130         void SetSocks4Host(const std::string& );
00132         void SetSocks4Port(port_t);
00134         void SetSocks4Userid(const std::string& );
00136         void SetSocks4TryDirect(bool x = true);
00139         ipaddr_t GetSocks4Host();
00142         port_t GetSocks4Port();
00145         const std::string& GetSocks4Userid();
00148         bool Socks4TryDirect();
00149 #endif // ENABLE_SOCKS4
00150 
00151         // DNS resolve server
00152 #ifdef ENABLE_RESOLVER
00153 
00155         void EnableResolver(port_t port = 16667);
00158         bool ResolverEnabled();
00162         int Resolve(Socket *,const std::string& host,port_t port);
00163 #ifdef ENABLE_IPV6
00164         int Resolve6(Socket *,const std::string& host,port_t port);
00165 #endif
00166 
00167         int Resolve(Socket *,ipaddr_t a);
00168 #ifdef ENABLE_IPV6
00169         int Resolve(Socket *,in6_addr& a);
00170 #endif
00171 
00172         port_t GetResolverPort();
00174         bool ResolverReady();
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:
00208         StdLog *m_stdlog; 
00209         Mutex& m_mutex; 
00210         bool m_b_use_mutex; 
00211 
00212 private:
00213         void CheckList(socket_v&,const std::string&); 
00214 
00215         void Remove(Socket *);
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         // state lists
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 #endif
00246 #ifdef ENABLE_POOL
00247         bool m_b_enable_pool; 
00248 #endif
00249 #ifdef ENABLE_TRIGGERS
00250         int m_next_trigger_id; 
00251         std::map<int, Socket *> m_trigger_src; 
00252         std::map<int, std::map<Socket *, bool> > m_trigger_dst; 
00253 #endif
00254 #ifdef ENABLE_DETACH
00255         bool m_slave; 
00256 #endif
00257 };
00258 
00259 
00260 #ifdef SOCKETS_NAMESPACE
00261 }
00262 #endif
00263 
00264 #endif // _SOCKETS_SocketHandler_H
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4