Logo
~Sockets~
~Examples~
~Contact~


Socket Class Reference
[Basic sockets]

Socket base class. More...

#include <Socket.h>

Inheritance diagram for Socket:
Collaboration diagram for Socket:

List of all members.


Public Member Functions

 Socket (ISocketHandler &)
 Socket mode flags.
virtual ~Socket ()
virtual SocketCreate ()
 Socket class instantiation method.
ISocketHandlerHandler () const
 Returns reference to sockethandler that owns the socket.
ISocketHandlerMasterHandler () const
 Returns reference to sockethandler that owns the socket.
virtual void Init ()
 Called by ListenSocket after accept but before socket is added to handler.
SOCKET CreateSocket (int af, int type, const std::string &protocol="")
 Create a socket file descriptor.
void Attach (SOCKET s)
 Assign this socket a file descriptor created by a call to socket() or otherwise.
SOCKET GetSocket ()
 Return file descriptor assigned to this socket.
virtual int Close ()
 Close connection immediately - internal use.
virtual bool Ready ()
 Returns true when socket file descriptor is valid and socket is not about to be closed.
SocketGetParent ()
 Returns pointer to ListenSocket that created this instance on an incoming connection.
void SetParent (Socket *)
 Used by ListenSocket to set parent pointer of newly created socket instance.
virtual port_t GetPort ()
 Get listening port from ListenSocket<>.
bool SetNonblocking (bool)
 Set socket non-block operation.
bool SetNonblocking (bool, SOCKET)
 Set socket non-block operation.
time_t Uptime ()
 Total lifetime of instance.
void SetClientRemoteAddress (SocketAddress &)
 Set address/port of last connect() call.
std::auto_ptr< SocketAddressGetClientRemoteAddress ()
 Get address/port of last connect() call.
virtual void SendBuf (const char *, size_t, int=0)
 Common interface for SendBuf used by Tcp and Udp sockets.
virtual void Send (const std::string &, int=0)
 Common interface for Send used by Tcp and Udp sockets.
virtual uint64_t GetBytesSent (bool clear=false)
 Outgoing traffic counter.
virtual uint64_t GetBytesReceived (bool clear=false)
 Incoming traffic counter.
void SetTimeout (time_t secs)
 Enable timeout control.
bool CheckTimeout ()
bool Timeout (time_t tnow)
 Check timeout.
void SetRemoteAddress (SocketAddress &)
 Used by ListenSocket.
port_t GetSockPort ()
 Returns local port number for bound socket file descriptor.
ipaddr_t GetSockIP4 ()
 Returns local ipv4 address for bound socket file descriptor.
std::string GetSockAddress ()
 Returns local ipv4 address as text for bound socket file descriptor.
socketuid_t UniqueIdentifier ()
void SetCallOnConnect (bool x=true)
 Instruct socket to call OnConnect callback next sockethandler cycle.
bool CallOnConnect ()
 Check call on connect flag.
void SetRetryClientConnect (bool x=true)
 Set flag to initiate a connection attempt after a connection timeout.
bool RetryClientConnect ()
 Check if a connection attempt should be made.
void SetTrafficMonitor (IFile *p)
 Write traffic to an IFile.
Event callbacks
virtual void OnRead ()
 Called when there is something to be read from the file descriptor.
virtual void OnWrite ()
 Called when there is room for another write on the file descriptor.
virtual void OnException ()
 Called on socket exception.
virtual void OnDelete ()
 Called before a socket class is deleted by the ISocketHandler.
virtual void OnConnect ()
 Called when a connection has completed.
virtual void OnAccept ()
 Called when an incoming connection has been completed.
virtual void OnLine (const std::string &)
 Called when a complete line has been read and the socket is in line protocol mode.
virtual void OnConnectFailed ()
 Called on connect timeout (5s).
virtual void OnOptions (int family, int type, int protocol, SOCKET s)=0
 Called when a client socket is created, to set socket options.
virtual bool OnConnectRetry ()
 Connection retry callback - return false to abort connection attempts.
virtual void OnReconnect ()
 a reconnect has been made
virtual void OnDisconnect ()
 TcpSocket: When a disconnect has been detected (recv/SSL_read returns 0 bytes).
virtual void OnDisconnect (short info, int code)
 TcpSocket: When a disconnect has been detected (recv/SSL_read returns 0 bytes).
virtual void OnTimeout ()
 Timeout callback.
virtual void OnConnectTimeout ()
 Connection timeout.
Socket mode flags, set/reset
void SetDeleteByHandler (bool=true)
 Set delete by handler true when you want the sockethandler to delete the socket instance after use.
bool DeleteByHandler ()
 Check delete by handler flag.
void SetCloseAndDelete (bool=true)
 Set close and delete to terminate the connection.
bool CloseAndDelete ()
 Check close and delete flag.
time_t TimeSinceClose ()
 Return number of seconds since socket was ordered to close.
void DisableRead (bool x=true)
 Ignore read events for an output only socket.
bool IsDisableRead ()
 Check ignore read events flag.
void SetConnected (bool=true)
 Set connected status.
bool IsConnected ()
 Check connected status.
void SetLost ()
 Connection lost - error while reading/writing from a socket - TcpSocket only.
bool Lost ()
 Check connection lost status flag, used by TcpSocket only.
void SetErasedByHandler (bool x=true)
 Set flag indicating the socket is being actively deleted by the sockethandler.
bool ErasedByHandler ()
 Get value of flag indicating socket is deleted by sockethandler.
Information about remote connection
std::auto_ptr< SocketAddressGetRemoteSocketAddress ()
 Returns address of remote end.
ipaddr_t GetRemoteIP4 ()
 Returns address of remote end: ipv4.
port_t GetRemotePort ()
 Returns remote port number: ipv4 and ipv6.
std::string GetRemoteAddress ()
 Returns remote ip as string? ipv4 and ipv6.
std::string GetRemoteHostname ()
 ipv4 and ipv6(not implemented)
IP options
When an ip or socket option is available on all of the operating systems I'm testing on (linux 2.4.x, _win32, macosx, solaris9 intel) they are not checked with an ifdef below.

This might cause a compile error on other operating systems.

bool SetIpOptions (const void *p, socklen_t len)
bool SetIpTOS (unsigned char tos)
unsigned char IpTOS ()
bool SetIpTTL (int ttl)
int IpTTL ()
bool SetIpHdrincl (bool x=true)
bool SetIpMulticastTTL (int)
int IpMulticastTTL ()
bool SetMulticastLoop (bool x=true)
bool IpAddMembership (struct ip_mreq &)
bool IpDropMembership (struct ip_mreq &)
Socket Options
bool SoAcceptconn ()
bool SetSoBroadcast (bool x=true)
bool SetSoDebug (bool x=true)
int SoError ()
bool SetSoDontroute (bool x=true)
bool SetSoLinger (int onoff, int linger)
bool SetSoOobinline (bool x=true)
bool SetSoRcvlowat (int)
bool SetSoSndlowat (int)
bool SetSoRcvtimeo (struct timeval &)
bool SetSoSndtimeo (struct timeval &)
bool SetSoRcvbuf (int)
int SoRcvbuf ()
bool SetSoSndbuf (int)
int SoSndbuf ()
int SoType ()
bool SetSoReuseaddr (bool x=true)
bool SetSoKeepalive (bool x=true)
SSL Support
virtual void OnSSLConnect ()
 SSL client/server support - internal use.
virtual void OnSSLAccept ()
 SSL client/server support - internal use.
virtual void OnSSLConnectFailed ()
 SSL negotiation failed for client connect.
virtual void OnSSLAcceptFailed ()
 SSL negotiation failed for server accept.
virtual bool SSLNegotiate ()
 new SSL support
bool IsSSL ()
 Check if SSL is Enabled for this TcpSocket.
void EnableSSL (bool x=true)
 Enable SSL operation for a TcpSocket.
bool IsSSLNegotiate ()
 Still negotiating ssl connection.
void SetSSLNegotiate (bool x=true)
 Set flag indicating ssl handshaking still in progress.
bool IsSSLServer ()
 OnAccept called with SSL Enabled.
void SetSSLServer (bool x=true)
 Set flag indicating that this is a TcpSocket with incoming SSL connection.
virtual SSL_CTX * GetSslContext ()
 SSL; Get pointer to ssl context structure.
virtual SSL * GetSsl ()
 SSL; Get pointer to ssl structure.
Connection Pool
void SetIsClient ()
 Client = connecting TcpSocket.
void SetSocketType (int x)
 Socket type from socket() call.
int GetSocketType ()
 Socket type from socket() call.
void SetSocketProtocol (const std::string &x)
 Protocol type from socket() call.
const std::string & GetSocketProtocol ()
 Protocol type from socket() call.
void SetRetain ()
 Instruct a client socket to stay open in the connection pool after use.
bool Retain ()
 Check retain flag.
void CopyConnection (Socket *sock)
 Copy connection parameters from sock.
Socks4 support
virtual void OnSocks4Connect ()
 Socks4 client support internal use.
virtual void OnSocks4ConnectFailed ()
 Socks4 client support internal use.
virtual bool OnSocks4Read ()
 Socks4 client support internal use.
bool Socks4 ()
 Called when the last write caused the tcp output buffer to become empty.
void SetSocks4 (bool x=true)
 Set flag indicating Socks4 handshaking in progress.
void SetSocks4Host (ipaddr_t a)
 Set socks4 server host address to use.
void SetSocks4Host (const std::string &)
 Set socks4 server hostname to use.
void SetSocks4Port (port_t p)
 Socks4 server port to use.
void SetSocks4Userid (const std::string &x)
 Provide a socks4 userid if required by the socks4 server.
ipaddr_t GetSocks4Host ()
 Get the ip address of socks4 server to use.
port_t GetSocks4Port ()
 Get the socks4 server port to use.
const std::string & GetSocks4Userid ()
 Get socks4 userid.
Asynchronous Resolver
int Resolve (const std::string &host, port_t port=0)
 Request an asynchronous dns resolution.
virtual void OnResolved (int id, ipaddr_t a, port_t port)
 Callback returning a resolved address.
int Resolve (ipaddr_t a)
 Request asynchronous reverse dns lookup.
virtual void OnReverseResolved (int id, const std::string &name)
 Callback returning reverse resolve results.
virtual void OnResolveFailed (int id)
 Callback indicating failed dns lookup.
Thread Support
virtual void OnDetached ()
 Callback fires when a new socket thread has started and this socket is ready for operation again.
void SetDetach (bool x=true)
 Internal use.
bool IsDetach ()
 Check detach flag.
void SetDetached (bool x=true)
 Internal use.
bool IsDetached () const
 Check detached flag.
bool Detach ()
 Order this socket to start its own thread and call OnDetached when ready for operation.
void SetSlaveHandler (ISocketHandler *)
 Store the slave sockethandler pointer.
void DetachSocket ()
 Create new thread for this socket to run detached in.

Protected Member Functions

 Socket ()
 default constructor not available
 Socket (const Socket &s)
 copy constructor not available
Socketoperator= (const Socket &)
 assignment operator not available.
IFileGetTrafficMonitor ()
 All traffic will be written to this IFile, if set.

Private Attributes

ISocketHandlerm_handler
 Reference of ISocketHandler in control of this socket.
SOCKET m_socket
 File descriptor.
bool m_bDel
 Delete by handler flag.
bool m_bClose
 Close and delete flag.
time_t m_tCreate
 Time in seconds when this socket was created.
Socketm_parent
 Pointer to ListenSocket class, valid for incoming sockets.
bool m_b_disable_read
 Disable checking for read events.
bool m_connected
 Socket is connected (tcp/udp).
bool m_b_erased_by_handler
 Set by handler before delete.
time_t m_tClose
 Time in seconds when ordered to close.
std::auto_ptr< SocketAddressm_client_remote_address
 Address of last connect().
std::auto_ptr< SocketAddressm_remote_address
 Remote end address.
IFilem_traffic_monitor
time_t m_timeout_start
 Set by SetTimeout.
time_t m_timeout_limit
 Defined by SetTimeout.
bool m_bLost
 connection lost
socketuid_t m_uid
bool m_call_on_connect
 OnConnect will be called next ISocketHandler cycle if true.
bool m_b_retry_connect
 Try another connection attempt next ISocketHandler cycle.
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.
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
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
bool m_detach
 Socket ordered to detach flag.
bool m_detached
 Socket has been detached.
SocketThreadm_pThread
 Detach socket thread class pointer.
ISocketHandlerm_slave_handler
 Actual sockethandler while detached.

Static Private Attributes

static socketuid_t m_next_uid = 0

Detailed Description

Socket base class.

Definition at line 63 of file Socket.h.


Constructor & Destructor Documentation

Socket::Socket ( ISocketHandler h  ) 

Socket mode flags.

"Default" constructor

Definition at line 76 of file Socket.cpp.

00077   :m_flags(0)
00078 :m_handler(h)
00079 ,m_socket( INVALID_SOCKET )
00080 ,m_bDel(false)
00081 ,m_bClose(false)
00082 ,m_tCreate(time(NULL))
00083 ,m_parent(NULL)
00084 ,m_b_disable_read(false)
00085 ,m_connected(false)
00086 ,m_b_erased_by_handler(false)
00087 ,m_tClose(0)
00088 ,m_client_remote_address(NULL)
00089 ,m_remote_address(NULL)
00090 ,m_traffic_monitor(NULL)
00091 ,m_timeout_start(0)
00092 ,m_timeout_limit(0)
00093 ,m_bLost(false)
00094 ,m_uid(++Socket::m_next_uid)
00095 ,m_call_on_connect(false)
00096 ,m_b_retry_connect(false)
00097 #ifdef HAVE_OPENSSL
00098 ,m_b_enable_ssl(false)
00099 ,m_b_ssl(false)
00100 ,m_b_ssl_server(false)
00101 #endif
00102 #ifdef ENABLE_IPV6
00103 ,m_ipv6(false)
00104 #endif
00105 #ifdef ENABLE_POOL
00106 ,m_socket_type(0)
00107 ,m_bClient(false)
00108 ,m_bRetain(false)
00109 #endif
00110 #ifdef ENABLE_SOCKS4
00111 ,m_bSocks4(false)
00112 ,m_socks4_host(h.GetSocks4Host())
00113 ,m_socks4_port(h.GetSocks4Port())
00114 ,m_socks4_userid(h.GetSocks4Userid())
00115 #endif
00116 #ifdef ENABLE_DETACH
00117 ,m_detach(false)
00118 ,m_detached(false)
00119 ,m_pThread(NULL)
00120 ,m_slave_handler(NULL)
00121 #endif
00122 {
00123 }

Socket::~Socket (  )  [virtual]

Definition at line 126 of file Socket.cpp.

References Close(), Handler(), INVALID_SOCKET, m_bRetain, m_socket, and ISocketHandler::Remove().

00127 {
00128         Handler().Remove(this);
00129         if (m_socket != INVALID_SOCKET
00130 #ifdef ENABLE_POOL
00131                  && !m_bRetain
00132 #endif
00133                 )
00134         {
00135                 Close();
00136         }
00137 }

Socket::Socket (  )  [inline, protected]

default constructor not available

Definition at line 645 of file Socket.h.

00645 : m_handler(m_handler) {}

Socket::Socket ( const Socket s  )  [inline, protected]

copy constructor not available

Definition at line 647 of file Socket.h.

00647 : m_handler(s.m_handler) {}


Member Function Documentation

virtual Socket* Socket::Create (  )  [inline, virtual]

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.

Definition at line 113 of file Socket.h.

Referenced by SctpSocket::PeelOff().

00113 { return NULL; }

ISocketHandler & Socket::Handler (  )  const

Returns reference to sockethandler that owns the socket.

If the socket is detached, this is a reference to the slave sockethandler.

Definition at line 296 of file Socket.cpp.

References IsDetached(), m_handler, and m_slave_handler.

Referenced by SctpSocket::AddAddress(), SctpSocket::AddConnection(), HttpPostSocket::AddFile(), UdpSocket::AddMulticastMembership(), UdpSocket::Bind(), SctpSocket::Bind(), TcpSocket::Close(), Close(), CreateSocket(), HttpdSocket::datetime2httpdate(), HttpGetSocket::DoConnect(), UdpSocket::DropMulticastMembership(), GetClientRemoteAddress(), SctpSocket::getladdrs(), UdpSocket::GetMulticastTTL(), SctpSocket::getpaddrs(), GetPort(), GetRemoteIP4(), TcpSocket::GetSsl(), TcpSocket::GetSslContext(), HttpDebugSocket::Init(), TcpSocket::InitializeContext(), TcpSocket::InitSSLServer(), IpAddMembership(), IpDropMembership(), IpMulticastTTL(), IpTOS(), IpTTL(), UdpSocket::IsBroadcast(), UdpSocket::IsMulticastLoop(), TcpSocket::OnConnectTimeout(), SctpSocket::OnConnectTimeout(), HttpClientSocket::OnData(), ResolvSocket::OnDelete(), ResolvSocket::OnDetached(), OnException(), HttpClientSocket::OnFirst(), ResolvSocket::OnLine(), HTTPSocket::OnLine(), UdpSocket::OnRead(), TcpSocket::OnRead(), SctpSocket::OnRead(), TcpSocket::OnResolved(), OnSocks4Connect(), TcpSocket::OnSocks4ConnectFailed(), OnSocks4ConnectFailed(), TcpSocket::OnSocks4Read(), OnSocks4Read(), TcpSocket::OnWrite(), SctpSocket::OnWrite(), UdpSocket::Open(), TcpSocket::Open(), SctpSocket::Open(), SctpSocket::PeelOff(), SctpSocket::RemoveAddress(), Resolve(), UdpSocket::SendBuf(), TcpSocket::SendBuf(), TcpSocket::SendFromOutputBuffer(), UdpSocket::SendToBuf(), UdpSocket::SetBroadcast(), SetCallOnConnect(), SetClientRemoteAddress(), SetCloseAndDelete(), SetDetach(), HttpPutSocket::SetFile(), SetIpHdrincl(), SetIpMulticastTTL(), SetIpOptions(), SetIpTOS(), SetIpTTL(), UdpSocket::SetMulticastDefaultInterface(), UdpSocket::SetMulticastLoop(), SetMulticastLoop(), UdpSocket::SetMulticastTTL(), SetNonblocking(), SetRetryClientConnect(), SetSoBroadcast(), SetSoDebug(), SetSoDontroute(), SetSoKeepalive(), SetSoLinger(), SetSoOobinline(), SetSoRcvbuf(), SetSoRcvlowat(), SetSoRcvtimeo(), SetSoReuseaddr(), SetSoSndbuf(), SetSoSndlowat(), SetSoSndtimeo(), TcpSocket::SetTcpNodelay(), SetTimeout(), SoAcceptconn(), SoError(), SoRcvbuf(), SoSndbuf(), SoType(), TcpSocket::SSLNegotiate(), TcpSocket::TryWrite(), HTTPSocket::url_this(), TcpSocket::UseCertificateChainFile(), and ~Socket().

00297 {
00298 #ifdef ENABLE_DETACH
00299         if (IsDetached())
00300                 return *m_slave_handler;
00301 #endif
00302         return m_handler;
00303 }

ISocketHandler & Socket::MasterHandler (  )  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.

Definition at line 306 of file Socket.cpp.

References m_handler.

00307 {
00308         return m_handler;
00309 }

void Socket::Init (  )  [virtual]

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 it can't be called directly from the CTcpSocket constructor. Also used to determine if incoming HTTP connection is normal (port 80) or ssl (port 443).

Reimplemented in HttpDebugSocket.

Definition at line 140 of file Socket.cpp.

Referenced by Ajp13Socket::IHttpServer_Respond(), and Ajp13Socket::ReceiveForwardRequest().

00141 {
00142 }

SOCKET Socket::CreateSocket ( int  af,
int  type,
const std::string &  protocol = "" 
)

Create a socket file descriptor.

Parameters:
af Address family AF_INET / AF_INET6 / ...
type SOCK_STREAM / SOCK_DGRAM / ...
protocol "tcp" / "udp" / ...

Definition at line 199 of file Socket.cpp.

References Attach(), Errno, Handler(), INVALID_SOCKET, LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_socket_protocol, m_socket_type, OnOptions(), SetCloseAndDelete(), and StrError.

Referenced by UdpSocket::Bind(), SctpSocket::Bind(), UdpSocket::CreateConnection(), UdpSocket::Open(), TcpSocket::Open(), SctpSocket::Open(), and UdpSocket::SendToBuf().

00200 {
00201         struct protoent *p = NULL;
00202         SOCKET s;
00203 
00204 #ifdef ENABLE_POOL
00205         m_socket_type = type;
00206         m_socket_protocol = protocol;
00207 #endif
00208         if (protocol.size())
00209         {
00210                 p = getprotobyname( protocol.c_str() );
00211                 if (!p)
00212                 {
00213                         Handler().LogError(this, "getprotobyname", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00214                         SetCloseAndDelete();
00215 #ifdef ENABLE_EXCEPTIONS
00216                         throw Exception(std::string("getprotobyname() failed: ") + StrError(Errno));
00217 #endif
00218                         return INVALID_SOCKET;
00219                 }
00220         }
00221         int protno = p ? p -> p_proto : 0;
00222 
00223         s = socket(af, type, protno);
00224         if (s == INVALID_SOCKET)
00225         {
00226                 Handler().LogError(this, "socket", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00227                 SetCloseAndDelete();
00228 #ifdef ENABLE_EXCEPTIONS
00229                 throw Exception(std::string("socket() failed: ") + StrError(Errno));
00230 #endif
00231                 return INVALID_SOCKET;
00232         }
00233         Attach(s);
00234         OnOptions(af, type, protno, s);
00235         Attach(INVALID_SOCKET);
00236         return s;
00237 }

void Socket::Attach ( SOCKET  s  ) 

Assign this socket a file descriptor created by a call to socket() or otherwise.

Definition at line 240 of file Socket.cpp.

References m_socket.

Referenced by UdpSocket::Bind(), SctpSocket::Bind(), CopyConnection(), UdpSocket::CreateConnection(), CreateSocket(), UdpSocket::Open(), TcpSocket::Open(), SctpSocket::Open(), SctpSocket::PeelOff(), and UdpSocket::SendToBuf().

00241 {
00242         m_socket = s;
00243 }

SOCKET Socket::GetSocket (  ) 

Return file descriptor assigned to this socket.

Definition at line 246 of file Socket.cpp.

References m_socket.

Referenced by SctpSocket::AddAddress(), SctpSocket::AddConnection(), UdpSocket::AddMulticastMembership(), UdpSocket::Bind(), SctpSocket::Bind(), TcpSocket::Close(), CopyConnection(), UdpSocket::CreateConnection(), UdpSocket::DropMulticastMembership(), SctpSocket::getladdrs(), UdpSocket::GetMulticastTTL(), SctpSocket::getpaddrs(), GetSockAddress(), GetSockIP4(), GetSockPort(), IpAddMembership(), IpDropMembership(), IpMulticastTTL(), IpTOS(), IpTTL(), UdpSocket::IsBroadcast(), UdpSocket::IsMulticastLoop(), UdpSocket::OnRead(), TcpSocket::OnRead(), SctpSocket::OnRead(), TcpSocket::OnSSLAccept(), TcpSocket::OnSSLConnect(), UdpSocket::Open(), SctpSocket::Open(), SctpSocket::PeelOff(), StreamSocket::Ready(), SctpSocket::RemoveAddress(), UdpSocket::SendBuf(), TcpSocket::SendBuf(), UdpSocket::SendToBuf(), UdpSocket::SetBroadcast(), SetIpHdrincl(), SetIpMulticastTTL(), SetIpOptions(), SetIpTOS(), SetIpTTL(), UdpSocket::SetMulticastDefaultInterface(), UdpSocket::SetMulticastLoop(), SetMulticastLoop(), UdpSocket::SetMulticastTTL(), SetSoBroadcast(), SetSoDebug(), SetSoDontroute(), SetSoKeepalive(), SetSoLinger(), SetSoOobinline(), SetSoRcvbuf(), SetSoRcvlowat(), SetSoRcvtimeo(), SetSoReuseaddr(), SetSoSndbuf(), SetSoSndlowat(), SetSoSndtimeo(), TcpSocket::SetTcpNodelay(), SoAcceptconn(), SoError(), SoRcvbuf(), SoSndbuf(), SoType(), and TcpSocket::TryWrite().

00247 {
00248         return m_socket;
00249 }

int Socket::Close (  )  [virtual]

Close connection immediately - internal use.

See also:
SetCloseAndDelete

Reimplemented in ListenSocket< X >, and TcpSocket.

Definition at line 180 of file Socket.cpp.

References closesocket, Errno, Handler(), INVALID_SOCKET, ISocketHandler::ISocketHandler_Del(), LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, ISocketHandler::LogError(), m_socket, and StrError.

Referenced by TcpSocket::Close(), ~Socket(), and UdpSocket::~UdpSocket().

00181 {
00182         if (m_socket == INVALID_SOCKET) // this could happen
00183         {
00184                 Handler().LogError(this, "Socket::Close", 0, "file descriptor invalid", LOG_LEVEL_WARNING);
00185                 return 0;
00186         }
00187         int n;
00188         Handler().ISocketHandler_Del(this); // remove from fd_set's
00189         if ((n = closesocket(m_socket)) == -1)
00190         {
00191                 // failed...
00192                 Handler().LogError(this, "close", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00193         }
00194         m_socket = INVALID_SOCKET;
00195         return n;
00196 }

bool Socket::Ready (  )  [virtual]

Returns true when socket file descriptor is valid and socket is not about to be closed.

Reimplemented in StreamSocket.

Definition at line 451 of file Socket.cpp.

References CloseAndDelete(), INVALID_SOCKET, and m_socket.

00452 {
00453         if (m_socket != INVALID_SOCKET && !CloseAndDelete())
00454                 return true;
00455         return false;
00456 }

Socket * Socket::GetParent (  ) 

Returns pointer to ListenSocket that created this instance on an incoming connection.

Definition at line 469 of file Socket.cpp.

References m_parent.

Referenced by HttpDebugSocket::Init().

00470 {
00471         return m_parent;
00472 }

void Socket::SetParent ( Socket x  ) 

Used by ListenSocket to set parent pointer of newly created socket instance.

Definition at line 475 of file Socket.cpp.

References m_parent.

00476 {
00477         m_parent = x;
00478 }

port_t Socket::GetPort (  )  [virtual]

Get listening port from ListenSocket<>.

Reimplemented in ListenSocket< X >, and UdpSocket.

Definition at line 481 of file Socket.cpp.

References Handler(), LOG_LEVEL_WARNING, and ISocketHandler::LogError().

Referenced by GetRemotePort(), and HttpDebugSocket::Init().

00482 {
00483         Handler().LogError(this, "GetPort", 0, "GetPort only implemented for ListenSocket", LOG_LEVEL_WARNING);
00484         return 0;
00485 }

bool Socket::SetNonblocking ( bool  bNb  ) 

Set socket non-block operation.

Definition at line 385 of file Socket.cpp.

References Errno, Handler(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), m_socket, and StrError.

Referenced by UdpSocket::Bind(), TcpSocket::Close(), UdpSocket::CreateConnection(), TcpSocket::OnSSLAccept(), TcpSocket::OnSSLConnect(), UdpSocket::Open(), TcpSocket::Open(), SctpSocket::Open(), and UdpSocket::SendToBuf().

00386 {
00387 #ifdef _WIN32
00388         unsigned long l = bNb ? 1 : 0;
00389         int n = ioctlsocket(m_socket, FIONBIO, &l);
00390         if (n != 0)
00391         {
00392                 Handler().LogError(this, "ioctlsocket(FIONBIO)", Errno, "");
00393                 return false;
00394         }
00395         return true;
00396 #else
00397         if (bNb)
00398         {
00399                 if (fcntl(m_socket, F_SETFL, O_NONBLOCK) == -1)
00400                 {
00401                         Handler().LogError(this, "fcntl(F_SETFL, O_NONBLOCK)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00402                         return false;
00403                 }
00404         }
00405         else
00406         {
00407                 if (fcntl(m_socket, F_SETFL, 0) == -1)
00408                 {
00409                         Handler().LogError(this, "fcntl(F_SETFL, 0)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00410                         return false;
00411                 }
00412         }
00413         return true;
00414 #endif
00415 }

bool Socket::SetNonblocking ( bool  bNb,
SOCKET  s 
)

Set socket non-block operation.

Definition at line 418 of file Socket.cpp.

References Errno, Handler(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), and StrError.

00419 {
00420 #ifdef _WIN32
00421         unsigned long l = bNb ? 1 : 0;
00422         int n = ioctlsocket(s, FIONBIO, &l);
00423         if (n != 0)
00424         {
00425                 Handler().LogError(this, "ioctlsocket(FIONBIO)", Errno, "");
00426                 return false;
00427         }
00428         return true;
00429 #else
00430         if (bNb)
00431         {
00432                 if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
00433                 {
00434                         Handler().LogError(this, "fcntl(F_SETFL, O_NONBLOCK)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00435                         return false;
00436                 }
00437         }
00438         else
00439         {
00440                 if (fcntl(s, F_SETFL, 0) == -1)
00441                 {
00442                         Handler().LogError(this, "fcntl(F_SETFL, 0)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00443                         return false;
00444                 }
00445         }
00446         return true;
00447 #endif
00448 }

time_t Socket::Uptime (  ) 

Total lifetime of instance.

Definition at line 501 of file Socket.cpp.

References m_tCreate.

00502 {
00503         return time(NULL) - m_tCreate;
00504 }

void Socket::SetClientRemoteAddress ( SocketAddress ad  ) 

Set address/port of last connect() call.

Definition at line 595 of file Socket.cpp.

References SocketAddress::GetCopy(), Handler(), SocketAddress::IsValid(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), and m_client_remote_address.

Referenced by CopyConnection(), and TcpSocket::Open().

00596 {
00597         if (!ad.IsValid())
00598         {
00599                 Handler().LogError(this, "SetClientRemoteAddress", 0, "remote address not valid", LOG_LEVEL_ERROR);
00600         }
00601         m_client_remote_address = ad.GetCopy();
00602 }

std::auto_ptr< SocketAddress > Socket::GetClientRemoteAddress (  ) 

Get address/port of last connect() call.

Definition at line 605 of file Socket.cpp.

References Handler(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), and m_client_remote_address.

Referenced by CopyConnection(), and TcpSocket::OnSocks4Connect().

00606 {
00607         if (!m_client_remote_address.get())
00608         {
00609                 Handler().LogError(this, "GetClientRemoteAddress", 0, "remote address not yet set", LOG_LEVEL_ERROR);
00610         }
00611         return m_client_remote_address -> GetCopy();
00612 }

void Socket::SendBuf ( const char *  ,
size_t  ,
int  = 0 
) [virtual]

Common interface for SendBuf used by Tcp and Udp sockets.

Reimplemented in TcpSocket, and UdpSocket.

Definition at line 533 of file Socket.cpp.

00534 {
00535 }

void Socket::Send ( const std::string &  ,
int  = 0 
) [virtual]

Common interface for Send used by Tcp and Udp sockets.

Reimplemented in TcpSocket, and UdpSocket.

Definition at line 538 of file Socket.cpp.

00539 {
00540 }

uint64_t Socket::GetBytesSent ( bool  clear = false  )  [virtual]

Outgoing traffic counter.

Reimplemented in TcpSocket.

Definition at line 615 of file Socket.cpp.

00616 {
00617         return 0;
00618 }

uint64_t Socket::GetBytesReceived ( bool  clear = false  )  [virtual]

Incoming traffic counter.

Reimplemented in TcpSocket.

Definition at line 621 of file Socket.cpp.

00622 {
00623         return 0;
00624 }

void Socket::SetTimeout ( time_t  secs  ) 

Enable timeout control.

0=disable timeout check.

Definition at line 1733 of file Socket.cpp.

References Handler(), m_timeout_limit, m_timeout_start, and ISocketHandler::SetTimeout().

Referenced by StreamSocket::SetConnecting().

01734 {
01735         if (!secs)
01736         {
01737                 m_timeout_start = 0;
01738                 m_timeout_limit = 0;
01739                 return;
01740         }
01741         m_timeout_start = time(NULL);
01742         m_timeout_limit = secs;
01743         Handler().SetTimeout();
01744 }

bool Socket::CheckTimeout (  ) 

Definition at line 1747 of file Socket.cpp.

References m_timeout_limit, and m_timeout_start.

01748 {
01749         return m_timeout_start > 0 && m_timeout_limit > 0;
01750 }

bool Socket::Timeout ( time_t  tnow  ) 

Check timeout.

Returns:
true if time limit reached

Definition at line 1763 of file Socket.cpp.

References m_timeout_limit, and m_timeout_start.

01764 {
01765         if (m_timeout_start > 0 && tnow - m_timeout_start > m_timeout_limit)
01766                 return true;
01767         return false;
01768 }

void Socket::SetRemoteAddress ( SocketAddress ad  ) 

Used by ListenSocket.

ipv4 and ipv6

Definition at line 284 of file Socket.cpp.

References SocketAddress::GetCopy(), and m_remote_address.

Referenced by CopyConnection(), and TcpSocket::Open().

00285 {
00286         m_remote_address = ad.GetCopy();
00287 }

void Socket::OnRead (  )  [virtual]

Called when there is something to be read from the file descriptor.

Reimplemented in ISocketHandler::PoolSocket, ListenSocket< X >, SctpSocket, TcpSocket, and UdpSocket.

Definition at line 145 of file Socket.cpp.

00146 {
00147 }

void Socket::OnWrite (  )  [virtual]

Called when there is room for another write on the file descriptor.

Reimplemented in SctpSocket, and TcpSocket.

Definition at line 150 of file Socket.cpp.

00151 {
00152 }

void Socket::OnException (  )  [virtual]

Called on socket exception.

Definition at line 155 of file Socket.cpp.

References Handler(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), SetCloseAndDelete(), SoError(), and StrError.

00156 {
00157         // %! exception doesn't always mean something bad happened, this code should be reworked
00158         // errno valid here?
00159         int err = SoError();
00160         Handler().LogError(this, "exception on select", err, StrError(err), LOG_LEVEL_FATAL);
00161         SetCloseAndDelete();
00162 }

void Socket::OnDelete (  )  [virtual]

Called before a socket class is deleted by the ISocketHandler.

Reimplemented in HttpClientSocket, and ResolvSocket.

Definition at line 165 of file Socket.cpp.

00166 {
00167 }

void Socket::OnConnect (  )  [virtual]

Called when a connection has completed.

Reimplemented in HttpGetSocket, HttpPostSocket, HttpPutSocket, and ResolvSocket.

Definition at line 170 of file Socket.cpp.

Referenced by TcpSocket::OnSocks4Read(), and TcpSocket::SSLNegotiate().

00171 {
00172 }

void Socket::OnAccept (  )  [virtual]

Called when an incoming connection has been completed.

Reimplemented in ResolvSocket, and SmtpdSocket.

Definition at line 175 of file Socket.cpp.

Referenced by TcpSocket::SSLNegotiate().

00176 {
00177 }

void Socket::OnLine ( const std::string &   )  [virtual]

Called when a complete line has been read and the socket is in line protocol mode.

Reimplemented in HTTPSocket, ResolvSocket, SmtpdSocket, and TcpSocket.

Definition at line 459 of file Socket.cpp.

00460 {
00461 }

void Socket::OnConnectFailed (  )  [virtual]

virtual void Socket::OnOptions ( int  family,
int  type,
int  protocol,
SOCKET  s 
) [pure virtual]

Called when a client socket is created, to set socket options.

Parameters:
family AF_INET, AF_INET6, etc
type SOCK_STREAM, SOCK_DGRAM, etc
protocol Protocol number (tcp, udp, sctp, etc)
s Socket file descriptor

Implemented in ISocketHandler::PoolSocket, ListenSocket< X >, SctpSocket, TcpSocket, and UdpSocket.

Referenced by CreateSocket().

bool Socket::OnConnectRetry (  )  [virtual]

Connection retry callback - return false to abort connection attempts.

Definition at line 488 of file Socket.cpp.

Referenced by TcpSocket::OnConnectTimeout(), and SctpSocket::OnConnectTimeout().

00489 {
00490         return true;
00491 }

void Socket::OnReconnect (  )  [virtual]

a reconnect has been made

Definition at line 495 of file Socket.cpp.

Referenced by TcpSocket::SSLNegotiate().

00496 {
00497 }

void Socket::OnDisconnect (  )  [virtual]

TcpSocket: When a disconnect has been detected (recv/SSL_read returns 0 bytes).

Definition at line 555 of file Socket.cpp.

Referenced by TcpSocket::OnRead(), and TcpSocket::TryWrite().

00556 {
00557 }

void Socket::OnDisconnect ( short  info,
int  code 
) [virtual]

TcpSocket: When a disconnect has been detected (recv/SSL_read returns 0 bytes).

Parameters:
info bit 0 read(0)/write(1) bit 1 normal(read or write returned 0)/error(r/w returned -1) bit 2 ssl
code error code from read/write call (errno / ssl error)

Definition at line 560 of file Socket.cpp.

00561 {
00562 }

void Socket::OnTimeout (  )  [virtual]

Timeout callback.

Definition at line 1753 of file Socket.cpp.

01754 {
01755 }

void Socket::OnConnectTimeout (  )  [virtual]

Connection timeout.

Reimplemented in SctpSocket, and TcpSocket.

Definition at line 1758 of file Socket.cpp.

01759 {
01760 }

void Socket::SetDeleteByHandler ( bool  x = true  ) 

Set delete by handler true when you want the sockethandler to delete the socket instance after use.

Definition at line 252 of file Socket.cpp.

References m_bDel.

Referenced by SctpSocket::PeelOff().

00253 {
00254         m_bDel = x;
00255 }

bool Socket::DeleteByHandler (  ) 

Check delete by handler flag.

Returns:
true if this instance should be deleted by the sockethandler

Definition at line 258 of file Socket.cpp.

References m_bDel.

Referenced by Detach().

00259 {
00260         return m_bDel;
00261 }

bool Socket::CloseAndDelete (  ) 

Check close and delete flag.

Returns:
true if this socket should be closed and the instance removed

Definition at line 278 of file Socket.cpp.

References m_bClose.

Referenced by HttpDebugSocket::OnDataComplete(), TcpSocket::OnRead(), StreamSocket::Ready(), Ready(), and TcpSocket::SendBuf().

00279 {
00280         return m_bClose;
00281 }

time_t Socket::TimeSinceClose (  ) 

Return number of seconds since socket was ordered to close.

See also:
SetCloseAndDelete

Definition at line 589 of file Socket.cpp.

References m_tClose.

00590 {
00591         return time(NULL) - m_tClose;
00592 }

void Socket::DisableRead ( bool  x = true  ) 

Ignore read events for an output only socket.

Definition at line 521 of file Socket.cpp.

References m_b_disable_read.

00522 {
00523         m_b_disable_read = x;
00524 }

bool Socket::IsDisableRead (  ) 

Check ignore read events flag.

Returns:
true if read events should be ignored

Definition at line 527 of file Socket.cpp.

References m_b_disable_read.

Referenced by TcpSocket::OnWrite(), SctpSocket::OnWrite(), TcpSocket::SendBuf(), and TcpSocket::SendFromOutputBuffer().

00528 {
00529         return m_b_disable_read;
00530 }

void Socket::SetConnected ( bool  x = true  ) 

Set connected status.

Definition at line 543 of file Socket.cpp.

References m_connected.

Referenced by UdpSocket::Open(), and TcpSocket::SSLNegotiate().

00544 {
00545         m_connected = x;
00546 }

bool Socket::IsConnected (  ) 

Check connected status.

Returns:
true if connected

Definition at line 549 of file Socket.cpp.

References m_connected.

Referenced by TcpSocket::Close(), UdpSocket::SendBuf(), and TcpSocket::SendBuf().

00550 {
00551         return m_connected;
00552 }

void Socket::SetLost (  ) 

Connection lost - error while reading/writing from a socket - TcpSocket only.

Definition at line 565 of file Socket.cpp.

References m_bLost.

Referenced by TcpSocket::OnRead(), and TcpSocket::TryWrite().

00566 {
00567         m_bLost = true;
00568 }

bool Socket::Lost (  ) 

Check connection lost status flag, used by TcpSocket only.

Returns:
true if there was an error while r/w causing the socket to close

Definition at line 571 of file Socket.cpp.

References m_bLost.

Referenced by TcpSocket::Close().

00572 {
00573         return m_bLost;
00574 }

void Socket::SetErasedByHandler ( bool  x = true  ) 

Set flag indicating the socket is being actively deleted by the sockethandler.

Definition at line 577 of file Socket.cpp.

References m_b_erased_by_handler.

00578 {
00579         m_b_erased_by_handler = x;
00580 }

bool Socket::ErasedByHandler (  ) 

Get value of flag indicating socket is deleted by sockethandler.

Definition at line 583 of file Socket.cpp.

References m_b_erased_by_handler.

00584 {
00585         return m_b_erased_by_handler;
00586 }

std::auto_ptr< SocketAddress > Socket::GetRemoteSocketAddress (  ) 

Returns address of remote end.

Definition at line 290 of file Socket.cpp.

References m_remote_address.

Referenced by CopyConnection().

00291 {
00292         return m_remote_address -> GetCopy();
00293 }

ipaddr_t Socket::GetRemoteIP4 (  ) 

Returns address of remote end: ipv4.

Definition at line 312 of file Socket.cpp.

References Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), and m_remote_address.

00313 {
00314         ipaddr_t l = 0;
00315 #ifdef ENABLE_IPV6
00316         if (m_ipv6)
00317         {
00318                 Handler().LogError(this, "GetRemoteIP4", 0, "get ipv4 address for ipv6 socket", LOG_LEVEL_WARNING);
00319         }
00320 #endif
00321         if (m_remote_address.get() != NULL)
00322         {
00323                 struct sockaddr *p = *m_remote_address;
00324                 struct sockaddr_in *sa = (struct sockaddr_in *)p;
00325                 memcpy(&l, &sa -> sin_addr, sizeof(struct in_addr));
00326         }
00327         return l;
00328 }

port_t Socket::GetRemotePort (  ) 

Returns remote port number: ipv4 and ipv6.

Definition at line 355 of file Socket.cpp.

References GetPort(), and m_remote_address.

00356 {
00357         if (!m_remote_address.get())
00358         {
00359                 return 0;
00360         }
00361         return m_remote_address -> GetPort();
00362 }

std::string Socket::GetRemoteAddress (  ) 

Returns remote ip as string? ipv4 and ipv6.

Definition at line 365 of file Socket.cpp.

References m_remote_address.

Referenced by HttpBaseSocket::OnFirst().

00366 {
00367         if (!m_remote_address.get())
00368         {
00369                 return "";
00370         }
00371         return m_remote_address -> Convert(false);
00372 }

std::string Socket::GetRemoteHostname (  ) 

ipv4 and ipv6(not implemented)

Definition at line 375 of file Socket.cpp.

References m_remote_address.

00376 {
00377         if (!m_remote_address.get())
00378         {
00379                 return "";
00380         }
00381         return m_remote_address -> Reverse();
00382 }

port_t Socket::GetSockPort (  ) 

Returns local port number for bound socket file descriptor.

Definition at line 1772 of file Socket.cpp.

References GetSocket().

Referenced by HttpBaseSocket::OnFirst().

01773 {
01774 #ifdef ENABLE_IPV6
01775 #ifdef IPPROTO_IPV6
01776         if (IsIpv6())
01777         {
01778                 struct sockaddr_in6 sa;
01779                 socklen_t sockaddr_length = sizeof(struct sockaddr_in6);
01780                 if (getsockname(GetSocket(), (struct sockaddr *)&sa, (socklen_t*)&sockaddr_length) == -1)
01781                         memset(&sa, 0, sizeof(sa));
01782                 return ntohs(sa.sin6_port);
01783         }
01784 #endif
01785 #endif
01786         struct sockaddr_in sa;
01787         socklen_t sockaddr_length = sizeof(struct sockaddr_in);
01788         if (getsockname(GetSocket(), (struct sockaddr *)&sa, (socklen_t*)&sockaddr_length) == -1)
01789                 memset(&sa, 0, sizeof(sa));
01790         return ntohs(sa.sin_port);
01791 }

ipaddr_t Socket::GetSockIP4 (  ) 

Returns local ipv4 address for bound socket file descriptor.

Definition at line 1795 of file Socket.cpp.

References GetSocket().

01796 {
01797 #ifdef ENABLE_IPV6
01798 #ifdef IPPROTO_IPV6
01799         if (IsIpv6())
01800         {
01801                 return 0;
01802         }
01803 #endif
01804 #endif
01805         struct sockaddr_in sa;
01806         socklen_t sockaddr_length = sizeof(struct sockaddr_in);
01807         if (getsockname(GetSocket(), (struct sockaddr *)&sa, (socklen_t*)&sockaddr_length) == -1)
01808                 memset(&sa, 0, sizeof(sa));
01809         ipaddr_t a;
01810         memcpy(&a, &sa.sin_addr, 4);
01811         return a;
01812 }

std::string Socket::GetSockAddress (  ) 

Returns local ipv4 address as text for bound socket file descriptor.

Definition at line 1816 of file Socket.cpp.

References Ipv4Address::Convert(), and GetSocket().

Referenced by HttpBaseSocket::OnFirst().

01817 {
01818 #ifdef ENABLE_IPV6
01819 #ifdef IPPROTO_IPV6
01820         if (IsIpv6())
01821         {
01822                 return "";
01823         }
01824 #endif
01825 #endif
01826         struct sockaddr_in sa;
01827         socklen_t sockaddr_length = sizeof(struct sockaddr_in);
01828         if (getsockname(GetSocket(), (struct sockaddr *)&sa, (socklen_t*)&sockaddr_length) == -1)
01829                 memset(&sa, 0, sizeof(sa));
01830         Ipv4Address addr( sa );
01831         return addr.Convert();
01832 }

socketuid_t Socket::UniqueIdentifier (  )  [inline]

Definition at line 334 of file Socket.h.

00334 { return m_uid; }

bool Socket::SetIpOptions ( const void *  p,
socklen_t  len 
)

Definition at line 972 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

00973 {
00974 #ifdef IP_OPTIONS
00975         if (setsockopt(GetSocket(), IPPROTO_IP, IP_OPTIONS, (char *)p, len) == -1)
00976         {
00977                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_OPTIONS)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00978                 return false;
00979         }
00980         return true;
00981 #else
00982         Handler().LogError(this, "ip option not available", 0, "IP_OPTIONS", LOG_LEVEL_INFO);
00983         return false;
00984 #endif
00985 }

bool Socket::SetIpTOS ( unsigned char  tos  ) 

Definition at line 1058 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01059 {
01060 #ifdef IP_TOS
01061         if (setsockopt(GetSocket(), IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(tos)) == -1)
01062         {
01063                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_TOS)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01064                 return false;
01065         }
01066         return true;
01067 #else
01068         Handler().LogError(this, "ip option not available", 0, "IP_TOS", LOG_LEVEL_INFO);
01069         return false;
01070 #endif
01071 }

unsigned char Socket::IpTOS (  ) 

Definition at line 1074 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01075 {
01076         unsigned char tos = 0;
01077 #ifdef IP_TOS
01078         socklen_t len = sizeof(tos);
01079         if (getsockopt(GetSocket(), IPPROTO_IP, IP_TOS, (char *)&tos, &len) == -1)
01080         {
01081                 Handler().LogError(this, "getsockopt(IPPROTO_IP, IP_TOS)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01082         }
01083 #else
01084         Handler().LogError(this, "ip option not available", 0, "IP_TOS", LOG_LEVEL_INFO);
01085 #endif
01086         return tos;
01087 }

bool Socket::SetIpTTL ( int  ttl  ) 

Definition at line 1090 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01091 {
01092 #ifdef IP_TTL
01093         if (setsockopt(GetSocket(), IPPROTO_IP, IP_TTL, (char *)&ttl, sizeof(ttl)) == -1)
01094         {
01095                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_TTL)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01096                 return false;
01097         }
01098         return true;
01099 #else
01100         Handler().LogError(this, "ip option not available", 0, "IP_TTL", LOG_LEVEL_INFO);
01101         return false;
01102 #endif
01103 }

int Socket::IpTTL (  ) 

Definition at line 1106 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01107 {
01108         int ttl = 0;
01109 #ifdef IP_TTL
01110         socklen_t len = sizeof(ttl);
01111         if (getsockopt(GetSocket(), IPPROTO_IP, IP_TTL, (char *)&ttl, &len) == -1)
01112         {
01113                 Handler().LogError(this, "getsockopt(IPPROTO_IP, IP_TTL)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01114         }
01115 #else
01116         Handler().LogError(this, "ip option not available", 0, "IP_TTL", LOG_LEVEL_INFO);
01117 #endif
01118         return ttl;
01119 }

bool Socket::SetIpHdrincl ( bool  x = true  ) 

Definition at line 1122 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01123 {
01124 #ifdef IP_HDRINCL
01125         int optval = x ? 1 : 0;
01126         if (setsockopt(GetSocket(), IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval)) == -1)
01127         {
01128                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_HDRINCL)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01129                 return false;
01130         }
01131         return true;
01132 #else
01133         Handler().LogError(this, "ip option not available", 0, "IP_HDRINCL", LOG_LEVEL_INFO);
01134         return false;
01135 #endif
01136 }

bool Socket::SetIpMulticastTTL ( int  ttl  ) 

Definition at line 1195 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01196 {
01197 #ifdef IP_MULTICAST_TTL
01198         if (setsockopt(GetSocket(), IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl)) == -1)
01199         {
01200                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_MULTICAST_TTL)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01201                 return false;
01202         }
01203         return true;
01204 #else
01205         Handler().LogError(this, "ip option not available", 0, "IP_MULTICAST_TTL", LOG_LEVEL_INFO);
01206         return false;
01207 #endif
01208 }

int Socket::IpMulticastTTL (  ) 

Definition at line 1211 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01212 {
01213         int ttl = 0;
01214 #ifdef IP_MULTICAST_TTL
01215         socklen_t len = sizeof(ttl);
01216         if (getsockopt(GetSocket(), IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, &len) == -1)
01217         {
01218                 Handler().LogError(this, "getsockopt(IPPROTO_IP, IP_MULTICAST_TTL)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01219         }
01220 #else
01221         Handler().LogError(this, "ip option not available", 0, "IP_MULTICAST_TTL", LOG_LEVEL_INFO);
01222 #endif
01223         return ttl;
01224 }

bool Socket::SetMulticastLoop ( bool  x = true  ) 

Reimplemented in UdpSocket.

Definition at line 1227 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01228 {
01229 #ifdef IP_MULTICAST_LOOP
01230         int optval = x ? 1 : 0;
01231         if (setsockopt(GetSocket(), IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&optval, sizeof(optval)) == -1)
01232         {
01233                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_MULTICAST_LOOP)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01234                 return false;
01235         }
01236         return true;
01237 #else
01238         Handler().LogError(this, "ip option not available", 0, "IP_MULTICAST_LOOP", LOG_LEVEL_INFO);
01239         return false;
01240 #endif
01241 }

bool Socket::IpAddMembership ( struct ip_mreq &  ref  ) 

Definition at line 1262 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01263 {
01264 #ifdef IP_ADD_MEMBERSHIP
01265         if (setsockopt(GetSocket(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ref, sizeof(struct ip_mreq)) == -1)
01266         {
01267                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01268                 return false;
01269         }
01270         return true;
01271 #else
01272         Handler().LogError(this, "ip option not available", 0, "IP_ADD_MEMBERSHIP", LOG_LEVEL_INFO);
01273         return false;
01274 #endif
01275 }

bool Socket::IpDropMembership ( struct ip_mreq &  ref  ) 

Definition at line 1296 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01297 {
01298 #ifdef IP_DROP_MEMBERSHIP
01299         if (setsockopt(GetSocket(), IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&ref, sizeof(struct ip_mreq)) == -1)
01300         {
01301                 Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_DROP_MEMBERSHIP)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01302                 return false;
01303         }
01304         return true;
01305 #else
01306         Handler().LogError(this, "ip option not available", 0, "IP_DROP_MEMBERSHIP", LOG_LEVEL_INFO);
01307         return false;
01308 #endif
01309 }

bool Socket::SoAcceptconn (  ) 

Definition at line 1363 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01364 {
01365         int value = 0;
01366 #ifdef SO_ACCEPTCONN
01367         socklen_t len = sizeof(value);
01368         if (getsockopt(GetSocket(), SOL_SOCKET, SO_ACCEPTCONN, (char *)&value, &len) == -1)
01369         {
01370                 Handler().LogError(this, "getsockopt(SOL_SOCKET, SO_ACCEPTCONN)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01371         }
01372 #else
01373         Handler().LogError(this, "socket option not available", 0, "SO_ACCEPTCONN", LOG_LEVEL_INFO);
01374 #endif
01375         return value ? true : false;
01376 }

bool Socket::SetSoBroadcast ( bool  x = true  ) 

Definition at line 1406 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01407 {
01408 #ifdef SO_BROADCAST
01409         int optval = x ? 1 : 0;
01410         if (setsockopt(GetSocket(), SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) == -1)
01411         {
01412                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_BROADCAST)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01413                 return false;
01414         }
01415         return true;
01416 #else
01417         Handler().LogError(this, "socket option not available", 0, "SO_BROADCAST", LOG_LEVEL_INFO);
01418         return false;
01419 #endif
01420 }

bool Socket::SetSoDebug ( bool  x = true  ) 

Definition at line 1423 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01424 {
01425 #ifdef SO_DEBUG
01426         int optval = x ? 1 : 0;
01427         if (setsockopt(GetSocket(), SOL_SOCKET, SO_DEBUG, (char *)&optval, sizeof(optval)) == -1)
01428         {
01429                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_DEBUG)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01430                 return false;
01431         }
01432         return true;
01433 #else
01434         Handler().LogError(this, "socket option not available", 0, "SO_DEBUG", LOG_LEVEL_INFO);
01435         return false;
01436 #endif
01437 }

int Socket::SoError (  ) 

Definition at line 1440 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

Referenced by OnException(), TcpSocket::OnWrite(), and SctpSocket::OnWrite().

01441 {
01442         int value = 0;
01443 #ifdef SO_ERROR
01444         socklen_t len = sizeof(value);
01445         if (getsockopt(GetSocket(), SOL_SOCKET, SO_ERROR, (char *)&value, &len) == -1)
01446         {
01447                 Handler().LogError(this, "getsockopt(SOL_SOCKET, SO_ERROR)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01448         }
01449 #else
01450         Handler().LogError(this, "socket option not available", 0, "SO_ERROR", LOG_LEVEL_INFO);
01451 #endif
01452         return value;
01453 }

bool Socket::SetSoDontroute ( bool  x = true  ) 

Definition at line 1456 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01457 {
01458 #ifdef SO_DONTROUTE
01459         int optval = x ? 1 : 0;
01460         if (setsockopt(GetSocket(), SOL_SOCKET, SO_DONTROUTE, (char *)&optval, sizeof(optval)) == -1)
01461         {
01462                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_DONTROUTE)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01463                 return false;
01464         }
01465         return true;
01466 #else
01467         Handler().LogError(this, "socket option not available", 0, "SO_DONTROUTE", LOG_LEVEL_INFO);
01468         return false;
01469 #endif
01470 }

bool Socket::SetSoLinger ( int  onoff,
int  linger 
)

Definition at line 1473 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01474 {
01475 #ifdef SO_LINGER
01476         struct linger stl;
01477         stl.l_onoff = onoff;
01478         stl.l_linger = linger;
01479         if (setsockopt(GetSocket(), SOL_SOCKET, SO_LINGER, (char *)&stl, sizeof(stl)) == -1)
01480         {
01481                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_LINGER)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01482                 return false;
01483         }
01484         return true;
01485 #else
01486         Handler().LogError(this, "socket option not available", 0, "SO_LINGER", LOG_LEVEL_INFO);
01487         return false;
01488 #endif
01489 }

bool Socket::SetSoOobinline ( bool  x = true  ) 

Definition at line 1492 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01493 {
01494 #ifdef SO_OOBINLINE
01495         int optval = x ? 1 : 0;
01496         if (setsockopt(GetSocket(), SOL_SOCKET, SO_OOBINLINE, (char *)&optval, sizeof(optval)) == -1)
01497         {
01498                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_OOBINLINE)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01499                 return false;
01500         }
01501         return true;
01502 #else
01503         Handler().LogError(this, "socket option not available", 0, "SO_OOBINLINE", LOG_LEVEL_INFO);
01504         return false;
01505 #endif
01506 }

bool Socket::SetSoRcvlowat ( int  x  ) 

Definition at line 1549 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01550 {
01551 #ifdef SO_RCVLOWAT
01552         if (setsockopt(GetSocket(), SOL_SOCKET, SO_RCVLOWAT, (char *)&x, sizeof(x)) == -1)
01553         {
01554                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_RCVLOWAT)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01555                 return false;
01556         }
01557         return true;
01558 #else
01559         Handler().LogError(this, "socket option not available", 0, "SO_RCVLOWAT", LOG_LEVEL_INFO);
01560         return false;
01561 #endif
01562 }

bool Socket::SetSoSndlowat ( int  x  ) 

Definition at line 1565 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01566 {
01567 #ifdef SO_SNDLOWAT
01568         if (setsockopt(GetSocket(), SOL_SOCKET, SO_SNDLOWAT, (char *)&x, sizeof(x)) == -1)
01569         {
01570                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_SNDLOWAT)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01571                 return false;
01572         }
01573         return true;
01574 #else
01575         Handler().LogError(this, "socket option not available", 0, "SO_SNDLOWAT", LOG_LEVEL_INFO);
01576         return false;
01577 #endif
01578 }

bool Socket::SetSoRcvtimeo ( struct timeval &  tv  ) 

Definition at line 1581 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01582 {
01583 #ifdef SO_RCVTIMEO
01584         if (setsockopt(GetSocket(), SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) == -1)
01585         {
01586                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_RCVTIMEO)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01587                 return false;
01588         }
01589         return true;
01590 #else
01591         Handler().LogError(this, "socket option not available", 0, "SO_RCVTIMEO", LOG_LEVEL_INFO);
01592         return false;
01593 #endif
01594 }

bool Socket::SetSoSndtimeo ( struct timeval &  tv  ) 

Definition at line 1597 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01598 {
01599 #ifdef SO_SNDTIMEO
01600         if (setsockopt(GetSocket(), SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)) == -1)
01601         {
01602                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_SNDTIMEO)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01603                 return false;
01604         }
01605         return true;
01606 #else
01607         Handler().LogError(this, "socket option not available", 0, "SO_SNDTIMEO", LOG_LEVEL_INFO);
01608         return false;
01609 #endif
01610 }

bool Socket::SetSoRcvbuf ( int  x  ) 

Definition at line 1613 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01614 {
01615 #ifdef SO_RCVBUF
01616         if (setsockopt(GetSocket(), SOL_SOCKET, SO_RCVBUF, (char *)&x, sizeof(x)) == -1)
01617         {
01618                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_RCVBUF)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01619                 return false;
01620         }
01621         return true;
01622 #else
01623         Handler().LogError(this, "socket option not available", 0, "SO_RCVBUF", LOG_LEVEL_INFO);
01624         return false;
01625 #endif
01626 }

int Socket::SoRcvbuf (  ) 

Definition at line 1629 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01630 {
01631         int value = 0;
01632 #ifdef SO_RCVBUF
01633         socklen_t len = sizeof(value);
01634         if (getsockopt(GetSocket(), SOL_SOCKET, SO_RCVBUF, (char *)&value, &len) == -1)
01635         {
01636                 Handler().LogError(this, "getsockopt(SOL_SOCKET, SO_RCVBUF)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01637         }
01638 #else
01639         Handler().LogError(this, "socket option not available", 0, "SO_RCVBUF", LOG_LEVEL_INFO);
01640 #endif
01641         return value;
01642 }

bool Socket::SetSoSndbuf ( int  x  ) 

Definition at line 1658 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01659 {
01660 #ifdef SO_SNDBUF
01661         if (setsockopt(GetSocket(), SOL_SOCKET, SO_SNDBUF, (char *)&x, sizeof(x)) == -1)
01662         {
01663                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_SNDBUF)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01664                 return false;
01665         }
01666         return true;
01667 #else
01668         Handler().LogError(this, "socket option not available", 0, "SO_SNDBUF", LOG_LEVEL_INFO);
01669         return false;
01670 #endif
01671 }

int Socket::SoSndbuf (  ) 

Definition at line 1674 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01675 {
01676         int value = 0;
01677 #ifdef SO_SNDBUF
01678         socklen_t len = sizeof(value);
01679         if (getsockopt(GetSocket(), SOL_SOCKET, SO_SNDBUF, (char *)&value, &len) == -1)
01680         {
01681                 Handler().LogError(this, "getsockopt(SOL_SOCKET, SO_SNDBUF)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01682         }
01683 #else
01684         Handler().LogError(this, "socket option not available", 0, "SO_SNDBUF", LOG_LEVEL_INFO);
01685 #endif
01686         return value;
01687 }

int Socket::SoType (  ) 

Definition at line 1717 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

01718 {
01719         int value = 0;
01720 #ifdef SO_TYPE
01721         socklen_t len = sizeof(value);
01722         if (getsockopt(GetSocket(), SOL_SOCKET, SO_TYPE, (char *)&value, &len) == -1)
01723         {
01724                 Handler().LogError(this, "getsockopt(SOL_SOCKET, SO_TYPE)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01725         }
01726 #else
01727         Handler().LogError(this, "socket option not available", 0, "SO_TYPE", LOG_LEVEL_INFO);
01728 #endif
01729         return value;
01730 }

bool Socket::SetSoReuseaddr ( bool  x = true  ) 

Definition at line 1315 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

Referenced by TcpSocket::OnOptions().

01316 {
01317 #ifdef SO_REUSEADDR
01318         int optval = x ? 1 : 0;
01319         if (setsockopt(GetSocket(), SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval)) == -1)
01320         {
01321                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_REUSEADDR)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01322                 return false;
01323         }
01324         return true;
01325 #else
01326         Handler().LogError(this, "socket option not available", 0, "SO_REUSEADDR", LOG_LEVEL_INFO);
01327         return false;
01328 #endif
01329 }

bool Socket::SetSoKeepalive ( bool  x = true  ) 

Definition at line 1332 of file Socket.cpp.

References Errno, GetSocket(), Handler(), LOG_LEVEL_FATAL, LOG_LEVEL_INFO, ISocketHandler::LogError(), and StrError.

Referenced by TcpSocket::OnOptions().

01333 {
01334 #ifdef SO_KEEPALIVE
01335         int optval = x ? 1 : 0;
01336         if (setsockopt(GetSocket(), SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) == -1)
01337         {
01338                 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_KEEPALIVE)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
01339                 return false;
01340         }
01341         return true;
01342 #else
01343         Handler().LogError(this, "socket option not available", 0, "SO_KEEPALIVE", LOG_LEVEL_INFO);
01344         return false;
01345 #endif
01346 }

void Socket::SetCallOnConnect ( bool  x = true  ) 

Instruct socket to call OnConnect callback next sockethandler cycle.

Definition at line 627 of file Socket.cpp.

References Handler(), m_call_on_connect, and ISocketHandler::SetCallOnConnect().

Referenced by TcpSocket::OnWrite(), SctpSocket::OnWrite(), and TcpSocket::Open().

00628 {
00629         m_call_on_connect = x;
00630         if (x)
00631                 Handler().SetCallOnConnect();
00632 }

bool Socket::CallOnConnect (  ) 

Check call on connect flag.

Returns:
true if OnConnect() should be called a.s.a.p

Definition at line 635 of file Socket.cpp.

References m_call_on_connect.

00636 {
00637         return m_call_on_connect;
00638 }

void Socket::SetRetryClientConnect ( bool  x = true  ) 

Set flag to initiate a connection attempt after a connection timeout.

Definition at line 641 of file Socket.cpp.

References Handler(), m_b_retry_connect, and ISocketHandler::SetRetry().

Referenced by TcpSocket::OnConnectTimeout(), SctpSocket::OnConnectTimeout(), and TcpSocket::OnSocks4ConnectFailed().

00642 {
00643         m_b_retry_connect = x;
00644         if (x)
00645                 Handler().SetRetry();
00646 }

bool Socket::RetryClientConnect (  ) 

Check if a connection attempt should be made.

Returns:
true when another attempt should be made

Definition at line 649 of file Socket.cpp.

References m_b_retry_connect.

00650 {
00651         return m_b_retry_connect;
00652 }

void Socket::OnSSLConnect (  )  [virtual]

SSL client/server support - internal use.

See also:
TcpSocket

Reimplemented in TcpSocket.

Definition at line 656 of file Socket.cpp.

00657 {
00658 }

void Socket::OnSSLAccept (  )  [virtual]

SSL client/server support - internal use.

See also:
TcpSocket

Reimplemented in TcpSocket.

Definition at line 661 of file Socket.cpp.

00662 {
00663 }

void Socket::OnSSLConnectFailed (  )  [virtual]

SSL negotiation failed for client connect.

Definition at line 708 of file Socket.cpp.

Referenced by TcpSocket::SSLNegotiate().

00709 {
00710 }

void Socket::OnSSLAcceptFailed (  )  [virtual]

SSL negotiation failed for server accept.

Definition at line 713 of file Socket.cpp.

Referenced by TcpSocket::SSLNegotiate().

00714 {
00715 }

bool Socket::SSLNegotiate (  )  [virtual]

new SSL support

Reimplemented in TcpSocket.

Definition at line 666 of file Socket.cpp.

00667 {
00668         return false;
00669 }

bool Socket::IsSSL (  ) 

Check if SSL is Enabled for this TcpSocket.

Returns:
true if this is a TcpSocket with SSL enabled

Definition at line 672 of file Socket.cpp.

References m_b_enable_ssl.

Referenced by TcpSocket::Close(), TcpSocket::OnRead(), TcpSocket::SendBuf(), and TcpSocket::TryWrite().

00673 {
00674         return m_b_enable_ssl;
00675 }

void Socket::EnableSSL ( bool  x = true  ) 

Enable SSL operation for a TcpSocket.

Definition at line 678 of file Socket.cpp.

References m_b_enable_ssl.

Referenced by HttpDebugSocket::Init(), and HTTPSocket::url_this().

00679 {
00680         m_b_enable_ssl = x;
00681 }

bool Socket::IsSSLNegotiate (  ) 

Still negotiating ssl connection.

Returns:
true if ssl negotiating is still in progress

Definition at line 684 of file Socket.cpp.

References m_b_ssl.

00685 {
00686         return m_b_ssl;
00687 }

void Socket::SetSSLNegotiate ( bool  x = true  ) 

Set flag indicating ssl handshaking still in progress.

Definition at line 690 of file Socket.cpp.

References m_b_ssl.

Referenced by TcpSocket::OnSSLAccept(), TcpSocket::OnSSLConnect(), and TcpSocket::SSLNegotiate().

00691 {
00692         m_b_ssl = x;
00693 }

bool Socket::IsSSLServer (  ) 

OnAccept called with SSL Enabled.

Returns:
true if this is a TcpSocket with an incoming SSL connection

Definition at line 696 of file Socket.cpp.

References m_b_ssl_server.

Referenced by TcpSocket::SSLNegotiate().

00697 {
00698         return m_b_ssl_server;
00699 }

void Socket::SetSSLServer ( bool  x = true  ) 

Set flag indicating that this is a TcpSocket with incoming SSL connection.

Definition at line 702 of file Socket.cpp.

References m_b_ssl_server.

Referenced by TcpSocket::OnSSLAccept().

00703 {
00704         m_b_ssl_server = x;
00705 }

virtual SSL_CTX* Socket::GetSslContext (  )  [inline, virtual]

SSL; Get pointer to ssl context structure.

Reimplemented in TcpSocket.

Definition at line 496 of file Socket.h.

00496 { return NULL; }

virtual SSL* Socket::GetSsl (  )  [inline, virtual]

SSL; Get pointer to ssl structure.

Reimplemented in TcpSocket.

Definition at line 498 of file Socket.h.

00498 { return NULL; }

void Socket::SetIsClient (  ) 

Client = connecting TcpSocket.

Definition at line 734 of file Socket.cpp.

References m_bClient.

Referenced by TcpSocket::Open().

00735 {
00736         m_bClient = true;
00737 }

void Socket::SetSocketType ( int  x  ) 

Socket type from socket() call.

Definition at line 740 of file Socket.cpp.

References m_socket_type.

Referenced by CopyConnection().

00741 {
00742         m_socket_type = x;
00743 }

int Socket::GetSocketType (  ) 

Socket type from socket() call.

Definition at line 746 of file Socket.cpp.

References m_socket_type.

Referenced by CopyConnection().

00747 {
00748         return m_socket_type;
00749 }

void Socket::SetSocketProtocol ( const std::string &  x  ) 

Protocol type from socket() call.

Definition at line 752 of file Socket.cpp.

References m_socket_protocol.

Referenced by CopyConnection().

00753 {
00754         m_socket_protocol = x;
00755 }

const std::string & Socket::GetSocketProtocol (  ) 

Protocol type from socket() call.

Definition at line 758 of file Socket.cpp.

References m_socket_protocol.

Referenced by CopyConnection().

00759 {
00760         return m_socket_protocol;
00761 }

void Socket::SetRetain (  ) 

Instruct a client socket to stay open in the connection pool after use.

If you have connected to a server using tcp, you can call SetRetain to leave the connection open after your socket instance has been deleted. The next connection you make to the same server will reuse the already opened connection, if it is still available.

Definition at line 764 of file Socket.cpp.

References m_bClient, and m_bRetain.

Referenced by HTTPSocket::OnLine().

00765 {
00766         if (m_bClient)
00767                 m_bRetain = true;
00768 }

bool Socket::Retain (  ) 

Check retain flag.

Returns:
true if the socket should be moved to connection pool after use

Definition at line 771 of file Socket.cpp.

References m_bRetain.

00772 {
00773         return m_bRetain;
00774 }

void Socket::CopyConnection ( Socket sock  ) 

Copy connection parameters from sock.

Definition at line 720 of file Socket.cpp.

References Attach(), GetClientRemoteAddress(), GetRemoteSocketAddress(), GetSocket(), GetSocketProtocol(), GetSocketType(), SetClientRemoteAddress(), SetRemoteAddress(), SetSocketProtocol(), and SetSocketType().

Referenced by TcpSocket::Open().

00721 {
00722         Attach( sock -> GetSocket() );
00723 #ifdef ENABLE_IPV6
00724         SetIpv6( sock -> IsIpv6() );
00725 #endif
00726         SetSocketType( sock -> GetSocketType() );
00727         SetSocketProtocol( sock -> GetSocketProtocol() );
00728 
00729         SetClientRemoteAddress( *sock -> GetClientRemoteAddress() );
00730         SetRemoteAddress( *sock -> GetRemoteSocketAddress() );
00731 }

void Socket::OnSocks4Connect (  )  [virtual]

Socks4 client support internal use.

See also:
TcpSocket

Reimplemented in TcpSocket.

Definition at line 781 of file Socket.cpp.

References Handler(), and ISocketHandler::LogError().

00782 {
00783         Handler().LogError(this, "OnSocks4Connect", 0, "Use with TcpSocket only");
00784 }

void Socket::OnSocks4ConnectFailed (  )  [virtual]

Socks4 client support internal use.

See also:
TcpSocket

Reimplemented in TcpSocket.

Definition at line 787 of file Socket.cpp.

References Handler(), and ISocketHandler::LogError().

Referenced by SctpSocket::OnConnectTimeout(), and SctpSocket::OnWrite().

00788 {
00789         Handler().LogError(this, "OnSocks4ConnectFailed", 0, "Use with TcpSocket only");
00790 }

bool Socket::OnSocks4Read (  )  [virtual]

Socks4 client support internal use.

See also:
TcpSocket

Reimplemented in TcpSocket.

Definition at line 793 of file Socket.cpp.

References Handler(), and ISocketHandler::LogError().

00794 {
00795         Handler().LogError(this, "OnSocks4Read", 0, "Use with TcpSocket only");
00796         return true;
00797 }

bool Socket::Socks4 (  ) 

Called when the last write caused the tcp output buffer to become empty.

socket still in socks4 negotiation mode

Definition at line 806 of file Socket.cpp.

References m_bSocks4.

Referenced by TcpSocket::OnConnectTimeout(), SctpSocket::OnConnectTimeout(), TcpSocket::OnRead(), TcpSocket::OnWrite(), SctpSocket::OnWrite(), and TcpSocket::Open().

00807 {
00808         return m_bSocks4;
00809 }

void Socket::SetSocks4 ( bool  x = true  ) 

Set flag indicating Socks4 handshaking in progress.

Definition at line 812 of file Socket.cpp.

References m_bSocks4.

Referenced by TcpSocket::OnSocks4Read(), and TcpSocket::Open().

00813 {
00814         m_bSocks4 = x;
00815 }

void Socket::SetSocks4Host ( ipaddr_t  a  ) 

Set socks4 server host address to use.

Definition at line 818 of file Socket.cpp.

References m_socks4_host.

00819 {
00820         m_socks4_host = a;
00821 }

void Socket::SetSocks4Host ( const std::string &  host  ) 

Set socks4 server hostname to use.

Definition at line 800 of file Socket.cpp.

References m_socks4_host, and Utility::u2ip().

00801 {
00802         Utility::u2ip(host, m_socks4_host);
00803 }

void Socket::SetSocks4Port ( port_t  p  ) 

Socks4 server port to use.

Definition at line 824 of file Socket.cpp.

References m_socks4_port.

00825 {
00826         m_socks4_port = p;
00827 }

void Socket::SetSocks4Userid ( const std::string &  x  ) 

Provide a socks4 userid if required by the socks4 server.

Definition at line 830 of file Socket.cpp.

References m_socks4_userid.

00831 {
00832         m_socks4_userid = x;
00833 }

ipaddr_t Socket::GetSocks4Host (  ) 

Get the ip address of socks4 server to use.

Returns:
socks4 server host address

Definition at line 836 of file Socket.cpp.

References m_socks4_host.

Referenced by TcpSocket::Open().

00837 {
00838         return m_socks4_host;
00839 }

port_t Socket::GetSocks4Port (  ) 

Get the socks4 server port to use.

Returns:
socks4 server port

Definition at line 842 of file Socket.cpp.

References m_socks4_port.

Referenced by TcpSocket::Open().

00843 {
00844         return m_socks4_port;
00845 }

const std::string & Socket::GetSocks4Userid (  ) 

Get socks4 userid.

Returns:
Socks4 userid

Definition at line 848 of file Socket.cpp.

References m_socks4_userid.

Referenced by TcpSocket::OnSocks4Connect().

00849 {
00850         return m_socks4_userid;
00851 }

int Socket::Resolve ( const std::string &  host,
port_t  port = 0 
)

Request an asynchronous dns resolution.

Parameters:
host hostname to be resolved
port port number passed along for the ride
Returns:
Resolve ID

Definition at line 918 of file Socket.cpp.

References Handler(), and ISocketHandler::Resolve().

Referenced by TcpSocket::Open().

00919 {
00920         return Handler().Resolve(this, host, port);
00921 }

void Socket::OnResolved ( int  id,
ipaddr_t  a,
port_t  port 
) [virtual]

Callback returning a resolved address.

Parameters:
id Resolve ID from Resolve call
a resolved ip address
port port number passed to Resolve

Reimplemented in TcpSocket.

Definition at line 946 of file Socket.cpp.

00947 {
00948 }

int Socket::Resolve ( ipaddr_t  a  ) 

Request asynchronous reverse dns lookup.

Parameters:
a in_addr to be translated

Definition at line 932 of file Socket.cpp.

References Handler(), and ISocketHandler::Resolve().

00933 {
00934         return Handler().Resolve(this, a);
00935 }

void Socket::OnReverseResolved ( int  id,
const std::string &  name 
) [virtual]

Callback returning reverse resolve results.

Parameters:
id Resolve ID
name Resolved hostname

Definition at line 958 of file Socket.cpp.

Referenced by ResolvSocket::OnLine().

00959 {
00960 }

void Socket::OnResolveFailed ( int  id  )  [virtual]

Callback indicating failed dns lookup.

Parameters:
id Resolve ID

Definition at line 963 of file Socket.cpp.

Referenced by ResolvSocket::OnDelete(), and ResolvSocket::OnLine().

00964 {
00965 }

void Socket::OnDetached (  )  [virtual]

Callback fires when a new socket thread has started and this socket is ready for operation again.

See also:
ResolvSocket

Reimplemented in ResolvSocket.

Definition at line 877 of file Socket.cpp.

00878 {
00879 }

void Socket::SetDetach ( bool  x = true  ) 

Internal use.

Definition at line 882 of file Socket.cpp.

References Handler(), m_detach, and ISocketHandler::SetDetach().

Referenced by Detach().

00883 {
00884         m_detach = x;
00885         if (x)
00886                 Handler().SetDetach();
00887 }

bool Socket::IsDetach (  ) 

Check detach flag.

Returns:
true if the socket should detach to its own thread

Definition at line 890 of file Socket.cpp.

References m_detach.

00891 {
00892         return m_detach;
00893 }

void Socket::SetDetached ( bool  x = true  ) 

Internal use.

Definition at line 896 of file Socket.cpp.

References m_detached.

Referenced by DetachSocket().

00897 {
00898         m_detached = x;
00899 }

bool Socket::IsDetached (  )  const

Check detached flag.

Returns:
true if the socket runs in its own thread.

Definition at line 902 of file Socket.cpp.

References m_detached.

Referenced by Handler().

00903 {
00904         return m_detached;
00905 }

bool Socket::Detach (  ) 

Order this socket to start its own thread and call OnDetached when ready for operation.

Definition at line 856 of file Socket.cpp.

References DeleteByHandler(), m_detached, m_pThread, and SetDetach().

Referenced by ResolvSocket::OnLine().

00857 {
00858         if (!DeleteByHandler())
00859                 return false;
00860         if (m_pThread)
00861                 return false;
00862         if (m_detached)
00863                 return false;
00864         SetDetach();
00865         return true;
00866 }

void Socket::SetSlaveHandler ( ISocketHandler p  ) 

Store the slave sockethandler pointer.

Definition at line 908 of file Socket.cpp.

References m_slave_handler.

00909 {
00910         m_slave_handler = p;
00911 }

void Socket::DetachSocket (  ) 

Create new thread for this socket to run detached in.

Definition at line 869 of file Socket.cpp.

References m_pThread, and SetDetached().

00870 {
00871         SetDetached();
00872         m_pThread = new SocketThread(this);
00873         m_pThread -> SetRelease(true);
00874 }

void Socket::SetTrafficMonitor ( IFile p  )  [inline]

Write traffic to an IFile.

Socket will not delete this object.

Definition at line 641 of file Socket.h.

00641 { m_traffic_monitor = p; }

Socket& Socket::operator= ( const Socket  )  [inline, protected]

assignment operator not available.

Definition at line 650 of file Socket.h.

00650 { return *this; }

IFile* Socket::GetTrafficMonitor (  )  [inline, protected]

All traffic will be written to this IFile, if set.

Definition at line 653 of file Socket.h.

Referenced by TcpSocket::OnRead(), and TcpSocket::TryWrite().

00653 { return m_traffic_monitor; }


Member Data Documentation

Reference of ISocketHandler in control of this socket.

Definition at line 658 of file Socket.h.

Referenced by Handler(), and MasterHandler().

File descriptor.

Definition at line 659 of file Socket.h.

Referenced by Attach(), Close(), GetSocket(), Ready(), SetNonblocking(), and ~Socket().

bool Socket::m_bDel [private]

Delete by handler flag.

Definition at line 660 of file Socket.h.

Referenced by DeleteByHandler(), and SetDeleteByHandler().

bool Socket::m_bClose [private]

Close and delete flag.

Definition at line 661 of file Socket.h.

Referenced by CloseAndDelete(), and SetCloseAndDelete().

time_t Socket::m_tCreate [private]

Time in seconds when this socket was created.

Definition at line 662 of file Socket.h.

Referenced by Uptime().

Pointer to ListenSocket class, valid for incoming sockets.

Reimplemented in ResolvSocket.

Definition at line 663 of file Socket.h.

Referenced by GetParent(), and SetParent().

bool Socket::m_b_disable_read [private]

Disable checking for read events.

Definition at line 664 of file Socket.h.

Referenced by DisableRead(), and IsDisableRead().

bool Socket::m_connected [private]

Socket is connected (tcp/udp).

Definition at line 665 of file Socket.h.

Referenced by IsConnected(), and SetConnected().

Set by handler before delete.

Definition at line 666 of file Socket.h.

Referenced by ErasedByHandler(), and SetErasedByHandler().

time_t Socket::m_tClose [private]

Time in seconds when ordered to close.

Definition at line 667 of file Socket.h.

Referenced by SetCloseAndDelete(), and TimeSinceClose().

Address of last connect().

Definition at line 668 of file Socket.h.

Referenced by GetClientRemoteAddress(), and SetClientRemoteAddress().

std::auto_ptr<SocketAddress> Socket::m_remote_address [private]

Remote end address.

Definition at line 669 of file Socket.h.

Referenced by GetRemoteAddress(), GetRemoteHostname(), GetRemoteIP4(), GetRemotePort(), GetRemoteSocketAddress(), and SetRemoteAddress().

Definition at line 670 of file Socket.h.

time_t Socket::m_timeout_start [private]

Set by SetTimeout.

Definition at line 671 of file Socket.h.

Referenced by CheckTimeout(), SetTimeout(), and Timeout().

time_t Socket::m_timeout_limit [private]

Defined by SetTimeout.

Definition at line 672 of file Socket.h.

Referenced by CheckTimeout(), SetTimeout(), and Timeout().

bool Socket::m_bLost [private]

connection lost

Definition at line 673 of file Socket.h.

Referenced by Lost(), and SetLost().

socketuid_t Socket::m_next_uid = 0 [static, private]

Definition at line 674 of file Socket.h.

Definition at line 675 of file Socket.h.

bool Socket::m_call_on_connect [private]

OnConnect will be called next ISocketHandler cycle if true.

Definition at line 676 of file Socket.h.

Referenced by CallOnConnect(), and SetCallOnConnect().

bool Socket::m_b_retry_connect [private]

Try another connection attempt next ISocketHandler cycle.

Definition at line 677 of file Socket.h.

Referenced by RetryClientConnect(), and SetRetryClientConnect().

bool Socket::m_b_enable_ssl [private]

Enable SSL for this TcpSocket.

Definition at line 684 of file Socket.h.

Referenced by EnableSSL(), and IsSSL().

bool Socket::m_b_ssl [private]

ssl negotiation mode (TcpSocket)

Definition at line 685 of file Socket.h.

Referenced by IsSSLNegotiate(), and SetSSLNegotiate().

bool Socket::m_b_ssl_server [private]

True if this is an incoming ssl TcpSocket connection.

Definition at line 686 of file Socket.h.

Referenced by IsSSLServer(), and SetSSLServer().

int Socket::m_socket_type [private]

Type of socket, from socket() call.

Definition at line 694 of file Socket.h.

Referenced by CreateSocket(), GetSocketType(), and SetSocketType().

std::string Socket::m_socket_protocol [private]

Protocol, from socket() call.

Definition at line 695 of file Socket.h.

Referenced by CreateSocket(), GetSocketProtocol(), and SetSocketProtocol().

bool Socket::m_bClient [private]

only client connections are pooled

Definition at line 696 of file Socket.h.

Referenced by SetIsClient(), and SetRetain().

bool Socket::m_bRetain [private]

keep connection on close

Definition at line 697 of file Socket.h.

Referenced by Retain(), SetRetain(), and ~Socket().

bool Socket::m_bSocks4 [private]

socks4 negotiation mode (TcpSocket)

Definition at line 701 of file Socket.h.

Referenced by SetSocks4(), and Socks4().

socks4 server address

Definition at line 702 of file Socket.h.

Referenced by GetSocks4Host(), and SetSocks4Host().

socks4 server port number

Definition at line 703 of file Socket.h.

Referenced by GetSocks4Port(), and SetSocks4Port().

std::string Socket::m_socks4_userid [private]

socks4 server usedid

Definition at line 704 of file Socket.h.

Referenced by GetSocks4Userid(), and SetSocks4Userid().

bool Socket::m_detach [private]

Socket ordered to detach flag.

Definition at line 708 of file Socket.h.

Referenced by IsDetach(), and SetDetach().

bool Socket::m_detached [private]

Socket has been detached.

Definition at line 709 of file Socket.h.

Referenced by Detach(), IsDetached(), and SetDetached().

Detach socket thread class pointer.

Definition at line 710 of file Socket.h.

Referenced by Detach(), and DetachSocket().

Actual sockethandler while detached.

Definition at line 711 of file Socket.h.

Referenced by Handler(), and SetSlaveHandler().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4