QTfrontend/netudpwidget.cpp
author unc0rr
Fri, 09 Feb 2007 19:38:52 +0000
changeset 421 ebd79d171804
parent 417 bc7fea9abd9c
child 468 8403d6884707
permissions -rw-r--r--
- Remake net game page - Set svn:eol-style to 'native'

#include <QUdpSocket>
#include <QListWidget>

#include "netudpwidget.h"

#include <QDebug>

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")) {
      qDebug() << "detected server at " << clientAddr << ":" << clientPort;
      serversList->addItem(clientAddr.toString());
    }
  }
}