aboutsummaryrefslogtreecommitdiff
path: root/dep/include/sockets/Socket.h
diff options
context:
space:
mode:
authormaximius <none@none>2009-10-17 15:51:44 -0700
committermaximius <none@none>2009-10-17 15:51:44 -0700
commite585187b248f48b3c6e9247b49fa07c6565d65e5 (patch)
tree637c5b7ddacf41040bef4ea4f75a97da64c6a9bc /dep/include/sockets/Socket.h
parent26b5e033ffde3d161382fc9addbfa99738379641 (diff)
*Backed out changeset 3be01fb200a5
--HG-- branch : trunk
Diffstat (limited to 'dep/include/sockets/Socket.h')
-rw-r--r--dep/include/sockets/Socket.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/dep/include/sockets/Socket.h b/dep/include/sockets/Socket.h
index cb1c2fb3c29..8a7fdc9d2fd 100644
--- a/dep/include/sockets/Socket.h
+++ b/dep/include/sockets/Socket.h
@@ -4,20 +4,25 @@
**/
/*
Copyright (C) 2004-2007 Anders Hedstrom
+
This software 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 license@alhem.net.
+
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.
@@ -25,25 +30,30 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef _SOCKETS_Socket_H
#define _SOCKETS_Socket_H
#include "sockets-config.h"
+
#include <string>
#include <vector>
#include <list>
#ifdef HAVE_OPENSSL
#include <openssl/ssl.h>
#endif
+
#include "socket_include.h"
#include <time.h>
#include "SocketAddress.h"
#include "Thread.h"
+
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
+
class ISocketHandler;
class SocketAddress;
class IFile;
+
/** \defgroup basic Basic sockets */
/** Socket base class.
\ingroup basic */
@@ -58,7 +68,9 @@ class Socket
public:
SocketThread(Socket *p);
~SocketThread();
+
void Run();
+
private:
Socket *GetSocket() const { return m_socket; }
SocketThread(const SocketThread& s) : m_socket(s.GetSocket()) {}
@@ -66,6 +78,7 @@ class Socket
Socket *m_socket;
};
#endif // ENABLE_DETACH
+
#ifdef ENABLE_TRIGGERS
public:
/** Data pass class from source to destination. */
@@ -74,12 +87,15 @@ public:
public:
TriggerData() : m_src(NULL) {}
virtual ~TriggerData() {}
+
Socket *GetSource() const { return m_src; }
void SetSource(Socket *x) { m_src = x; }
+
private:
Socket *m_src;
};
#endif // ENABLE_TRIGGERS
+
/** Socket mode flags. */
/*
enum {
@@ -88,17 +104,20 @@ public:
SOCK_CLOSE = 0x02, ///< Close and delete flag
SOCK_DISABLE_READ = 0x04, ///< Disable checking for read events
SOCK_CONNECTED = 0x08, ///< Socket is connected (tcp/udp)
+
SOCK_ERASED_BY_HANDLER = 0x10, ///< Set by handler before delete
// HAVE_OPENSSL
SOCK_ENABLE_SSL = 0x20, ///< Enable SSL for this TcpSocket
SOCK_SSL = 0x40, ///< ssl negotiation mode (TcpSocket)
SOCK_SSL_SERVER = 0x80, ///< True if this is an incoming ssl TcpSocket connection
+
// ENABLE_IPV6
SOCK_IPV6 = 0x0100, ///< This is an ipv6 socket if this one is true
// ENABLE_POOL
SOCK_CLIENT = 0x0200, ///< only client connections are pooled
SOCK_RETAIN = 0x0400, ///< keep connection on close
SOCK_LOST = 0x0800, ///< connection lost
+
// ENABLE_SOCKS4
SOCK_SOCKS4 = 0x1000, ///< socks4 negotiation mode (TcpSocket)
// ENABLE_DETACH
@@ -106,30 +125,38 @@ public:
SOCK_DETACHED = 0x4000, ///< Socket has been detached
// StreamSocket
STREAMSOCK_CONNECTING = 0x8000, ///< Flag indicating connection in progress
+
STREAMSOCK_FLUSH_BEFORE_CLOSE = 0x010000L, ///< Send all data before closing (default true)
STREAMSOCK_CALL_ON_CONNECT = 0x020000L, ///< OnConnect will be called next ISocketHandler cycle if true
STREAMSOCK_RETRY_CONNECT = 0x040000L, ///< Try another connection attempt next ISocketHandler cycle
STREAMSOCK_LINE_PROTOCOL = 0x080000L, ///< Line protocol mode flag
+
};
*/
+
public:
/** "Default" constructor */
Socket(ISocketHandler&);
+
virtual ~Socket();
+
/** Socket class instantiation method. Used when a "non-standard" constructor
* needs to be used for the socket class. Note: the socket class still needs
* the "default" constructor with one ISocketHandler& as input parameter.
*/
virtual Socket *Create() { return NULL; }
+
/** Returns reference to sockethandler that owns the socket.
If the socket is detached, this is a reference to the slave sockethandler.
*/
ISocketHandler& Handler() const;
+
/** Returns reference to sockethandler that owns the socket.
This one always returns the reference to the original sockethandler,
even if the socket is detached.
*/
ISocketHandler& MasterHandler() const;
+
/** Called by ListenSocket after accept but before socket is added to handler.
* CTcpSocket uses this to create its ICrypt member variable.
* The ICrypt member variable is created by a virtual method, therefore
@@ -138,59 +165,83 @@ public:
* or ssl (port 443).
*/
virtual void Init();
+
/** Create a socket file descriptor.
\param af Address family AF_INET / AF_INET6 / ...
\param type SOCK_STREAM / SOCK_DGRAM / ...
\param protocol "tcp" / "udp" / ... */
SOCKET CreateSocket(int af,int type,const std::string& protocol = "");
+
/** Assign this socket a file descriptor created
by a call to socket() or otherwise. */
void Attach(SOCKET s);
+
/** Return file descriptor assigned to this socket. */
SOCKET GetSocket();
+
/** Close connection immediately - internal use.
\sa SetCloseAndDelete */
virtual int Close();
+
/** Add file descriptor to sockethandler fd_set's. */
void Set(bool bRead,bool bWrite,bool bException = true);
+
/** Returns true when socket file descriptor is valid
and socket is not about to be closed. */
virtual bool Ready();
+
/** Returns pointer to ListenSocket that created this instance
* on an incoming connection. */
Socket *GetParent();
+
/** Used by ListenSocket to set parent pointer of newly created
* socket instance. */
void SetParent(Socket *);
+
/** Get listening port from ListenSocket<>. */
virtual port_t GetPort();
+
/** Set socket non-block operation. */
bool SetNonblocking(bool);
+
/** Set socket non-block operation. */
bool SetNonblocking(bool, SOCKET);
+
/** Total lifetime of instance. */
time_t Uptime();
+
/** Set address/port of last connect() call. */
void SetClientRemoteAddress(SocketAddress&);
+
/** Get address/port of last connect() call. */
std::auto_ptr<SocketAddress> GetClientRemoteAddress();
+
/** Common interface for SendBuf used by Tcp and Udp sockets. */
virtual void SendBuf(const char *,size_t,int = 0);
+
/** Common interface for Send used by Tcp and Udp sockets. */
virtual void Send(const std::string&,int = 0);
+
/** Outgoing traffic counter. */
virtual uint64_t GetBytesSent(bool clear = false);
+
/** Incoming traffic counter. */
virtual uint64_t GetBytesReceived(bool clear = false);
+
// LIST_TIMEOUT
+
/** Enable timeout control. 0=disable timeout check. */
void SetTimeout(time_t secs);
+
/** Check timeout. \return true if time limit reached */
bool Timeout(time_t tnow);
+
/** Used by ListenSocket. ipv4 and ipv6 */
void SetRemoteAddress(SocketAddress&);
+
/** \name Event callbacks */
//@{
+
/** Called when there is something to be read from the file descriptor. */
virtual void OnRead();
/** Called when there is room for another write on the file descriptor. */
@@ -228,6 +279,7 @@ public:
/** Connection timeout. */
virtual void OnConnectTimeout();
//@}
+
/** \name Socket mode flags, set/reset */
//@{
/** Set delete by handler true when you want the sockethandler to
@@ -236,34 +288,43 @@ public:
/** Check delete by handler flag.
\return true if this instance should be deleted by the sockethandler */
bool DeleteByHandler();
+
// LIST_CLOSE - conditional event queue
+
/** Set close and delete to terminate the connection. */
void SetCloseAndDelete(bool = true);
/** Check close and delete flag.
\return true if this socket should be closed and the instance removed */
bool CloseAndDelete();
+
/** Return number of seconds since socket was ordered to close. \sa SetCloseAndDelete */
time_t TimeSinceClose();
+
/** Ignore read events for an output only socket. */
void DisableRead(bool x = true);
/** Check ignore read events flag.
\return true if read events should be ignored */
bool IsDisableRead();
+
/** Set connected status. */
void SetConnected(bool = true);
/** Check connected status.
\return true if connected */
bool IsConnected();
+
/** Connection lost - error while reading/writing from a socket - TcpSocket only. */
void SetLost();
/** Check connection lost status flag, used by TcpSocket only.
\return true if there was an error while r/w causing the socket to close */
bool Lost();
+
/** Set flag indicating the socket is being actively deleted by the sockethandler. */
void SetErasedByHandler(bool x = true);
/** Get value of flag indicating socket is deleted by sockethandler. */
bool ErasedByHandler();
+
//@}
+
/** \name Information about remote connection */
//@{
/** Returns address of remote end. */
@@ -283,6 +344,7 @@ public:
/** ipv4 and ipv6(not implemented) */
std::string GetRemoteHostname();
//@}
+
/** Returns local port number for bound socket file descriptor. */
port_t GetSockPort();
/** Returns local ipv4 address for bound socket file descriptor. */
@@ -304,8 +366,10 @@ public:
checked with an #ifdef below.
This might cause a compile error on other operating systems. */
// --------------------------------------------------------------------------
+
// IP options
//@{
+
bool SetIpOptions(const void *p, socklen_t len);
bool SetIpTOS(unsigned char tos);
unsigned char IpTOS();
@@ -317,6 +381,7 @@ public:
bool SetMulticastLoop(bool x = true);
bool IpAddMembership(struct ip_mreq&);
bool IpDropMembership(struct ip_mreq&);
+
#ifdef IP_PKTINFO
bool SetIpPktinfo(bool x = true);
#endif
@@ -351,9 +416,11 @@ public:
bool IpDropMembership(struct ip_mreqn&);
#endif
//@}
+
// SOCKET options
/** @name Socket Options */
//@{
+
bool SoAcceptconn();
bool SetSoBroadcast(bool x = true);
bool SetSoDebug(bool x = true);
@@ -372,6 +439,7 @@ public:
int SoType();
bool SetSoReuseaddr(bool x = true);
bool SetSoKeepalive(bool x = true);
+
#ifdef SO_BSDCOMPAT
bool SetSoBsdcompat(bool x = true);
#endif
@@ -400,8 +468,10 @@ public:
bool SetSoNosigpipe(bool x = true);
#endif
//@}
+
// TCP options in TcpSocket.h/TcpSocket.cpp
+
#ifdef HAVE_OPENSSL
/** @name SSL Support */
//@{
@@ -436,6 +506,7 @@ public:
virtual SSL *GetSsl() { return NULL; }
//@}
#endif // HAVE_OPENSSL
+
#ifdef ENABLE_IPV6
/** Enable ipv6 for this socket. */
void SetIpv6(bool x = true);
@@ -443,6 +514,7 @@ public:
\return true if this is an ipv6 socket */
bool IsIpv6();
#endif
+
#ifdef ENABLE_POOL
/** @name Connection Pool */
//@{
@@ -470,6 +542,7 @@ public:
void CopyConnection(Socket *sock);
//@}
#endif // ENABLE_POOL
+
#ifdef ENABLE_SOCKS4
/** \name Socks4 support */
//@{
@@ -485,6 +558,7 @@ public:
bool Socks4();
/** Set flag indicating Socks4 handshaking in progress */
void SetSocks4(bool x = true);
+
/** Set socks4 server host address to use */
void SetSocks4Host(ipaddr_t a);
/** Set socks4 server hostname to use. */
@@ -504,6 +578,7 @@ public:
const std::string& GetSocks4Userid();
//@}
#endif // ENABLE_SOCKS4
+
#ifdef ENABLE_RESOLVER
/** \name Asynchronous Resolver */
//@{
@@ -538,6 +613,7 @@ public:
virtual void OnResolveFailed(int id);
//@}
#endif // ENABLE_RESOLVER
+
#ifdef ENABLE_DETACH
/** \name Thread Support */
//@{
@@ -545,12 +621,15 @@ public:
socket is ready for operation again.
\sa ResolvSocket */
virtual void OnDetached();
+
// LIST_DETACH
+
/** Internal use. */
void SetDetach(bool x = true);
/** Check detach flag.
\return true if the socket should detach to its own thread */
bool IsDetach();
+
/** Internal use. */
void SetDetached(bool x = true);
/** Check detached flag.
@@ -565,8 +644,10 @@ public:
void DetachSocket();
//@}
#endif // ENABLE_DETACH
+
/** Write traffic to an IFile. Socket will not delete this object. */
void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; }
+
#ifdef ENABLE_TRIGGERS
/** \name Triggers */
//@{
@@ -580,16 +661,21 @@ public:
virtual void OnCancelled(int id);
//@}
#endif
+
protected:
/** default constructor not available */
Socket() : m_handler(m_handler) {}
/** copy constructor not available */
Socket(const Socket& s) : m_handler(s.m_handler) {}
+
/** assignment operator not available. */
Socket& operator=(const Socket& ) { return *this; }
+
/** All traffic will be written to this IFile, if set. */
IFile *GetTrafficMonitor() { return m_traffic_monitor; }
+
// unsigned long m_flags; ///< boolean flags, replacing old 'bool' members
+
private:
ISocketHandler& m_handler; ///< Reference of ISocketHandler in control of this socket
SOCKET m_socket; ///< File descriptor
@@ -607,29 +693,35 @@ private:
time_t m_timeout_start; ///< Set by SetTimeout
time_t m_timeout_limit; ///< Defined by SetTimeout
bool m_bLost; ///< connection lost
+
#ifdef _WIN32
static WSAInitializer m_winsock_init; ///< Winsock initialization singleton class
#endif
+
#ifdef HAVE_OPENSSL
bool m_b_enable_ssl; ///< Enable SSL for this TcpSocket
bool m_b_ssl; ///< ssl negotiation mode (TcpSocket)
bool m_b_ssl_server; ///< True if this is an incoming ssl TcpSocket connection
#endif
+
#ifdef ENABLE_IPV6
bool m_ipv6; ///< This is an ipv6 socket if this one is true
#endif
+
#ifdef ENABLE_POOL
int m_socket_type; ///< Type of socket, from socket() call
std::string m_socket_protocol; ///< Protocol, from socket() call
bool m_bClient; ///< only client connections are pooled
bool m_bRetain; ///< keep connection on close
#endif
+
#ifdef ENABLE_SOCKS4
bool m_bSocks4; ///< socks4 negotiation mode (TcpSocket)
ipaddr_t m_socks4_host; ///< socks4 server address
port_t m_socks4_port; ///< socks4 server port number
std::string m_socks4_userid; ///< socks4 server usedid
#endif
+
#ifdef ENABLE_DETACH
bool m_detach; ///< Socket ordered to detach flag
bool m_detached; ///< Socket has been detached
@@ -637,9 +729,12 @@ static WSAInitializer m_winsock_init; ///< Winsock initialization singleton cla
ISocketHandler *m_slave_handler; ///< Actual sockethandler while detached
#endif
};
+
#ifdef SOCKETS_NAMESPACE
}
#endif
+
#endif // _SOCKETS_Socket_H
+