*Backed out changeset 3be01fb200a5
--HG-- branch : trunk
This commit is contained in:
@@ -4,40 +4,50 @@
|
||||
**/
|
||||
/*
|
||||
Copyright (C) 2004-2007 Anders Hedstrom
|
||||
|
||||
This library is made available under the terms of the GNU GPL.
|
||||
|
||||
If you would like to use this library in a closed-source application,
|
||||
a separate license agreement is available. For information about
|
||||
the closed-source license agreement for the C++ sockets library,
|
||||
please visit http://www.alhem.net/Sockets/license.html and/or
|
||||
email [email protected].
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
#ifndef _SOCKETS_SocketHandler_H
|
||||
#define _SOCKETS_SocketHandler_H
|
||||
|
||||
#include "sockets-config.h"
|
||||
#include <map>
|
||||
#include <list>
|
||||
|
||||
#include "socket_include.h"
|
||||
#include "ISocketHandler.h"
|
||||
|
||||
#ifdef SOCKETS_NAMESPACE
|
||||
namespace SOCKETS_NAMESPACE {
|
||||
#endif
|
||||
|
||||
|
||||
class Socket;
|
||||
#ifdef ENABLE_RESOLVER
|
||||
class ResolvServer;
|
||||
#endif
|
||||
class Mutex;
|
||||
|
||||
/** Socket container class, event generator.
|
||||
\ingroup basic */
|
||||
class SocketHandler : public ISocketHandler
|
||||
@@ -45,43 +55,60 @@ class SocketHandler : public ISocketHandler
|
||||
protected:
|
||||
/** Map type for holding file descriptors/socket object pointers. */
|
||||
typedef std::map<SOCKET,Socket *> socket_m;
|
||||
|
||||
public:
|
||||
/** SocketHandler constructor.
|
||||
\param log Optional log class pointer */
|
||||
SocketHandler(StdLog *log = NULL);
|
||||
|
||||
/** SocketHandler threadsafe constructor.
|
||||
\param mutex Externally declared mutex variable
|
||||
\param log Optional log class pointer */
|
||||
SocketHandler(Mutex& mutex,StdLog *log = NULL);
|
||||
|
||||
~SocketHandler();
|
||||
|
||||
/** Get mutex reference for threadsafe operations. */
|
||||
Mutex& GetMutex() const;
|
||||
|
||||
/** Register StdLog object for error callback.
|
||||
\param log Pointer to log class */
|
||||
void RegStdLog(StdLog *log);
|
||||
|
||||
/** Log error to log class for print out / storage. */
|
||||
void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING);
|
||||
|
||||
/** Add socket instance to socket map. Removal is always automatic. */
|
||||
void Add(Socket *);
|
||||
|
||||
/** Get status of read/write/exception file descriptor set for a socket. */
|
||||
void Get(SOCKET s,bool& r,bool& w,bool& e);
|
||||
|
||||
/** Set read/write/exception file descriptor sets (fd_set). */
|
||||
void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true);
|
||||
|
||||
/** Wait for events, generate callbacks. */
|
||||
int Select(long sec,long usec);
|
||||
|
||||
/** This method will not return until an event has been detected. */
|
||||
int Select();
|
||||
|
||||
/** Wait for events, generate callbacks. */
|
||||
int Select(struct timeval *tsel);
|
||||
|
||||
/** Check that a socket really is handled by this socket handler. */
|
||||
bool Valid(Socket *);
|
||||
|
||||
/** Return number of sockets handled by this handler. */
|
||||
size_t GetCount();
|
||||
|
||||
/** Override and return false to deny all incoming connections.
|
||||
\param p ListenSocket class pointer (use GetPort to identify which one) */
|
||||
bool OkToAccept(Socket *p);
|
||||
|
||||
/** Called by Socket when a socket changes state. */
|
||||
void AddList(SOCKET s,list_t which_one,bool add);
|
||||
|
||||
// Connection pool
|
||||
#ifdef ENABLE_POOL
|
||||
/** Find available open connection (used by connection pool). */
|
||||
@@ -92,6 +119,7 @@ public:
|
||||
\return true if connection pool is enabled */
|
||||
bool PoolEnabled();
|
||||
#endif // ENABLE_POOL
|
||||
|
||||
// Socks4
|
||||
#ifdef ENABLE_SOCKS4
|
||||
/** Set socks4 server ip that all new tcp sockets should use. */
|
||||
@@ -117,6 +145,7 @@ public:
|
||||
\return true if direct connection should be tried if connection to socks4 server fails */
|
||||
bool Socks4TryDirect();
|
||||
#endif // ENABLE_SOCKS4
|
||||
|
||||
// DNS resolve server
|
||||
#ifdef ENABLE_RESOLVER
|
||||
/** Enable asynchronous DNS.
|
||||
@@ -144,6 +173,7 @@ public:
|
||||
/** Returns true if the socket is waiting for a resolve event. */
|
||||
bool Resolving(Socket *);
|
||||
#endif // ENABLE_RESOLVER
|
||||
|
||||
#ifdef ENABLE_TRIGGERS
|
||||
/** Fetch unique trigger id. */
|
||||
int TriggerID(Socket *src);
|
||||
@@ -158,22 +188,27 @@ public:
|
||||
Leave them in place if 'false' - if a trigger should be called many times */
|
||||
void Trigger(int id, Socket::TriggerData& data, bool erase = true);
|
||||
#endif // ENABLE_TRIGGERS
|
||||
|
||||
#ifdef ENABLE_DETACH
|
||||
/** Indicates that the handler runs under SocketThread. */
|
||||
void SetSlave(bool x = true);
|
||||
/** Indicates that the handler runs under SocketThread. */
|
||||
bool IsSlave();
|
||||
#endif
|
||||
|
||||
/** Sanity check of those accursed lists. */
|
||||
void CheckSanity();
|
||||
|
||||
protected:
|
||||
socket_m m_sockets; ///< Active sockets map
|
||||
socket_m m_add; ///< Sockets to be added to sockets map
|
||||
std::list<Socket *> m_delete; ///< Sockets to be deleted (failed when Add)
|
||||
|
||||
protected:
|
||||
StdLog *m_stdlog; ///< Registered log class, or NULL
|
||||
Mutex& m_mutex; ///< Thread safety mutex
|
||||
bool m_b_use_mutex; ///< Mutex correctly initialized
|
||||
|
||||
private:
|
||||
void CheckList(socket_v&,const std::string&); ///< Used by CheckSanity
|
||||
/** Remove socket from socket map, used by Socket class. */
|
||||
@@ -185,6 +220,7 @@ private:
|
||||
int m_preverror; ///< debug select() error
|
||||
int m_errcnt; ///< debug select() error
|
||||
time_t m_tlast; ///< timeout control
|
||||
|
||||
// state lists
|
||||
socket_v m_fds; ///< Active file descriptor list
|
||||
socket_v m_fds_erase; ///< File descriptors that are to be erased from m_sockets
|
||||
@@ -195,6 +231,7 @@ private:
|
||||
socket_v m_fds_timeout; ///< checklist timeout
|
||||
socket_v m_fds_retry; ///< checklist retry client connect
|
||||
socket_v m_fds_close; ///< checklist close and delete
|
||||
|
||||
#ifdef ENABLE_SOCKS4
|
||||
ipaddr_t m_socks4_host; ///< Socks4 server host ip
|
||||
port_t m_socks4_port; ///< Socks4 server port number
|
||||
@@ -220,8 +257,11 @@ private:
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
#ifdef SOCKETS_NAMESPACE
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _SOCKETS_SocketHandler_H
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user