diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-01-13 11:53:15 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-01-13 11:53:15 +0100 |
commit | ea5594186bafa5489d6086a26d71b8f3d1ccf9cd (patch) | |
tree | a307b0882a867b415c68cd7d644241abe0c971e1 /lib/UdpSocket.h | |
parent | f908d28e72887b68391a246ceb328cb52dcb2aaa (diff) | |
download | dabmod-ea5594186bafa5489d6086a26d71b8f3d1ccf9cd.tar.gz dabmod-ea5594186bafa5489d6086a26d71b8f3d1ccf9cd.tar.bz2 dabmod-ea5594186bafa5489d6086a26d71b8f3d1ccf9cd.zip |
Add threaded UDP input for EDI
Diffstat (limited to 'lib/UdpSocket.h')
-rw-r--r-- | lib/UdpSocket.h | 29 |
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; +}; + + |