author | displacer |
Fri, 23 Feb 2007 13:15:04 +0000 | |
changeset 484 | 3583f6289eee |
parent 468 | 8403d6884707 |
child 486 | 7ea71cd3acd5 |
permissions | -rw-r--r-- |
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
1 |
#include <QUdpSocket> |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
2 |
#include <QListWidget> |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
3 |
|
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
4 |
#include "netudpwidget.h" |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
5 |
|
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
6 |
HWNetUdpWidget::HWNetUdpWidget(QWidget* parent) : |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
7 |
QWidget(parent), mainLayout(this) |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
8 |
{ |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
9 |
serversList = new QListWidget(this); |
421 | 10 |
mainLayout.setMargin(0); |
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
11 |
mainLayout.addWidget(serversList); |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
12 |
pUdpSocket = new QUdpSocket(this); |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
13 |
|
417 | 14 |
pUdpSocket->bind(); |
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
15 |
connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead())); |
416 | 16 |
updateList(); |
17 |
} |
|
18 |
||
19 |
void HWNetUdpWidget::updateList() |
|
20 |
{ |
|
21 |
serversList->clear(); |
|
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
22 |
pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631); |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
23 |
} |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
24 |
|
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
25 |
void HWNetUdpWidget::onClientRead() |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
26 |
{ |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
27 |
while (pUdpSocket->hasPendingDatagrams()) { |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
28 |
QByteArray datagram; |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
29 |
datagram.resize(pUdpSocket->pendingDatagramSize()); |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
30 |
QHostAddress clientAddr; |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
31 |
quint16 clientPort; |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
32 |
pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort); |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
33 |
if(QString("%1").arg(datagram.data())==QString("hedgewars server")) { |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
34 |
serversList->addItem(clientAddr.toString()); |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
35 |
} |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
36 |
} |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
37 |
} |