[Orxonox-commit 2988] r7684 - in code/branches/masterserver/src: libraries/network modules/masterserver
smerkli at orxonox.net
smerkli at orxonox.net
Wed Dec 1 13:51:15 CET 2010
Author: smerkli
Date: 2010-12-01 13:51:15 +0100 (Wed, 01 Dec 2010)
New Revision: 7684
Modified:
code/branches/masterserver/src/libraries/network/MasterServerComm.cc
code/branches/masterserver/src/libraries/network/MasterServerComm.h
code/branches/masterserver/src/modules/masterserver/MasterServer.cc
code/branches/masterserver/src/modules/masterserver/MasterServer.h
Log:
further testing.
Modified: code/branches/masterserver/src/libraries/network/MasterServerComm.cc
===================================================================
--- code/branches/masterserver/src/libraries/network/MasterServerComm.cc 2010-11-30 22:09:18 UTC (rev 7683)
+++ code/branches/masterserver/src/libraries/network/MasterServerComm.cc 2010-12-01 12:51:15 UTC (rev 7684)
@@ -45,6 +45,9 @@
{ COUT(1) << "An error occurred while initializing ENet.\n";
return 1;
}
+
+ /* initialize the event holder */
+ this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
/* install atexit handler for enet */
atexit( enet_deinitialize );
@@ -88,8 +91,8 @@
}
/* Wait up to 2 seconds for the connection attempt to succeed. */
- if (enet_host_service (this->client, &this->event, 2000) > 0 &&
- this->event.type == ENET_EVENT_TYPE_CONNECT )
+ if (enet_host_service (this->client, this->event, 2000) > 0 &&
+ this->event->type == ENET_EVENT_TYPE_CONNECT )
fprintf( stdout, "Connection to server succeeded." );
else
{
@@ -106,18 +109,18 @@
{
/* see whether anything happened */
/* WORK MARK REMOVE THIS OUTPUT */
- COUT(2) << "MARK polling...\n";
+ //COUT(2) << "MARK polling...\n";
/* enet_host_service returns 0 if no event occured */
/* just newly set below test to >0 from >= 0, to be tested */
- if( enet_host_service( this->client, &this->event, 1000 ) > 0 )
+ if( enet_host_service( this->client, this->event, 1000 ) > 0 )
{
/* address buffer */
char *addrconv = NULL;
int retval = 0;
/* check what type of event it is and react accordingly */
- switch (this->event.type)
+ switch (this->event->type)
{ /* new connection, not supposed to happen. */
case ENET_EVENT_TYPE_CONNECT: break;
@@ -127,22 +130,23 @@
/* incoming data */
case ENET_EVENT_TYPE_RECEIVE:
addrconv = (char *) calloc( 50, 1 );
- enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
+ enet_address_get_host_ip( &(this->event->peer->address),
+ addrconv, 49 );
/* DEBUG */
printf( "A packet of length %u containing %s was "
"received from %s on channel %u.\n",
- this->event.packet->dataLength,
- this->event.packet->data,
+ this->event->packet->dataLength,
+ this->event->packet->data,
addrconv,
- this->event.channelID );
+ this->event->channelID );
/* END DEBUG */
/* call the supplied callback, if any. */
if( (*callback) != NULL )
- retval = (*callback)( addrconv, &(this->event) );
+ retval = (*callback)( addrconv, (this->event) );
- enet_packet_destroy( event.packet );
+ enet_packet_destroy( event->packet );
if( addrconv ) free( addrconv );
break;
default: break;
Modified: code/branches/masterserver/src/libraries/network/MasterServerComm.h
===================================================================
--- code/branches/masterserver/src/libraries/network/MasterServerComm.h 2010-11-30 22:09:18 UTC (rev 7683)
+++ code/branches/masterserver/src/libraries/network/MasterServerComm.h 2010-12-01 12:51:15 UTC (rev 7684)
@@ -84,7 +84,7 @@
ENetHost *client;
/** event data holder */
- ENetEvent event;
+ ENetEvent *event;
/** address holder */
ENetAddress address;
Modified: code/branches/masterserver/src/modules/masterserver/MasterServer.cc
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.cc 2010-11-30 22:09:18 UTC (rev 7683)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.cc 2010-12-01 12:51:15 UTC (rev 7684)
@@ -36,6 +36,74 @@
/* singleton stuff */
//ManageScopedSingleton( MasterServer, ScopeID::Root, false );
+ /* helpers */
+ static void
+ helper_output_debug( ENetEvent *event, char *addrconv )
+ {
+ COUT(4) << "A packet of length"
+ << event->packet->dataLength
+ << " containing "
+ << (const char*)event->packet->data
+ << " was received from "
+ << addrconv
+ << " on channel "
+ << event->channelID << "\n";
+ }
+
+ void
+ MasterServer::helper_sendlist( ENetEvent *event )
+ {
+ /* get an iterator */
+ std::list<packet::ServerInformation>::iterator i;
+
+ /* packet holder */
+ ENetPacket *reply;
+
+ /* loop through list elements */
+ for( i = mainlist.serverlist.begin(); i
+ != mainlist.serverlist.end(); ++i )
+ {
+ /* WORK MARK */
+ /* send this particular server */
+ /* build reply string */
+ char *tosend = (char *)calloc( (*i).getServerIP().length()
+ + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
+ if( !tosend )
+ { COUT(2) << "Masterserver.cc: Memory allocation failed.\n";
+ continue;
+ }
+ sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM,
+ (*i).getServerIP().c_str() );
+
+ /* create packet from it */
+ reply = enet_packet_create( tosend,
+ strlen( tosend ) + 1,
+ ENET_PACKET_FLAG_RELIABLE);
+
+ /* Send the reply to the peer over channel id 0. */
+ enet_peer_send( event->peer, 0, reply );
+
+ /* One could just use enet_host_service() instead. */
+ enet_host_flush( this->server );
+
+ /* free the tosend buffer */
+ free( tosend );
+ }
+
+ /* send end-of-list packet */
+ reply = enet_packet_create( MSPROTO_SERVERLIST_END,
+ MSPROTO_SERVERLIST_END_LEN + 1,
+ ENET_PACKET_FLAG_RELIABLE );
+
+ enet_peer_send( event->peer, 0, reply );
+
+ /* One could just use enet_host_service() instead. */
+ enet_host_flush( this->server );
+ }
+
+
+
+
/***** EVENTS *****/
/* connect event */
int
@@ -104,33 +172,8 @@
/* DEBUG */
/* output debug info about the data that has come, to be removed */
- COUT(4) << "A packet of length"
- << event->packet->dataLength
- << " containing "
- << (const char*)event->packet->data
- << " was received from "
- << addrconv
- << " on channel "
- << event->channelID << "\n";
+ helper_output_debug( event, addrconv );
- /*
- //[> send some packet back for testing <]
- //[> TESTING <]
-
- //[> Create a reliable reply of size 7 containing "reply\0" <]
- //ENetPacket * reply = enet_packet_create ("reply",
- //strlen ("reply") + 1,
- //ENET_PACKET_FLAG_RELIABLE);
-
- //[> Send the reply to the peer over channel id 0. <]
- //enet_peer_send( event->peer, 0, reply );
-
- //[> One could just use enet_host_service() instead. <]
- //enet_host_flush( this->server );
-
- //[> /TESTING <]
- */
-
/* GAME SERVER OR CLIENT CONNECTION? */
if( !strncmp( (char *)event->packet->data, MSPROTO_GAME_SERVER,
MSPROTO_GAME_SERVER_LEN ) )
@@ -150,49 +193,10 @@
else if( !strncmp( (char *)event->packet->data, MSPROTO_CLIENT,
MSPROTO_CLIENT_LEN) )
{ /* client */
-
if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1,
MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) )
- { /* send server list */
-
- /* get an iterator */
- std::list<packet::ServerInformation>::iterator i;
-
- /* packet holder */
- ENetPacket *reply;
-
- /* loop through list elements */
- for( i = mainlist.serverlist.begin(); i != mainlist.serverlist.end(); ++i )
- {
- /* WORK MARK */
- /* send this particular server */
- /* build reply string */
- char *tosend = (char *)calloc( (*i).getServerIP().length() + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
- sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM, (*i).getServerIP().c_str() );
-
- /* create packet from it */
- reply = enet_packet_create( tosend,
- strlen( tosend ) + 1,
- ENET_PACKET_FLAG_RELIABLE);
-
- /* Send the reply to the peer over channel id 0. */
- enet_peer_send( event->peer, 0, reply );
-
- /* One could just use enet_host_service() instead. */
- enet_host_flush( this->server );
- }
-
- /* send end-of-list packet */
- reply = enet_packet_create( MSPROTO_SERVERLIST_END,
- MSPROTO_SERVERLIST_END_LEN + 1,
- ENET_PACKET_FLAG_RELIABLE );
-
- enet_peer_send( event->peer, 0, reply );
-
- /* One could just use enet_host_service() instead. */
- enet_host_flush( this->server );
-
- }
+ /* send server list */
+ helper_sendlist( event );
}
else
{ /* bad message, don't do anything. */ }
Modified: code/branches/masterserver/src/modules/masterserver/MasterServer.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.h 2010-11-30 22:09:18 UTC (rev 7683)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.h 2010-12-01 12:51:15 UTC (rev 7684)
@@ -69,6 +69,9 @@
int eventDisconnect( ENetEvent *event );
int eventData( ENetEvent *event );
+ /* helpers */
+ void helper_sendlist( ENetEvent *event );
+
/* members */
ENetAddress address;
ENetHost *server;
More information about the Orxonox-commit
mailing list