25 #ifndef __CLOCKUTILS_SOCKETS_TCPSOCKET_H__ 26 #define __CLOCKUTILS_SOCKETS_TCPSOCKET_H__ 28 #include <condition_variable> 42 #if CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_WIN32 46 #elif CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_LINUX 47 #include <arpa/inet.h> 51 #include <netinet/in.h> 52 #include <sys/socket.h> 53 #include <sys/types.h> 57 #define INVALID_SOCKET -1 113 ClockError listen(uint16_t listenPort,
int maxParallelConnections,
bool acceptMultiple,
const acceptCallback acb);
125 return ClockError::INVALID_IP;
127 return connect(ip, remotePort, timeout);
140 return ClockError::INVALID_IP;
142 return connect(ip, remotePort, timeout);
151 ClockError connect(
const IPv4 remoteIP, uint16_t remotePort,
unsigned int timeout);
161 std::string getRemoteIP()
const;
166 uint16_t getRemotePort()
const;
171 static std::vector<std::pair<std::string, std::string>> enumerateLocalIPs();
176 std::string getLocalIP()
const;
181 std::string getPublicIP()
const;
186 uint16_t getLocalPort()
const;
193 ClockError writePacket(
const void * str,
const size_t length);
200 ClockError writePacket(
const std::vector<uint8_t> & vec);
208 return writePacket(str.c_str(), str.size());
216 ClockError writePacketAsync(
const void * str,
const size_t length);
223 ClockError writePacketAsync(
const std::vector<uint8_t> & vec);
231 return writePacketAsync(std::vector<uint8_t>(str.begin(), str.end()));
238 ClockError receivePacket(std::vector<uint8_t> & buffer);
243 ClockError receivePacket(std::string & buffer);
256 ClockError write(
const void * str,
size_t length);
263 ClockError write(
const std::vector<uint8_t> & vec);
271 return write(str.c_str(), str.length());
279 ClockError writeAsync(
const void * str,
const size_t length);
286 ClockError writeAsync(
const std::vector<uint8_t> & vec);
294 return writeAsync(std::vector<uint8_t>(str.begin(), str.end()));
300 template<
class Container>
302 if (_status != SocketStatus::CONNECTED) {
303 return ClockError::NOT_READY;
310 #if CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_LINUX 311 rc = recv(_sock, &buffer[0], 256, 0);
312 #elif CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_WIN32 313 rc = recv(_sock, reinterpret_cast<char *>(&buffer[0]), 256, 0);
319 if (error == ClockError::IN_PROGRESS) {
324 }
else if (rc == 0) {
325 return ClockError::NOT_CONNECTED;
330 buffer.resize(
size_t(rc));
332 return ClockError::SUCCESS;
341 std::stringstream ss;
343 writePacket(ss.str());
353 std::stringstream ss;
355 writePacket(ss.str());
366 receivePacket(buffer);
367 std::stringstream ss(buffer);
381 receivePacket(buffer);
382 std::stringstream ss(buffer);
391 enum class SocketStatus {
405 SocketStatus _status;
410 std::mutex _writePacketAsyncLock;
411 std::mutex _writeAsyncLock;
416 std::queue<std::vector<uint8_t>> _writePacketAsyncQueue;
417 std::queue<std::vector<uint8_t>> _writeAsyncQueue;
422 std::vector<uint8_t> _buffer;
426 std::thread * _worker;
427 std::thread * _listenThread;
429 std::condition_variable _condVar;
430 std::mutex _condMutex;
432 std::thread * _callbackThread;
452 void listenFunc(SOCKET sock,
bool acceptMultiple,
const acceptCallback acb);
ClockError writeAsync(const std::string &str)
sends a message asynchron, doesn't work with receivePacket See Writing to the socket ...
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 ...
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...
class for sockets using tcp
ClockError connectToHostname(const std::string &remoteHostname, uint16_t remotePort, unsigned int timeout)
creates a connection to the given pair of hostname and port
ClockError read(Container &buffer)
receives data on the socket
#define CLOCK_SOCKETS_API
ClockError write(const std::string &str)
sends a message, doesn't work with receivePacket See Writing to the socket
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 ...
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 ...
ClockError writePacket(const std::string &str)
sends a packet being able to be completely received in one call of receivePacket See Writing to the...
std::function< void(TcpSocket *, ClockError)> acceptCallback
this function type is used as accept callback, so every accepted socket will reach this function on h...
CLOCK_SOCKETS_API IPv4 convertIP(const std::string &ip)
converts an IP in the numbers-and-dots notation into an IPv4 integer
CLOCK_SOCKETS_API IPv4 resolveHostname(const std::string &hn)
returns the IP for a given hostname
ClockError writePacketAsync(const std::string &str)
sends a packet being able to be completely received in one call of receivePacket See Writing to the...
ClockError connectToIP(const std::string &remoteIP, uint16_t remotePort, unsigned int timeout)
creates a connection to the given pair of IP and port
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 ...