Send server's name in udp packet. Fixes issue #25
authorunc0rr
Fri, 15 Apr 2011 21:36:59 +0400
changeset 5146 7b65e83e261d
parent 5145 120f4271f197
child 5147 a2ae7702bb75
Send server's name in udp packet. Fixes issue #25
QTfrontend/netudpserver.cpp
QTfrontend/netudpserver.h
QTfrontend/netudpwidget.cpp
--- a/QTfrontend/netudpserver.cpp	Thu Apr 14 18:17:30 2011 -0400
+++ b/QTfrontend/netudpserver.cpp	Fri Apr 15 21:36:59 2011 +0400
@@ -22,7 +22,8 @@
 #include "netudpserver.h"
 
 HWNetUdpServer::HWNetUdpServer(QObject *parent, const QString & descr, quint16 port) :
-  HWNetRegisterServer(parent, descr, port)
+  HWNetRegisterServer(parent, descr, port),
+  m_descr(descr)
 {
   pUdpSocket = new QUdpSocket(this);
   pUdpSocket->bind(46631);
@@ -37,9 +38,9 @@
     QHostAddress clientAddr;
     quint16 clientPort;
     pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
-    if(QString("%1").arg(datagram.data())==QString("hedgewars client")) {
+    if(datagram.startsWith("hedgewars client")) {
       // send answer to client
-      pUdpSocket->writeDatagram("hedgewars server", clientAddr, clientPort);
+      pUdpSocket->writeDatagram(QString("hedgewars server\n%1").arg(m_descr).toUtf8(), clientAddr, clientPort);
     }
   }
 }
--- a/QTfrontend/netudpserver.h	Thu Apr 14 18:17:30 2011 -0400
+++ b/QTfrontend/netudpserver.h	Fri Apr 15 21:36:59 2011 +0400
@@ -40,6 +40,7 @@
 
  private:
   QUdpSocket* pUdpSocket;
+  QString m_descr;
 };
 
 #endif // _NET_UDPSERVER_INCLUDED
--- a/QTfrontend/netudpwidget.cpp	Thu Apr 14 18:17:30 2011 -0400
+++ b/QTfrontend/netudpwidget.cpp	Fri Apr 15 21:36:59 2011 +0400
@@ -37,6 +37,7 @@
     reset();
 
     pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
+    pUdpSocket->writeDatagram("hedgewars client", QHostAddress("127.0.0.1"), 46631);
 }
 
 void HWNetUdpModel::onClientRead()
@@ -49,9 +50,10 @@
 
         pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
 
-        if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
+        QString packet = QString::fromUtf8(datagram.data());
+        if(packet.startsWith("hedgewars server")) {
             QStringList sl;
-            sl << "-" << clientAddr.toString() << "46631";
+            sl << packet.remove(0, 17) << clientAddr.toString() << "46631";
             games.append(sl);
         }
     }