20 #include <QUdpSocket> |
20 #include <QUdpSocket> |
21 |
21 |
22 #include "netudpserver.h" |
22 #include "netudpserver.h" |
23 |
23 |
24 HWNetUdpServer::HWNetUdpServer(QObject *parent, const QString & descr, quint16 port) : |
24 HWNetUdpServer::HWNetUdpServer(QObject *parent, const QString & descr, quint16 port) : |
25 HWNetRegisterServer(parent, descr, port), |
25 HWNetRegisterServer(parent, descr, port), |
26 m_descr(descr) |
26 m_descr(descr) |
27 { |
27 { |
28 pUdpSocket = new QUdpSocket(this); |
28 pUdpSocket = new QUdpSocket(this); |
29 pUdpSocket->bind(46631); |
29 pUdpSocket->bind(46631); |
30 connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead())); |
30 connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead())); |
31 } |
31 } |
32 |
32 |
33 void HWNetUdpServer::onClientRead() |
33 void HWNetUdpServer::onClientRead() |
34 { |
34 { |
35 while (pUdpSocket->hasPendingDatagrams()) { |
35 while (pUdpSocket->hasPendingDatagrams()) |
36 QByteArray datagram; |
36 { |
37 datagram.resize(pUdpSocket->pendingDatagramSize()); |
37 QByteArray datagram; |
38 QHostAddress clientAddr; |
38 datagram.resize(pUdpSocket->pendingDatagramSize()); |
39 quint16 clientPort; |
39 QHostAddress clientAddr; |
40 pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort); |
40 quint16 clientPort; |
41 if(datagram.startsWith("hedgewars client")) { |
41 pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort); |
42 // send answer to client |
42 if(datagram.startsWith("hedgewars client")) |
43 pUdpSocket->writeDatagram(QString("hedgewars server\n%1").arg(m_descr).toUtf8(), clientAddr, clientPort); |
43 { |
|
44 // send answer to client |
|
45 pUdpSocket->writeDatagram(QString("hedgewars server\n%1").arg(m_descr).toUtf8(), clientAddr, clientPort); |
|
46 } |
44 } |
47 } |
45 } |
|
46 } |
48 } |
47 |
49 |
48 void HWNetUdpServer::unregister() |
50 void HWNetUdpServer::unregister() |
49 { |
51 { |
50 deleteLater(); |
52 deleteLater(); |
51 } |
53 } |