QTfrontend/netudpwidget.cpp
author unc0rr
Wed, 21 Feb 2007 19:40:06 +0000
changeset 471 e1eb64ba5e9c
parent 468 8403d6884707
child 486 7ea71cd3acd5
permissions -rw-r--r--
Small cleanup, faster compilation as a result
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ebd79d171804 - Remake net game page
unc0rr
parents: 417
diff changeset
    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
bc7fea9abd9c randomly chosen ports
displacer
parents: 416
diff changeset
    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
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    16
  updateList();
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    17
}
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    18
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    19
void HWNetUdpWidget::updateList()
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    20
{
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    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
}