From fdff20e96d7198124542e6d03c27e1d496c4b328 Mon Sep 17 00:00:00 2001 From: Sergiy G Date: Wed, 31 Jan 2018 10:41:34 +0200 Subject: Enabled multicast support. Fix missing header in PAPRStats --- lib/UdpSocket.cpp | 16 ++++++++++++---- lib/UdpSocket.h | 6 ++++-- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/UdpSocket.cpp b/lib/UdpSocket.cpp index e711577..7c99724 100644 --- a/lib/UdpSocket.cpp +++ b/lib/UdpSocket.cpp @@ -170,7 +170,7 @@ int UdpSocket::send(const std::vector& data, InetAddress destination) * @param groupname The multicast address to join. * @return 0 if ok, -1 if error */ -int UdpSocket::joinGroup(char* groupname) +int UdpSocket::joinGroup(const char* groupname, const char* if_addr) { ip_mreqn group; if ((group.imr_multiaddr.s_addr = inet_addr(groupname)) == INADDR_NONE) { @@ -181,7 +181,7 @@ int UdpSocket::joinGroup(char* groupname) setInetError("Not a multicast address"); return -1; } - group.imr_address.s_addr = htons(INADDR_ANY);; + group.imr_address.s_addr = inet_addr(if_addr); group.imr_ifindex = 0; if (setsockopt(listenSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group)) == SOCKET_ERROR) { @@ -261,8 +261,10 @@ UdpReceiver::~UdpReceiver() { } } -void UdpReceiver::start(int port, size_t max_packets_queued) { +void UdpReceiver::start(int port, std::string& bindto, std::string& mcastaddr, size_t max_packets_queued) { m_port = port; + m_bindto = bindto; + m_mcastaddr = mcastaddr; m_max_packets_queued = max_packets_queued; m_thread = std::thread(&UdpReceiver::m_run, this); } @@ -288,7 +290,13 @@ void UdpReceiver::m_run() private: atomic& m_stop; } autoSetStop(m_stop); - m_sock.reinit(m_port, "0.0.0.0"); + if(IN_MULTICAST(ntohl(inet_addr(m_mcastaddr.c_str())))){ + m_sock.reinit(m_port, m_mcastaddr); + m_sock.setMulticastSource(m_bindto.c_str()); + m_sock.joinGroup(m_mcastaddr.c_str(), m_bindto.c_str()); + } else { + m_sock.reinit(m_port, m_bindto); + } const size_t packsize = 8192; UdpPacket packet(packsize); diff --git a/lib/UdpSocket.h b/lib/UdpSocket.h index 8c968d2..8c71b9d 100644 --- a/lib/UdpSocket.h +++ b/lib/UdpSocket.h @@ -111,7 +111,7 @@ class UdpSocket */ int receive(UdpPacket& packet); - int joinGroup(char* groupname); + int joinGroup(const char* groupname, const char *if_addr); int setMulticastSource(const char* source_addr); int setMulticastTTL(int ttl); @@ -184,7 +184,7 @@ class UdpReceiver { UdpReceiver operator=(const UdpReceiver&) = delete; // Start the receiver in a separate thread - void start(int port, size_t max_packets_queued); + void start(int port, std::string& bindto, std::string& mcastaddr, size_t max_packets_queued); // Get the data contained in a UDP packet, blocks if none available // In case of error, throws a runtime_error @@ -194,6 +194,8 @@ class UdpReceiver { void m_run(void); int m_port; + std::string m_bindto; + std::string m_mcastaddr; size_t m_max_packets_queued; std::thread m_thread; std::atomic m_stop; -- cgit v1.2.3