QTfrontend/netudpwidget.cpp
author displacer
Tue, 20 Feb 2007 22:34:42 +0000
changeset 468 8403d6884707
parent 421 ebd79d171804
child 486 7ea71cd3acd5
permissions -rw-r--r--
some debug messages removed oldlocalcfg signal connect removed

#include <QUdpSocket>
#include <QListWidget>

#include "netudpwidget.h"

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

  pUdpSocket->bind();
  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
  updateList();
}

void HWNetUdpWidget::updateList()
{
  serversList->clear();
  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")) {
      serversList->addItem(clientAddr.toString());
    }
  }
}