aboutsummaryrefslogtreecommitdiffstats
path: root/lib/UdpSocket.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/UdpSocket.h')
-rw-r--r--lib/UdpSocket.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/UdpSocket.h b/lib/UdpSocket.h
index f51e87c..81a7d2b 100644
--- a/lib/UdpSocket.h
+++ b/lib/UdpSocket.h
@@ -31,6 +31,7 @@
#endif
#include "InetAddress.h"
+#include "ThreadsafeQueue.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
@@ -45,6 +46,8 @@
#include <stdlib.h>
#include <iostream>
#include <vector>
+#include <thread>
+#include <atomic>
class UdpPacket;
@@ -172,3 +175,29 @@ class UdpPacket
InetAddress address;
};
+/* Threaded UDP receiver */
+class UdpReceiver {
+ public:
+ UdpReceiver() : m_port(0), m_thread(), m_stop(false), m_packets() {}
+ ~UdpReceiver();
+ UdpReceiver(const UdpReceiver&) = delete;
+ UdpReceiver operator=(const UdpReceiver&) = delete;
+
+ // Start the receiver in a separate thread
+ void start(int port);
+
+ // Get the data contained in a UDP packet, blocks if none available
+ // In case of error, throws a runtime_error
+ std::vector<uint8_t> get_packet_buffer(void);
+
+ private:
+ void m_run(void);
+
+ int m_port;
+ std::thread m_thread;
+ std::atomic<bool> m_stop;
+ ThreadsafeQueue<UdpPacket> m_packets;
+ UdpSocket m_sock;
+};
+
+