Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/multiplayerFS15/src/libraries/network/MasterServer.cc @ 10434

Last change on this file since 10434 was 10434, checked in by frovelli, 9 years ago

Implemented playercount in multiplayer lobby

  • Property svn:eol-style set to native
File size: 13.9 KB
Line 
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
29#include "MasterServer.h"
30#include "util/ScopedSingletonManager.h"
31#include "core/command/ConsoleCommand.h"
32#include "core/CoreIncludes.h"
33#include "core/CorePrereqs.h"
34#include "util/Output.h"
35
36namespace orxonox
37{
38  /*** MACROS ***/
39  /* commands for the terminal interface */
40  SetConsoleCommand( "ms-listservers", &MasterServer::listServers );
41  SetConsoleCommand( "ms-delserver", &MasterServer::delServer );
42  //SetConsoleCommand( "ms-serverinfo", &MasterServer::serverInfo );
43
44  /* forward declaration so the linker doesn't complain */
45  MasterServer *MasterServer::instance = NULL;
46
47
48
49
50  /* command: list servers */
51  void
52  MasterServer::listServers( void )
53  {
54    /* get an iterator */
55    std::list<ServerListElem>::iterator i;
56
57    /* print list header */
58    orxout(user_info) << "List of connected servers" << std::endl;
59
60    /* loop through list elements */
61    for( i = MasterServer::getInstance()->mainlist.serverlist.begin();
62      i != MasterServer::getInstance()->mainlist.serverlist.end(); ++i )
63    {
64      orxout(user_info) << "  " << (*i).ServerInfo.getServerIP() << std::endl;
65    }
66
67    /* display end of list */
68    orxout(user_info) << MasterServer::getInstance()->mainlist.serverlist.size() <<
69      " servers connected." << std::endl;
70  }
71
72  void
73  MasterServer::delServer( std::string todeladdr )
74  {
75    /* tell the user we're now removing the entry from the server list */
76    orxout(user_info) << "MS: Deleting server \"" << todeladdr << "\"..."
77      << std::endl;
78
79    /* see if we actually have that server on our list */
80    ServerListSearchResult shandle =
81      MasterServer::getInstance()->mainlist.findServerByAddress(todeladdr);
82
83    if( !shandle.success )
84    { orxout(user_info) << "MS: Server not found, not removing." << std::endl;
85      return;
86    }
87
88    /* force-disconnect the server */
89    enet_peer_disconnect( shandle.result.peer, 0 );
90
91    /* actually remove the entry from the server list by address */
92    MasterServer::getInstance()->mainlist.delServerByAddress( todeladdr);
93
94    /* tell the user about our success */
95    orxout(user_info) << "MS: Server deletion successful." << std::endl;
96  }
97
98
99  /* helpers */
100  static void
101  helper_output_debug( ENetEvent *event, char *addrconv )
102  {
103    orxout(verbose, context::master_server)
104      << "A packet of length"
105      << event->packet->dataLength
106      << " containing "
107      << (const char*)event->packet->data
108      << " was received from "
109      << addrconv
110      << " on channel "
111      << event->channelID << endl;
112  }
113
114  void
115  MasterServer::helper_sendlist( ENetEvent *event )
116  {
117    /* get an iterator */
118    std::list<ServerListElem>::iterator i;
119
120    /* packet holder */
121    ENetPacket *reply;
122
123    /* loop through list elements */
124    for( i = mainlist.serverlist.begin(); i
125        != mainlist.serverlist.end(); ++i )
126    {
127      /* send this particular server */
128      /* build reply string */
129      int packetlen = MSPROTO_SERVERLIST_ITEM_LEN + 1 + (*i).ServerInfo.getServerIP().length() + 1 + (*i).ServerInfo.getServerName().length() + 1 + sizeof((*i).ServerInfo.getClientNumber()) + 1;
130      char *tosend = (char *)calloc(packetlen ,1 );
131      if( !tosend )
132      { orxout(internal_warning, context::master_server) << "Masterserver.cc: Memory allocation failed." << endl;
133        continue;
134      }
135      sprintf( tosend, "%s %s %s %u", MSPROTO_SERVERLIST_ITEM,
136          (*i).ServerInfo.getServerIP().c_str(), (*i).ServerInfo.getServerName().c_str(), (*i).ServerInfo.getClientNumber());
137
138      /* create packet from it */
139      reply = enet_packet_create( tosend,
140          strlen( tosend ) + 1,
141          ENET_PACKET_FLAG_RELIABLE);
142
143      /* Send the reply to the peer over channel id 0. */
144      enet_peer_send( event->peer, 0, reply );
145
146      /* One could just use enet_host_service() instead. */
147      enet_host_flush( this->server );
148
149      /* free the tosend buffer */
150      free( tosend );
151    }
152
153    /* create end-of-list packet */
154    reply = enet_packet_create( MSPROTO_SERVERLIST_END,
155        MSPROTO_SERVERLIST_END_LEN + 1,
156        ENET_PACKET_FLAG_RELIABLE );
157
158    /* send end-of-list packet */
159    enet_peer_send( event->peer, 0, reply );
160
161    /* One could just use enet_host_service() instead. */
162    enet_host_flush( this->server );
163  }
164
165  /* maybe the two methods below can be merged into one and
166   * made to use ENet's RTT functionality to check for disconnected
167   * servers.
168   */
169  void
170  MasterServer::helper_cleanupServers( void )
171  {
172    /* get an iterator */
173    std::list<ServerListElem>::iterator i;
174
175    if( mainlist.serverlist.size() == 0 )
176      return;
177
178    /* loop through list elements */
179    for( i = mainlist.serverlist.begin(); i
180        != mainlist.serverlist.end(); ++i )
181    { /* see if we have a disconnected peer */
182      if( (*i).peer &&
183         ((*i).peer->state == ENET_PEER_STATE_DISCONNECTED ||
184          (*i).peer->state == ENET_PEER_STATE_ZOMBIE ))
185      {
186        /* Remove it from the list */
187        orxout(internal_warning) << (char*)(*i).peer->data << " timed out.\n";
188        mainlist.delServerByName( (*i).ServerInfo.getServerName() );
189
190        /* stop iterating, we manipulated the list */
191        /* TODO note: this only removes one dead server per loop
192         * iteration. not beautiful, but one iteration is ~100ms,
193         * so not really relevant for the moment.
194         */
195        break;
196      }
197    }
198
199  }
200
201
202
203
204  /***** EVENTS *****/
205  /* connect event */
206  int
207  MasterServer::eventConnect( ENetEvent *event )
208  { /* check for bad parameters */
209    if( !event )
210    { orxout(internal_warning, context::master_server) << "MasterServer::eventConnect: No event given." << endl;
211      return -1;
212    }
213
214    /* convert address to string. */
215    char *addrconv = (char *) calloc( 50, 1 );
216    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
217
218    /* output debug info */
219    orxout(verbose, context::master_server) << "A new client connected from "
220      << addrconv
221      << " on port "
222      << event->peer->address.port << endl;
223
224    /* store string form of address here */
225    event->peer->data = addrconv;
226
227    /* all fine. */
228    return 0;
229  }
230
231  /* disconnect event */
232  int
233  MasterServer::eventDisconnect( ENetEvent *event )
234  { /* check for bad parameters */
235    if( !event )
236    { orxout(internal_warning, context::master_server) << "No event given." << endl;
237      return -1;
238    }
239
240    /* output that the disconnect happened */
241    orxout(verbose, context::master_server) << (char*)event->peer->data << " disconnected." << endl;
242
243    /* create string from peer data */
244    std::string name = std::string( (char*)event->peer->data );
245
246    /* remove the server from the list it belongs to */
247    this->mainlist.delServerByName( name );
248
249    /* Reset the peer's client information. */
250    if( event->peer->data ) free( event->peer->data );
251
252    /* done */
253    return 0;
254  }
255
256  /* data event */
257  int
258  MasterServer::eventData( ENetEvent *event )
259  { /* validate packet */
260    if( !event || !(event->packet) || !(event->peer) )
261    { orxout(internal_warning, context::master_server) << "No complete event given." << endl;
262      return -1;
263    }
264
265    /* generate address in readable form */
266    char *addrconv = (char *) calloc( 50, 1 );
267    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
268
269    /* output debug info about the data that has come */
270    helper_output_debug( event, addrconv );
271
272    /* GAME SERVER OR CLIENT CONNECTION? */
273    if( !strncmp( (char *)event->packet->data, MSPROTO_GAME_SERVER,
274      MSPROTO_GAME_SERVER_LEN ) )
275    { /* Game server */
276
277      if( !strncmp( (char *)event->packet->data
278        + MSPROTO_GAME_SERVER_LEN+1,
279        MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
280      { /* register new server */
281        mainlist.addServer( packet::ServerInformation( event ),
282          event->peer );
283
284        /* tell people we did so */
285        orxout(internal_info, context::master_server) << "Added new server to list: " <<
286          packet::ServerInformation( event ).getServerIP() << endl;
287      }
288
289      else if( !strncmp( (char *)event->packet->data
290        + MSPROTO_GAME_SERVER_LEN+1,
291        MSPROTO_SERVERDC, MSPROTO_SERVERDC_LEN ) )
292      {
293        /* create string from peer data */
294        std::string ip = std::string( addrconv );
295
296        /* remove the server from the list it belongs to */
297        this->mainlist.delServerByAddress( ip );
298
299        /* tell the user */
300        orxout(internal_info, context::master_server) << "Removed server " << ip << " from list." << endl;
301      }
302      /* TODO add hook for disconnect here */
303
304      else if( !strncmp( (char *)event->packet->data
305        + MSPROTO_GAME_SERVER_LEN+1,
306        MSPROTO_SET_NAME, MSPROTO_SET_NAME_LEN ) )
307      {
308        /* create string from peer data */
309        std::string ip = std::string( addrconv );
310        std::string data (event->packet->data,event->packet->data + event->packet->dataLength );
311        std::string name = data.substr(MSPROTO_GAME_SERVER_LEN+1 + MSPROTO_SET_NAME_LEN + 1);
312
313        /* remove the server from the list it belongs to */
314        this->mainlist.setNameByAddress( ip, name );
315
316        /* tell the user */
317        orxout(internal_info, context::master_server) << "Updated server " << ip << " with new name " << name << endl;
318      }
319
320      else if( !strncmp( (char *)event->packet->data
321        + MSPROTO_GAME_SERVER_LEN+1,
322        MSPROTO_SET_CLIENTS, MSPROTO_SET_CLIENTS_LEN ) )
323      {
324        /* create string from peer data */
325        std::string ip = std::string( addrconv );
326        std::string data (event->packet->data,event->packet->data + event->packet->dataLength );
327        std::string textform= data.substr(MSPROTO_GAME_SERVER_LEN + 1 + MSPROTO_SET_CLIENTS_LEN + 1);
328        int clientNumber = Ogre::StringConverter::parseInt(textform);
329
330        this->mainlist.setClientsByAddress( ip, clientNumber);
331
332        /* tell the user */
333        orxout(internal_info, context::master_server) << "Updated server " << ip << " with new client number " << clientNumber << endl;
334      }
335    }
336    else if( !strncmp( (char *)event->packet->data, MSPROTO_CLIENT,
337      MSPROTO_CLIENT_LEN) )
338    { /* client */
339      if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1,
340        MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) )
341        /* send server list */
342        helper_sendlist( event );
343    }
344    else
345    { /* bad message, don't do anything. */ }
346
347    /* delete addrconv */
348    if( addrconv ) free( addrconv );
349
350    /* Clean up the packet now that we're done using it. */
351    enet_packet_destroy( event->packet );
352    return 0;
353  }
354
355
356  /**** MAIN ROUTINE *****/
357  int
358  MasterServer::run()
359  {
360    /***** ENTER MAIN LOOP *****/
361    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
362    if( event == NULL )
363    {
364      orxout(user_error, context::master_server) << "Could not create ENetEvent structure, exiting." << endl;
365      exit( EXIT_FAILURE );
366    }
367
368    /* check for timed out peers and remove those from * the server list */
369    helper_cleanupServers();
370
371
372    /* create an iterator for the loop */
373    enet_host_service( this->server, event, 100 );
374
375    /* check what type of event it is and react accordingly */
376    switch (event->type)
377    { /* new connection */
378      case ENET_EVENT_TYPE_CONNECT:
379        eventConnect( event ); break;
380
381        /* disconnect */
382      case ENET_EVENT_TYPE_DISCONNECT:
383        eventDisconnect( event ); break;
384
385        /* incoming data */
386      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
387      default: break;
388    }
389
390    /* done */
391    free(event);
392    return 0;
393  }
394
395  /* constructor */
396  MasterServer::MasterServer()
397  {
398    /***** INITIALIZE NETWORKING *****/
399    if( enet_initialize () != 0)
400    { orxout(user_error, context::master_server) << "An error occurred while initializing ENet." << endl;
401      exit( EXIT_FAILURE );
402    }
403
404    /* register deinitialization */
405    atexit( enet_deinitialize );
406
407    /* set the quit flag to false */
408    this->quit = false;
409
410    /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
411    this->address.host = ENET_HOST_ANY;
412    this->address.port = ORX_MSERVER_PORT;
413
414    /* create a host with the above settings (the last two 0 mean: accept
415     * any input/output bandwidth */
416    this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS,
417        ORX_MSERVER_MAXCHANS, 0, 0 );
418    assert(this->server);
419
420    /* see if creation worked */
421    if( !this->server )
422    { orxout(user_error, context::master_server) <<
423        "An error occurred while trying to create an ENet server host." << endl;
424      exit( EXIT_FAILURE );
425    }
426
427    /* set pointer to this instance */
428    MasterServer::setInstance( this );
429
430    /* tell people we're now initialized */
431    orxout(internal_status, context::master_server) << "MasterServer initialized, waiting for connections." << endl;
432  }
433
434  /* destructor */
435  MasterServer::~MasterServer()
436  {
437    /***** CLEANUP PROCESS *****/
438    /* terminate all networking connections */
439    enet_host_destroy( this->server );
440
441    /* free all used memory */
442    /* clear the list of connected game servers */
443    /* clear the list of connected game clients */
444  }
445
446/* end of namespace */
447}
Note: See TracBrowser for help on using the repository browser.