[Orxonox-commit 2950] r7650 - code/branches/masterserver/src/libraries/network

smerkli at orxonox.net smerkli at orxonox.net
Wed Nov 17 16:05:13 CET 2010


Author: smerkli
Date: 2010-11-17 16:05:13 +0100 (Wed, 17 Nov 2010)
New Revision: 7650

Added:
   code/branches/masterserver/src/libraries/network/MasterServerProtocol.h
Modified:
   code/branches/masterserver/src/libraries/network/MasterServerComm.cc
   code/branches/masterserver/src/libraries/network/MasterServerComm.h
   code/branches/masterserver/src/libraries/network/Server.cc
   code/branches/masterserver/src/libraries/network/Server.h
   code/branches/masterserver/src/libraries/network/WANDiscovery.cc
   code/branches/masterserver/src/libraries/network/WANDiscovery.h
Log:
Minimum target achieved for today, servers can log on to master server, clients can get server list. To be debugged.

Modified: code/branches/masterserver/src/libraries/network/MasterServerComm.cc
===================================================================
--- code/branches/masterserver/src/libraries/network/MasterServerComm.cc	2010-11-17 14:56:50 UTC (rev 7649)
+++ code/branches/masterserver/src/libraries/network/MasterServerComm.cc	2010-11-17 15:05:13 UTC (rev 7650)
@@ -110,6 +110,7 @@
     { 
       /* address buffer */
       char *addrconv = NULL;
+      int retval = 0;
 
       /* check what type of event it is and react accordingly */
       switch (this->event.type)
@@ -135,7 +136,7 @@
 
           /* call the supplied callback, if any. */
           if( (*callback) != NULL )
-            (*callback)( addrconv, &(this->event) );
+            retval = (*callback)( addrconv, &(this->event) );
 
           enet_packet_destroy( event.packet );
           if( addrconv ) free( addrconv );
@@ -144,7 +145,7 @@
       }
 
       /* event handled, return 0 */
-      return 0;
+      return retval;
     }
 
     /* show that no event occured */
@@ -170,6 +171,25 @@
     return 0;
   }
 
+  int MasterServerComm::sendRequest( std::string data )
+  {
+    /* send the data to the friend */
+    /* Create a reliable packet of size 7 containing "packet\0" */
+    ENetPacket * packet = enet_packet_create( data.c_str(), 
+        data.length() + 1, 
+        ENET_PACKET_FLAG_RELIABLE);
+
+    /* Send the packet to the peer over channel id 0. */
+    enet_peer_send (this->peer, 0, packet);
+
+    /* One could just use enet_host_service() instead. */
+    enet_host_flush( this->client );
+    if( packet ) free( packet );
+
+    /* all done. */
+    return 0;
+  }
+
 }
 
 

Modified: code/branches/masterserver/src/libraries/network/MasterServerComm.h
===================================================================
--- code/branches/masterserver/src/libraries/network/MasterServerComm.h	2010-11-17 14:56:50 UTC (rev 7649)
+++ code/branches/masterserver/src/libraries/network/MasterServerComm.h	2010-11-17 15:05:13 UTC (rev 7650)
@@ -28,6 +28,7 @@
 
 #include <cstdlib>
 #include <cstdio>
+#include <string>
 #include <cstring>
 #include <enet/enet.h>
 
@@ -64,6 +65,14 @@
        */
       int sendRequest( char *data );
 
+      /** \param data The data to be sent. 
+       * \return 0 for success, other for error. 
+       * 
+       * Send a request to the master server containing data specified in data
+       * (string version)
+       */
+      int sendRequest( std::string data );
+
       /** \param callback The callback function to call with data receivced.
        * \return 0 for success, other for error
        * 

Added: code/branches/masterserver/src/libraries/network/MasterServerProtocol.h
===================================================================
--- code/branches/masterserver/src/libraries/network/MasterServerProtocol.h	                        (rev 0)
+++ code/branches/masterserver/src/libraries/network/MasterServerProtocol.h	2010-11-17 15:05:13 UTC (rev 7650)
@@ -0,0 +1,70 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Sandro 'smerkli' Merkli
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef MASTER_SERVER_PROTO
+#define MASTER_SERVER_PROTO
+
+/* master server address (to be moved elsewhere later) */
+#define MS_ADDRESS "localhost"
+
+#define MSPROTO_CLIENT "CL"
+#define MSPROTO_CLIENT_LEN 2 
+
+#define MSPROTO_REQ_LIST "REQ:LIST"
+
+
+
+
+#define MSPROTO_GAME_SERVER "GS"
+#define MSPROTO_GAME_SERVER_LEN 2
+
+#define MSPROTO_REGISTER_SERVER "REG:SER"
+#define MSPROTO_REGISTER_SERVER_LEN 7
+
+#define MSPROTO_SERVERLIST_ITEM "SI"
+#define MSPROTO_SERVERLIST_ITEM_LEN 2
+#define MSPROTO_SERVERLIST_END "SL_END"
+#define MSPROTO_SERVERLIST_END_LEN 6
+
+
+
+
+
+
+#define ORX_MSERVER_PORT 1234
+#define MS_ADDRESS "localhost"
+
+
+
+#define ORX_MSERVER_MAXCONNS 32
+#define ORX_MSERVER_MAXCHANS 2
+
+
+
+
+#endif /* MASTER_SERVER_PROTO */

Modified: code/branches/masterserver/src/libraries/network/Server.cc
===================================================================
--- code/branches/masterserver/src/libraries/network/Server.cc	2010-11-17 14:56:50 UTC (rev 7649)
+++ code/branches/masterserver/src/libraries/network/Server.cc	2010-11-17 15:05:13 UTC (rev 7650)
@@ -104,14 +104,18 @@
   {
     /* initialize it and see if it worked */
     if( msc.initialize() )
-      COUT(1) << "Error: could not initialize master server communications!\n";
+    { COUT(1) << "Error: could not initialize master server communications!\n";
+      return;
+    }
 
     /* connect and see if it worked */
     if( msc.connect( MS_ADDRESS, 1234 ) )
-      COUT(1) << "Error: could not connect to master server!\n";
+    { COUT(1) << "Error: could not connect to master server!\n";
+      return;
+    }
 
-    /* TODO */
     /* now send the master server some note we're here */
+    msc.sendRequest( MSPROTO_GAME_SERVER " " MSPROTO_REGISTER_SERVER );
   }
 
   /**
@@ -163,7 +167,7 @@
 
 
   /* TODO */
-  int replyhandler( char *addr, ENetEvent *ev )
+  int rephandler( char *addr, ENetEvent *ev )
   { 
     /* handle incoming data */
 
@@ -173,7 +177,7 @@
 
   void Server::helper_HandleMasterServerRequests()
   { 
-    this->msc.pollForReply( replyhandler ); 
+    this->msc.pollForReply( rephandler ); 
   }
 
   /**

Modified: code/branches/masterserver/src/libraries/network/Server.h
===================================================================
--- code/branches/masterserver/src/libraries/network/Server.h	2010-11-17 14:56:50 UTC (rev 7649)
+++ code/branches/masterserver/src/libraries/network/Server.h	2010-11-17 15:05:13 UTC (rev 7650)
@@ -38,9 +38,8 @@
 #include "ServerConnection.h"
 #include "LANDiscoverable.h"
 #include "MasterServerComm.h"
+#include "MasterServerProtocol.h"
 
-/* proto (move to central point soon) */ 
-#define MS_ADDRESS "localhost"
 
 namespace orxonox
 {
@@ -56,7 +55,11 @@
     Server(int port, const std::string& bindAddress);
     ~Server();
 
+    /* helpers */
     void helper_ConnectToMasterserver();
+    void helper_HandleMasterServerRequests();
+    int replyhandler( char *addr, ENetEvent *ev );
+
     void open();
     void close();
     bool processChat(const std::string& message, unsigned int playerID);

Modified: code/branches/masterserver/src/libraries/network/WANDiscovery.cc
===================================================================
--- code/branches/masterserver/src/libraries/network/WANDiscovery.cc	2010-11-17 14:56:50 UTC (rev 7649)
+++ code/branches/masterserver/src/libraries/network/WANDiscovery.cc	2010-11-17 15:05:13 UTC (rev 7650)
@@ -62,13 +62,29 @@
 
   /* callback for the network reply poller */
   /* NOTE implement protocol-specific part here. */
-  int replyhandler( char *addr, ENetEvent *ev )
+  int rhandler( char *addr, ENetEvent *ev )
   { 
-    /* handle incoming data
-     * if a list entry arrives add to list
-     * if a done entry arrives set done to true
-     */
+    /* handle incoming data */
+    /* if a list entry arrives add to list */
+    if( !strncmp( ev->packet->data, MSPROTO_SERVERLIST_ITEM,
+      MSPROTO_SERVERLIST_ITEM_LEN ) )
+    { 
+      /* create server structure from that item */
+      ServerInformation toadd;
 
+      /* fill in data */
+      toadd->setName( std::string(ev->packet->data + 
+        MSPROTO_SERVERLIST_ITEM_LEN) );
+      toadd->setIP( std::string(ev->packet->data + 
+        MSPROTO_SERVERLIST_ITEM_LEN) );
+
+      /* add to list */
+      this->servers_.add( toadd );
+    }
+    else if( !strncmp( ev->packet->data, MSPROTO_SERVERLIST_END,
+      MSPROTO_SERVERLIST_END_LEN ) )
+    { return 1; }
+
     /* done handling, return all ok code 0 */
     return 0;
   }
@@ -79,10 +95,10 @@
     this->servers_.clear();
 
     /* send request to server */
-    msc.sendRequest( MSPROTO_CLIENT MSPROTO_REQ_LIST );
+    msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST );
 
     /* deal with replies */
-    while( msc.pollForReply( replyhandler ) ) 
+    while( !msc.pollForReply( rhandler ) ) 
       /* nothing */;
 
     /* done receiving. */

Modified: code/branches/masterserver/src/libraries/network/WANDiscovery.h
===================================================================
--- code/branches/masterserver/src/libraries/network/WANDiscovery.h	2010-11-17 14:56:50 UTC (rev 7649)
+++ code/branches/masterserver/src/libraries/network/WANDiscovery.h	2010-11-17 15:05:13 UTC (rev 7650)
@@ -33,16 +33,11 @@
 #include "packet/ServerInformation.h"
 #include "util/Singleton.h"
 #include "MasterServerComm.h"
+#include "MasterServerProtocol.h"
 
 #include <vector>
 
-/* master server address (to be moved elsewhere later) */
-#define MS_ADDRESS "localhost"
 
-/* protocol (to be moved elsewhere later) */
-#define MSPROTO_CLIENT "CL "
-#define MSPROTO_REQ_LIST "REQ:LIST"
-
 // tolua_begin
 namespace orxonox
 {




More information about the Orxonox-commit mailing list