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);
00068         SocketHandler(Mutex& mutex,StdLog *log = NULL);
00069         ~SocketHandler();
00070 
00072         void Add(Socket *);
00073 private:
00075         void Remove(Socket *);
00076 public:
00078         void Get(SOCKET s,bool& r,bool& w,bool& e);
00080         void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true);
00081 
00083         int Select(long sec,long usec);
00085         int Select();
00087         int Select(struct timeval *tsel);
00088 
00090         bool Valid(Socket *);
00092         size_t GetCount();
00093 
00096         bool OkToAccept(Socket *p);
00097 
00099         void AddList(SOCKET s,list_t which_one,bool add);
00100 
00101         // Connection pool
00102 #ifdef ENABLE_POOL
00103 
00104         ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&);
00106         void EnablePool(bool x = true);
00109         bool PoolEnabled();
00110 #endif // ENABLE_POOL
00111 
00112         // Socks4
00113 #ifdef ENABLE_SOCKS4
00114 
00115         void SetSocks4Host(ipaddr_t);
00117         void SetSocks4Host(const std::string& );
00119         void SetSocks4Port(port_t);
00121         void SetSocks4Userid(const std::string& );
00123         void SetSocks4TryDirect(bool x = true);
00126         ipaddr_t GetSocks4Host();
00129         port_t GetSocks4Port();
00132         const std::string& GetSocks4Userid();
00135         bool Socks4TryDirect();
00136 #endif // ENABLE_SOCKS4
00137 
00138         // DNS resolve server
00139 #ifdef ENABLE_RESOLVER
00140 
00142         void EnableResolver(port_t port = 16667);
00145         bool ResolverEnabled();
00149         int Resolve(Socket *,const std::string& host,port_t port);
00150 #ifdef ENABLE_IPV6
00151         int Resolve6(Socket *,const std::string& host,port_t port);
00152 #endif
00153 
00154         int Resolve(Socket *,ipaddr_t a);
00155 #ifdef ENABLE_IPV6
00156         int Resolve(Socket *,in6_addr& a);
00157 #endif
00158 
00159         port_t GetResolverPort();
00161         bool ResolverReady();
00162 #endif
00163 
00165         void CheckSanity();
00166 
00167 protected:
00168         socket_m m_sockets; 
00169         socket_m m_add; 
00170         std::list<Socket *> m_delete; 
00171 
00172 private:
00173         void CheckList(socket_v&,const std::string&); 
00174         SOCKET m_maxsock; 
00175         fd_set m_rfds; 
00176         fd_set m_wfds; 
00177         fd_set m_efds; 
00178         int m_preverror; 
00179         int m_errcnt;
00180 #ifdef ENABLE_SOCKS4
00181         ipaddr_t m_socks4_host; 
00182         port_t m_socks4_port; 
00183         std::string m_socks4_userid; 
00184         bool m_bTryDirect; 
00185 #endif
00186 #ifdef ENABLE_RESOLVER
00187         int m_resolv_id; 
00188         ResolvServer *m_resolver; 
00189         port_t m_resolver_port; 
00190 #endif
00191 #ifdef ENABLE_POOL
00192         bool m_b_enable_pool; 
00193 #endif
00194         socket_v m_fds; 
00195         socket_v m_fds_erase; 
00196         socket_v m_fds_callonconnect; 
00197 #ifdef ENABLE_DETACH
00198         socket_v m_fds_detach; 
00199 #endif
00200         socket_v m_fds_connecting; 
00201         socket_v m_fds_retry; 
00202         socket_v m_fds_close; 
00203 };
00204 
00205 
00206 #ifdef SOCKETS_NAMESPACE
00207 }
00208 #endif
00209 
00210 #endif // _SOCKETS_SocketHandler_H
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4