From d959b89b873e67b68a55809cc09e09c726f45050 Mon Sep 17 00:00:00 2001 From: Andrew Stanley-Jones Date: Thu, 31 May 2012 13:50:07 +1000 Subject: Reconnect if the interface goes away Multicast sockets don't tell us if the interface comes and goes. Poll and try to reconnect if we notice the interface is missing. Change-Id: I5c8542ec1d92b1fd59afd1069b7093d70cd80625 Reviewed-by: Alex Wilson --- src/tools/sfwlisten/main.cpp | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'src/tools') diff --git a/src/tools/sfwlisten/main.cpp b/src/tools/sfwlisten/main.cpp index 96c6e9aa..72b1262c 100644 --- a/src/tools/sfwlisten/main.cpp +++ b/src/tools/sfwlisten/main.cpp @@ -63,15 +63,19 @@ public: SFWReceiver(const QString &intfFilter); public slots: - void makeSockets(); void socketReadyRead(); + void socketVerify(); + void makeSockets(); private: QString intfFilter; + QTimer socketCheck; + QUdpSocket *socket; }; SFWReceiver::SFWReceiver(const QString &intfFilter) : intfFilter(intfFilter) { + connect(&socketCheck, SIGNAL(timeout()), this, SLOT(socketVerify())); makeSockets(); } @@ -89,7 +93,7 @@ void SFWReceiver::makeSockets() || intf.name().startsWith("tun") || intf.name().startsWith("tap")) continue; - QUdpSocket *socket = new QUdpSocket(this); + socket = new QUdpSocket(this); printf("Trying interface %s ...", qPrintable(intf.name())); if (!socket->bind(QHostAddress::AnyIPv4, 10520, QUdpSocket::ShareAddress)) { printf("Couldn't bind: %s\n", qPrintable(socket->errorString())); @@ -113,12 +117,36 @@ void SFWReceiver::makeSockets() if (!gotone) { QTimer::singleShot(200, this, SLOT(makeSockets())); + socketCheck.stop(); + } else { + socketCheck.setInterval(1000); + socketCheck.start(); + } +} + +void SFWReceiver::socketVerify() +{ + QList intfs = QNetworkInterface::allInterfaces(); + bool gotone = false; + QString name = socket->multicastInterface().name(); + + foreach (const QNetworkInterface &intf, intfs) { + if (intf.name() == name) { + gotone = true; + break; + } + } + + if (!gotone) { + printf("Interface down...\n"); + delete socket; + socket = 0; + makeSockets(); } } void SFWReceiver::socketReadyRead() { - QUdpSocket *socket = qobject_cast(sender()); QString intf = socket->multicastInterface().name(); while (socket->hasPendingDatagrams()) { -- cgit v1.2.3