clockUtils  1.1
clockUtils::sockets::TcpSocket Class Reference

class for sockets using tcp More...

#include <TcpSocket.h>

Public Types

typedef std::function< void(TcpSocket *, ClockError)> acceptCallback
 this function type is used as accept callback, so every accepted socket will reach this function on host More...
 
typedef std::function< void(std::vector< uint8_t > packet, TcpSocket *socket, ClockError err)> packetCallback
 this function type is used receiving a packet using receiveCallback and is called for every packet More...
 

Public Member Functions

 TcpSocket ()
 constructor More...
 
 ~TcpSocket ()
 destructor, closes socket if not happened before More...
 
ClockError listen (uint16_t listenPort, int maxParallelConnections, bool acceptMultiple, const acceptCallback acb)
 creates listen socket listening on the given port and calling callback for every accepted connection More...
 
ClockError connectToIP (const std::string &remoteIP, uint16_t remotePort, unsigned int timeout)
 creates a connection to the given pair of IP and port More...
 
ClockError connectToHostname (const std::string &remoteHostname, uint16_t remotePort, unsigned int timeout)
 creates a connection to the given pair of hostname and port More...
 
ClockError connect (const IPv4 remoteIP, uint16_t remotePort, unsigned int timeout)
 creates a connection to the given pair of IP and port More...
 
void close ()
 closes a connection if socket is connected More...
 
std::string getRemoteIP () const
 returns the ip of the socket this socket is connected with More...
 
uint16_t getRemotePort () const
 returns the port of the socket this socket is connected with More...
 
std::string getLocalIP () const
 returns the local IP of this socket More...
 
std::string getPublicIP () const
 returns the public IP of this socket shown in network More...
 
uint16_t getLocalPort () const
 returns the port this socket uses for connection More...
 
ClockError writePacket (const void *str, const size_t length)
 sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket More...
 
ClockError writePacket (const std::vector< uint8_t > &vec)
 sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket More...
 
ClockError writePacket (const std::string &str)
 sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket More...
 
ClockError writePacketAsync (const void *str, const size_t length)
 sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket More...
 
ClockError writePacketAsync (const std::vector< uint8_t > &vec)
 sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket More...
 
ClockError writePacketAsync (const std::string &str)
 sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket More...
 
ClockError receivePacket (std::vector< uint8_t > &buffer)
 receives a packet sent with writePacket, doesn't work with write this functions blocks until a packet is received More...
 
ClockError receivePacket (std::string &buffer)
 receives a packet sent with writePacket, doesn't work with write More...
 
ClockError receiveCallback (packetCallback pcb)
 calls the callback for every packet that is received on this socket This function will return immediately More...
 
ClockError write (const void *str, size_t length)
 sends a message, doesn't work with receivePacket
See Writing to the socket More...
 
ClockError write (const std::vector< uint8_t > &vec)
 sends a message, doesn't work with receivePacket
See Writing to the socket More...
 
ClockError write (const std::string &str)
 sends a message, doesn't work with receivePacket
See Writing to the socket More...
 
ClockError writeAsync (const void *str, const size_t length)
 sends a message asynchron, doesn't work with receivePacket
See Writing to the socket More...
 
ClockError writeAsync (const std::vector< uint8_t > &vec)
 sends a message asynchron, doesn't work with receivePacket
See Writing to the socket More...
 
ClockError writeAsync (const std::string &str)
 sends a message asynchron, doesn't work with receivePacket
See Writing to the socket More...
 
template<class Container >
ClockError read (Container &buffer)
 receives data on the socket More...
 
template<typename T >
std::enable_if< std::is_enum< T >::value, TcpSocket & >::type operator<< (const T &a)
 sends parameter as a packet being receivable using operator>> or receivePacket T is an enum value More...
 
template<typename T >
std::enable_if<!std::is_enum< T >::value, TcpSocket & >::type operator<< (const T &a)
 sends parameter as a packet being receivable using operator>> or receivePacket T has to be streamable More...
 
template<typename T >
std::enable_if< std::is_enum< T >::value, TcpSocket & >::type operator>> (T &a)
 receives a packet being sent using operator<< or writePacket(Async) T is an enum value More...
 
template<typename T >
std::enable_if<!std::is_enum< T >::value, TcpSocket & >::type operator>> (T &a)
 receives a packet being sent using operator<< or writePacket(Async) T has to be streamable More...
 
template<>
CLOCK_SOCKETS_API TcpSocketoperator>> (std::string &s)
 specialization of stream operator for std::string to reduce overhead through converting to std::string using stringstream More...
 

Static Public Member Functions

static std::vector< std::pair< std::string, std::string > > enumerateLocalIPs ()
 returns all local IP's More...
 

Detailed Description

class for sockets using tcp

Writing to the socket

Two different writing modes are available: stream and packet.
Stream is the normal TCP behaviour. Several packets are sent that form a continous stream. Several writes can be received with a single read and a single write can be split up in several reads. It is the users task to concatenate and split the stream if necessary.
Packet is an addition provided by this socket class. A single packet is received as a single packet. This allows to easily send messages, objects, etc. without worrying about the TCP internals. The behaviour is similar to UDP sockets but with the reliability of TCP. Internally, the packets are mapped to a stream and extracted and the other endpoint.

Attention
Never mix stream and packet on the same socket and retrieve the data with the same method like it was sent. Otherwise the socket is likely to crash. However, e.g., sending a string packet and retrieving the packet as a vector is possible.
Note
All write methods return SUCCESS after successfully sending the data (or storing it in the buffer with async operations). However, no guarantees about the endpoint receiving the data is given. Even shortly after the remote endpoint disconnected ungratefully, writes will return SUCCESS

Definition at line 84 of file TcpSocket.h.

Member Typedef Documentation

§ acceptCallback

this function type is used as accept callback, so every accepted socket will reach this function on host

Definition at line 89 of file TcpSocket.h.

§ packetCallback

typedef std::function<void(std::vector<uint8_t> packet, TcpSocket * socket, ClockError err)> clockUtils::sockets::TcpSocket::packetCallback

this function type is used receiving a packet using receiveCallback and is called for every packet

Definition at line 94 of file TcpSocket.h.

Constructor & Destructor Documentation

§ TcpSocket()

clockUtils::sockets::TcpSocket::TcpSocket ( )

constructor

§ ~TcpSocket()

clockUtils::sockets::TcpSocket::~TcpSocket ( )

destructor, closes socket if not happened before

Member Function Documentation

§ close()

void clockUtils::sockets::TcpSocket::close ( )

closes a connection if socket is connected

§ connect()

ClockError clockUtils::sockets::TcpSocket::connect ( const IPv4  remoteIP,
uint16_t  remotePort,
unsigned int  timeout 
)

creates a connection to the given pair of IP and port

Parameters
[in]remoteIPthe ip of the connection listening as IPv4 integer
[in]remotePortthe port of the connection listening
[in]timeoutthe time in milliseconds a connect request should last in maximum

§ connectToHostname()

ClockError clockUtils::sockets::TcpSocket::connectToHostname ( const std::string &  remoteHostname,
uint16_t  remotePort,
unsigned int  timeout 
)
inline

creates a connection to the given pair of hostname and port

Parameters
[in]remoteHostnamethe hostname of the connection listening as std::string
[in]remotePortthe port of the connection listening
[in]timeoutthe time in milliseconds a connect request should last in maximum

Definition at line 136 of file TcpSocket.h.

Here is the call graph for this function:

§ connectToIP()

ClockError clockUtils::sockets::TcpSocket::connectToIP ( const std::string &  remoteIP,
uint16_t  remotePort,
unsigned int  timeout 
)
inline

creates a connection to the given pair of IP and port

Parameters
[in]remoteIPthe ip of the connection listening as std::string
[in]remotePortthe port of the connection listening
[in]timeoutthe time in milliseconds a connect request should last in maximum

Definition at line 121 of file TcpSocket.h.

Here is the call graph for this function:

§ enumerateLocalIPs()

static std::vector<std::pair<std::string, std::string> > clockUtils::sockets::TcpSocket::enumerateLocalIPs ( )
static

returns all local IP's

§ getLocalIP()

std::string clockUtils::sockets::TcpSocket::getLocalIP ( ) const

returns the local IP of this socket

§ getLocalPort()

uint16_t clockUtils::sockets::TcpSocket::getLocalPort ( ) const

returns the port this socket uses for connection

§ getPublicIP()

std::string clockUtils::sockets::TcpSocket::getPublicIP ( ) const

returns the public IP of this socket shown in network

§ getRemoteIP()

std::string clockUtils::sockets::TcpSocket::getRemoteIP ( ) const

returns the ip of the socket this socket is connected with

§ getRemotePort()

uint16_t clockUtils::sockets::TcpSocket::getRemotePort ( ) const

returns the port of the socket this socket is connected with

§ listen()

ClockError clockUtils::sockets::TcpSocket::listen ( uint16_t  listenPort,
int  maxParallelConnections,
bool  acceptMultiple,
const acceptCallback  acb 
)

creates listen socket listening on the given port and calling callback for every accepted connection

Parameters
[in]listenPortthe port the socket is listening on
[in]maxParallelConnectionsthe amount of connections being able to be handled at once
[in]acceptMultipleif set to false, only one connection is accepted, otherwise infinite
[in]acbthe callback to be called for every accepted connection. Will also be called when socket is closed during listen or an error occurs. If no error occurs, ClockError::SUCCESS is passed as second parameter

§ operator<<() [1/2]

template<typename T >
std::enable_if<std::is_enum<T>::value, TcpSocket &>::type clockUtils::sockets::TcpSocket::operator<< ( const T &  a)
inline

sends parameter as a packet being receivable using operator>> or receivePacket T is an enum value

Definition at line 340 of file TcpSocket.h.

§ operator<<() [2/2]

template<typename T >
std::enable_if<!std::is_enum<T>::value, TcpSocket &>::type clockUtils::sockets::TcpSocket::operator<< ( const T &  a)
inline

sends parameter as a packet being receivable using operator>> or receivePacket T has to be streamable

Definition at line 352 of file TcpSocket.h.

§ operator>>() [1/3]

template<typename T >
std::enable_if<std::is_enum<T>::value, TcpSocket &>::type clockUtils::sockets::TcpSocket::operator>> ( T &  a)
inline

receives a packet being sent using operator<< or writePacket(Async) T is an enum value

Definition at line 364 of file TcpSocket.h.

§ operator>>() [2/3]

template<typename T >
std::enable_if<!std::is_enum<T>::value, TcpSocket &>::type clockUtils::sockets::TcpSocket::operator>> ( T &  a)
inline

receives a packet being sent using operator<< or writePacket(Async) T has to be streamable

Definition at line 379 of file TcpSocket.h.

§ operator>>() [3/3]

template<>
CLOCK_SOCKETS_API TcpSocket& clockUtils::sockets::TcpSocket::operator>> ( std::string &  s)

specialization of stream operator for std::string to reduce overhead through converting to std::string using stringstream

§ read()

template<class Container >
ClockError clockUtils::sockets::TcpSocket::read ( Container &  buffer)
inline

receives data on the socket

Definition at line 301 of file TcpSocket.h.

§ receiveCallback()

ClockError clockUtils::sockets::TcpSocket::receiveCallback ( packetCallback  pcb)

calls the callback for every packet that is received on this socket This function will return immediately

§ receivePacket() [1/2]

ClockError clockUtils::sockets::TcpSocket::receivePacket ( std::vector< uint8_t > &  buffer)

receives a packet sent with writePacket, doesn't work with write this functions blocks until a packet is received

§ receivePacket() [2/2]

ClockError clockUtils::sockets::TcpSocket::receivePacket ( std::string &  buffer)

receives a packet sent with writePacket, doesn't work with write

§ write() [1/3]

ClockError clockUtils::sockets::TcpSocket::write ( const void *  str,
size_t  length 
)

sends a message, doesn't work with receivePacket
See Writing to the socket

§ write() [2/3]

ClockError clockUtils::sockets::TcpSocket::write ( const std::vector< uint8_t > &  vec)

sends a message, doesn't work with receivePacket
See Writing to the socket

§ write() [3/3]

ClockError clockUtils::sockets::TcpSocket::write ( const std::string &  str)
inline

sends a message, doesn't work with receivePacket
See Writing to the socket

Definition at line 270 of file TcpSocket.h.

§ writeAsync() [1/3]

ClockError clockUtils::sockets::TcpSocket::writeAsync ( const void *  str,
const size_t  length 
)

sends a message asynchron, doesn't work with receivePacket
See Writing to the socket

§ writeAsync() [2/3]

ClockError clockUtils::sockets::TcpSocket::writeAsync ( const std::vector< uint8_t > &  vec)

sends a message asynchron, doesn't work with receivePacket
See Writing to the socket

§ writeAsync() [3/3]

ClockError clockUtils::sockets::TcpSocket::writeAsync ( const std::string &  str)
inline

sends a message asynchron, doesn't work with receivePacket
See Writing to the socket

Definition at line 293 of file TcpSocket.h.

§ writePacket() [1/3]

ClockError clockUtils::sockets::TcpSocket::writePacket ( const void *  str,
const size_t  length 
)

sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket

§ writePacket() [2/3]

ClockError clockUtils::sockets::TcpSocket::writePacket ( const std::vector< uint8_t > &  vec)

sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket

§ writePacket() [3/3]

ClockError clockUtils::sockets::TcpSocket::writePacket ( const std::string &  str)
inline

sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket

Definition at line 207 of file TcpSocket.h.

§ writePacketAsync() [1/3]

ClockError clockUtils::sockets::TcpSocket::writePacketAsync ( const void *  str,
const size_t  length 
)

sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket

§ writePacketAsync() [2/3]

ClockError clockUtils::sockets::TcpSocket::writePacketAsync ( const std::vector< uint8_t > &  vec)

sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket

§ writePacketAsync() [3/3]

ClockError clockUtils::sockets::TcpSocket::writePacketAsync ( const std::string &  str)
inline

sends a packet being able to be completely received in one call of receivePacket
See Writing to the socket

Definition at line 230 of file TcpSocket.h.


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