| [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] | 34 | namespace 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 */ |
|---|
| [8202] | 54 | std::list<ServerListElem>::iterator i; |
|---|
| [7684] | 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 */ |
|---|
| [8202] | 65 | char *tosend = (char *)calloc( (*i).ServerInfo.getServerIP().length() |
|---|
| [7684] | 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, |
|---|
| [8202] | 72 | (*i).ServerInfo.getServerIP().c_str() ); |
|---|
| [7684] | 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 |
|---|
| [8202] | 106 | MasterServer::helper_cleanupServers() |
|---|
| [8163] | 107 | { |
|---|
| 108 | /* get an iterator */ |
|---|
| [8202] | 109 | std::list<ServerListElem>::iterator i; |
|---|
| [8203] | 110 | |
|---|
| 111 | if( mainlist.serverlist.size() == 0 ) |
|---|
| 112 | return; |
|---|
| [8202] | 113 | |
|---|
| [8163] | 114 | /* loop through list elements */ |
|---|
| 115 | for( i = mainlist.serverlist.begin(); i |
|---|
| 116 | != mainlist.serverlist.end(); ++i ) |
|---|
| 117 | { |
|---|
| [8203] | 118 | if( (*i).peer && |
|---|
| [8202] | 119 | ((*i).peer->state == ENET_PEER_STATE_DISCONNECTED || |
|---|
| 120 | (*i).peer->state == ENET_PEER_STATE_ZOMBIE )) |
|---|
| 121 | { mainlist.delServerByName( (*i).ServerInfo.getServerName() ); |
|---|
| 122 | COUT(2) << "someone timed out.\n"; |
|---|
| [8203] | 123 | break; |
|---|
| [8202] | 124 | } |
|---|
| [8163] | 125 | } |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| [7684] | 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| [8163] | 131 | |
|---|
| [7590] | 132 | /***** EVENTS *****/ |
|---|
| 133 | /* connect event */ |
|---|
| 134 | int |
|---|
| [7611] | 135 | MasterServer::eventConnect( ENetEvent *event ) |
|---|
| [7590] | 136 | { /* check for bad parameters */ |
|---|
| 137 | if( !event ) |
|---|
| [7611] | 138 | { COUT(2) << "MasterServer::eventConnect: No event given.\n" ; |
|---|
| [7590] | 139 | return -1; |
|---|
| 140 | } |
|---|
| [7565] | 141 | |
|---|
| [7611] | 142 | /* convert address to string. */ |
|---|
| 143 | char *addrconv = (char *) calloc( 50, 1 ); |
|---|
| 144 | enet_address_get_host_ip( &(event->peer->address), addrconv, 49 ); |
|---|
| 145 | |
|---|
| [7590] | 146 | /* output debug info */ |
|---|
| [7611] | 147 | COUT(4) << "A new client connected from " |
|---|
| 148 | << addrconv |
|---|
| 149 | << " on port " |
|---|
| 150 | << event->peer->address.port << "\n"; |
|---|
| [7565] | 151 | |
|---|
| [7611] | 152 | /* store string form of address here */ |
|---|
| 153 | event->peer->data = addrconv; |
|---|
| 154 | |
|---|
| 155 | /* all fine. */ |
|---|
| [7590] | 156 | return 0; |
|---|
| [7565] | 157 | } |
|---|
| 158 | |
|---|
| [7590] | 159 | /* disconnect event */ |
|---|
| 160 | int |
|---|
| [7611] | 161 | MasterServer::eventDisconnect( ENetEvent *event ) |
|---|
| [7590] | 162 | { /* check for bad parameters */ |
|---|
| 163 | if( !event ) |
|---|
| [7611] | 164 | { COUT(2) << "No event given.\n"; |
|---|
| [7590] | 165 | return -1; |
|---|
| 166 | } |
|---|
| [7565] | 167 | |
|---|
| [7651] | 168 | /* output that the disconnect happened */ |
|---|
| [8202] | 169 | COUT(2) << (char*)event->peer->data << " disconnected.\n"; |
|---|
| [7565] | 170 | |
|---|
| [7651] | 171 | /* create string from peer data */ |
|---|
| 172 | std::string name = std::string( (char*)event->peer->data ); |
|---|
| 173 | |
|---|
| [7590] | 174 | /* remove the server from the list it belongs to */ |
|---|
| [7657] | 175 | this->mainlist.delServerByName( name ); |
|---|
| [7565] | 176 | |
|---|
| [7590] | 177 | /* Reset the peer's client information. */ |
|---|
| [7611] | 178 | if( event->peer->data ) free( event->peer->data ); |
|---|
| [7651] | 179 | |
|---|
| 180 | /* done */ |
|---|
| [7590] | 181 | return 0; |
|---|
| [7565] | 182 | } |
|---|
| 183 | |
|---|
| [7590] | 184 | /* data event */ |
|---|
| 185 | int |
|---|
| [7611] | 186 | MasterServer::eventData( ENetEvent *event ) |
|---|
| [7651] | 187 | { /* validate packet */ |
|---|
| [7611] | 188 | if( !event || !(event->packet) || !(event->peer) ) |
|---|
| 189 | { COUT(2) << "No complete event given.\n"; |
|---|
| [7590] | 190 | return -1; |
|---|
| 191 | } |
|---|
| [7611] | 192 | |
|---|
| 193 | /* generate address in readable form */ |
|---|
| 194 | char *addrconv = (char *) calloc( 50, 1 ); |
|---|
| 195 | enet_address_get_host_ip( &(event->peer->address), addrconv, 49 ); |
|---|
| [7590] | 196 | |
|---|
| [7750] | 197 | /* output debug info about the data that has come */ |
|---|
| [7684] | 198 | helper_output_debug( event, addrconv ); |
|---|
| [7590] | 199 | |
|---|
| [7630] | 200 | /* GAME SERVER OR CLIENT CONNECTION? */ |
|---|
| [7651] | 201 | if( !strncmp( (char *)event->packet->data, MSPROTO_GAME_SERVER, |
|---|
| 202 | MSPROTO_GAME_SERVER_LEN ) ) |
|---|
| 203 | { /* Game server */ |
|---|
| [7630] | 204 | |
|---|
| [7651] | 205 | if( !strncmp( (char *)event->packet->data |
|---|
| 206 | + MSPROTO_GAME_SERVER_LEN+1, |
|---|
| 207 | MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) ) |
|---|
| 208 | { /* register new server */ |
|---|
| [8202] | 209 | mainlist.addServer( packet::ServerInformation( event ), |
|---|
| 210 | event->peer ); |
|---|
| [7658] | 211 | |
|---|
| 212 | /* tell people we did so */ |
|---|
| 213 | COUT(2) << "Added new server to list: " << |
|---|
| 214 | packet::ServerInformation( event ).getServerIP() << "\n"; |
|---|
| [7651] | 215 | } |
|---|
| [7756] | 216 | |
|---|
| [7763] | 217 | else if( !strncmp( (char *)event->packet->data |
|---|
| 218 | + MSPROTO_GAME_SERVER_LEN+1, |
|---|
| 219 | MSPROTO_SERVERDC, MSPROTO_SERVERDC_LEN ) ) |
|---|
| 220 | { |
|---|
| 221 | /* create string from peer data */ |
|---|
| 222 | std::string name = std::string( addrconv ); |
|---|
| 223 | |
|---|
| 224 | /* remove the server from the list it belongs to */ |
|---|
| [7765] | 225 | this->mainlist.delServerByAddress( name ); |
|---|
| [7763] | 226 | |
|---|
| 227 | /* tell the user */ |
|---|
| 228 | COUT(2) << "Removed server " << name << " from list.\n"; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| [7756] | 231 | /* TODO add hook for disconnect here */ |
|---|
| [7651] | 232 | } |
|---|
| 233 | else if( !strncmp( (char *)event->packet->data, MSPROTO_CLIENT, |
|---|
| 234 | MSPROTO_CLIENT_LEN) ) |
|---|
| 235 | { /* client */ |
|---|
| 236 | if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1, |
|---|
| [7657] | 237 | MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) ) |
|---|
| [7684] | 238 | /* send server list */ |
|---|
| 239 | helper_sendlist( event ); |
|---|
| [7651] | 240 | } |
|---|
| 241 | else |
|---|
| 242 | { /* bad message, don't do anything. */ } |
|---|
| 243 | |
|---|
| [7611] | 244 | /* delete addrconv */ |
|---|
| 245 | if( addrconv ) free( addrconv ); |
|---|
| 246 | |
|---|
| [7590] | 247 | /* Clean up the packet now that we're done using it. */ |
|---|
| 248 | enet_packet_destroy( event->packet ); |
|---|
| 249 | return 0; |
|---|
| 250 | } |
|---|
| [7565] | 251 | |
|---|
| [7611] | 252 | |
|---|
| [7590] | 253 | /**** MAIN ROUTINE *****/ |
|---|
| 254 | int |
|---|
| 255 | MasterServer::run() |
|---|
| 256 | { |
|---|
| 257 | /***** ENTER MAIN LOOP *****/ |
|---|
| 258 | ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char)); |
|---|
| 259 | if( event == NULL ) |
|---|
| [7611] | 260 | { |
|---|
| 261 | COUT(1) << "Could not create ENetEvent structure, exiting.\n"; |
|---|
| [7590] | 262 | exit( EXIT_FAILURE ); |
|---|
| 263 | } |
|---|
| [7565] | 264 | |
|---|
| [8163] | 265 | /* check for timed out pings and remove those guys from |
|---|
| 266 | * the server list |
|---|
| 267 | */ |
|---|
| 268 | helper_cleanupServers(); |
|---|
| 269 | |
|---|
| 270 | |
|---|
| [7743] | 271 | /* create an iterator for the loop */ |
|---|
| 272 | enet_host_service( this->server, event, 100 ); |
|---|
| [7611] | 273 | |
|---|
| [7743] | 274 | /* check what type of event it is and react accordingly */ |
|---|
| 275 | switch (event->type) |
|---|
| 276 | { /* new connection */ |
|---|
| 277 | case ENET_EVENT_TYPE_CONNECT: |
|---|
| 278 | eventConnect( event ); break; |
|---|
| [7565] | 279 | |
|---|
| [7743] | 280 | /* disconnect */ |
|---|
| 281 | case ENET_EVENT_TYPE_DISCONNECT: |
|---|
| 282 | eventDisconnect( event ); break; |
|---|
| 283 | |
|---|
| 284 | /* incoming data */ |
|---|
| 285 | case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break; |
|---|
| 286 | default: break; |
|---|
| [7590] | 287 | } |
|---|
| [7565] | 288 | |
|---|
| [7590] | 289 | /* done */ |
|---|
| 290 | return 0; |
|---|
| 291 | } |
|---|
| [7565] | 292 | |
|---|
| [7590] | 293 | /* constructor */ |
|---|
| 294 | MasterServer::MasterServer() |
|---|
| 295 | { |
|---|
| 296 | /***** INITIALIZE NETWORKING *****/ |
|---|
| 297 | if( enet_initialize () != 0) |
|---|
| [7611] | 298 | { COUT(1) << "An error occurred while initializing ENet.\n"; |
|---|
| [7590] | 299 | exit( EXIT_FAILURE ); |
|---|
| 300 | } |
|---|
| [7565] | 301 | |
|---|
| [7590] | 302 | /* register deinitialization */ |
|---|
| 303 | atexit( enet_deinitialize ); |
|---|
| [7565] | 304 | |
|---|
| [7729] | 305 | /* set the quit flag to false */ |
|---|
| 306 | this->quit = false; |
|---|
| 307 | |
|---|
| [7590] | 308 | /* Bind the server to the default localhost and port ORX_MSERVER_PORT */ |
|---|
| 309 | this->address.host = ENET_HOST_ANY; |
|---|
| 310 | this->address.port = ORX_MSERVER_PORT; |
|---|
| [7589] | 311 | |
|---|
| [7590] | 312 | /* create a host with the above settings (the last two 0 mean: accept |
|---|
| 313 | * any input/output bandwidth */ |
|---|
| 314 | this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS, |
|---|
| [7801] | 315 | ORX_MSERVER_MAXCHANS, 0, 0 ); |
|---|
| 316 | assert(this->server); |
|---|
| [7590] | 317 | |
|---|
| 318 | /* see if creation worked */ |
|---|
| 319 | if( !this->server ) |
|---|
| [7611] | 320 | { COUT(1) << |
|---|
| 321 | "An error occurred while trying to create an ENet server host.\n"; |
|---|
| 322 | exit( EXIT_FAILURE ); |
|---|
| [7590] | 323 | } |
|---|
| [7565] | 324 | |
|---|
| [7611] | 325 | /***** INITIALIZE GAME SERVER AND PEER LISTS *****/ |
|---|
| 326 | this->peers = new PeerList(); |
|---|
| [7743] | 327 | |
|---|
| 328 | /* tell people we're now initialized */ |
|---|
| 329 | COUT(0) << "MasterServer initialized, waiting for connections.\n"; |
|---|
| [7565] | 330 | } |
|---|
| 331 | |
|---|
| [7590] | 332 | /* destructor */ |
|---|
| 333 | MasterServer::~MasterServer() |
|---|
| 334 | { |
|---|
| 335 | /***** CLEANUP PROCESS *****/ |
|---|
| 336 | /* terminate all networking connections */ |
|---|
| 337 | enet_host_destroy( this->server ); |
|---|
| [7565] | 338 | |
|---|
| [7611] | 339 | /* free all used memory */ |
|---|
| [7590] | 340 | /* clear the list of connected game servers */ |
|---|
| 341 | /* clear the list of connected game clients */ |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | /* end of namespace */ |
|---|
| 345 | } |
|---|