diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2021-01-16 08:05:41 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2021-01-18 13:22:09 +0100 |
commit | 9719087ebcb4bd422c592f994f2e1691191e91d6 (patch) | |
tree | 07b002cf120bec05d6f61d101f825f5906c1374c /lib/Socket.h | |
parent | a6ab8b3e97d72996ec30062a564cecde7c1b44a2 (diff) | |
download | dabmux-9719087ebcb4bd422c592f994f2e1691191e91d6.tar.gz dabmux-9719087ebcb4bd422c592f994f2e1691191e91d6.tar.bz2 dabmux-9719087ebcb4bd422c592f994f2e1691191e91d6.zip |
Common 405ad00: Reimplement UDPReceiver with poll instead of a thread
Diffstat (limited to 'lib/Socket.h')
-rw-r--r-- | lib/Socket.h | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/lib/Socket.h b/lib/Socket.h index 2291dd5..33cdc05 100644 --- a/lib/Socket.h +++ b/lib/Socket.h @@ -56,6 +56,8 @@ struct InetAddress { struct sockaddr *as_sockaddr() { return reinterpret_cast<sockaddr*>(&addr); }; void resolveUdpDestination(const std::string& destination, int port); + + std::string to_string() const; }; /** This class represents a UDP packet. @@ -103,6 +105,8 @@ class UDPSocket ~UDPSocket(); UDPSocket(const UDPSocket& other) = delete; const UDPSocket& operator=(const UDPSocket& other) = delete; + UDPSocket(UDPSocket&& other); + const UDPSocket& operator=(UDPSocket&& other); /** Close the already open socket, and create a new one. Throws a runtime_error on error. */ void reinit(int port); @@ -121,36 +125,36 @@ class UDPSocket */ void setBlocking(bool block); + SOCKET getNativeSocket() const; + int getPort() const; + protected: - SOCKET m_sock; + SOCKET m_sock = INVALID_SOCKET; + int m_port = 0; }; -/* Threaded UDP receiver */ +/* UDP packet receiver supporting receiving from several ports at once */ class UDPReceiver { public: - UDPReceiver(); - ~UDPReceiver(); - UDPReceiver(const UDPReceiver&) = delete; - UDPReceiver operator=(const UDPReceiver&) = delete; + void add_receive_port(int port, const std::string& bindto, const std::string& mcastaddr); - // Start the receiver in a separate thread - void start(int port, const std::string& bindto, const std::string& mcastaddr, size_t max_packets_queued); + struct ReceivedPacket { + std::vector<uint8_t> packetdata; + InetAddress received_from; + int port_received_on; + }; - // 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); + class Interrupted {}; + class Timeout {}; + /* Returns one or several packets, + * throws a Timeout on timeout, Interrupted on EINTR, a runtime_error + * on error. */ + std::vector<ReceivedPacket> receive(int timeout_ms); private: void m_run(void); - int m_port = 0; - std::string m_bindto; - std::string m_mcastaddr; - size_t m_max_packets_queued = 1; - std::thread m_thread; - std::atomic<bool> m_stop = ATOMIC_VAR_INIT(false); - ThreadsafeQueue<UDPPacket> m_packets; - UDPSocket m_sock; + std::vector<UDPSocket> m_sockets; }; class TCPSocket { |