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