UDP Message Server now joins a multicast group on all available multicast capable network interfaces

This commit is contained in:
Bill Somerville 2019-01-11 23:17:43 +00:00
parent d17efce7a1
commit a4d496cdb0

View File

@ -2,6 +2,7 @@
#include <stdexcept> #include <stdexcept>
#include <QNetworkInterface>
#include <QUdpSocket> #include <QUdpSocket>
#include <QString> #include <QString>
#include <QTimer> #include <QTimer>
@ -95,16 +96,30 @@ MessageServer::impl::BindMode constexpr MessageServer::impl::bind_mode_;
void MessageServer::impl::leave_multicast_group () 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 () void MessageServer::impl::join_multicast_group ()
{ {
if (BoundState == state () 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 () if (IPv4Protocol == multicast_group_address_.protocol ()
&& IPv4Protocol != localAddress ().protocol ()) && IPv4Protocol != localAddress ().protocol ())
@ -112,7 +127,15 @@ void MessageServer::impl::join_multicast_group ()
close (); close ();
bind (QHostAddress::AnyIPv4, port_, bind_mode_); 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 (); multicast_group_address_.clear ();
} }