plptools
Loading...
Searching...
No Matches
TCPSocket Class Reference

A class for dealing with sockets. More...

#include <tcpsocket.h>

Collaboration diagram for TCPSocket:

Public Member Functions

 TCPSocket ()
 Constructs a TCPSocket. More...
 
 TCPSocket (const TCPSocket &)
 Copy constructor. More...
 
virtual ~TCPSocket ()
 Destructor. More...
 
virtual bool connect (const char *const Peer, int PeerPort, const char *const Host=NULL, int HostPort=0)
 Connects to a given host. More...
 
virtual bool reconnect ()
 Reopens the connection after closing it. More...
 
virtual std::string toString ()
 Retrieve a string representation of the TCPSocket. More...
 
virtual bool listen (const char *const Host, int Port)
 Starts listening. More...
 
TCPSocketaccept (std::string *Peer)
 Accept a connection; blocking, non-cancellable. More...
 
TCPSocketaccept (std::string *Peer, int cancellationFd)
 Accept a connection; blocking, cancellable. More...
 
bool dataToGet (int sec, int usec) const
 Check and optionally wait for incoming data. More...
 
int getBufferStore (bufferStore &a, bool wait=true)
 Receive data into a bufferStore . More...
 
bool sendBufferStore (const bufferStore &a)
 Sends data from a bufferStore . More...
 
bool closeSocket (void)
 Closes the connection. More...
 
bool bindSocket (const char *const Host, int Port)
 Binds to a local address and port. More...
 
bool bindInRange (const char *const Host, int Low, int High, int Retries)
 Tries repeated binds to a local address and port. More...
 
bool linger (bool LingerOn, int LingerTime=0)
 Sets the linger parameter of the socket. More...
 
bool getPeer (std::string *Peer, int *Port)
 Retrieves peer information. More...
 
bool getHost (std::string *Host, int *Port)
 Retrieves local information. More...
 
void setWatch (IOWatch *watch)
 Registers an IOWatch for this socket. More...
 

Private Member Functions

virtual bool createSocket (void)
 Creates the socket. More...
 
int getLastError (void)
 
bool setPeer (const char *const Peer, int Port)
 
bool setHost (const char *const Host, int Port)
 
int recv (void *buf, int len, int flags)
 
int send (const void *const buf, int len, int flags)
 

Private Attributes

struct sockaddr m_HostAddr
 
struct sockaddr m_PeerAddr
 
int m_Socket
 
int m_Port
 
bool m_Bound
 
int m_LastError
 
IOWatchmyWatch
 

Detailed Description

A class for dealing with sockets.

Definition at line 37 of file tcpsocket.h.

Constructor & Destructor Documentation

◆ TCPSocket() [1/2]

TCPSocket::TCPSocket ( )

Constructs a TCPSocket.

Definition at line 63 of file tcpsocket.cc.

◆ TCPSocket() [2/2]

TCPSocket::TCPSocket ( const TCPSocket another)

Copy constructor.

Definition at line 52 of file tcpsocket.cc.

◆ ~TCPSocket()

TCPSocket::~TCPSocket ( )
virtual

Destructor.

Definition at line 78 of file tcpsocket.cc.

Member Function Documentation

◆ accept() [1/2]

TCPSocket * TCPSocket::accept ( std::string *  Peer)

Accept a connection; blocking, non-cancellable.

Parameters
PeerIf non-Null, the peer's name is returned here.
Returns
A pointer to a new instance for the accepted connection or NULL if an error happened.

Definition at line 207 of file tcpsocket.cc.

◆ accept() [2/2]

TCPSocket * TCPSocket::accept ( std::string *  Peer,
int  cancellationFd 
)

Accept a connection; blocking, cancellable.

Parameters
PeerIf non-Null, the peer's name is returned here.
cancellationFdFile descriptor that can be used to signal cancellation.
Returns
A pointer to a new instance for the accepted connection or NULL if an error happened.

Definition at line 251 of file tcpsocket.cc.

◆ bindInRange()

bool TCPSocket::bindInRange ( const char *const  Host,
int  Low,
int  High,
int  Retries 
)

Tries repeated binds to a local address and port.

If Retries is <= High - Low, then the port to bind is randomly chosen in the given range. Otherwise, all ports starting from High up to Low are tried in sequence.

Parameters
HostThe local address to bind to.
LowThe lowest local port to listen on.
HighThe highest local port to listen on.
RetriesThe number of retries until giving up.
Returns
true on success, false otherwise.

Definition at line 424 of file tcpsocket.cc.

◆ bindSocket()

bool TCPSocket::bindSocket ( const char *const  Host,
int  Port 
)

Binds to a local address and port.

Parameters
HostThe local address to bind to.
PortThe local port to listen on.
Returns
true on success, false otherwise.

Definition at line 387 of file tcpsocket.cc.

◆ closeSocket()

bool TCPSocket::closeSocket ( void  )

Closes the connection.

Returns
true on success, false otherwise.

Definition at line 373 of file tcpsocket.cc.

◆ connect()

bool TCPSocket::connect ( const char *const  Peer,
int  PeerPort,
const char *const  Host = NULL,
int  HostPort = 0 
)
virtual

Connects to a given host.

Parameters
PeerThe Host to connect to (name or dotquad-string).
PeerPortThe port to connect to.
HostThe local address to bind to.
HostPortThe local port to bind to.
Returns
true on success, false otherwise.

Definition at line 152 of file tcpsocket.cc.

◆ createSocket()

bool TCPSocket::createSocket ( void  )
privatevirtual

Creates the socket.

Definition at line 501 of file tcpsocket.cc.

◆ dataToGet()

bool TCPSocket::dataToGet ( int  sec,
int  usec 
) const

Check and optionally wait for incoming data.

Parameters
secTimeout in seconds
usecTimeout in microseconds
Returns
true if data is available, false otherwise.

Definition at line 276 of file tcpsocket.cc.

◆ getBufferStore()

int TCPSocket::getBufferStore ( bufferStore a,
bool  wait = true 
)

Receive data into a bufferStore .

Parameters
aThe bufferStore to fill with received data.
waitIf true, wait until something is received, else return if no data is available.
Returns
1 if a bufferStore received, 0, if no bufferStore received, -1 on error.

Definition at line 288 of file tcpsocket.cc.

◆ getHost()

bool TCPSocket::getHost ( std::string *  Host,
int *  Port 
)

Retrieves local information.

Parameters
HostThe local name is returned here.
PortThe local port is returned here.
Returns
true on success, false otherwise.

Definition at line 608 of file tcpsocket.cc.

◆ getLastError()

int TCPSocket::getLastError ( void  )
inlineprivate

Definition at line 222 of file tcpsocket.h.

◆ getPeer()

bool TCPSocket::getPeer ( std::string *  Peer,
int *  Port 
)

Retrieves peer information.

Parameters
PeerThe peers name is returned here.
PortThe peers port is returned here.
Returns
true on success, false otherwise.

Definition at line 555 of file tcpsocket.cc.

◆ linger()

bool TCPSocket::linger ( bool  LingerOn,
int  LingerTime = 0 
)

Sets the linger parameter of the socket.

Parameters
LingerOntrue, if lingering should be on.
LingerTimeIf lingering is on, the linger-time.
Returns
true on success, false otherwise.

Definition at line 472 of file tcpsocket.cc.

◆ listen()

bool TCPSocket::listen ( const char *const  Host,
int  Port 
)
virtual

Starts listening.

Parameters
HostThe local address to bind to.
PortThe local port to listen on.
Returns
true on success, false otherwise.

Definition at line 182 of file tcpsocket.cc.

◆ reconnect()

bool TCPSocket::reconnect ( )
virtual

Reopens the connection after closing it.

Returns
true on success, false otherwise.

Definition at line 99 of file tcpsocket.cc.

◆ recv()

int TCPSocket::recv ( void *  buf,
int  len,
int  flags 
)
private

Definition at line 351 of file tcpsocket.cc.

◆ send()

int TCPSocket::send ( const void *const  buf,
int  len,
int  flags 
)
private

Definition at line 362 of file tcpsocket.cc.

◆ sendBufferStore()

bool TCPSocket::sendBufferStore ( const bufferStore a)

Sends data from a bufferStore .

Parameters
aThe bufferStore to send.
Returns
true on success, false otherwise.

Definition at line 324 of file tcpsocket.cc.

◆ setHost()

bool TCPSocket::setHost ( const char *const  Host,
int  Port 
)
private

Definition at line 573 of file tcpsocket.cc.

◆ setPeer()

bool TCPSocket::setPeer ( const char *const  Peer,
int  Port 
)
private

Definition at line 522 of file tcpsocket.cc.

◆ setWatch()

void TCPSocket::setWatch ( IOWatch watch)

Registers an IOWatch for this socket.

This IOWatch gets the socket added/removed automatically.

Parameters
watchThe IOWatch to register.

Definition at line 88 of file tcpsocket.cc.

◆ toString()

string TCPSocket::toString ( )
virtual

Retrieve a string representation of the TCPSocket.

Returns
a string in the form "<host>:<hostport> -> <peer>:<peerport>" where elements not known, are replaced by "???" and none-existing elements are represented by the word "none".

Definition at line 127 of file tcpsocket.cc.

Member Data Documentation

◆ m_Bound

bool TCPSocket::m_Bound
private

Definition at line 232 of file tcpsocket.h.

◆ m_HostAddr

struct sockaddr TCPSocket::m_HostAddr
private

Definition at line 228 of file tcpsocket.h.

◆ m_LastError

int TCPSocket::m_LastError
private

Definition at line 233 of file tcpsocket.h.

◆ m_PeerAddr

struct sockaddr TCPSocket::m_PeerAddr
private

Definition at line 229 of file tcpsocket.h.

◆ m_Port

int TCPSocket::m_Port
private

Definition at line 231 of file tcpsocket.h.

◆ m_Socket

int TCPSocket::m_Socket
private

Definition at line 230 of file tcpsocket.h.

◆ myWatch

IOWatch* TCPSocket::myWatch
private

Definition at line 234 of file tcpsocket.h.


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