From ea5594186bafa5489d6086a26d71b8f3d1ccf9cd Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 13 Jan 2017 11:53:15 +0100 Subject: Add threaded UDP input for EDI --- lib/UdpSocket.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'lib/UdpSocket.h') 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 #include #include @@ -45,6 +46,8 @@ #include #include #include +#include +#include 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 get_packet_buffer(void); + + private: + void m_run(void); + + int m_port; + std::thread m_thread; + std::atomic m_stop; + ThreadsafeQueue m_packets; + UdpSocket m_sock; +}; + + -- cgit v1.2.3