-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
48 lines (42 loc) · 1.24 KB
/
client.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
#include "client.h"
#include <QUdpSocket>
Client::Client(QObject *parent) :
QObject(parent)
{
m_tcpSocket = new QTcpSocket(this);
m_pingSocket = new QUdpSocket(this);
connect(m_pingSocket, SIGNAL(readyRead()),
SLOT(onNewServeurRecieved()));
m_pingSocket->bind(QHostAddress::Any, 8002);
}
void Client::updateServerRequest()
{
m_pingSocket->writeDatagram("ping", QHostAddress::Broadcast, 8000);
}
void Client::connectToServer(QHostAddress serverAdress)
{
m_serverAdress = serverAdress;
m_tcpSocket->connectToHost(m_serverAdress, 8001);
}
void Client::sendMessage(const Message& message)
{
while(!m_tcpSocket->isWritable());
m_tcpSocket->write(message.serialize());
}
void Client::onNewServeurRecieved()
{
qDebug() << "NewServeurRecieved";
while (m_pingSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(m_pingSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
m_pingSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
if(!m_hostList.contains(sender))
{
m_hostList << sender;
emit serverListChanged();
}
}
}