QTfrontend/netudpwidget.cpp
author displacer
Thu, 08 Feb 2007 19:03:23 +0000
changeset 416 595d8663254d
parent 414 52e6be3a0985
child 417 bc7fea9abd9c
permissions -rw-r--r--
update button for udp servers list
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);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    12
  mainLayout.addWidget(serversList);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    13
  pUdpSocket = new QUdpSocket(this);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    14
416
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    15
  pUdpSocket->bind(46632, QUdpSocket::ShareAddress);
413
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    16
  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
416
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    17
  updateList();
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
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    20
void HWNetUdpWidget::updateList()
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    21
{
595d8663254d update button for udp servers list
displacer
parents: 414
diff changeset
    22
  serversList->clear();
413
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    23
  pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
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
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    26
void HWNetUdpWidget::onClientRead()
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    27
{
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    28
  while (pUdpSocket->hasPendingDatagrams()) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    29
    QByteArray datagram;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    30
    datagram.resize(pUdpSocket->pendingDatagramSize());
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    31
    QHostAddress clientAddr;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    32
    quint16 clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    33
    pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    34
    if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    35
      qDebug() << "detected server at " << clientAddr << ":" << clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    36
      serversList->addItem(clientAddr.toString());
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    37
    }
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
}