Logo
~Sockets~
~Examples~
~Contact~


ISocketHandler.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_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         // Socket stuff
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         // Connection pool
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         // Socks4
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         // DNS resolve server
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
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4