QTfrontend/netudpwidget.cpp
author displacer
Wed, 07 Feb 2007 23:01:32 +0000
changeset 414 52e6be3a0985
parent 413 523f1769f2bc
child 416 595d8663254d
permissions -rw-r--r--
binded to port
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
414
52e6be3a0985 binded to port
displacer
parents: 413
diff changeset
    15
  pUdpSocket->bind(46632);
413
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    16
  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    17
  pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    18
}
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    19
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    20
void HWNetUdpWidget::onClientRead()
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    21
{
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    22
  while (pUdpSocket->hasPendingDatagrams()) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    23
    QByteArray datagram;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    24
    datagram.resize(pUdpSocket->pendingDatagramSize());
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    25
    QHostAddress clientAddr;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    26
    quint16 clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    27
    pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    28
    if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    29
      qDebug() << "detected server at " << clientAddr << ":" << clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    30
      serversList->addItem(clientAddr.toString());
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    31
    }
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    32
  }
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    33
}