Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver2/src/libraries/network/MasterServer.cc @ 8163

Last change on this file since 8163 was 8163, checked in by smerkli, 15 years ago

Started thinking about server list cleanups

File size: 9.4 KB
RevLine 
[7569]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Sandro 'smerkli' Merkli
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[7565]29#include "MasterServer.h"
[7590]30#include "util/ScopedSingletonManager.h"
31#include "core/CoreIncludes.h"
32#include "core/CorePrereqs.h"
[7565]33
[7590]34namespace orxonox
35{
[7684]36  /* helpers */
37  static void 
38  helper_output_debug( ENetEvent *event, char *addrconv )
39  {
40    COUT(4) << "A packet of length" 
41      << event->packet->dataLength
42      << " containing "
43      << (const char*)event->packet->data
44      << " was received from "
45      << addrconv
46      << " on channel "
47      << event->channelID << "\n";
48  }
49
50  void
51  MasterServer::helper_sendlist( ENetEvent *event )
52  {
53    /* get an iterator */
54    std::list<packet::ServerInformation>::iterator i;
55
56    /* packet holder */
57    ENetPacket *reply;
58
59    /* loop through list elements */
60    for( i = mainlist.serverlist.begin(); i
61        != mainlist.serverlist.end(); ++i ) 
62    {
63      /* send this particular server */
64      /* build reply string */
65      char *tosend = (char *)calloc( (*i).getServerIP().length() 
66          + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
67      if( !tosend ) 
68      { COUT(2) << "Masterserver.cc: Memory allocation failed.\n";
69        continue;
70      } 
71      sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM, 
72          (*i).getServerIP().c_str() );
73
74      /* create packet from it */
75      reply = enet_packet_create( tosend,
76          strlen( tosend ) + 1, 
77          ENET_PACKET_FLAG_RELIABLE);
78
79      /* Send the reply to the peer over channel id 0. */
80      enet_peer_send( event->peer, 0, reply );
81
82      /* One could just use enet_host_service() instead. */
83      enet_host_flush( this->server );
84
85      /* free the tosend buffer */
86      free( tosend );
87    } 
88
[7750]89    /* create end-of-list packet */
[7684]90    reply = enet_packet_create( MSPROTO_SERVERLIST_END,
91        MSPROTO_SERVERLIST_END_LEN + 1,
92        ENET_PACKET_FLAG_RELIABLE );
93
[7750]94    /* send end-of-list packet */
[7684]95    enet_peer_send( event->peer, 0, reply );
96
97    /* One could just use enet_host_service() instead. */
98    enet_host_flush( this->server );
99  }
100
[8163]101  /* maybe the two methods below can be merged into one and
102   * made to use ENet's RTT functionality to check for disconnected
103   * servers.
104   */
105  void 
106  MasterServer::helper_pingServers( void )
107  {
108    /* get an iterator */
109    std::list<packet::ServerInformation>::iterator i;
110    /* loop through list elements */
111    for( i = mainlist.serverlist.begin(); i
112        != mainlist.serverlist.end(); ++i ) 
113    {
114      /* to be implemented, waiting for Oli to reply to my email - sandro */
115   
116    }
117 
118  }
[7684]119
[8163]120  void 
121  MasterServer::helper_cleanupServers()
122  {
123    /* same as above. */
[7684]124
[8163]125  }
[7684]126
[8163]127
128
129
[7590]130  /***** EVENTS *****/
131  /* connect event */
132  int 
[7611]133  MasterServer::eventConnect( ENetEvent *event )
[7590]134  { /* check for bad parameters */
135    if( !event )
[7611]136    { COUT(2) << "MasterServer::eventConnect: No event given.\n" ;
[7590]137      return -1;
138    }
[7565]139
[7611]140    /* convert address to string. */
141    char *addrconv = (char *) calloc( 50, 1 );
142    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
143
[7590]144    /* output debug info */
[7611]145    COUT(4) << "A new client connected from " 
146      << addrconv
147      << " on port " 
148      << event->peer->address.port << "\n";
[7565]149
[7611]150    /* store string form of address here */
151    event->peer->data = addrconv; 
152
153    /* all fine. */
[7590]154    return 0;
[7565]155  }
156
[7590]157  /* disconnect event */
158  int 
[7611]159  MasterServer::eventDisconnect( ENetEvent *event )
[7590]160  { /* check for bad parameters */
161    if( !event )
[7611]162    { COUT(2) << "No event given.\n";
[7590]163      return -1;
164    }
[7565]165
[7651]166    /* output that the disconnect happened */
[7611]167    COUT(4) << (char*)event->peer->data << " disconnected.\n";
[7565]168
[7651]169    /* create string from peer data */
170    std::string name = std::string( (char*)event->peer->data );
171
[7590]172    /* remove the server from the list it belongs to */
[7657]173    this->mainlist.delServerByName( name );
[7565]174
[7590]175    /* Reset the peer's client information. */
[7611]176    if( event->peer->data ) free( event->peer->data );
[7651]177
178    /* done */
[7590]179    return 0;
[7565]180  }
181
[7590]182  /* data event */
183  int 
[7611]184  MasterServer::eventData( ENetEvent *event )
[7651]185  { /* validate packet */
[7611]186    if( !event || !(event->packet) || !(event->peer) )
187    { COUT(2) << "No complete event given.\n";
[7590]188      return -1;
189    }
[7611]190     
191    /* generate address in readable form */
192    char *addrconv = (char *) calloc( 50, 1 );
193    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
[7590]194
[7750]195    /* output debug info about the data that has come */
[7684]196    helper_output_debug( event, addrconv );
[7590]197
[7630]198    /* GAME SERVER OR CLIENT CONNECTION? */
[7651]199    if( !strncmp( (char *)event->packet->data, MSPROTO_GAME_SERVER, 
200      MSPROTO_GAME_SERVER_LEN ) )
201    { /* Game server */
[7630]202
[7651]203      if( !strncmp( (char *)event->packet->data
204        + MSPROTO_GAME_SERVER_LEN+1, 
205        MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
206      { /* register new server */
[7657]207        mainlist.addServer( packet::ServerInformation( event ) );
[7658]208       
209        /* tell people we did so */
210        COUT(2) << "Added new server to list: " << 
211          packet::ServerInformation( event ).getServerIP() << "\n";
[7651]212      }
[7756]213
[7763]214      else if( !strncmp( (char *)event->packet->data
215        + MSPROTO_GAME_SERVER_LEN+1,
216        MSPROTO_SERVERDC, MSPROTO_SERVERDC_LEN ) )
217      {
218        /* create string from peer data */
219        std::string name = std::string( addrconv );
220
221        /* remove the server from the list it belongs to */
[7765]222        this->mainlist.delServerByAddress( name );
[7763]223
224        /* tell the user */
225        COUT(2) << "Removed server " << name << " from list.\n";
226      }
227
[7756]228      /* TODO add hook for disconnect here */
[7651]229    }
230    else if( !strncmp( (char *)event->packet->data, MSPROTO_CLIENT, 
231      MSPROTO_CLIENT_LEN) )
232    { /* client */
233      if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1,
[7657]234        MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) )
[7684]235        /* send server list */
236        helper_sendlist( event );
[7651]237    }
238    else
239    { /* bad message, don't do anything. */ } 
240
[7611]241    /* delete addrconv */
242    if( addrconv ) free( addrconv );
243
[7590]244    /* Clean up the packet now that we're done using it. */
245    enet_packet_destroy( event->packet );
246    return 0;
247  }
[7565]248
[7611]249
[7590]250  /**** MAIN ROUTINE *****/
251  int 
252  MasterServer::run()
253  {
254    /***** ENTER MAIN LOOP *****/
255    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
256    if( event == NULL )
[7611]257    { 
258      COUT(1) << "Could not create ENetEvent structure, exiting.\n";
[7590]259      exit( EXIT_FAILURE );
260    }
[7565]261
[7756]262    /* TODO schedule pings for servers somewhere here */
[8163]263    /* Iterate through servers and send pings */
264    helper_pingServers();
[7729]265   
[8163]266    /* check for timed out pings and remove those guys from
267     * the server list
268     */
269    helper_cleanupServers();
270
271
[7743]272    /* create an iterator for the loop */
273    enet_host_service( this->server, event, 100 );
[7611]274
[7743]275    /* check what type of event it is and react accordingly */
276    switch (event->type)
277    { /* new connection */
278      case ENET_EVENT_TYPE_CONNECT: 
279        eventConnect( event ); break;
[7565]280
[7743]281        /* disconnect */
282      case ENET_EVENT_TYPE_DISCONNECT: 
283        eventDisconnect( event ); break;
284
285        /* incoming data */
286      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
287      default: break;
[7590]288    }
[7565]289
[7590]290    /* done */
291    return 0;
292  } 
[7565]293
[7590]294  /* constructor */
295  MasterServer::MasterServer()
296  {
297    /***** INITIALIZE NETWORKING *****/
298    if( enet_initialize () != 0)
[7611]299    { COUT(1) << "An error occurred while initializing ENet.\n";
[7590]300      exit( EXIT_FAILURE );
301    }
[7565]302
[7590]303    /* register deinitialization */
304    atexit( enet_deinitialize );
[7565]305
[7729]306    /* set the quit flag to false */
307    this->quit = false;
308
[7590]309    /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
310    this->address.host = ENET_HOST_ANY;
311    this->address.port = ORX_MSERVER_PORT;
[7589]312
[7590]313    /* create a host with the above settings (the last two 0 mean: accept
314     * any input/output bandwidth */
315    this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS, 
[7801]316        ORX_MSERVER_MAXCHANS, 0, 0 );
317    assert(this->server);
[7590]318
319    /* see if creation worked */
320    if( !this->server )
[7611]321    { COUT(1) << 
322        "An error occurred while trying to create an ENet server host.\n";
323      exit( EXIT_FAILURE );
[7590]324    }
[7565]325
[7611]326    /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
327    this->peers = new PeerList();
[7743]328
329    /* tell people we're now initialized */
330    COUT(0) << "MasterServer initialized, waiting for connections.\n";
[7565]331  }
332
[7590]333  /* destructor */
334  MasterServer::~MasterServer()
335  {
336    /***** CLEANUP PROCESS *****/
337    /* terminate all networking connections */
338    enet_host_destroy( this->server );
[7565]339
[7611]340    /* free all used memory */
[7590]341    /* clear the list of connected game servers */
342    /* clear the list of connected game clients */
343  }
344
345/* end of namespace */
346}
Note: See TracBrowser for help on using the repository browser.