-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_discovery.cpp
53 lines (46 loc) · 1.86 KB
/
port_discovery.cpp
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
43
44
45
46
47
48
49
50
51
52
53
// ------------------------------------------------------------------------------------------------------------------
#include "port_discovery.h"
#include <QUdpSocket>
#include <QNetworkDatagram>
// ------------------------------------------------------------------------------------------------------------------
void CUdpHolePunchingServer::start()
{
qDebug() << "Start Hole Punching on Port " << port();
m_Socket = new QUdpSocket(this);
m_Socket->bind(port());
QObject::connect(m_Socket, &QUdpSocket::readyRead, this, &CUdpHolePunchingServer::onSocketReadyRead);
}
void CUdpHolePunchingServer::onSocketReadyRead()
{
if (m_Socket->hasPendingDatagrams())
{
QNetworkDatagram datagram = m_Socket->receiveDatagram();
DiscoveryResult result;
result.address = datagram.senderAddress();
result.port = datagram.senderPort();
m_Socket->writeDatagram(QByteArray(), datagram.senderAddress(), datagram.senderPort());
qDebug() << "Discovered " << result.address << " " << result.port;
m_Socket->close();
emit clientDiscovered(result);
}
}
// ------------------------------------------------------------------------------------------------------------------
void CUdpHolePunchingClient::start()
{
m_Socket = new QUdpSocket(this);
m_Socket->bind(bindPort());
//QObject::connect(m_Socket, &QUdpSocket::readyRead, this, &CUdpHolePunchingClient::onSocketReadyRead);
QByteArray emptyPackage;
m_Socket->writeDatagram(emptyPackage, host(), destinationPort());
m_Socket->flush();
m_Socket->close();
}
void CUdpHolePunchingClient::onSocketReadyRead()
{
/*if (m_Socket->hasPendingDatagrams())
{
qDebug() << "Server responded.";
emit serverResponded();
}*/
}
// ------------------------------------------------------------------------------------------------------------------