SocketClient Class Reference

#include <sockets.h>

Inheritance diagram for SocketClient:

Inheritance graph
[legend]
Collaboration diagram for SocketClient:

Collaboration graph
[legend]
List of all members.

Detailed Description

Socket client is a "connection" that is used to transfer data between two peers.

Definition at line 157 of file sockets.h.


Public Types

typedef SCEventHandler HandlerType
 Type of event handler used by this object.

Public Member Functions

 DECLARE_EVENT_TABLE (SocketClient *, SocketEvent)
 SocketClient (SCEventHandler ehandler=0)
 Constructor.
void destroy ()
 Destroy this socket.
uint32_t write (const char *buffer, uint32_t length)
 Write data into socket.
uint32_t read (void *buffer, uint32_t length)
 Read data from socket.
void connect (IPV4Address addr, uint32_t timeout=5000)
 Make an outgoing connection.
void disconnect ()
 Close the connection.
void setTimeout (uint32_t t)
 Set timeout; if no events happen within this timeout, SOCK_TIMEOUT is emitted.
IPV4Address getPeer () const
IPV4Address getAddr () const
Accessors for various internal variables
bool isConnected () const
bool isOk () const
bool isConnecting () const
bool isWritable () const

Private Member Functions

 SocketClient (SOCKET s)
 Construct from existing SOCKET object.
virtual ~SocketClient ()
 Destructor hidden, use destroy() method instead.
void setPeerName ()
 Update m_peer member.
void close ()
 Closes the underlying socket.

Private Attributes

bool m_connected
 If the socket is connected.
bool m_hasData
 If there is incoming data to be read.
bool m_connecting
 If a connection attempt is in progress.
bool m_erronous
 If the socket is erronous.
bool m_writable
 If this socket is writable.
uint64_t m_timeout
 Timeout counter for connection attempts.
IPV4Address m_peer
 Peer to where we are connected to.

Friends

class SocketWatcher
class SocketServer
SocketClientoperator<< (SocketClient &c, const std::string &data)
 Input operator; writes the specified data into socket.
SocketClientoperator>> (SocketClient &c, std::string &buf)
 Extractor operator; reads all current data from socket to buffer.

Member Typedef Documentation

typedef SCEventHandler SocketClient::HandlerType
 

Type of event handler used by this object.

Definition at line 162 of file sockets.h.


Constructor & Destructor Documentation

SocketClient::SocketClient SCEventHandler  ehandler = 0  ) 
 

Constructor.

Parameters:
ehandler Optional handler for socket events

Definition at line 218 of file sockets.cpp.

References initSockets(), logTrace(), and TRACE_SOCKET.

Here is the call graph for this function:

SocketClient::SocketClient SOCKET  s  )  [private]
 

Construct from existing SOCKET object.

Definition at line 230 of file sockets.cpp.

SocketClient::~SocketClient  )  [private, virtual]
 

Destructor hidden, use destroy() method instead.

Definition at line 236 of file sockets.cpp.


Member Function Documentation

void SocketClient::close  )  [private]
 

Closes the underlying socket.

Definition at line 296 of file sockets.cpp.

References closesocket, logTrace(), logWarning(), m_connected, m_connecting, m_timeout, SocketWatcher::removeSocket(), socketError(), and TRACE_SOCKET.

Referenced by connect(), destroy(), disconnect(), SocketWatcher::doPoll(), SocketWatcher::handleErronousSocket(), SocketWatcher::handleReadableSocket(), SocketWatcher::handleWritableSocket(), read(), and write().

Here is the call graph for this function:

void SocketClient::connect IPV4Address  addr,
uint32_t  timeout = 5000
 

Make an outgoing connection.

Parameters:
addr Address to connect to.
timeout Time in milliseconds to attempt to connect. Once the timeout passes, SOCK_ETIMEOUT event will be posted if the connection attempt hasn't been completed to that point. Defaults to 5 seconds.
Preconditions: addr.m_port != 0, addr.m_ip != 0
Exceptions:
SocketError if something goes wrong.

Definition at line 245 of file sockets.cpp.

References SocketWatcher::addSocket(), close(), IPV4Address::getAddr(), getLastError(), IPV4Address::getPort(), INVALID_SOCKET, logTrace(), m_connected, m_connecting, m_erronous, m_peer, setNonBlocking(), setTimeout(), SOCK_EINPROGRESS, SOCK_EWOULDBLOCK, SOCKET_ERROR, socketError(), and TRACE_SOCKET.

Here is the call graph for this function:

SocketClient::DECLARE_EVENT_TABLE SocketClient ,
SocketEvent 
 

void SocketClient::destroy  )  [virtual]
 

Destroy this socket.

It is safe to call this method multiple times.

Implements SocketBase.

Definition at line 240 of file sockets.cpp.

References close().

Here is the call graph for this function:

void SocketClient::disconnect  ) 
 

Close the connection.

Exceptions:
SocketError if something goes wrong.

Definition at line 312 of file sockets.cpp.

References close(), logTrace(), m_peer, and TRACE_SOCKET.

Here is the call graph for this function:

IPV4Address SocketClient::getAddr  )  const
 

Returns:
The local address this socket is bound to
Note:
When *this is disconnected, this returns 0, however, if *this is connected, it returns our external IP address if we are connected to the net directly; when we are behind a gateway though, this returns our intranet address, so don't rely on this giving you our external IP always (altough it's worth a shot).

Definition at line 370 of file sockets.cpp.

References SOCKET_ERROR.

IPV4Address SocketClient::getPeer  )  const [inline]
 

Returns:
The address this socket is connected to.

Definition at line 248 of file sockets.h.

bool SocketClient::isConnected  )  const [inline]
 

Definition at line 231 of file sockets.h.

bool SocketClient::isConnecting  )  const [inline]
 

Definition at line 233 of file sockets.h.

bool SocketClient::isOk  )  const [inline]
 

Definition at line 232 of file sockets.h.

bool SocketClient::isWritable  )  const [inline]
 

Definition at line 234 of file sockets.h.

uint32_t SocketClient::read void *  buffer,
uint32_t  length
 

Read data from socket.

Parameters:
buffer Buffer into what to place the read data.
length Size of buffer, amount of data to be read.
Returns:
Number of bytes actually read.
Exceptions:
SocketError if something goes wrong.

Definition at line 319 of file sockets.cpp.

References close(), getLastError(), m_connected, m_connecting, m_erronous, m_hasData, MSG_NOSIGNAL, SOCK_EAGAIN, SOCK_LOST, and SOCKET_ERROR.

Here is the call graph for this function:

void SocketClient::setPeerName  )  [private]
 

Update m_peer member.

void SocketClient::setTimeout uint32_t  t  )  [inline]
 

Set timeout; if no events happen within this timeout, SOCK_TIMEOUT is emitted.

Parameters:
t Timeout, in milliseconds

Definition at line 243 of file sockets.h.

References Utils::getTick().

Referenced by connect().

Here is the call graph for this function:

uint32_t SocketClient::write const char *  buffer,
uint32_t  length
 

Write data into socket.

Parameters:
buffer Pointer to buffer of data to be written.
length Amount of data to send.
Returns:
Number of bytes written to socket.
Exceptions:
SocketError if something goes wrong.
Note:
The socket API does not take ownership of the pointer passed to this function. It attempts to send out as much data as is possible at this moment, but does not buffer anything. Client code is responsible for freeing the memory pointed to by the buffer pointer.

It is recommended to use SSocket API, which implements internal buffering and is safer to use than this raw API.

Definition at line 347 of file sockets.cpp.

References close(), getLastError(), m_connected, m_connecting, m_erronous, m_writable, MSG_NOSIGNAL, SOCK_EAGAIN, SOCK_LOST, and SOCKET_ERROR.

Here is the call graph for this function:


Friends And Related Function Documentation

SocketClient& operator<< SocketClient c,
const std::string &  data
[friend]
 

Input operator; writes the specified data into socket.

Definition at line 264 of file sockets.h.

SocketClient& operator>> SocketClient c,
std::string &  buf
[friend]
 

Extractor operator; reads all current data from socket to buffer.

Definition at line 274 of file sockets.h.

friend class SocketServer [friend]
 

Definition at line 284 of file sockets.h.

friend class SocketWatcher [friend]
 

Definition at line 283 of file sockets.h.


Member Data Documentation

bool SocketClient::m_connected [private]
 

If the socket is connected.

Definition at line 296 of file sockets.h.

Referenced by SocketServer::accept(), close(), connect(), SocketWatcher::doPoll(), SocketWatcher::handleErronousSocket(), SocketWatcher::handleReadableSocket(), SocketWatcher::handleWritableSocket(), read(), and write().

bool SocketClient::m_connecting [private]
 

If a connection attempt is in progress.

Definition at line 298 of file sockets.h.

Referenced by close(), connect(), SocketWatcher::doPoll(), SocketWatcher::handleErronousSocket(), SocketWatcher::handleReadableSocket(), SocketWatcher::handleWritableSocket(), read(), and write().

bool SocketClient::m_erronous [private]
 

If the socket is erronous.

Definition at line 299 of file sockets.h.

Referenced by connect(), SocketWatcher::doPoll(), SocketWatcher::handleErronousSocket(), SocketWatcher::handleReadableSocket(), read(), and write().

bool SocketClient::m_hasData [private]
 

If there is incoming data to be read.

Definition at line 297 of file sockets.h.

Referenced by SocketWatcher::doPoll(), SocketWatcher::handleReadableSocket(), and read().

IPV4Address SocketClient::m_peer [private]
 

Peer to where we are connected to.

Definition at line 302 of file sockets.h.

Referenced by SocketServer::accept(), connect(), and disconnect().

uint64_t SocketClient::m_timeout [private]
 

Timeout counter for connection attempts.

Definition at line 301 of file sockets.h.

Referenced by close(), SocketWatcher::doPoll(), SocketWatcher::handleErronousSocket(), SocketWatcher::handleReadableSocket(), and SocketWatcher::handleWritableSocket().

bool SocketClient::m_writable [private]
 

If this socket is writable.

Definition at line 300 of file sockets.h.

Referenced by SocketWatcher::doPoll(), SocketWatcher::handleWritableSocket(), and write().


The documentation for this class was generated from the following files: