udp server detecting experimental version (added files)
authordisplacer
Wed, 07 Feb 2007 22:59:49 +0000
changeset 413 523f1769f2bc
parent 412 2ddcc3e3e644
child 414 52e6be3a0985
udp server detecting experimental version (added files)
QTfrontend/netudpserver.cpp
QTfrontend/netudpserver.h
QTfrontend/netudpwidget.cpp
QTfrontend/netudpwidget.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/netudpserver.cpp	Wed Feb 07 22:59:49 2007 +0000
@@ -0,0 +1,31 @@
+#include <QUdpSocket>
+
+#include "netudpserver.h"
+
+#include <QDebug>
+
+HWNetUdpServer::HWNetUdpServer(QObject* parent) :
+  QObject(parent)
+{
+  pUdpSocket = new QUdpSocket(this);
+  pUdpSocket->bind(46631);
+
+  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
+  
+}
+
+void HWNetUdpServer::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 client")) {
+      // send answer to client
+      qDebug() << "received UDP query from " << clientAddr << ":" << clientPort;
+      pUdpSocket->writeDatagram("hedgewars server", clientAddr, 46632);
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/netudpserver.h	Wed Feb 07 22:59:49 2007 +0000
@@ -0,0 +1,22 @@
+#ifndef _NET_UDPSERVER_INCLUDED
+#define _NET_UDPSERVER_INCLUDED
+
+#include <QObject>
+
+class QUdpSocket;
+
+class HWNetUdpServer : public QObject
+{
+  Q_OBJECT
+
+ public:
+  HWNetUdpServer(QObject *parent = 0);
+
+ private slots:
+  void onClientRead();
+
+ private:
+  QUdpSocket* pUdpSocket;
+};
+
+#endif // _NET_UDPSERVER_INCLUDED
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/netudpwidget.cpp	Wed Feb 07 22:59:49 2007 +0000
@@ -0,0 +1,33 @@
+#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());
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/netudpwidget.h	Wed Feb 07 22:59:49 2007 +0000
@@ -0,0 +1,26 @@
+#ifndef _NET_UDPWIDGET_INCLUDED
+#define _NET_UDPWIDGET_INCLUDED
+
+#include <QWidget>
+#include <QVBoxLayout>
+
+class QUdpSocket;
+class QListWidget;
+
+class HWNetUdpWidget : public QWidget
+{
+  Q_OBJECT
+
+ public:
+  HWNetUdpWidget(QWidget *parent = 0);
+
+ private slots:
+  void onClientRead();
+
+ private:
+  QVBoxLayout mainLayout;
+  QUdpSocket* pUdpSocket;
+  QListWidget* serversList;
+};
+
+#endif // _NET_UDPWIDGET_INCLUDED