QTfrontend/netudpwidget.cpp
author unc0rr
Sat, 10 Feb 2007 17:59:30 +0000
changeset 426 7523417d84d6
parent 421 ebd79d171804
child 468 8403d6884707
permissions -rw-r--r--
- Enable 'norsk' theme - Many images improvements - Show 0.9 version
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
#include <QDebug>
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     7
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     8
HWNetUdpWidget::HWNetUdpWidget(QWidget* parent) :
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     9
  QWidget(parent), mainLayout(this)
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    10
{
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    11
  serversList = new QListWidget(this);
421
ebd79d171804 - Remake net game page
unc0rr
parents: 417
diff changeset
    12
  mainLayout.setMargin(0);
413
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    13
  mainLayout.addWidget(serversList);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    14
  pUdpSocket = new QUdpSocket(this);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    15
417
bc7fea9abd9c randomly chosen ports
displacer
parents: 416
diff changeset
    16
  pUdpSocket->bind();
413
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    17
  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
416
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    18
  updateList();
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    19
}
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
void HWNetUdpWidget::updateList()
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    22
{
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    23
  serversList->clear();
413
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    24
  pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    25
}
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
void HWNetUdpWidget::onClientRead()
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    28
{
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    29
  while (pUdpSocket->hasPendingDatagrams()) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    30
    QByteArray datagram;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    31
    datagram.resize(pUdpSocket->pendingDatagramSize());
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    32
    QHostAddress clientAddr;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    33
    quint16 clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    34
    pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    35
    if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    36
      qDebug() << "detected server at " << clientAddr << ":" << clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    37
      serversList->addItem(clientAddr.toString());
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    38
    }
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    39
  }
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    40
}