-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackgroundbroadcastreceiver.cpp
More file actions
42 lines (38 loc) · 1.35 KB
/
backgroundbroadcastreceiver.cpp
File metadata and controls
42 lines (38 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "backgroundbroadcastreceiver.h"
#include "shareloc.h"
#include <QtWidgets>
#include <QtNetwork>
#include <QSet>
#include <QFileDialog>
BackgroundBroadcastReceiver::BackgroundBroadcastReceiver(QWidget *parent) : QWidget(parent)
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(45454, QUdpSocket::ShareAddress);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
}
void BackgroundBroadcastReceiver::processPendingDatagrams()
{
QByteArray datagram;
QSet<QString> localIPs;
const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost){
localIPs.insert(address.toString());
}
}
while(udpSocket->hasPendingDatagrams()) {
datagram.resize(int(udpSocket->pendingDatagramSize()));
udpSocket->readDatagram(datagram.data(), datagram.size());
QString IPaddr = datagram.constData();
QHostAddress address(IPaddr);
if(QAbstractSocket::IPv4Protocol != address.protocol()){
continue;
}
if(localIPs.contains(IPaddr)){
continue;
}
sharedData::activeIPs.insert(IPaddr);
}
emit updatedIPs(sharedData::activeIPs);
}