| 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 | |
|---|
| 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[INTSIZE]; |
|---|
| 88 | |
|---|
| 89 | assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE ); |
|---|
| 90 | |
|---|
| 91 | MessageManager::getInstance()->sendMessage( MSGID_PROXY_NEWCLIENT, data, INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * this is the handler for proxy signals: new clients |
|---|
| 97 | * |
|---|
| 98 | * @param messageType the type of the message |
|---|
| 99 | * @param data message data |
|---|
| 100 | * @param dataLength length of the message data |
|---|
| 101 | * @param someData some other atteched data |
|---|
| 102 | * @param senderId id of the sender client |
|---|
| 103 | * @param destinationId id of the destination client |
|---|
| 104 | * @return true if succeeded |
|---|
| 105 | */ |
|---|
| 106 | bool ProxyControl::messageHandlerNewClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ) |
|---|
| 107 | { |
|---|
| 108 | // body data length correct? |
|---|
| 109 | if ( dataLength != INTSIZE ) |
|---|
| 110 | { |
|---|
| 111 | PRINTF(2)("new client message has wrong size: %d\n", dataLength ); |
|---|
| 112 | return true; |
|---|
| 113 | } |
|---|
| 114 | // read the userId fromt he message body |
|---|
| 115 | int newClientId = 0; |
|---|
| 116 | assert( Converter::byteArrayToInt( data, &newClientId) == INTSIZE ); |
|---|
| 117 | |
|---|
| 118 | PRINTF(0)("Got Signal: from %i new player arrived with userId: %i\n", senderId, newClientId); |
|---|
| 119 | // part for the master server |
|---|
| 120 | if( SharedNetworkData::getInstance()->isMasterServer()) |
|---|
| 121 | { |
|---|
| 122 | // we now create the new player ship and stuff... |
|---|
| 123 | NetworkGameManager::getInstance()->signalNewPlayer(newClientId); |
|---|
| 124 | } |
|---|
| 125 | else if(SharedNetworkData::getInstance()->isProxyServerActive()) |
|---|
| 126 | { |
|---|
| 127 | |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | return true; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * signals client disconnect |
|---|
| 137 | * @param userId userId of the old client |
|---|
| 138 | */ |
|---|
| 139 | void ProxyControl::signalLeaveClient(int userId) |
|---|
| 140 | { |
|---|
| 141 | PRINTF(0)("Signaling new Client: %i\n", userId); |
|---|
| 142 | // make sure we are a proxy server |
|---|
| 143 | assert(SharedNetworkData::getInstance()->isProxyServerActive()); |
|---|
| 144 | |
|---|
| 145 | byte data[INTSIZE]; |
|---|
| 146 | |
|---|
| 147 | assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE ); |
|---|
| 148 | |
|---|
| 149 | MessageManager::getInstance()->sendMessage( MSGID_PROXY_LEAVECLIENT, data, INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * this is the handler for proxy signals: removing clients |
|---|
| 155 | * |
|---|
| 156 | * @param messageType the type of the message |
|---|
| 157 | * @param data message data |
|---|
| 158 | * @param dataLength length of the message data |
|---|
| 159 | * @param someData some other atteched data |
|---|
| 160 | * @param senderId id of the sender client |
|---|
| 161 | * @param destinationId id of the destination client |
|---|
| 162 | * @return true if succeeded |
|---|
| 163 | */ |
|---|
| 164 | bool ProxyControl::messageHandlerLeaveClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId ) |
|---|
| 165 | { |
|---|
| 166 | // body data length correct? |
|---|
| 167 | if ( dataLength != INTSIZE ) |
|---|
| 168 | { |
|---|
| 169 | PRINTF(2)("leave client message has wrong size: %d\n", dataLength ); |
|---|
| 170 | return true; |
|---|
| 171 | } |
|---|
| 172 | // read the userId fromt he message body |
|---|
| 173 | int leaveClientId = 0; |
|---|
| 174 | assert( Converter::byteArrayToInt( data, &leaveClientId) == INTSIZE ); |
|---|
| 175 | |
|---|
| 176 | PRINTF(0)("Got Signal: from %i new player left with userId: %i\n", senderId, leaveClientId); |
|---|
| 177 | // part for the master server |
|---|
| 178 | if( SharedNetworkData::getInstance()->isMasterServer()) |
|---|
| 179 | { |
|---|
| 180 | // we now create the new player ship and stuff... |
|---|
| 181 | NetworkGameManager::getInstance()->signalLeftPlayer(leaveClientId); |
|---|
| 182 | } |
|---|
| 183 | else if(SharedNetworkData::getInstance()->isProxyServerActive()) |
|---|
| 184 | { |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | return true; |
|---|
| 189 | } |
|---|