QTfrontend/netudpwidget.cpp
author displacer
Wed, 07 Feb 2007 22:59:49 +0000
changeset 413 523f1769f2bc
child 414 52e6be3a0985
permissions -rw-r--r--
udp server detecting experimental version (added files)

#include <QUdpSocket>
#include <QListWidget>

#include "netudpwidget.h"

#include <QDebug>

HWNetUdpWidget::HWNetUdpWidget(QWidget* parent) :
  QWidget(parent), mainLayout(this)
{
  serversList = new QListWidget(this);
  mainLayout.addWidget(serversList);
  pUdpSocket = new QUdpSocket(this);

  pUdpSocket->bind();//46632);
  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
  pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
}

void HWNetUdpWidget::onClientRead()
{
  while (pUdpSocket->hasPendingDatagrams()) {
    QByteArray datagram;
    datagram.resize(pUdpSocket->pendingDatagramSize());
    QHostAddress clientAddr;
    quint16 clientPort;
    pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
    if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
      qDebug() << "detected server at " << clientAddr << ":" << clientPort;
      serversList->addItem(clientAddr.toString());
    }
  }
}