[9537] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include "proxy_control.h" |
---|
| 16 | |
---|
[9600] | 17 | #include "shell_command.h" |
---|
[9537] | 18 | |
---|
| 19 | #include "state.h" |
---|
| 20 | #include "shared_network_data.h" |
---|
[9600] | 21 | #include "network_manager.h" |
---|
[9541] | 22 | #include "network_game_manager.h" |
---|
[9584] | 23 | #include "ip.h" |
---|
| 24 | #include "peer_info.h" |
---|
[9654] | 25 | #include "network_stream.h" |
---|
[9537] | 26 | |
---|
| 27 | #include "converter.h" |
---|
| 28 | |
---|
[9869] | 29 | #include "parser/preferences/preferences.h" |
---|
[9537] | 30 | |
---|
| 31 | #include "debug.h" |
---|
| 32 | |
---|
[9582] | 33 | #include "monitor/network_monitor.h" |
---|
[9537] | 34 | |
---|
| 35 | |
---|
[9603] | 36 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_NETWORK |
---|
| 37 | |
---|
[9537] | 38 | ProxyControl* ProxyControl::singletonRef = NULL; |
---|
| 39 | |
---|
| 40 | |
---|
[9625] | 41 | SHELL_COMMAND(forceReconnect, ProxyControl, forceReconnectionShell); |
---|
[9869] | 42 | ObjectListDefinition(ProxyControl); |
---|
[9600] | 43 | |
---|
[9537] | 44 | /** |
---|
| 45 | * constructor |
---|
| 46 | */ |
---|
| 47 | ProxyControl::ProxyControl() |
---|
| 48 | { |
---|
[9869] | 49 | this->registerObject(this, ProxyControl::_objectList); |
---|
[9537] | 50 | |
---|
[9638] | 51 | this->setSynchronized(false); |
---|
| 52 | |
---|
[9538] | 53 | MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_NEWCLIENT, messageHandlerNewClient, NULL ); |
---|
[9572] | 54 | MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_LEAVECLIENT, messageHandlerLeaveClient, NULL ); |
---|
[9599] | 55 | MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_COMMAND, messageHandlerCommand, NULL ); |
---|
[9537] | 56 | |
---|
[9572] | 57 | |
---|
[9537] | 58 | PRINTF(0)("ProxyControl created\n"); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * standard deconstructor |
---|
| 64 | */ |
---|
| 65 | ProxyControl::~ProxyControl() |
---|
| 66 | { |
---|
| 67 | ProxyControl::singletonRef = NULL; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * override this function to be notified on change |
---|
| 73 | * of your registred variables. |
---|
| 74 | * @param id id's which have changed |
---|
| 75 | */ |
---|
| 76 | void ProxyControl::varChangeHandler( std::list< int > & id ) |
---|
| 77 | { |
---|
| 78 | // if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() ) |
---|
| 79 | // { |
---|
| 80 | // this->setPlayableUniqueId( this->playableUniqueId ); |
---|
| 81 | // |
---|
| 82 | // PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID()); |
---|
| 83 | // } |
---|
| 84 | } |
---|
| 85 | |
---|
[9538] | 86 | |
---|
| 87 | /** |
---|
| 88 | * signals new client connected to this local proxy |
---|
[9584] | 89 | * |
---|
| 90 | * byte 0 - 3 : userId |
---|
| 91 | * byte 4 - 7 : ip address (IPaddress.host) |
---|
| 92 | * |
---|
[9538] | 93 | * @param userId userId of the new client |
---|
| 94 | */ |
---|
| 95 | void ProxyControl::signalNewClient(int userId) |
---|
| 96 | { |
---|
[9553] | 97 | PRINTF(0)("Signaling new Client: %i\n", userId); |
---|
[9538] | 98 | // make sure we are a proxy server |
---|
| 99 | assert(SharedNetworkData::getInstance()->isProxyServerActive()); |
---|
| 100 | |
---|
[9582] | 101 | byte data[2 * INTSIZE]; |
---|
| 102 | // write the userId in the message |
---|
| 103 | assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE ); |
---|
| 104 | // and the ip as an int |
---|
[9583] | 105 | PeerInfo* pInfo = SharedNetworkData::getInstance()->getNetworkMonitor()->getPeerByUserId(userId); |
---|
[9584] | 106 | assert(pInfo != NULL); |
---|
| 107 | assert( Converter::intToByteArray( pInfo->ip.host(), data + INTSIZE, INTSIZE ) == INTSIZE ); |
---|
[9539] | 108 | |
---|
[9584] | 109 | MessageManager::getInstance()->sendMessage( MSGID_PROXY_NEWCLIENT, data, 2*INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); |
---|
[9538] | 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * this is the handler for proxy signals: new clients |
---|
| 115 | * |
---|
| 116 | * @param messageType the type of the message |
---|
| 117 | * @param data message data |
---|
| 118 | * @param dataLength length of the message data |
---|
| 119 | * @param someData some other atteched data |
---|
| 120 | * @param senderId id of the sender client |
---|
| 121 | * @param destinationId id of the destination client |
---|
| 122 | * @return true if succeeded |
---|
| 123 | */ |
---|
| 124 | bool ProxyControl::messageHandlerNewClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ) |
---|
| 125 | { |
---|
[9541] | 126 | // body data length correct? |
---|
[9585] | 127 | if ( dataLength != 2 * INTSIZE ) |
---|
[9541] | 128 | { |
---|
| 129 | PRINTF(2)("new client message has wrong size: %d\n", dataLength ); |
---|
| 130 | return true; |
---|
| 131 | } |
---|
| 132 | // read the userId fromt he message body |
---|
[9555] | 133 | int newClientId = 0; |
---|
| 134 | assert( Converter::byteArrayToInt( data, &newClientId) == INTSIZE ); |
---|
[9584] | 135 | // now read the ip address |
---|
| 136 | int ipHost = 0; |
---|
| 137 | assert( Converter::byteArrayToInt( data + INTSIZE, &ipHost) == INTSIZE ); |
---|
[9541] | 138 | |
---|
[9584] | 139 | // register the new node at the network monitor |
---|
| 140 | NetworkMonitor* netMon = SharedNetworkData::getInstance()->getNetworkMonitor(); |
---|
[9589] | 141 | NetworkNode* nNode = netMon->getNodeByUserId(senderId); // this gets the proxy server who sent the msg |
---|
[9637] | 142 | if( nNode == NULL) |
---|
| 143 | PRINTF(0)("Couldn't find node %i! Error\n", senderId); |
---|
[9584] | 144 | PeerInfo* pInfo = new PeerInfo(); |
---|
| 145 | pInfo->bLocal = false; |
---|
[9585] | 146 | pInfo->ip = IP(ipHost, 9999); |
---|
[9584] | 147 | pInfo->nodeType = NET_CLIENT; |
---|
[9586] | 148 | pInfo->userId = newClientId; |
---|
[9589] | 149 | netMon->addNode(nNode, pInfo); |
---|
[9584] | 150 | |
---|
[9637] | 151 | PRINTF(0)("Got Signal: from %i new player arrived with userId: %i and ip: %s on %i\n", senderId, newClientId, pInfo->ip.ipString().c_str(), senderId); |
---|
[9538] | 152 | // part for the master server |
---|
| 153 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
| 154 | { |
---|
[9541] | 155 | // we now create the new player ship and stuff... |
---|
[9555] | 156 | NetworkGameManager::getInstance()->signalNewPlayer(newClientId); |
---|
[9538] | 157 | } |
---|
| 158 | |
---|
| 159 | return true; |
---|
| 160 | } |
---|
[9570] | 161 | |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * signals client disconnect |
---|
| 166 | * @param userId userId of the old client |
---|
| 167 | */ |
---|
| 168 | void ProxyControl::signalLeaveClient(int userId) |
---|
| 169 | { |
---|
| 170 | PRINTF(0)("Signaling new Client: %i\n", userId); |
---|
| 171 | // make sure we are a proxy server |
---|
| 172 | assert(SharedNetworkData::getInstance()->isProxyServerActive()); |
---|
| 173 | |
---|
| 174 | byte data[INTSIZE]; |
---|
| 175 | |
---|
| 176 | assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE ); |
---|
| 177 | |
---|
| 178 | MessageManager::getInstance()->sendMessage( MSGID_PROXY_LEAVECLIENT, data, INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * this is the handler for proxy signals: removing clients |
---|
| 184 | * |
---|
| 185 | * @param messageType the type of the message |
---|
| 186 | * @param data message data |
---|
| 187 | * @param dataLength length of the message data |
---|
| 188 | * @param someData some other atteched data |
---|
| 189 | * @param senderId id of the sender client |
---|
| 190 | * @param destinationId id of the destination client |
---|
| 191 | * @return true if succeeded |
---|
| 192 | */ |
---|
| 193 | bool ProxyControl::messageHandlerLeaveClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ) |
---|
| 194 | { |
---|
| 195 | // body data length correct? |
---|
| 196 | if ( dataLength != INTSIZE ) |
---|
| 197 | { |
---|
| 198 | PRINTF(2)("leave client message has wrong size: %d\n", dataLength ); |
---|
| 199 | return true; |
---|
| 200 | } |
---|
| 201 | // read the userId fromt he message body |
---|
| 202 | int leaveClientId = 0; |
---|
| 203 | assert( Converter::byteArrayToInt( data, &leaveClientId) == INTSIZE ); |
---|
| 204 | |
---|
[9586] | 205 | // remove the node from the network monitor |
---|
| 206 | NetworkMonitor* netMon = SharedNetworkData::getInstance()->getNetworkMonitor(); |
---|
[9589] | 207 | NetworkNode* nNode = netMon->getNodeByUserId(senderId); // this gets the proxy server who sent the msg |
---|
| 208 | netMon->removeNode(nNode, netMon->getPeerByUserId(leaveClientId)); |
---|
[9586] | 209 | |
---|
[9599] | 210 | PRINTF(0)("Got Signal: from %i player left with userId: %i\n", senderId, leaveClientId); |
---|
[9570] | 211 | // part for the master server |
---|
| 212 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
| 213 | { |
---|
| 214 | // we now create the new player ship and stuff... |
---|
[9571] | 215 | NetworkGameManager::getInstance()->signalLeftPlayer(leaveClientId); |
---|
[9570] | 216 | } |
---|
| 217 | else if(SharedNetworkData::getInstance()->isProxyServerActive()) |
---|
| 218 | { |
---|
| 219 | |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | return true; |
---|
| 223 | } |
---|
[9599] | 224 | |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * forces a client to reconnect to another server |
---|
| 229 | * @param userId the userId of the client/user :D |
---|
| 230 | * @param newAddress the addresss the client should connect to (fully quali dns) |
---|
| 231 | */ |
---|
| 232 | void ProxyControl::forceReconnection(int userId, const std::string& newAddress) |
---|
| 233 | { |
---|
| 234 | IP ipAddr = IP(newAddress, 9999); |
---|
| 235 | this->forceReconnection(userId, newAddress); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | |
---|
[9603] | 239 | |
---|
[9600] | 240 | /** |
---|
[9603] | 241 | * thumb command: staticly reconnect node 10 to node 0 |
---|
| 242 | */ |
---|
| 243 | void ProxyControl::forceReconnectionShellThumb() |
---|
| 244 | { |
---|
| 245 | this->forceReconnectionShell(10, 0); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
[9600] | 249 | * a shell command wrapper |
---|
| 250 | * @param userId the userId of the client/user :D |
---|
| 251 | * @param serverId the userId of the server to connect to |
---|
| 252 | */ |
---|
| 253 | void ProxyControl::forceReconnectionShell(int userId, int serverId) |
---|
| 254 | { |
---|
[9625] | 255 | PRINTF(0)("shell reconnectoin command: %i to %i\n", userId, serverId); |
---|
| 256 | |
---|
[9600] | 257 | this->forceReconnection(userId, serverId); |
---|
| 258 | } |
---|
[9599] | 259 | |
---|
[9600] | 260 | |
---|
[9599] | 261 | /** |
---|
| 262 | * forces a client to reconnect to another server |
---|
| 263 | * @param userId the userId of the client/user :D |
---|
| 264 | * @param serverId the userId of the server to connect to |
---|
| 265 | */ |
---|
| 266 | void ProxyControl::forceReconnection(int userId, int serverId) |
---|
| 267 | { |
---|
| 268 | PeerInfo* serverInfo = SharedNetworkData::getInstance()->getNetworkMonitor()->getPeerByUserId(serverId); |
---|
| 269 | if( serverInfo == NULL) |
---|
| 270 | { |
---|
[9625] | 271 | PRINTF(0)("There is no server with userId %i registered in this network. Check the uid again\n", serverId); |
---|
[9599] | 272 | return; |
---|
| 273 | } |
---|
[9625] | 274 | else if( serverInfo->isClient()) |
---|
[9599] | 275 | { |
---|
[9625] | 276 | PRINTF(0)("You can't connec to to a client (userId %i)\n", serverId); |
---|
[9599] | 277 | return; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
[9654] | 281 | this->forceReconnection(userId, serverId, serverInfo->ip); |
---|
[9599] | 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | /** |
---|
| 286 | * forces a client to reconnect to another server |
---|
| 287 | * @param userId the userId of the client/user :D |
---|
| 288 | * @param newAddress the addresss the client should connect to |
---|
| 289 | */ |
---|
[9654] | 290 | void ProxyControl::forceReconnection(int userId, int serverId, IP newAddress) |
---|
[9599] | 291 | { |
---|
| 292 | PRINTF(0)("forcing reconnection: userId %i to %s\n", userId, newAddress.ipString().c_str()); |
---|
| 293 | // make sure we are a proxy server |
---|
| 294 | |
---|
| 295 | if( SharedNetworkData::getInstance()->isClient()) |
---|
| 296 | { |
---|
[9602] | 297 | PRINTF(0)("I am client, got no right to force reconnection\n"); |
---|
[9599] | 298 | return; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | byte data[3 * INTSIZE]; |
---|
| 303 | |
---|
| 304 | // write type of message |
---|
| 305 | int type = PXY_RECONNECT; |
---|
| 306 | assert( Converter::intToByteArray( type, data, INTSIZE ) == INTSIZE ); |
---|
| 307 | // write the userId in the message |
---|
| 308 | assert( Converter::intToByteArray( userId, data + INTSIZE, INTSIZE ) == INTSIZE ); |
---|
| 309 | // and the ip as an int |
---|
| 310 | PeerInfo* pInfo = SharedNetworkData::getInstance()->getNetworkMonitor()->getPeerByUserId(userId); |
---|
| 311 | if( pInfo == NULL) |
---|
| 312 | { |
---|
[9625] | 313 | PRINTF(0)("There is no client with userId %i registered in this network. Check the uid again\n", userId); |
---|
[9599] | 314 | return; |
---|
| 315 | } |
---|
| 316 | else if( pInfo->isMasterServer() || pInfo->isProxyServerActive()) |
---|
| 317 | { |
---|
| 318 | PRINTF(0)("You cannont reconnect a %s, abording\n", pInfo->getNodeTypeString().c_str()); |
---|
| 319 | return; |
---|
| 320 | } |
---|
| 321 | assert( Converter::intToByteArray( newAddress.host(), data + 2 * INTSIZE, INTSIZE ) == INTSIZE ); |
---|
| 322 | |
---|
[9625] | 323 | PRINTF(0)("Sending reconnection command to: %i\n", userId); |
---|
[9654] | 324 | // send the reconnection signal to the client who must reconnect |
---|
[9638] | 325 | MessageManager::getInstance()->sendMessage( MSGID_PROXY_COMMAND, data, 3*INTSIZE, RT_USER, userId, MP_HIGHBANDWIDTH ); |
---|
[9654] | 326 | // and to the server who needs to accept the new client |
---|
| 327 | MessageManager::getInstance()->sendMessage( MSGID_PROXY_COMMAND, data, 3*INTSIZE, RT_USER, serverId, MP_HIGHBANDWIDTH ); |
---|
[9599] | 328 | } |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | * this is the handler for proxy commands |
---|
| 333 | * |
---|
| 334 | * @param messageType the type of the message |
---|
| 335 | * @param data message data |
---|
| 336 | * @param dataLength length of the message data |
---|
| 337 | * @param someData some other atteched data |
---|
| 338 | * @param senderId id of the sender client |
---|
| 339 | * @param destinationId id of the destination client |
---|
| 340 | * @return true if succeeded |
---|
| 341 | */ |
---|
| 342 | bool ProxyControl::messageHandlerCommand( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ) |
---|
| 343 | { |
---|
| 344 | // body data length correct? |
---|
| 345 | if ( dataLength != 3 * INTSIZE ) |
---|
| 346 | { |
---|
| 347 | PRINTF(1)("leave client message has wrong size: %d\n", dataLength ); |
---|
| 348 | return true; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | // read the command type |
---|
| 352 | int type = 0; |
---|
| 353 | assert( Converter::byteArrayToInt( data, &type) == INTSIZE ); |
---|
| 354 | PRINTF(0)("got command from %i with code %i\n", senderId, type); |
---|
| 355 | |
---|
| 356 | // now distingush all different sorts of commands |
---|
| 357 | switch( type) |
---|
| 358 | { |
---|
| 359 | case PXY_RECONNECT: |
---|
| 360 | { |
---|
| 361 | // now read the user id |
---|
| 362 | int userId; |
---|
| 363 | assert( Converter::byteArrayToInt( data + INTSIZE, &userId) == INTSIZE ); |
---|
| 364 | // and read the dest address |
---|
| 365 | int ipHost = 0; |
---|
| 366 | assert( Converter::byteArrayToInt( data + INTSIZE, &ipHost) == INTSIZE ); |
---|
| 367 | |
---|
[9638] | 368 | PRINTF(0)("Got reconnection command from %i: reconnect!\n", senderId); |
---|
[9599] | 369 | // handle it |
---|
[9638] | 370 | if( SharedNetworkData::getInstance()->getHostID() == userId) |
---|
[9599] | 371 | { |
---|
[9654] | 372 | if( SharedNetworkData::getInstance()->isClient()) |
---|
| 373 | SharedNetworkData::getInstance()->getDefaultSyncStream()->softReconnectToServer(userId, IP(ipHost, 9999)); |
---|
| 374 | else |
---|
| 375 | SharedNetworkData::getInstance()->getDefaultSyncStream()->prepareSoftConnection(userId); |
---|
[9599] | 376 | } |
---|
| 377 | break; |
---|
| 378 | } |
---|
| 379 | default: |
---|
| 380 | PRINTF(0)("Command not known with id %i\n", type); |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | |
---|
| 384 | |
---|
| 385 | return true; |
---|
| 386 | } |
---|
| 387 | |
---|