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
1 changed files with 27 additions and 4 deletions

View File

@ -2,6 +2,7 @@
#include <stdexcept>
#include <QNetworkInterface>
#include <QUdpSocket>
#include <QString>
#include <QTimer>
@ -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 ();
}