clockUtils
1.1
|
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 TcpSocket & | operator>> (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... | |
class for sockets using tcp
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.
Definition at line 84 of file TcpSocket.h.
typedef std::function<void(TcpSocket *, ClockError)> clockUtils::sockets::TcpSocket::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.
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.
clockUtils::sockets::TcpSocket::TcpSocket | ( | ) |
constructor
clockUtils::sockets::TcpSocket::~TcpSocket | ( | ) |
destructor, closes socket if not happened before
void clockUtils::sockets::TcpSocket::close | ( | ) |
closes a connection if socket is connected
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
[in] | remoteIP | the ip of the connection listening as IPv4 integer |
[in] | remotePort | the port of the connection listening |
[in] | timeout | the time in milliseconds a connect request should last in maximum |
|
inline |
creates a connection to the given pair of hostname and port
[in] | remoteHostname | the hostname of the connection listening as std::string |
[in] | remotePort | the port of the connection listening |
[in] | timeout | the time in milliseconds a connect request should last in maximum |
Definition at line 136 of file TcpSocket.h.
|
inline |
creates a connection to the given pair of IP and port
[in] | remoteIP | the ip of the connection listening as std::string |
[in] | remotePort | the port of the connection listening |
[in] | timeout | the time in milliseconds a connect request should last in maximum |
Definition at line 121 of file TcpSocket.h.
|
static |
returns all local IP's
std::string clockUtils::sockets::TcpSocket::getLocalIP | ( | ) | const |
returns the local IP of this socket
uint16_t clockUtils::sockets::TcpSocket::getLocalPort | ( | ) | const |
returns the port this socket uses for connection
std::string clockUtils::sockets::TcpSocket::getPublicIP | ( | ) | const |
returns the public IP of this socket shown in network
std::string clockUtils::sockets::TcpSocket::getRemoteIP | ( | ) | const |
returns the ip of the socket this socket is connected with
uint16_t clockUtils::sockets::TcpSocket::getRemotePort | ( | ) | const |
returns the port of the socket this socket is connected with
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
[in] | listenPort | the port the socket is listening on |
[in] | maxParallelConnections | the amount of connections being able to be handled at once |
[in] | acceptMultiple | if set to false, only one connection is accepted, otherwise infinite |
[in] | acb | the 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 |
|
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.
|
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.
|
inline |
receives a packet being sent using operator<< or writePacket(Async) T is an enum value
Definition at line 364 of file TcpSocket.h.
|
inline |
receives a packet being sent using operator<< or writePacket(Async) T has to be streamable
Definition at line 379 of file TcpSocket.h.
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
|
inline |
receives data on the socket
Definition at line 301 of file TcpSocket.h.
ClockError clockUtils::sockets::TcpSocket::receiveCallback | ( | packetCallback | pcb | ) |
calls the callback for every packet that is received on this socket This function will return immediately
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
ClockError clockUtils::sockets::TcpSocket::receivePacket | ( | std::string & | buffer | ) |
receives a packet sent with writePacket, doesn't work with write
ClockError clockUtils::sockets::TcpSocket::write | ( | const void * | str, |
size_t | length | ||
) |
sends a message, doesn't work with receivePacket
See Writing to the socket
ClockError clockUtils::sockets::TcpSocket::write | ( | const std::vector< uint8_t > & | vec | ) |
sends a message, doesn't work with receivePacket
See Writing to the socket
|
inline |
sends a message, doesn't work with receivePacket
See Writing to the socket
Definition at line 270 of file TcpSocket.h.
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
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
|
inline |
sends a message asynchron, doesn't work with receivePacket
See Writing to the socket
Definition at line 293 of file TcpSocket.h.
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
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
|
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.
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
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
|
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.