[Orxonox-commit 2887] r7590 - code/branches/masterserver/src/modules/masterserver

smerkli at orxonox.net smerkli at orxonox.net
Wed Oct 27 16:16:08 CEST 2010


Author: smerkli
Date: 2010-10-27 16:16:08 +0200 (Wed, 27 Oct 2010)
New Revision: 7590

Modified:
   code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
   code/branches/masterserver/src/modules/masterserver/MasterServer.h
   code/branches/masterserver/src/modules/masterserver/PeerList.cpp
Log:
done for today, everything compiles, test client as well. beginning testing of basic networking functionality over the course of the next week.

Modified: code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	2010-10-27 13:24:56 UTC (rev 7589)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	2010-10-27 14:16:08 UTC (rev 7590)
@@ -27,172 +27,191 @@
  */
 
 #include "MasterServer.h"
+#include "util/ScopedSingletonManager.h"
+#include "core/CoreIncludes.h"
+#include "core/CorePrereqs.h"
 
-using namespace std;
+namespace orxonox 
+{
+  /* singleton stuff */
+  ManageScopedSingleton( MasterServer, ScopeID::Root, false );
 
-/***** EVENTS *****/
-/* connect event */
-int eventConnect( ENetEvent *event, 
-  orxonox::ServerList *serverlist,
-  orxonox::PeerList *peers )
-{ /* check for bad parameters */
-  if( !event )
-  { fprintf( stderr, "No event given.\n" );
-    return -1;
-  }
+  /***** EVENTS *****/
+  /* connect event */
+  int 
+  eventConnect( ENetEvent *event )
+  { /* check for bad parameters */
+    if( !event )
+    { fprintf( stderr, "No event given.\n" );
+      return -1;
+    }
 
-  /* output debug info */
-  printf( "A new client connected from %x:%u.\n", 
-      event->peer->address.host,
-      event->peer->address.port);
+    /* output debug info */
+    printf( "A new client connected from %x:%u.\n", 
+        (event->peer->address.host),
+        event->peer->address.port );
 
-  /* game server or client connection? */
-  /* -> decide in protocol */
+    /* game server or client connection? */
+    /* -> decide in protocol */
     /* game server */
-      /* add to game server list */
-      /*  */
+    /* add to game server list */
+    /*  */
 
     /* client */
-      /* add to client list */
- 
-  /* NOTE this seems to be some weird snipped from the tutorial as peer.data
-   * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S 
-   */
-  /* Store any relevant client information here. */
-  /* event->peer->data = "Client information"; */
-  /* /NOTE */
-  return 0;
-}
+    /* add to client list */
 
-/* disconnect event */
-int eventDisconnect( ENetEvent *event,
-  orxonox::ServerList *serverlist,
-  orxonox::PeerList *peers )
-{ /* check for bad parameters */
-  if( !event )
-  { fprintf( stderr, "No event given.\n" );
-    return -1;
+    /* NOTE this seems to be some weird snipped from the tutorial as peer.data
+     * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S 
+     */
+    /* Store any relevant client information here. */
+    /* event->peer->data = "Client information"; */
+    /* /NOTE */
+    return 0;
   }
 
-  /* output that the disconnect happened, to be removed at a later time. */
-  printf( "%s disconnected.\n", event->peer->data );
+  /* disconnect event */
+  int 
+  eventDisconnect( ENetEvent *event )
+  { /* check for bad parameters */
+    if( !event )
+    { fprintf( stderr, "No event given.\n" );
+      return -1;
+    }
 
-  /* remove the server from the list it belongs to */
+    /* output that the disconnect happened, to be removed at a later time. */
+    printf( "%s disconnected.\n", (char*)event->peer->data );
 
-  /* Reset the peer's client information. */
-  event->peer->data = NULL;
-  return 0;
-}
+    /* remove the server from the list it belongs to */
 
-/* data event */
-int eventData( ENetEvent *event )
-{ /* output what's in the packet (to be removed later) */
-  if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
-  { fprintf( stderr, "No complete event given.\n" );
-    return -1;
+    /* Reset the peer's client information. */
+    event->peer->data = NULL;
+    return 0;
   }
 
-  /* output debug info about the data that has come, to be removed */
-  //printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
-      //event->packet->dataLength,
-      //event->packet->data,
-      //event->peer->data,
-      //event->channelID );
- 
-  /* game server or client connection? */
+  /* data event */
+  int 
+  eventData( ENetEvent *event )
+  { /* output what's in the packet (to be removed later) */
+    if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
+    { fprintf( stderr, "No complete event given.\n" );
+      return -1;
+    }
+
+    /* output debug info about the data that has come, to be removed */
+    //printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
+    //event->packet->dataLength,
+    //event->packet->data,
+    //event->peer->data,
+    //event->channelID );
+
+    /* game server or client connection? */
     /* game server */
-      /* parse data */
-      /* start actions */
-      /* and send reply */
+    /* parse data */
+    /* start actions */
+    /* and send reply */
     /* client */
-      /* parse data */
-      /* start actions */
-      /* and send reply */
+    /* parse data */
+    /* start actions */
+    /* and send reply */
 
-  /* Clean up the packet now that we're done using it. */
-  enet_packet_destroy( event->packet );
-  return 0;
-}
+    /* Clean up the packet now that we're done using it. */
+    enet_packet_destroy( event->packet );
+    return 0;
+  }
 
+  /**** MAIN ROUTINE *****/
+  int 
+  MasterServer::run()
+  {
+    COUT(0) << "omg, i got baschtl'd!\n";
 
+    /***** ENTER MAIN LOOP *****/
+    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
+    if( event == NULL )
+    { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
+      exit( EXIT_FAILURE );
+    }
 
-/**** MAIN ROUTINE *****/
-int main( int argc, char *argv[] )
-{
-  /***** INITIALIZE NETWORKING *****/
-  if( enet_initialize () != 0)
-  { fprintf( stderr, "An error occurred while initializing ENet.\n");
-    return EXIT_FAILURE;
-  }
+    /* create an iterator for the loop */
+    while( enet_host_service( this->server, event, 1000 ) > 0 )
+    { /* check what type of event it is and react accordingly */
+      switch (event->type)
+      { /* new connection */
+        case ENET_EVENT_TYPE_CONNECT: 
+          eventConnect( event ); break;
 
-  /* register deinitialization */
-  atexit( enet_deinitialize );
+          /* disconnect */
+        case ENET_EVENT_TYPE_DISCONNECT: 
+          eventDisconnect( event ); break;
 
-  /* define our address and a host structure */
-  ENetAddress address;
-  ENetHost *server;
+          /* incoming data */
+        case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
+        default: break;
+      }
+    }
 
-  /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
-  address.host = ENET_HOST_ANY;
-  address.port = ORX_MSERVER_PORT;
+    /* free the event */
+    if( event ) free( event );
 
-  /* create a host with the above settings (the last two 0 mean: accept 
-   * any input/output bandwidth */
-  server = enet_host_create( &address, ORX_MSERVER_MAXCONNS, 
-    ORX_MSERVER_MAXCHANS, 0, 0 );     
+    /* done */
+    return 0;
+  } 
 
-  /* see if creation worked */
-  if( !server )
-  { fprintf( stderr, 
-        "An error occurred while trying to create an ENet server host.\n" );
-    exit( EXIT_FAILURE );
-  }
+  /* constructor */
+  MasterServer::MasterServer()
+  {
+    /***** INITIALIZE NETWORKING *****/
+    if( enet_initialize () != 0)
+    { fprintf( stderr, "An error occurred while initializing ENet.\n");
+      exit( EXIT_FAILURE );
+    }
 
-  /***** INITIALIZE GAME SERVER LIST *****/
-  orxonox::ServerList *mainlist = new orxonox::ServerList();
-  if( mainlist == NULL )
-  { fprintf( stderr, "Error creating server list.\n" );
-    exit( EXIT_FAILURE );
-  }
+    /* register deinitialization */
+    atexit( enet_deinitialize );
 
-  /***** INITIALIZE PEER LIST *****/
-  orxonox::PeerList *peers = new orxonox::PeerList();
+    /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
+    this->address.host = ENET_HOST_ANY;
+    this->address.port = ORX_MSERVER_PORT;
 
-  /***** ENTER MAIN LOOP *****/
-  ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
-  if( event == NULL )
-  { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
+    /* create a host with the above settings (the last two 0 mean: accept 
+     * any input/output bandwidth */
+    this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS, 
+        ORX_MSERVER_MAXCHANS, 0, 0 );     
+
+    /* see if creation worked */
+    if( !this->server )
+    { fprintf( stderr, 
+        "An error occurred while trying to create an ENet server host.\n" );
     exit( EXIT_FAILURE );
-  }
+    }
 
-  /* create an iterator for the loop */
-  while( enet_host_service( server, event, 1000 ) > 0 )
-  { /* check what type of event it is and react accordingly */
-    switch (event->type)
-    { /* new connection */
-      case ENET_EVENT_TYPE_CONNECT: 
-        eventConnect( event, mainlist, peers ); break;
+    /***** INITIALIZE GAME SERVER LIST *****/
+    this->mainlist = new ServerList();
+    if( this->mainlist == NULL )
+    { fprintf( stderr, "Error creating server list.\n" );
+      exit( EXIT_FAILURE );
+    }
 
-      /* disconnect */
-      case ENET_EVENT_TYPE_DISCONNECT: 
-        eventDisconnect( event, mainlist, peers ); break;
+    /***** INITIALIZE PEER LIST *****/
+    this->peers = new PeerList();
 
-      /* incoming data */
-      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
-      default: break;
-    }
+    /* run the main method */
+    run();
   }
 
-  /***** CLEANUP PROCESS *****/
-  /* terminate all networking connections */
-  enet_host_destroy( server );
+  /* destructor */
+  MasterServer::~MasterServer()
+  {
+    /***** CLEANUP PROCESS *****/
+    /* terminate all networking connections */
+    enet_host_destroy( this->server );
 
-  /* clear the list of connected game servers */
-  /* clear the list of connected game clients */
+    /* clear the list of connected game servers */
+    /* clear the list of connected game clients */
 
-  /* free all used memory */
-  if( event ) free( event );
+    /* free all used memory */
 
-  /* all done */
-  return EXIT_SUCCESS;
-} 
+  }
+
+/* end of namespace */
+}

Modified: code/branches/masterserver/src/modules/masterserver/MasterServer.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.h	2010-10-27 13:24:56 UTC (rev 7589)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.h	2010-10-27 14:16:08 UTC (rev 7590)
@@ -37,6 +37,7 @@
 #include <network/packet/FunctionIDs.h>
 #include <network/packet/Gamestate.h>
 #include <network/packet/Welcome.h>
+#include <util/Singleton.h>
 
 /* my includes */
 #include "ServerList.h"
@@ -49,4 +50,30 @@
 #define ORX_MSERVER_MAXCONNS 32
 #define ORX_MSERVER_MAXCHANS 2
 
+namespace orxonox 
+{
+  /* singleton */
+  class MasterServer: public Singleton<MasterServer>
+  {
+    public:
+      MasterServer();
+      ~MasterServer();
+
+      friend class Singleton<MasterServer>;
+      static MasterServer& getInstance(void) 
+      { return Singleton<MasterServer>::getInstance(); }
+
+      /* data fields */
+      static MasterServer* singletonPtr_s;
+    
+    private:
+      ENetAddress address;
+      ENetHost *server;
+      ServerList *mainlist;
+      PeerList *peers;
+      int run();
+
+  };
+}
+
 #endif /* _MasterServer_ */

Modified: code/branches/masterserver/src/modules/masterserver/PeerList.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/PeerList.cpp	2010-10-27 13:24:56 UTC (rev 7589)
+++ code/branches/masterserver/src/modules/masterserver/PeerList.cpp	2010-10-27 14:16:08 UTC (rev 7590)
@@ -27,6 +27,7 @@
  */
 
 #include "PeerList.h"
+#include <network/packet/ServerInformation.h>
 #include <cstdio>
 
 namespace orxonox
@@ -48,18 +49,27 @@
   }
 
   bool sub_compAddr( ENetAddress addr1, ENetAddress addr2 )
-  { return ( (addr1.host == addr2.host) && (addr1.port == addr2.port) ); }
+  { 
+    for( int i = 0; i < 16; ++i )
+      if( addr1.host.addr[i] < addr2.host.addr[i] )
+        return -i;
+      else if( addr1.host.addr[i] > addr2.host.addr[i] )
+        return i;
 
+    return 0;
+  }
+    
+
   bool
   PeerList::remPeerByAddr( ENetAddress addr )
   { /* get an iterator */
-    list<packet::ENetPeer *>::iterator i;
+    std::list<ENetPeer *>::iterator i;
 
     /* loop through list elements */
     for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
-      if( sub_compAddr((*i)->address, addr ) )
+      if( !sub_compAddr((*i)->address, addr ) )
       { /* found this name, remove and quit */
-        this->peerlist.remove( i );
+        this->peerlist.remove( *i );
         return true;
       }
 
@@ -70,13 +80,13 @@
   ENetPeer *
   PeerList::findPeerByAddr( ENetAddress addr )
   { /* get an iterator */
-    list<packet::ENetPeer *>::iterator i;
+    std::list<ENetPeer *>::iterator i;
 
     /* loop through list elements */
     for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
-      if( sub_compAddr((*i)->address, addr ) )
+      if( !sub_compAddr((*i)->address, addr ) )
         /* found this name, remove and quit */
-        return i;
+        return *i;
 
     /* not found */
     return NULL;




More information about the Orxonox-commit mailing list