From a4d496cdb067155cb3b988a93e0ec967168cf20f Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Fri, 11 Jan 2019 23:17:43 +0000 Subject: [PATCH] UDP Message Server now joins a multicast group on all available multicast capable network interfaces --- MessageServer.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/MessageServer.cpp b/MessageServer.cpp index 609d24a9a..0748bcaa0 100644 --- a/MessageServer.cpp +++ b/MessageServer.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -95,16 +96,30 @@ MessageServer::impl::BindMode constexpr MessageServer::impl::bind_mode_; void MessageServer::impl::leave_multicast_group () { - if (!multicast_group_address_.isNull () && BoundState == state ()) + if (!multicast_group_address_.isNull () && BoundState == state () +#if QT_VERSION >= 0x050600 + && multicast_group_address_.isMulticast () +#endif + ) { - leaveMulticastGroup (multicast_group_address_); + for (auto const& interface : QNetworkInterface::allInterfaces ()) + { + if (QNetworkInterface::CanMulticast & interface.flags ()) + { + leaveMulticastGroup (multicast_group_address_, interface); + } + } } } void MessageServer::impl::join_multicast_group () { if (BoundState == state () - && !multicast_group_address_.isNull ()) + && !multicast_group_address_.isNull () +#if QT_VERSION >= 0x050600 + && multicast_group_address_.isMulticast () +#endif + ) { if (IPv4Protocol == multicast_group_address_.protocol () && IPv4Protocol != localAddress ().protocol ()) @@ -112,7 +127,15 @@ void MessageServer::impl::join_multicast_group () close (); bind (QHostAddress::AnyIPv4, port_, bind_mode_); } - if (!joinMulticastGroup (multicast_group_address_)) + bool joined {false}; + for (auto const& interface : QNetworkInterface::allInterfaces ()) + { + if (QNetworkInterface::CanMulticast & interface.flags ()) + { + joined |= joinMulticastGroup (multicast_group_address_, interface); + } + } + if (!joined) { multicast_group_address_.clear (); }