Skip to content

Commit

Permalink
Merge commit '5da24ae77037c3344817bf227996218eee60b371' into v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGLM committed May 24, 2023
2 parents 8a8c306 + 5da24ae commit ea3159c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions library/src/networkdiscovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void NetworkDiscovery::searchDevices()
discovered_devices.clear();
QByteArray data;
auto dStream = std::make_unique<QDataStream>(&data, QIODevice::ReadWrite);
(*dStream) << static_cast<quint16>(0x2a) << static_cast<quint16>(m_device_type);
(*dStream) << static_cast<quint16>(0x2a);
(*dStream) << static_cast<quint16>(m_device_type);

if (socket != nullptr) {
// auto datagram = QNetworkDatagram{data,m_broadcast_address, m_port};
Expand All @@ -54,29 +55,39 @@ void NetworkDiscovery::readPendingDatagrams()
{
while (socket->hasPendingDatagrams()) {
auto datagram = socket->receiveDatagram();
auto sender_address = QHostAddress(datagram.senderAddress().toIPv4Address());
auto data = datagram.data();
if (datagram.isNull() || !datagram.isValid() || data.size() < 4) {
datagram.clear();
return;
}
auto sender_address = QHostAddress(datagram.senderAddress().toIPv4Address());
QDataStream inStream { &data, QIODevice::ReadOnly };
quint16 the_answer_to_everything;
quint16 device_type;
inStream >> the_answer_to_everything;
if (the_answer_to_everything != 0x2a) {
datagram.clear();
return;
}
inStream >> device_type;
qDebug() << "found device: " << sender_address << " type: " << device_type;
if (device_type == static_cast<quint16>(m_device_type)) {
bool skip = false;
for (auto address : m_own_ipv4) {
if (address == sender_address)
if (address == sender_address){
skip = true;
}
}
if (skip)
if (skip){
continue; // do not answer or discover self
}
}
discovered_devices.append(QPair<quint16, QHostAddress> { static_cast<quint16>(device_type), sender_address});

if (static_cast<DeviceType>(device_type) == DeviceType::GUI) {
data = QByteArray();
QDataStream outStream { &data, QIODevice::ReadWrite };
outStream << static_cast<quint16>(m_device_type);
outStream << static_cast<quint16>(0x2a) << static_cast<quint16>(m_device_type);
socket->writeDatagram(data, sender_address, m_port);
}
}
Expand Down

0 comments on commit ea3159c

Please sign in to comment.