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