25 #ifndef __CLOCKUTILS_SOCKETS_UDPSOCKET_H__ 26 #define __CLOCKUTILS_SOCKETS_UDPSOCKET_H__ 28 #include <condition_variable> 42 #if CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_WIN32 49 #elif CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_LINUX 50 #include <arpa/inet.h> 54 #include <netinet/in.h> 55 #include <sys/socket.h> 56 #include <sys/types.h> 60 #define INVALID_SOCKET -1 84 const size_t MAX_PACKET_SIZE = 32 * 1024;
110 ClockError writePacket(
IPv4 ip, uint16_t port,
const void * str,
const size_t length);
116 ClockError writePacket(
IPv4 ip, uint16_t port,
const std::vector<uint8_t> & vec);
123 return writePacket(ip, port, str.c_str(), str.length());
130 ClockError writePacketAsync(
IPv4 ip, uint16_t port,
const void * str,
const size_t length);
136 ClockError writePacketAsync(
IPv4 ip, uint16_t port,
const std::vector<uint8_t> & vec);
143 return writePacketAsync(ip, port, std::vector<uint8_t>(str.begin(), str.end()));
150 ClockError write(
IPv4 ip, uint16_t port,
const void * str,
size_t length);
157 return write(ip, port, const_cast<const unsigned char *>(&vec[0]), vec.size());
165 return write(ip, port, str.c_str(), str.length());
172 ClockError writeAsync(
IPv4 ip, uint16_t port,
const void * str,
size_t length);
178 ClockError writeAsync(
IPv4 ip, uint16_t port,
const std::vector<uint8_t> & vec);
185 return writeAsync(ip, port, std::vector<uint8_t>(str.begin(), str.end()));
191 template<
typename... T>
193 return writePacket(
convertIP(ip), port, data...);
200 template<
typename... T>
202 return writePacketAsync(
convertIP(ip), port, data...);
209 template<
typename... T>
211 return write(
convertIP(ip), port, data...);
218 template<
typename... T>
220 return writeAsync(
convertIP(ip), port, data...);
227 template<
typename... T>
236 template<
typename... T>
245 template<
typename... T>
254 template<
typename... T>
266 ClockError receivePacket(std::vector<uint8_t> & buffer, std::string & ip, uint16_t & port);
274 ClockError receivePacket(std::string & buffer, std::string & ip, uint16_t & port);
285 template<
class Container>
287 if (_sock == INVALID_SOCKET) {
288 return ClockError::NOT_READY;
291 buffer.resize(MAX_PACKET_SIZE + 4);
293 struct sockaddr_in remaddr;
294 remaddr.sin_family = 0;
295 remaddr.sin_port = 0;
296 memset(&remaddr.sin_addr, 0,
sizeof(remaddr.sin_addr));
297 memset(remaddr.sin_zero, 0, 8);
301 #if CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_LINUX 302 rc = recvfrom(_sock, &buffer[0], MAX_PACKET_SIZE, 0, (
struct sockaddr *) &remaddr, &addrlen);
303 #elif CLOCKUTILS_PLATFORM == CLOCKUTILS_PLATFORM_WIN32 304 rc = recvfrom(_sock, reinterpret_cast<char *>(&buffer[0]),
int(MAX_PACKET_SIZE), 0, (
struct sockaddr *) &remaddr, &addrlen);
307 port =
static_cast<uint16_t
>(ntohs(remaddr.sin_port));
308 ip = inet_ntoa(remaddr.sin_addr);
313 if (error == ClockError::IN_PROGRESS) {
318 }
else if (rc == 0) {
319 return ClockError::NOT_CONNECTED;
325 buffer.resize(
size_t(rc));
327 return ClockError::SUCCESS;
339 std::map<std::pair<std::string, uint16_t>, std::vector<uint8_t>> _buffer;
344 std::thread * _callbackThread;
349 std::mutex _writePacketAsyncLock;
350 std::mutex _writeAsyncLock;
355 enum AsyncQueueInfo {
360 std::queue<std::tuple<std::vector<uint8_t>,
IPv4, uint16_t>> _writePacketAsyncQueue;
361 std::queue<std::tuple<std::vector<uint8_t>,
IPv4, uint16_t>> _writeAsyncQueue;
363 std::condition_variable _condVar;
364 std::mutex _condMutex;
366 std::thread * _worker;
ClockError write(IPv4 ip, uint16_t port, const std::vector< uint8_t > &vec)
sends a packet, doesn't work with receivePacket
ClockError writePacketToIP(const std::string &ip, uint16_t port, T... data)
sends a packet being able to be completely received in one call of receivePacket
ClockError writePacketAsync(IPv4 ip, uint16_t port, const std::string &str)
sends a packet asynchronous being able to be completely received in one call of receivePacket ...
ClockError writePacket(IPv4 ip, uint16_t port, const std::string &str)
sends a packet being able to be completely received in one call of receivePacket
ClockError writeToIP(const std::string &ip, uint16_t port, T... data)
sends a packet, doesn't work with receivePacket
ClockError write(IPv4 ip, uint16_t port, const std::string &str)
sends a packet, doesn't work with receivePacket
ClockError writeToHostnameAsync(const std::string &hostname, uint16_t port, T... data)
sends a message asynchronous, doesn't work with receivePacket
#define CLOCK_SOCKETS_API
ClockError writePacketToHostnameAsync(const std::string &hostname, uint16_t port, T... data)
sends a packet asynchronous being able to be completely received in one call of receivePacket ...
ClockError writeToIPAsync(const std::string &ip, uint16_t port, T... data)
sends a message asynchronous, doesn't work with receivePacket
CLOCK_SOCKETS_API IPv4 convertIP(const std::string &ip)
converts an IP in the numbers-and-dots notation into an IPv4 integer
ClockError writePacketToHostname(const std::string &hostname, uint16_t port, T... data)
sends a packet being able to be completely received in one call of receivePacket
CLOCK_SOCKETS_API IPv4 resolveHostname(const std::string &hn)
returns the IP for a given hostname
class for sockets using udp
ClockError writeAsync(IPv4 ip, uint16_t port, const std::string &str)
sends a message asynchronous, doesn't work with receivePacket
ClockError read(Container &buffer, std::string &ip, uint16_t &port)
receives data on the socket
ClockError writePacketToIPAsync(const std::string &ip, uint16_t port, T... data)
sends a packet asynchronous being able to be completely received in one call of receivePacket ...
ClockError writeToHostname(const std::string &hostname, uint16_t port, T... data)
sends a packet, doesn't work with receivePacket
std::function< void(std::vector< uint8_t > packet, std::string ip, uint16_t port, ClockError err)> packetCallback
this function type is used receiving a packet using receiveCallback and is called for every packet ...