QTfrontend/netudpserver.cpp
author displacer
Sun, 18 Feb 2007 12:20:36 +0000
changeset 448 e968e19a464c
parent 417 bc7fea9abd9c
child 468 8403d6884707
permissions -rw-r--r--
waiting state for net Go button
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;
417
bc7fea9abd9c randomly chosen ports
displacer
parents: 413
diff changeset
    28
      pUdpSocket->writeDatagram("hedgewars server", clientAddr, clientPort);
413
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
}