[Orxonox-commit 2886] r7589 - code/branches/masterserver/src/modules/masterserver
smerkli at orxonox.net
smerkli at orxonox.net
Wed Oct 27 15:24:57 CEST 2010
Author: smerkli
Date: 2010-10-27 15:24:56 +0200 (Wed, 27 Oct 2010)
New Revision: 7589
Added:
code/branches/masterserver/src/modules/masterserver/TestClient.cpp
Modified:
code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
code/branches/masterserver/src/modules/masterserver/MasterServer.h
code/branches/masterserver/src/modules/masterserver/PeerList.h
code/branches/masterserver/src/modules/masterserver/ServerList.cpp
Log:
wrote some test client and further implemented the master server itself. now we need to make the module actually work for debugging.
Modified: code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.cpp 2010-10-27 11:15:28 UTC (rev 7588)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.cpp 2010-10-27 13:24:56 UTC (rev 7589)
@@ -32,7 +32,9 @@
/***** EVENTS *****/
/* connect event */
-int eventConnect( ENetEvent *event )
+int eventConnect( ENetEvent *event,
+ orxonox::ServerList *serverlist,
+ orxonox::PeerList *peers )
{ /* check for bad parameters */
if( !event )
{ fprintf( stderr, "No event given.\n" );
@@ -40,13 +42,16 @@
}
/* output debug info */
- //printf( "A new client connected from %x:%u.\n",
- //event->peer->address.host,
- //event->peer->address.port);
+ 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 */
/* add to game server list */
+ /* */
+
/* client */
/* add to client list */
@@ -60,7 +65,9 @@
}
/* disconnect event */
-int eventDisconnect( ENetEvent *event )
+int eventDisconnect( ENetEvent *event,
+ orxonox::ServerList *serverlist,
+ orxonox::PeerList *peers )
{ /* check for bad parameters */
if( !event )
{ fprintf( stderr, "No event given.\n" );
@@ -68,7 +75,7 @@
}
/* output that the disconnect happened, to be removed at a later time. */
- //printf ("%s disconnected.\n", event->peer -> data);
+ printf( "%s disconnected.\n", event->peer->data );
/* remove the server from the list it belongs to */
@@ -125,19 +132,14 @@
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"); */
+ /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
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 */);
+ /* 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 );
/* see if creation worked */
if( !server )
@@ -148,11 +150,14 @@
/***** INITIALIZE GAME SERVER LIST *****/
orxonox::ServerList *mainlist = new orxonox::ServerList();
- //if( mainlist == NULL )
- //{ fprintf( stderr, "Error creating server list.\n" );
- //exit( EXIT_FAILURE );
- //}
+ if( mainlist == NULL )
+ { fprintf( stderr, "Error creating server list.\n" );
+ exit( EXIT_FAILURE );
+ }
+ /***** INITIALIZE PEER LIST *****/
+ orxonox::PeerList *peers = new orxonox::PeerList();
+
/***** ENTER MAIN LOOP *****/
ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
if( event == NULL )
@@ -160,22 +165,21 @@
exit( EXIT_FAILURE );
}
- /* NOTE this only waits on one client, we need to find some way to
- * actually listen on all active connections. This will be implemented
- * together with the list of active connections.
- */
- /* Wait up to 1000 milliseconds for an event. */
- while (enet_host_service (client, event, 1000) > 0)
+ /* 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)
+ switch (event->type)
{ /* new connection */
- case ENET_EVENT_TYPE_CONNECT: eventConnect( event ); break;
+ case ENET_EVENT_TYPE_CONNECT:
+ eventConnect( event, mainlist, peers ); break;
/* disconnect */
- case ENET_EVENT_TYPE_DISCONNECT: eventDisconnect( event ); break;
+ case ENET_EVENT_TYPE_DISCONNECT:
+ eventDisconnect( event, mainlist, peers ); break;
/* incoming data */
case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
+ default: break;
}
}
Modified: code/branches/masterserver/src/modules/masterserver/MasterServer.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.h 2010-10-27 11:15:28 UTC (rev 7588)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.h 2010-10-27 13:24:56 UTC (rev 7589)
@@ -40,6 +40,7 @@
/* my includes */
#include "ServerList.h"
+#include "PeerList.h"
/* c compatibility */
#include <cstdio>
Modified: code/branches/masterserver/src/modules/masterserver/PeerList.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/PeerList.h 2010-10-27 11:15:28 UTC (rev 7588)
+++ code/branches/masterserver/src/modules/masterserver/PeerList.h 2010-10-27 13:24:56 UTC (rev 7589)
@@ -66,9 +66,15 @@
* Find a connection by address */
ENetPeer *findPeerByAddr( ENetAddress addr );
+ /* NOTE: making this list public so it can easily
+ * be iterated. This makes sense since iterating it
+ * will happen all the time, and using getter methods
+ * for the next in list would slow things down unnecessarily.
+ */
+ /** the list of servers for internal storage */
+ std::list<ENetPeer *> peerlist;
private:
- /** the list of servers for internal storage */
- std::list<packet::ENetPeer *> peerlist;
+ /* nothing so far. */
};
}
Modified: code/branches/masterserver/src/modules/masterserver/ServerList.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/ServerList.cpp 2010-10-27 11:15:28 UTC (rev 7588)
+++ code/branches/masterserver/src/modules/masterserver/ServerList.cpp 2010-10-27 13:24:56 UTC (rev 7589)
@@ -26,7 +26,7 @@
*
*/
- #include "ServerList.h"
+#include "ServerList.h"
namespace orxonox
{
@@ -35,24 +35,26 @@
ServerList::~ServerList()
{ /* delete the list */
- serverlist.clear();}
+ serverlist.clear();
}
- int ServerList::addServer( ServerInformation *toadd )
+ int
+ ServerList::addServer( packet::ServerInformation *toadd )
{ this->serverlist.push_back( toadd );
return 0;
}
- bool ServerList::delServerByName( std::string name )
+ bool
+ ServerList::delServerByName( std::string name )
{
/* get an iterator */
- list<ServerInformation *>::iterator i;
+ std::list<packet::ServerInformation *>::iterator i;
/* loop through list elements */
for( i = serverlist.begin(); i != serverlist.end(); ++i )
- if( (*i)->serverName_ == name )
+ if( (*i)->getServerName() == name )
{ /* found this name, remove and quit */
- this->serverlist.remove( i );
+ this->serverlist.remove( *i );
return true;
}
return false;
@@ -61,13 +63,13 @@
bool ServerList::delServerByAddress( std::string address )
{
/* get an iterator */
- list<ServerInformation *>::iterator i;
+ std::list<packet::ServerInformation *>::iterator i;
/* loop through list elements */
for( i=serverlist.begin(); i != serverlist.end(); ++i )
- if( (*i)->serverIP_ == address )
+ if( (*i)->getServerIP() == address )
{ /* found this name, remove and quit */
- this->serverlist.remove( i );
+ this->serverlist.remove( *i );
return true;
}
return false;
@@ -75,8 +77,9 @@
/* sort by name */
- bool sub_compare_names( ServerInformation *no1, ServerInformation *no2 )
- { return no1->serverName_ > no2->serverName_; }
+ bool sub_compare_names( packet::ServerInformation *no1,
+ packet::ServerInformation *no2 )
+ { return no1->getServerName() > no2->getServerName(); }
void ServerList::sortByName()
{
@@ -84,8 +87,12 @@
}
/* sort by ping */
- bool sub_compare_pings( ServerInformation *no1, ServerInformation *no2 )
- { TODO return no1->serverName_ > no2->serverName_; }
+ bool sub_compare_pings( packet::ServerInformation *no1,
+ packet::ServerInformation *no2 )
+ {
+ /* TODO */
+ return no1->getServerName() > no2->getServerName();
+ }
void ServerList::sortByPing()
{
Added: code/branches/masterserver/src/modules/masterserver/TestClient.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/TestClient.cpp (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/TestClient.cpp 2010-10-27 13:24:56 UTC (rev 7589)
@@ -0,0 +1,113 @@
+/*
+ * 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 <cstdlib>
+#include <cstdio>
+#include <cstring>
+#include <enet/enet.h>
+
+int main( int argc, char *argv[] )
+{
+ /* initialize Enet */
+ if (enet_initialize () != 0)
+ { fprintf (stderr, "An error occurred while initializing ENet.\n");
+ return EXIT_FAILURE;
+ }
+ atexit (enet_deinitialize);
+
+ /* client handle */
+ ENetHost *client;
+
+ /* initiate the client */
+ client = enet_host_create( NULL /* create a client host */,
+ 1,
+ 2, /* allow up 2 channels to be used, 0 and 1 */
+ 0,
+ 0 );
+
+ /* see if it worked */
+ if (client == NULL)
+ { fprintf (stderr,
+ "An error occurred while trying to create an ENet client host.\n");
+ exit (EXIT_FAILURE);
+ }
+
+ ENetAddress address;
+ ENetEvent event;
+ ENetPeer *peer;
+
+ /* Connect to some.server.net:1234. */
+ enet_address_set_host (& address, argv[1] );
+ address.port = 1234;
+
+ /* Initiate the connection, allocating the two channels 0 and 1. */
+ peer = enet_host_connect(client, &address, 2, 0);
+
+ if (peer == NULL )
+ { fprintf( stderr,
+ "No available peers for initiating an ENet connection.\n");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Wait up to 5 seconds for the connection attempt to succeed. */
+ if (enet_host_service (client, & event, 5000) > 0 &&
+ event.type == ENET_EVENT_TYPE_CONNECT)
+ { fprintf( stdout, "Connection to some.server.net:1234 succeeded." );
+ char *theinput = (char *)calloc( 100,1 );
+ while( true )
+ {
+ fgets( theinput, 90, stdin );
+ if( !strncmp( theinput, "quit", 4 ) )
+ break;
+
+ /* send the data to the friend */
+ /* Create a reliable packet of size 7 containing "packet\0" */
+ ENetPacket * packet = enet_packet_create( theinput,
+ strlen( theinput ) + 1,
+ ENET_PACKET_FLAG_RELIABLE);
+
+ /* Send the packet to the peer over channel id 0. */
+ enet_peer_send (peer, 0, packet);
+
+ /* One could just use enet_host_service() instead. */
+ enet_host_flush( client );
+ if( packet ) free( packet );
+ }
+ }
+ else
+ {
+ /* Either the 5 seconds are up or a disconnect event was */
+ /* received. Reset the peer in the event the 5 seconds */
+ /* had run out without any significant event. */
+ enet_peer_reset (peer);
+ fprintf( stdout, "Connection to some.server.net:1234 failed." );
+ exit(EXIT_FAILURE);
+ }
+
+ enet_host_destroy(client);
+}
More information about the Orxonox-commit
mailing list