| 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 |  | 
|---|
| 17 | #include "class_list.h" | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | #include "player.h" | 
|---|
| 21 | #include "state.h" | 
|---|
| 22 | #include "shared_network_data.h" | 
|---|
| 23 | #include "network_game_manager.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "converter.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "preferences.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include "debug.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "monitor/network_monitor.h" | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | ProxyControl* ProxyControl::singletonRef = NULL; | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | /** | 
|---|
| 38 |  * constructor | 
|---|
| 39 |  */ | 
|---|
| 40 | ProxyControl::ProxyControl() | 
|---|
| 41 | { | 
|---|
| 42 |   this->setClassID( CL_PROXY_CONTROL, "ProxyControl" ); | 
|---|
| 43 |  | 
|---|
| 44 |   MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_NEWCLIENT, messageHandlerNewClient, NULL ); | 
|---|
| 45 |   MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_LEAVECLIENT, messageHandlerLeaveClient, NULL ); | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 |   PRINTF(0)("ProxyControl created\n"); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | /** | 
|---|
| 53 |  * standard deconstructor | 
|---|
| 54 |  */ | 
|---|
| 55 | ProxyControl::~ProxyControl() | 
|---|
| 56 | { | 
|---|
| 57 |   ProxyControl::singletonRef = NULL; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 |  /** | 
|---|
| 62 |  * override this function to be notified on change | 
|---|
| 63 |  * of your registred variables. | 
|---|
| 64 |  * @param id id's which have changed | 
|---|
| 65 |   */ | 
|---|
| 66 | void ProxyControl::varChangeHandler( std::list< int > & id ) | 
|---|
| 67 | { | 
|---|
| 68 | //   if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() ) | 
|---|
| 69 | //   { | 
|---|
| 70 | //     this->setPlayableUniqueId( this->playableUniqueId ); | 
|---|
| 71 | // | 
|---|
| 72 | //     PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID()); | 
|---|
| 73 | //   } | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | /** | 
|---|
| 78 |  *  signals new client connected to this local proxy | 
|---|
| 79 |  * @param userId userId of the new client | 
|---|
| 80 |  */ | 
|---|
| 81 | void ProxyControl::signalNewClient(int userId) | 
|---|
| 82 | { | 
|---|
| 83 |   PRINTF(0)("Signaling new Client: %i\n", userId); | 
|---|
| 84 |   // make sure we are a proxy server | 
|---|
| 85 |   assert(SharedNetworkData::getInstance()->isProxyServerActive()); | 
|---|
| 86 |  | 
|---|
| 87 |   byte data[2 * INTSIZE]; | 
|---|
| 88 |   // write the userId in the message | 
|---|
| 89 |   assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE ); | 
|---|
| 90 |   // and the ip as an int | 
|---|
| 91 |  | 
|---|
| 92 |   assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE ); | 
|---|
| 93 |  | 
|---|
| 94 |   MessageManager::getInstance()->sendMessage( MSGID_PROXY_NEWCLIENT, data, INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | /** | 
|---|
| 99 |  * this is the handler for proxy signals: new clients | 
|---|
| 100 |  * | 
|---|
| 101 |  * @param messageType the type of the message | 
|---|
| 102 |  * @param data message data | 
|---|
| 103 |  * @param dataLength length of the message data | 
|---|
| 104 |  * @param someData some other atteched data | 
|---|
| 105 |  * @param senderId id of the sender client | 
|---|
| 106 |  * @param destinationId id of the destination client | 
|---|
| 107 |  * @return true if succeeded | 
|---|
| 108 |  */ | 
|---|
| 109 | bool ProxyControl::messageHandlerNewClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  ) | 
|---|
| 110 | { | 
|---|
| 111 |   // body data length correct? | 
|---|
| 112 |   if ( dataLength != INTSIZE ) | 
|---|
| 113 |   { | 
|---|
| 114 |     PRINTF(2)("new client message has wrong size: %d\n", dataLength ); | 
|---|
| 115 |     return true; | 
|---|
| 116 |   } | 
|---|
| 117 |   // read the userId fromt he message body | 
|---|
| 118 |   int newClientId = 0; | 
|---|
| 119 |   assert( Converter::byteArrayToInt( data, &newClientId) == INTSIZE ); | 
|---|
| 120 |  | 
|---|
| 121 |   PRINTF(0)("Got Signal: from %i new player arrived with userId: %i\n", senderId, newClientId); | 
|---|
| 122 |   // part for the master server | 
|---|
| 123 |   if( SharedNetworkData::getInstance()->isMasterServer()) | 
|---|
| 124 |   { | 
|---|
| 125 |     // we now create the new player ship and stuff... | 
|---|
| 126 |     NetworkGameManager::getInstance()->signalNewPlayer(newClientId); | 
|---|
| 127 |   } | 
|---|
| 128 |   else if(SharedNetworkData::getInstance()->isProxyServerActive()) | 
|---|
| 129 |   { | 
|---|
| 130 |  | 
|---|
| 131 |   } | 
|---|
| 132 |  | 
|---|
| 133 |   return true; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 | /** | 
|---|
| 139 |  *  signals client disconnect | 
|---|
| 140 |  * @param userId userId of the old client | 
|---|
| 141 |  */ | 
|---|
| 142 | void ProxyControl::signalLeaveClient(int userId) | 
|---|
| 143 | { | 
|---|
| 144 |   PRINTF(0)("Signaling new Client: %i\n", userId); | 
|---|
| 145 |   // make sure we are a proxy server | 
|---|
| 146 |   assert(SharedNetworkData::getInstance()->isProxyServerActive()); | 
|---|
| 147 |  | 
|---|
| 148 |   byte data[INTSIZE]; | 
|---|
| 149 |  | 
|---|
| 150 |   assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE ); | 
|---|
| 151 |  | 
|---|
| 152 |   MessageManager::getInstance()->sendMessage( MSGID_PROXY_LEAVECLIENT, data, INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 | /** | 
|---|
| 157 |  * this is the handler for proxy signals: removing clients | 
|---|
| 158 |  * | 
|---|
| 159 |  * @param messageType the type of the message | 
|---|
| 160 |  * @param data message data | 
|---|
| 161 |  * @param dataLength length of the message data | 
|---|
| 162 |  * @param someData some other atteched data | 
|---|
| 163 |  * @param senderId id of the sender client | 
|---|
| 164 |  * @param destinationId id of the destination client | 
|---|
| 165 |  * @return true if succeeded | 
|---|
| 166 |  */ | 
|---|
| 167 | bool ProxyControl::messageHandlerLeaveClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  ) | 
|---|
| 168 | { | 
|---|
| 169 |   // body data length correct? | 
|---|
| 170 |   if ( dataLength != INTSIZE ) | 
|---|
| 171 |   { | 
|---|
| 172 |     PRINTF(2)("leave client message has wrong size: %d\n", dataLength ); | 
|---|
| 173 |     return true; | 
|---|
| 174 |   } | 
|---|
| 175 |   // read the userId fromt he message body | 
|---|
| 176 |   int leaveClientId = 0; | 
|---|
| 177 |   assert( Converter::byteArrayToInt( data, &leaveClientId) == INTSIZE ); | 
|---|
| 178 |  | 
|---|
| 179 |   PRINTF(0)("Got Signal: from %i new player left with userId: %i\n", senderId, leaveClientId); | 
|---|
| 180 |   // part for the master server | 
|---|
| 181 |   if( SharedNetworkData::getInstance()->isMasterServer()) | 
|---|
| 182 |   { | 
|---|
| 183 |     // we now create the new player ship and stuff... | 
|---|
| 184 |     NetworkGameManager::getInstance()->signalLeftPlayer(leaveClientId); | 
|---|
| 185 |   } | 
|---|
| 186 |   else if(SharedNetworkData::getInstance()->isProxyServerActive()) | 
|---|
| 187 |   { | 
|---|
| 188 |  | 
|---|
| 189 |   } | 
|---|
| 190 |  | 
|---|
| 191 |   return true; | 
|---|
| 192 | } | 
|---|