[Orxonox-commit 2862] r7565 - in code/branches/masterserver/src/modules: . masterserver

smerkli at orxonox.net smerkli at orxonox.net
Wed Oct 20 14:53:53 CEST 2010


Author: smerkli
Date: 2010-10-20 14:53:53 +0200 (Wed, 20 Oct 2010)
New Revision: 7565

Added:
   code/branches/masterserver/src/modules/masterserver/
   code/branches/masterserver/src/modules/masterserver/.MasterServer.h.swp-backup
   code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
   code/branches/masterserver/src/modules/masterserver/MasterServer.h
   code/branches/masterserver/src/modules/masterserver/ServerList.cpp
   code/branches/masterserver/src/modules/masterserver/ServerList.h
Log:
Initial check-in of the master server code. Nowhere near completion yet, just for backup purposes now.

Added: code/branches/masterserver/src/modules/masterserver/.MasterServer.h.swp-backup
===================================================================
(Binary files differ)


Property changes on: code/branches/masterserver/src/modules/masterserver/.MasterServer.h.swp-backup
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	2010-10-20 12:53:53 UTC (rev 7565)
@@ -0,0 +1,165 @@
+#include "MasterServer.h"
+
+using namespace std;
+
+/***** 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);
+
+  /* game server or client connection? */
+    /* game server */
+      /* 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;
+}
+
+/* disconnect event */
+int eventDisconnect( ENetEvent *event )
+{ 
+  /* check for bad parameters */
+  if( !event )
+  { fprintf( stderr, "No event given.\n" );
+    return -1;
+  }
+
+  /* output that the disconnect happened, to be removed at a later time. */
+  printf ("%s disconnected.\n", event.peer -> data);
+
+  /* remove the server from the list it belongs to */
+
+  /* Reset the peer's client information. */
+  event->peer->data = NULL;
+  return 0;
+}
+
+/* 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;
+  }
+
+
+  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 */
+    /* client */
+      /* 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;
+}
+
+
+
+/**** 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;
+  }
+
+  /* register deinitialization */
+  atexit( enet_deinitialize );
+
+  /* define our address and a host structure */
+  ENetAddress address;
+  ENetHost *server;
+
+  /* Bind the server to the default localhost.     */
+  /* A specific host address can be specified by   */
+  /* enet_address_set_host (& address, "x.x.x.x"); */
+  address.host = ENET_HOST_ANY;
+
+  /* Bind the server to port 1234. */
+  address.port = ORX_MSERVER_PORT;
+
+  server = enet_host_create( & address /* the address to bind the server host to */, 
+      ORX_MSERVER_MAXCONNS      /* allow up to 32 clients and/or outgoing connections */,
+      ORX_MSERVER_MAXCHANS      /* allow up to 2 channels to be used, 0 and 1 */,
+      0      /* assume any amount of incoming bandwidth */,
+      0      /* assume any amount of outgoing bandwidth */);
+
+  /* see if creation worked */
+  if( !server )
+  { fprintf( stderr, 
+        "An error occurred while trying to create an ENet server host.\n" );
+    exit( EXIT_FAILURE );
+  }
+
+  /***** INITIALIZE GAME SERVER LIST *****/
+  ServerList mainlist = new ServerList();
+  if( !mainlist )
+  { fprintf( stderr, "Error creating server list.\n" );
+    exit( EXIT_FAILURE );
+  }
+
+  /***** ENTER MAIN LOOP *****/
+  ENetEvent *event = calloc(sizeof(ENetEvent));
+  if( !event )
+  { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
+    exit( EXIT_FAILURE );
+  }
+
+  /* Wait up to 1000 milliseconds for an event. */
+  while (enet_host_service (client, 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;
+
+      /* disconnect */
+      case ENET_EVENT_TYPE_DISCONNECT: eventDisconnect( event ); break;
+
+      /* incoming data */
+      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
+    }
+  }
+
+  /***** CLEANUP PROCESS *****/
+  /* terminate all networking connections */
+  enet_host_destroy( server );
+
+  /* clear the list of connected game servers */
+  /* clear the list of connected game clients */
+
+  /* free all used memory */
+  if( event ) free( event );
+
+  /* all done */
+  return EXIT_SUCCESS;
+} 

Added: code/branches/masterserver/src/modules/masterserver/MasterServer.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.h	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.h	2010-10-20 12:53:53 UTC (rev 7565)
@@ -0,0 +1,23 @@
+#ifndef _MasterServer_
+#define _MasterServer_
+
+/* orxonox includes */
+#include <enet/enet.h>
+#include "packet/Chat.h"
+#include "packet/ClassID.h"
+#include "packet/DeleteObjects.h"
+#include "packet/FunctionIDs.h"
+#include "packet/Gamestate.h"
+#include "packet/Welcome.h"
+
+/* my includes */
+#include <ServerList.h>
+
+/* c compatibility */
+#include <cstdio>
+
+#define ORX_MSERVER_PORT 1234
+#define ORX_MSERVER_MAXCONNS 32
+#define ORX_MSERVER_MAXCHANS 2
+
+#endif /* _MasterServer_ */

Added: code/branches/masterserver/src/modules/masterserver/ServerList.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/ServerList.cpp	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/ServerList.cpp	2010-10-20 12:53:53 UTC (rev 7565)
@@ -0,0 +1,95 @@
+/*
+ *   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:
+ *      ...
+ *
+ */
+
+ #include "ServerList.h"
+
+namespace orxonox
+{ 
+  ServerList::ServerList()
+  { /* create a new list */ }
+
+  ServerList::~ServerList()
+  { /* delete the list */
+    serverlist.clear();}
+  }
+
+  int ServerList::addServer( ServerInformation *toadd )
+  { this->serverlist.push_back( toadd ); 
+    return 0;
+  }
+
+  bool ServerList::delServerByName( std::string name )
+  { 
+    /* get an iterator */
+    list<ServerInformation *>::iterator i;
+
+    /* loop through list elements */
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i)->serverName_ == name )
+      { /* found this name, remove and quit */
+        this->serverlist.remove( i );
+        return true;
+      }
+    return false;
+  }
+
+  bool ServerList::delServerByAddress( std::string address )
+  { 
+    /* get an iterator */
+    list<ServerInformation *>::iterator i;
+
+    /* loop through list elements */
+    for( i=serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i)->serverIP_ == address )
+      { /* found this name, remove and quit */
+        this->serverlist.remove( i );
+        return true;
+      }
+    return false;
+  }
+
+
+  /* sort by name */
+  bool sub_compare_names( ServerInformation *no1, ServerInformation *no2 )
+  { return no1->serverName_ > no2->serverName_; }
+
+  void ServerList::sortByName()
+  { 
+    this->serverlist.sort( sub_compare_names ); 
+  }
+  
+  /* sort by ping */
+  bool sub_compare_pings( ServerInformation *no1, ServerInformation *no2 )
+  { TODO return no1->serverName_ > no2->serverName_; }
+
+  void ServerList::sortByPing()
+  {
+    this->serverlist.sort( sub_compare_pings ); 
+  }
+
+}

Added: code/branches/masterserver/src/modules/masterserver/ServerList.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/ServerList.h	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/ServerList.h	2010-10-20 12:53:53 UTC (rev 7565)
@@ -0,0 +1,85 @@
+/*
+ *   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 _ServerList_
+#define _ServerList_
+
+#include <list>
+#include <string>
+#include "ServerInformation.h"
+
+/* methods necessary */
+namespace orxonox 
+{ 
+  /** This class is keeps a list of game servers
+   * and some info about them.
+   */
+  class ServerList 
+  { public:
+      /** constructor */
+      ServerList();
+
+      /** destructor */
+      ~ServerList();
+
+
+      /* BASIC MANIPULATION */
+      /** \param toadd the server to add.
+       * 
+       * Add server to the game server list
+       */
+      int addServer( ServerInformation *toadd );
+
+      /** \param name Name of the server to remove
+       * 
+       * Remove server by name 
+       */
+      bool delServerByName( std::string name );
+
+      /** \param address IP address of the server to remove
+       * 
+       * Remove server by address
+       */
+      bool delServerByAddress( std::string address );
+
+
+      /* SORTING */
+
+      /** sort by name  */
+      void sortByName();
+      
+      /** sort by ping */
+      void sortByPing();
+
+    private:
+      /** the list of servers for internal storage */
+      std::list<ServerInformation *> serverlist;
+  };
+}
+
+#endif /*_ServerList_*/




More information about the Orxonox-commit mailing list