QTfrontend/netudpserver.cpp
author displacer
Wed, 07 Feb 2007 22:59:49 +0000
changeset 413 523f1769f2bc
child 417 bc7fea9abd9c
permissions -rw-r--r--
udp server detecting experimental version (added files)
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
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     3
#include "netudpserver.h"
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     4
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     5
#include <QDebug>
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     6
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     7
HWNetUdpServer::HWNetUdpServer(QObject* parent) :
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     8
  QObject(parent)
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
     9
{
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    10
  pUdpSocket = new QUdpSocket(this);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    11
  pUdpSocket->bind(46631);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    12
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    13
  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    14
  
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    15
}
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    16
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    17
void HWNetUdpServer::onClientRead()
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
  while (pUdpSocket->hasPendingDatagrams()) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    20
    QByteArray datagram;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    21
    datagram.resize(pUdpSocket->pendingDatagramSize());
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    22
    QHostAddress clientAddr;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    23
    quint16 clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    24
    pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    25
    if(QString("%1").arg(datagram.data())==QString("hedgewars client")) {
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    26
      // send answer to client
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    27
      qDebug() << "received UDP query from " << clientAddr << ":" << clientPort;
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    28
      pUdpSocket->writeDatagram("hedgewars server", clientAddr, 46632);
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    29
    }
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    30
  }
523f1769f2bc udp server detecting experimental version (added files)
displacer
parents:
diff changeset
    31
}