Changeset 9347 in orxonox.OLD for branches/proxy/src/lib/network
- Timestamp:
- Jul 20, 2006, 11:43:27 AM (18 years ago)
- Location:
- branches/proxy/src/lib/network
- Files:
-
- 2 deleted
- 31 edited
- 16 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/proxy/src/lib/network/Makefile.am
r8623 r9347 8 8 shared_network_data.cc \ 9 9 network_socket.cc \ 10 connection_monitor.cc \11 10 network_stream.cc \ 12 11 data_stream.cc \ … … 20 19 network_game_manager.cc \ 21 20 converter.cc \ 21 peer_info.cc \ 22 22 message_manager.cc \ 23 23 network_log.cc \ … … 25 25 player_stats.cc \ 26 26 udp_broadcast.cc \ 27 \ 28 proxy/proxy_settings.cc \ 29 \ 30 monitor/connection_monitor.cc \ 31 monitor/network_monitor.cc \ 32 monitor/network_node.cc \ 27 33 \ 28 34 synchronizeable_var/synchronizeable_var.cc \ … … 33 39 synchronizeable_var/synchronizeable_float.cc \ 34 40 synchronizeable_var/synchronizeable_bool.cc \ 35 synchronizeable_var/synchronizeable_uint.cc 41 synchronizeable_var/synchronizeable_uint.cc \ 42 synchronizeable_var/synchronizeable_ip.cc \ 43 \ 44 ip.cc 36 45 37 46 … … 42 51 shared_network_data.h \ 43 52 network_socket.h \ 44 connection_monitor.h \45 53 network_stream.h \ 46 54 data_stream.h \ … … 54 62 network_game_manager.h \ 55 63 converter.h \ 64 peer_info.h \ 56 65 netdefs.h \ 57 66 message_manager.h \ … … 60 69 player_stats.h \ 61 70 udp_broadcast.h \ 71 \ 72 proxy/proxy_settings.cc \ 73 \ 74 monitor/connection_monitor.h \ 75 monitor/network_monitor.h \ 76 monitor/network_node.h \ 62 77 \ 63 78 synchronizeable_var/synchronizeable_var.h \ … … 68 83 synchronizeable_var/synchronizeable_float.h \ 69 84 synchronizeable_var/synchronizeable_bool.h \ 70 synchronizeable_var/synchronizeable_uint.h 85 synchronizeable_var/synchronizeable_uint.h \ 86 synchronizeable_var/synchronizeable_ip.h \ 87 \ 88 ip.h 71 89 72 90 -
branches/proxy/src/lib/network/handshake.cc
r9235 r9347 10 10 11 11 ### File Specific: 12 main-programmer: christoph13 co-programmer: 12 main-programmer: Christoph Renner (rennerc@ee.ethz.ch) 13 co-programmer: Patirck Boenzli (boenzlip@orxonox.ethz.ch) 14 14 */ 15 15 16 16 17 /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module18 For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput19 */20 17 #define DEBUG_MODULE_NETWORK 21 18 … … 25 22 #include "debug.h" 26 23 27 Handshake::Handshake( bool server, int clientId, int networkGameManagerId, int messageManagerId ) 24 25 26 27 28 Handshake::Handshake( int nodeType, int clientId, int networkGameManagerId, int messageManagerId ) 28 29 : Synchronizeable() 29 30 { … … 31 32 this->setClassID(CL_HANDSHAKE, "Handshake"); 32 33 33 this->setIsServer(server); 34 34 // register all variable handlers 35 35 orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId", PERMISSION_ALL ) ); 36 36 version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version", PERMISSION_ALL ) ); … … 42 42 errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString", PERMISSION_ALL ) ); 43 43 44 this->nodeTypeHandler = registerVarId( new SynchronizeableInt( &localState.nodeType, &remoteState.nodeType, "nodeType", PERMISSION_ALL ) ); 45 44 46 candel_id = registerVarId( new SynchronizeableInt( &localState.canDel, &remoteState.canDel, "canDel", PERMISSION_ALL ) ); 45 47 46 48 registerVar( new SynchronizeableString( &localState.preferedNickName, &remoteState.preferedNickName, "preferedNickName", PERMISSION_ALL ) ); 49 // now synchronize only two of the available proxy server addresses 50 registerVar( new SynchronizeableIP( &localState.proxy1, &remoteState.proxy1, "proxy server 1", PERMISSION_ALL ) ); 51 registerVar( new SynchronizeableIP( &localState.proxy2, &remoteState.proxy1, "proxy server 2", PERMISSION_ALL ) ); 47 52 53 // init the local state 48 54 localState.completed = 0; 49 55 localState.error = 0; … … 54 60 localState.orxId = _ORXONOX_ID; 55 61 localState.version = _ORXONOX_VERSION; 62 localState.nodeType = nodeType; 56 63 localState.canDel = 0; 64 localState.redirectProxy = 0; 65 localState.proxy1 = IP(0, 0); 66 localState.proxy2 = IP (0, 0); 57 67 68 69 // init the remote state 58 70 remoteState.completed = 0; 59 71 remoteState.error = 0; … … 64 76 remoteState.orxId = 0; 65 77 remoteState.version = 0; 78 remoteState.nodeType = NET_CLIENT; 66 79 remoteState.canDel = 0; 80 remoteState.redirectProxy = 0; 81 remoteState.proxy1 = IP(0, 0); 82 remoteState.proxy2 = IP(0, 0); 67 83 84 // activate the synchronization process 68 85 this->setSynchronized(true); 69 86 PRINTF(0)("Handshake created clientId = %d\n", clientId); 70 87 } 88 71 89 72 90 /** … … 78 96 for ( std::list<int>::iterator it = id.begin(); it != id.end(); it++ ) 79 97 { 80 if ( *it == orxId_handler ) 98 // orxonox id handler 99 if ( *it == this->orxId_handler ) 81 100 { 82 101 if ( remoteState.orxId != _ORXONOX_ID ) … … 87 106 continue; 88 107 } 89 90 108 } 91 109 92 if ( *it == version_handler ) 110 // orxonox version handler 111 if ( *it == this->version_handler ) 93 112 { 94 113 if ( remoteState.version != _ORXONOX_VERSION ) … … 99 118 continue; 100 119 } 101 102 120 } 103 121 122 // node type handler: for each node type there is a specific action to be taken 123 if ( *it == this->nodeTypeHandler) 124 { 125 if ( remoteState.nodeType == NET_MASTER_SERVER ) 126 { 127 continue; 128 } 129 else if( remoteState.nodeType == NET_PROXY_SERVER_ACTIVE) 130 { 131 continue; 132 } 133 else if( remoteState.nodeType == NET_CLIENT) 134 { 135 continue; 136 } 137 } 138 139 // cancel 104 140 if ( *it == candel_id ) 105 141 { … … 109 145 } 110 146 111 if (112 113 remoteState.version == _ORXONOX_VERSION114 )147 // handshake completed 148 if ( remoteState.orxId == _ORXONOX_ID && 149 remoteState.version == _ORXONOX_VERSION ) 150 { 115 151 localState.completed = 1; 152 } 116 153 } 117 154 -
branches/proxy/src/lib/network/handshake.h
r9235 r9347 1 1 /*! 2 2 * @file network_stream.h 3 * implementation of a network pipe 3 * implementation of a network pipe. This node handles the handshake of two network nodes and exchanges informationas 4 * vial to the connection setup. 5 * 6 * the local node only writes in the localState variable. the remoteState variable will be written from the remote network node 7 * so don't write into it! 4 8 */ 5 9 … … 8 12 9 13 #include "base_object.h" 14 #include "ip.h" 10 15 #include "synchronizeable.h" 11 16 12 #define _ORXONOX_ID 0xF91337A0 17 //!< a struct to save the handshakes to 18 struct HandshakeState 19 { 20 int orxId; //!< orxonox id 21 int version; //!< network protocol version 13 22 14 #define _ORXONOX_VERSION 1 23 int networkManagerId; //!< unique id of the network manager 24 int messageManagerId; //!< unique id of the message manager 25 int hostId; //!< host id 26 int nodeType; //!< type of the network node 15 27 16 struct HandshakeState { 17 int orxId; 18 int version; 19 20 int networkManagerId; 21 int messageManagerId; 22 int hostId; 23 24 int completed; 25 int canDel; 26 27 int error; 28 29 std::string errorString; 30 28 int completed; //!< true if completed 29 int canDel; //!< true if marked for deletion 30 31 int error; //!< error number 32 33 std::string errorString; //!< error string 34 31 35 //additional data 32 std::string preferedNickName; 36 std::string preferedNickName; //!< prefered nick name 37 38 int redirectProxy; //!< true if the client should reconnect to a proxy server (either 1 or 2 ) 39 IP proxy1; //!< ip address of the first proxy (0.0.0.0 of not available) 40 IP proxy2; //!< ip address of the second proxy (0.0.0.0 of not available) 33 41 }; 34 42 43 44 //!< the handshake itself with some interface functions 35 45 class Handshake : public Synchronizeable 36 46 { 47 37 48 public: 38 Handshake( bool server, int clientId = 0, int networkGameManagerId = 0, int messageManagerId = 0 ); 39 inline bool completed(){ return localState.completed != 0 && remoteState.completed != 0; } 40 inline bool ok(){ return localState.error == 0 && remoteState.error == 0; } 41 inline int getHostId(){ return remoteState.hostId; } 42 inline int getNetworkGameManagerId(){ return remoteState.networkManagerId; } 43 inline int getMessageManagerId(){ return remoteState.messageManagerId; } 44 inline void doReject( std::string reason ){ localState.error = 1; localState.errorString = "the server rejected your connection ["+ reason +"]"; } 45 inline bool canDel(){ return localState.canDel == 1 && remoteState.canDel == 1; } 46 inline bool allowDel(){ return localState.canDel == 1; } 47 inline void del(){ localState.canDel = 1; } 48 49 inline void setPreferedNickName( const std::string & nick ){ localState.preferedNickName = nick; } 49 Handshake( int nodeType, int clientId = 0, int networkGameManagerId = 0, int messageManagerId = 0 ); 50 51 52 /* functions indicating states of the handshake */ 53 /** @returns true if the handshake is completed */ 54 inline bool completed(){ return localState.completed != 0 && remoteState.completed != 0; } 55 /** @returns true if no error has occured until now */ 56 inline bool ok(){ return localState.error == 0 && remoteState.error == 0; } 57 /** stops the handshake and reject the other side with @param reason: string describing the reason */ 58 inline void doReject( std::string reason ){ localState.error = 1; localState.errorString = "the server rejected your connection ["+ reason +"]"; } 59 /** @returns true if the handshake is finished and the instances can be deleted */ 60 inline bool canDel(){ return localState.canDel == 1 && remoteState.canDel == 1; } 61 /** @returns true if the local state can be removed*/ 62 inline bool allowDel(){ return localState.canDel == 1; } 63 /** marks the handshake to be deleted */ 64 inline void del(){ localState.canDel = 1; } 65 66 67 /* the actual informations exchanged in the handshake */ 68 /** @returns the host id of the remote host */ 69 inline int getHostId(){ return remoteState.hostId; } 70 /** @returns the unique id of the network game manager*/ 71 inline int getNetworkGameManagerId(){ return remoteState.networkManagerId; } 72 /** @returns the unique id of the message manager */ 73 inline int getMessageManagerId(){ return remoteState.messageManagerId; } 74 /** @returns the node type of the remote host */ 75 inline int getRemoteNodeType() { return this->remoteState.nodeType; } 76 77 /** sets @param nick the prefereded nick name */ 78 inline void setPreferedNickName( const std::string & nick ){ localState.preferedNickName = nick; } 79 /** @returns the prefered nick name */ 50 80 inline std::string getPreferedNickName(){ return remoteState.preferedNickName; } 51 81 82 /** @returns if true the local client should reconnect to a proxy server from the proxy server list */ 83 inline bool redirect() { return this->remoteState.redirectProxy;} 84 /** @param flag: indicating if the client should be redirected */ 85 inline void setRedirect(bool flag) { this->localState.redirectProxy = flag; } 86 87 /** @param address: the address of the proxy server 1 if any */ 88 inline void setProxy1Address(IP address) { this->localState.proxy1 = address; } 89 /** @returns the address of the proxy server 1 if any */ 90 inline IP getProxy1Address() { return this->remoteState.proxy1; } 91 /** @param address: the address of the proxy server 2 if any */ 92 inline void setProxy2Address(IP address) { this->localState.proxy2 = address; } 93 /** @returns the address of the proxy server 2 if any */ 94 inline IP getProxy2Address() { return this->remoteState.proxy2; } 95 96 97 /* variable handler function */ 52 98 virtual void varChangeHandler( std::list<int> & id ); 53 99 100 54 101 private: 55 HandshakeState localState; 56 HandshakeState remoteState; 57 58 int netManId_handler; 59 int msgManId_handler; 60 int hostId_handler; 61 int completed_handler; 62 int error_handler; 63 int errorString_handler; 64 int orxId_handler; 65 int version_handler; 66 int candel_id; 102 HandshakeState localState; //!< the local handshake state 103 HandshakeState remoteState; //!< the remote handshake state 104 105 int orxId_handler; //!< orxonox id handler 106 int version_handler; //!< orxonox version id handler 107 int netManId_handler; //!< network manager handler 108 int msgManId_handler; //!< message manager handler 109 int hostId_handler; //!< host id handler 110 int nodeTypeHandler; //!< node type handler 111 112 int completed_handler; //!< handshake completion handler 113 int error_handler; //!< handshake error handler 114 int errorString_handler; //!< handshake error string handler 115 int candel_id; //!< handshake deletion handler 116 int nodeType; //!, the type of the network node 67 117 68 118 }; -
branches/proxy/src/lib/network/message_manager.cc
r9059 r9347 52 52 } 53 53 } 54 54 55 55 it->second.messages.clear(); 56 56 it->second.toAck.clear(); 57 57 } 58 58 59 59 messageQueue.clear(); 60 60 61 61 this->messageHandlerMap.clear(); 62 62 63 63 MessageManager::singletonRef = NULL; 64 64 } … … 82 82 int i = 0; 83 83 int n; 84 84 85 85 n = Converter::intToByteArray( messageQueue[userId].toAck.size(), data + i, maxLength ); 86 86 i += n; 87 87 assert( n == INTSIZE ); 88 88 89 89 for ( std::list<int>::iterator it = messageQueue[userId].toAck.begin(); it != messageQueue[userId].toAck.end(); it++) 90 90 { … … 93 93 assert( n == INTSIZE ); 94 94 } 95 95 96 96 messageQueue[userId].toAck.clear(); 97 97 98 98 n = Converter::intToByteArray( messageQueue[userId].messages.size(), data + i, maxLength ); 99 99 i += n; 100 100 assert( n == INTSIZE ); 101 101 102 102 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ ) 103 103 { … … 105 105 i += n; 106 106 assert( n == INTSIZE ); 107 107 108 108 n = Converter::intToByteArray( it->number, data + i, maxLength ); 109 109 i += n; 110 110 assert( n == INTSIZE ); 111 111 112 112 n = Converter::intToByteArray( it->messageId, data + i, maxLength ); 113 113 i += n; 114 114 assert( n == INTSIZE ); 115 115 116 116 assert( i + it->length <= maxLength ); 117 117 memcpy( data + i, it->data, it->length ); 118 118 i += it->length; 119 119 } 120 120 121 121 return i; 122 122 } … … 136 136 int i = 0; 137 137 int n; 138 138 139 139 int nAcks; 140 140 141 141 assert( i + INTSIZE <= length ); 142 142 n = Converter::byteArrayToInt( data + i, &nAcks ); 143 143 assert( n == INTSIZE ); 144 144 i += n; 145 145 146 146 std::list<int> acks; 147 147 148 148 int number; 149 149 150 150 for ( int j = 0; j < nAcks; j++ ) 151 151 { … … 154 154 assert( n == INTSIZE ); 155 155 i += n; 156 156 157 157 acks.push_back( number ); 158 158 } 159 159 160 160 int nMessages; 161 161 162 162 assert( i + INTSIZE <= length ); 163 163 n = Converter::byteArrayToInt( data + i, &nMessages ); … … 166 166 167 167 int messageLength, messageId; 168 168 169 169 for ( int j = 0; j < nMessages; j++ ) 170 170 { … … 173 173 assert( n == INTSIZE ); 174 174 i += n; 175 175 176 176 assert( i + INTSIZE <= length ); 177 177 n = Converter::byteArrayToInt( data + i, &number ); 178 178 assert( n == INTSIZE ); 179 179 i += n; 180 180 181 181 assert( i + INTSIZE <= length ); 182 182 n = Converter::byteArrayToInt( data + i, &messageId ); 183 183 assert( n == INTSIZE ); 184 184 i += n; 185 185 186 186 if ( number > 0 ) 187 187 messageQueue[userId].toAck.push_back( number ); 188 188 189 189 assert( i + messageLength <= length ); 190 190 assert( messageHandlerMap.find( (MessageId)messageId ) != messageHandlerMap.end() ); … … 194 194 { 195 195 NetworkMessage msg; 196 196 197 197 msg.data = new byte[messageLength]; 198 198 memcpy( msg.data, data + i, messageLength ); … … 200 200 msg.messageId = (MessageId)messageId; 201 201 msg.number = userId; 202 202 203 203 incomingMessageBuffer.push_back( msg ); 204 204 } … … 207 207 i += messageLength; 208 208 } 209 210 209 210 211 211 //TODO maybe handle incomingMessage in tick function. else local messages will not be handled if no clients are connected 212 212 for ( std::list<NetworkMessage>::iterator it = incomingMessageBuffer.begin(); it != incomingMessageBuffer.end(); ) … … 223 223 it++; 224 224 } 225 225 226 226 //walk throu message queue and remove acked messages 227 227 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); ) … … 236 236 it++; 237 237 } 238 238 239 239 //TODO find bether way. maybe with timestamp 240 240 if ( messageQueue[userId].recievedMessages.size() > 1000 ) … … 255 255 if ( messageQueue.find( userId ) == messageQueue.end() ) 256 256 return; 257 257 258 258 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ ) 259 259 { … … 262 262 it->data = NULL; 263 263 } 264 264 265 265 messageQueue[userId].toAck.clear(); 266 266 267 267 messageQueue.erase( userId ); 268 268 } … … 270 270 /** 271 271 * registers function to handle messages with id messageId. someData is passed to callbackfuntion 272 * @param messageId message id to handle 272 * @param messageId message id to handle 273 273 * @param cb function pointer to callback function 274 274 * @param someData this pointer is passed to callback function without modification … … 278 278 { 279 279 MessageHandler messageHandler; 280 280 281 281 messageHandler.cb = cb; 282 282 messageHandler.messageId = messageId; 283 283 messageHandler.someData = someData; 284 284 285 285 messageHandlerMap[messageId] = messageHandler; 286 286 287 287 return true; 288 288 } … … 309 309 * @param data pointer to data 310 310 * @param dataLength length of data 311 * @param recieverType 312 * @param reciever 311 * @param recieverType 312 * @param reciever 313 313 */ 314 314 void MessageManager::sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ) … … 316 316 for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ ) 317 317 { 318 if ( 318 if ( 319 319 recieverType == RT_ALL_ME || 320 320 recieverType == RT_ALL_NOT_ME || 321 321 recieverType == RT_USER && it->first == reciever || 322 322 recieverType == RT_NOT_USER && it->first != reciever || 323 recieverType == RT_SERVER && getNetworkStream()->isUser Server( it->first )323 recieverType == RT_SERVER && getNetworkStream()->isUserMasterServer( it->first ) 324 324 ) 325 325 { … … 336 336 } 337 337 } 338 338 339 339 if ( recieverType == RT_ALL_ME ) 340 340 { -
branches/proxy/src/lib/network/netdefs.h
r9246 r9347 1 /* 2 orxonox - the future of 3D-vertical-scrollers 1 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: Christoph Renner rennerc@ee.ethz.ch 13 co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch 14 15 June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch) 16 July 2006: some code rearangement and integration of the proxy server mechanism (boenzlip@ee.ethz.ch) 17 */ 2 18 3 19 /*! 4 20 * @file network_manager.h 5 21 * Main interface for the network module. Manages all the modules 6 7 22 */ 8 23 9 24 #ifndef _NETDEFS 10 25 #define _NETDEFS 26 11 27 12 28 #ifdef HAVE_SDL_NET_H … … 17 33 18 34 19 #define MAX_CONNECTIONS 1000 35 //!< maximal connectinons for the server 36 #define NET_MAX_CONNECTIONS 100 20 37 38 //!< the amount of slots used before a proxy server is activated 39 #define NET_CONNECTION_SATURATION 0.75 40 41 //!< network polling frequency 21 42 #define NETWORK_FREQUENCY 66 43 44 45 //!< orxonox protocol id 46 #define _ORXONOX_ID 0xF91337A0 47 //!< orxonox network version identifier 48 #define _ORXONOX_VERSION 1 22 49 23 50 … … 27 54 //!< enum indicating the type of the node 28 55 typedef enum { 29 NET_SERVER, 30 NET_CLIENT 56 NET_MASTER_SERVER, 57 NET_PROXY_SERVER_ACTIVE, 58 NET_PROXY_SERVER_PASSIVE, 59 NET_CLIENT, 60 61 NET_NR_TYPES 62 31 63 } NodeType; 32 64 -
branches/proxy/src/lib/network/network_game_manager.cc
r9235 r9347 10 10 11 11 ### File Specific: 12 main-programmer: Benjamin Wuest 13 co-programmer: ... 12 main-programmer: Christoph Renner rennerc@ee.ethz.ch 13 co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch 14 15 June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch) 16 July 2006: some code rearangement and integration of the proxy server mechanism (boenzlip@ee.ethz.ch) 14 17 */ 15 18 16 19 17 /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module 18 For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput 19 */ 20 20 21 #define DEBUG_MODULE_NETWORK 21 22 … … 45 46 46 47 47 /* using namespace std is default, this needs to be here */48 48 using namespace std; 49 49 … … 62 62 63 63 this->setSynchronized(true); 64 64 65 65 MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL ); 66 66 MessageManager::getInstance()->registerMessageHandler( MSGID_PREFEREDTEAM, preferedTeamHandler, NULL ); 67 67 MessageManager::getInstance()->registerMessageHandler( MSGID_CHATMESSAGE, chatMessageHandler, NULL ); 68 68 69 69 this->gameState = 0; 70 70 registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState" ) ); … … 77 77 { 78 78 delete MessageManager::getInstance(); 79 79 80 80 PlayerStats::deleteAllPlayerStats(); 81 81 82 82 NetworkGameManager::singletonRef = NULL; 83 83 } … … 86 86 /** 87 87 * insert new player into game 88 * @param userId 89 * @return 88 * @param userId 89 * @return 90 90 */ 91 91 bool NetworkGameManager::signalNewPlayer( int userId ) 92 92 { 93 assert( SharedNetworkData::getInstance()->is GameServer() );93 assert( SharedNetworkData::getInstance()->isMasterServer() ); 94 94 assert( State::getGameRules() ); 95 95 assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); 96 96 97 97 NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); 98 98 99 99 int team = rules.getTeamForNewUser(); 100 100 ClassID playableClassId = rules.getPlayableClassId( userId, team ); … … 102 102 std::string playableTexture = rules.getPlayableModelFileName( userId, team, playableClassId ); 103 103 float playableScale = rules.getPlayableScale( userId, team, playableClassId ); 104 104 105 105 BaseObject * bo = Factory::fabricate( playableClassId ); 106 106 107 107 assert( bo != NULL ); 108 108 assert( bo->isA( CL_PLAYABLE ) ); 109 109 110 110 Playable & playable = *(dynamic_cast<Playable*>(bo)); 111 111 112 112 playable.loadMD2Texture( playableTexture ); 113 113 114 114 playable.loadModel( playableModel, 100.0f ); 115 115 playable.setOwner( userId ); 116 116 playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); 117 117 playable.setSynchronized( true ); 118 118 119 119 PlayerStats * stats = rules.getNewPlayerStats( userId ); 120 120 121 121 stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); 122 122 stats->setSynchronized( true ); 123 123 stats->setOwner( SharedNetworkData::getInstance()->getHostID() ); 124 124 125 125 stats->setTeamId( team ); 126 126 stats->setPlayableClassId( playableClassId ); 127 127 stats->setPlayableUniqueId( playable.getUniqueID() ); 128 128 stats->setModelFileName( playableModel ); 129 129 130 130 if ( userId == 0 ) 131 131 stats->setNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Server" ) ); 132 132 133 133 if ( rules.isA( CL_MULTIPLAYER_TEAM_DEATHMATCH ) ) 134 134 dynamic_cast<MultiplayerTeamDeathmatch*>(&rules)->respawnPlayable( &playable, team, 0.0f ); 135 135 136 136 return true; 137 137 } … … 140 140 /** 141 141 * remove player from game 142 * @param userID 143 * @return 142 * @param userID 143 * @return 144 144 */ 145 145 bool NetworkGameManager::signalLeftPlayer(int userID) … … 151 151 delete PlayerStats::getStats( userID ); 152 152 } 153 153 154 154 return true; 155 155 } … … 159 159 /** 160 160 * handler for remove synchronizeable messages 161 * @param messageId 162 * @param data 163 * @param dataLength 164 * @param someData 165 * @param userId 161 * @param messageId 162 * @param data 163 * @param dataLength 164 * @param someData 165 * @param userId 166 166 * @return true on successfull handling else handler will be called again 167 167 */ 168 168 bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) 169 169 { 170 if ( getInstance()->isServer() )170 if ( SharedNetworkData::getInstance()->isMasterServer() ) 171 171 { 172 172 PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId); 173 173 return true; 174 174 } 175 175 176 176 int uniqueId = 0; 177 177 int len = Converter::byteArrayToInt( data, &uniqueId ); 178 178 179 179 if ( len != dataLength ) 180 180 { … … 182 182 return true; 183 183 } 184 184 185 185 const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE ); 186 186 187 187 for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) 188 188 { … … 194 194 return true; 195 195 } 196 196 197 197 delete dynamic_cast<Synchronizeable*>(*it); 198 198 return true; 199 199 } 200 200 } 201 201 202 202 return true; 203 203 } … … 210 210 { 211 211 byte buf[INTSIZE]; 212 212 213 213 assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE ); 214 214 … … 220 220 /** 221 221 * handler for MSGID_PREFEREDTEAM message 222 * @param messageId 223 * @param data 224 * @param dataLength 225 * @param someData 226 * @param userId 227 * @return 222 * @param messageId 223 * @param data 224 * @param dataLength 225 * @param someData 226 * @param userId 227 * @return 228 228 */ 229 229 bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) 230 230 { 231 assert( NetworkGameManager::getInstance()->isServer() );232 231 assert( SharedNetworkData::getInstance()->isMasterServer() ); 232 233 233 int teamId = 0; 234 234 int len = Converter::byteArrayToInt( data, &teamId ); 235 235 236 236 if ( len != dataLength ) 237 237 { … … 239 239 return true; 240 240 } 241 241 242 242 NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId ); 243 243 244 244 return true; 245 245 } … … 249 249 if ( !PlayerStats::getStats( userId ) ) 250 250 return; 251 251 252 252 PlayerStats & stats = *(PlayerStats::getStats( userId )); 253 253 254 254 stats.setPreferedTeamId( teamId ); 255 255 } … … 257 257 /** 258 258 * set prefered team for this host 259 * @param teamId 259 * @param teamId 260 260 */ 261 261 void NetworkGameManager::prefereTeam( int teamId ) 262 262 { 263 if ( isServer() )263 if ( SharedNetworkData::getInstance()->isMasterServer() ) 264 264 setPreferedTeam( SharedNetworkData::getInstance()->getHostID(), teamId ); 265 265 else 266 266 { 267 267 byte buf[INTSIZE]; 268 268 269 269 assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE ); 270 270 271 271 MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH ); 272 272 } … … 285 285 { 286 286 const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); 287 287 288 288 if ( list && std::find( list->begin(), list->end(), *it ) != list->end() ) 289 { 289 { 290 290 PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassName(), (*it)->getOwner() ); 291 291 std::list<Playable*>::iterator delit = it; … … 305 305 { 306 306 PRINTF(0)("NetworkGameManager::chatMessageHandler %d %d\n", userId, SharedNetworkData::getInstance()->getHostID() ); 307 if ( NetworkGameManager::getInstance()->isServer() && userId != SharedNetworkData::getInstance()->getHostID() )307 if ( SharedNetworkData::getInstance()->isMasterServer() && userId != SharedNetworkData::getInstance()->getHostID() ) 308 308 { 309 309 MessageManager::getInstance()->sendMessage( messageId, data, dataLength, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); 310 310 } 311 311 312 312 assert( State::getGameRules() ); 313 313 assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); 314 314 315 315 NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); 316 316 317 317 if ( dataLength < 3*INTSIZE ) 318 318 { 319 319 PRINTF(2)("got too small chatmessage from client %d\n", userId); 320 320 321 321 return true; 322 322 } 323 323 324 324 int messageType = 0; 325 325 Converter::byteArrayToInt( data, &messageType ); … … 328 328 std::string message; 329 329 Converter::byteArrayToString( data+2*INTSIZE, message, dataLength-2*INTSIZE ); 330 330 331 331 rules.handleChatMessage( senderUserId, message, messageType ); 332 332 … … 346 346 assert( Converter::intToByteArray( SharedNetworkData::getInstance()->getHostID(), buf+INTSIZE, INTSIZE ) == INTSIZE ); 347 347 assert( Converter::stringToByteArray(message, buf+2*INTSIZE, message.length()+INTSIZE) == message.length()+INTSIZE ); 348 349 if ( this->isServer() )348 349 if ( SharedNetworkData::getInstance()->isMasterServer() ) 350 350 MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_ME, 0, MP_HIGHBANDWIDTH ); 351 351 else 352 352 MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); 353 353 354 354 355 355 delete [] buf; 356 356 } -
branches/proxy/src/lib/network/network_game_manager.h
r8708 r9347 28 28 NET_YOU_ARE_ENTITY, 29 29 NET_REQUEST_ENTITY_LIST, 30 NET_REQUEST_PNODE_PATH,31 NET_SEND_PNODE_PATH,32 30 33 31 NET_NUMBER … … 42 40 43 41 /*! 44 * a class that can create and remove entities 42 * a class that can create and remove entities over the network 45 43 */ 46 44 class NetworkGameManager: public Synchronizeable 47 45 { 46 48 47 public: 49 48 virtual ~NetworkGameManager(); 50 49 51 50 static NetworkGameManager* NetworkGameManager::getInstance() 52 51 { if (!NetworkGameManager::singletonRef) NetworkGameManager::singletonRef = new NetworkGameManager(); return NetworkGameManager::singletonRef; } … … 55 54 bool signalNewPlayer( int userId ); 56 55 bool signalLeftPlayer( int userID ); 57 58 void removeSynchronizeable( int uniqueId ); 59 56 60 57 void prefereTeam( int teamId ); 58 61 59 62 60 inline void setGameState( int gameState ){ this->gameState = gameState; } 63 61 inline int getGameState(){ return this->gameState; } 64 62 65 63 void tick( float ds ); 66 64 65 void removeSynchronizeable( int uniqueId ); 67 66 void sendChatMessage( const std::string & message, int messageType ); 67 68 68 69 69 private: 70 70 NetworkGameManager(); 71 71 72 72 static bool delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ); 73 73 static bool preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ); 74 74 static bool chatMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ); 75 75 76 76 void setPreferedTeam( int userId, int teamId ); 77 77 78 79 private: 78 80 static NetworkGameManager* singletonRef; 79 81 80 82 int gameState; 81 83 82 84 std::list<Playable*> playablesToDelete; 83 85 }; -
branches/proxy/src/lib/network/network_manager.cc
r9059 r9347 53 53 this->networkStream = NULL; 54 54 this->elapsedTime = 0.0f; 55 56 55 56 57 57 int port = Preferences::getInstance()->getInt( "network", "telnetport", 0 ); 58 58 59 59 if ( port > 0 ) 60 60 NetworkLog::getInstance()->listen( port ); … … 75 75 this->networkStream = NULL; 76 76 } 77 77 78 78 NetworkManager::singletonRef = NULL; 79 79 } … … 118 118 { 119 119 SharedNetworkData::getInstance()->setHostID(0); 120 SharedNetworkData::getInstance()->set GameServer(true);120 SharedNetworkData::getInstance()->setNodeType(NET_MASTER_SERVER); 121 121 this->networkStream = new NetworkStream(port); 122 122 SharedNetworkData::getInstance()->setDefaultSyncStream(this->networkStream); … … 148 148 if ( networkStream ) 149 149 networkStream->processData(); 150 150 151 151 NetworkGameManager::getInstance()->tick( this->elapsedTime ); 152 152 } -
branches/proxy/src/lib/network/network_manager.h
r9059 r9347 41 41 /** Returns the hostID @return The hostID of the object */ 42 42 inline int getHostID() { return SharedNetworkData::getInstance()->getHostID(); } 43 inline bool isGameServer() { return SharedNetworkData::getInstance()->isGameServer(); } 43 inline bool isMasterServer() { return SharedNetworkData::getInstance()->isMasterServer(); } 44 inline bool isProxyServer() { return SharedNetworkData::getInstance()->isProxyServer(); } 45 inline bool isClient() { return SharedNetworkData::getInstance()->isClient(); } 44 46 45 47 -
branches/proxy/src/lib/network/network_socket.cc
r7954 r9347 33 33 { 34 34 bOk = true; 35 this->ip.host = 0; 36 this->ip.port = 0; 35 37 } 36 38 -
branches/proxy/src/lib/network/network_socket.h
r7954 r9347 27 27 */ 28 28 virtual void connectToServer( std::string host, int port ) = 0; 29 29 30 30 /** 31 31 * disconnect from server 32 32 */ 33 33 virtual void disconnectServer() = 0; 34 35 /** 36 * reconnecting to another server by terminating old connection and init a new connection 37 */ 38 virtual void reconnectToServer( std::string host, int port) = 0; 39 40 /** 41 * reconnecting to anoter server softly 42 */ 43 virtual void reconnectToServerSoft( std::string host, int port) = 0; 34 44 35 45 /** … … 40 50 */ 41 51 virtual bool writePacket(byte * data, int length) = 0; 42 52 43 53 /** 44 54 * read a packet sent by another NetworkSocket … … 54 64 */ 55 65 inline bool isOk() { return this->bOk; } 56 66 67 /** @returns the ip adderess of the destination socket */ 68 IPaddress getRemoteAddress() { return ip; } 69 70 57 71 protected: 58 bool bOk; //!< check for socket status72 bool bOk; //!< check for socket status 59 73 74 IPaddress ip; //!< host,port 60 75 }; 61 76 -
branches/proxy/src/lib/network/network_stream.cc
r9246 r9347 12 12 main-programmer: Christoph Renner rennerc@ee.ethz.ch 13 13 co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch 14 15 June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch) 16 July 2006: some code rearangement and integration of the proxy server mechanism (boenzlip@ee.ethz.ch) 14 17 */ 15 18 … … 22 25 #include "udp_socket.h" 23 26 #include "udp_server_socket.h" 24 #include "connection_monitor.h" 27 #include "monitor/connection_monitor.h" 28 #include "monitor/network_monitor.h" 25 29 #include "synchronizeable.h" 30 #include "ip.h" 26 31 #include "network_game_manager.h" 27 32 #include "shared_network_data.h" … … 42 47 #include <algorithm> 43 48 44 /* include your own header */ 49 45 50 #include "network_stream.h" 46 51 47 /* probably unnecessary */ 52 48 53 using namespace std; 49 54 … … 52 57 53 58 59 /** 60 * empty constructor 61 */ 54 62 NetworkStream::NetworkStream() 55 63 : DataStream() … … 57 65 this->init(); 58 66 /* initialize the references */ 59 this-> type = NET_CLIENT;60 } 61 62 63 /** 64 * connect to a server as a client67 this->pInfo->nodeType = NET_CLIENT; 68 } 69 70 71 /** 72 * start as a client, connect to a server 65 73 * @param host: host name (address) 66 74 * @param port: port number … … 68 76 NetworkStream::NetworkStream( std::string host, int port ) 69 77 { 70 this->type = NET_CLIENT;71 78 this->init(); 79 // init the peers[0] the server to ceonnect to 72 80 this->peers[0].socket = new UdpSocket( host, port ); 73 81 this->peers[0].userId = 0; 74 this->peers[0]. isServer = true;82 this->peers[0].nodeType = NET_MASTER_SERVER; 75 83 this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); 84 this->peers[0].ip = this->peers[0].socket->getRemoteAddress(); 85 // and set also the localhost 86 this->pInfo->nodeType = NET_CLIENT; 87 // get the local ip address 88 IPaddress ip; 89 SDLNet_ResolveHost( &ip, NULL, port ); 90 this->pInfo->ip = ip; 76 91 } 77 92 … … 83 98 NetworkStream::NetworkStream( int port ) 84 99 { 85 this->type = NET_SERVER;86 100 this->init(); 87 101 this->serverSocket = new UdpServerSocket(port); 88 this->bActive = true; 102 this->pInfo->nodeType = NET_MASTER_SERVER; 103 // get the local ip address 104 IPaddress ip; 105 SDLNet_ResolveHost( &ip, NULL, port ); 106 this->pInfo->ip = ip; 89 107 } 90 108 … … 97 115 /* set the class id for the base object */ 98 116 this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); 99 this->bActive = false;100 117 this->serverSocket = NULL; 101 118 this->networkGameManager = NULL; 102 myHostId = 0; 103 currentState = 0; 119 this->networkMonitor = NULL; 120 121 this->pInfo = new PeerInfo(); 122 this->pInfo->userId = 0; 123 this->pInfo->lastAckedState = 0; 124 this->pInfo->lastRecvedState = 0; 125 126 127 this->currentState = 0; 104 128 105 129 remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); … … 147 171 for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) 148 172 (*it)->setNetworkStream( NULL ); 173 174 if( this->pInfo) 175 delete this->pInfo; 176 177 if( this->networkMonitor) 178 delete this->networkMonitor; 149 179 } 150 180 … … 165 195 /** 166 196 * starts the network handshake 197 * handsakes are always initialized from the client side first. this starts the handshake and therefore is only 198 * executed as client 167 199 */ 168 200 void NetworkStream::startHandshake() 169 201 { 170 Handshake* hs = new Handshake( false);202 Handshake* hs = new Handshake(this->pInfo->nodeType); 171 203 hs->setUniqueID( 0 ); 172 204 assert( peers[0].handshake == NULL ); 173 205 peers[0].handshake = hs; 174 206 207 // set the preferred nick name 175 208 hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) ); 176 209 177 // peers[0].handshake->setSynchronized( true );178 //this->connectSynchronizeable(*hs);179 //this->connectSynchronizeable(*hs);180 210 PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); 181 211 } … … 192 222 sync.setNetworkStream( this ); 193 223 194 this->bActive = true;224 // this->bActive = true; 195 225 } 196 226 … … 215 245 void NetworkStream::processData() 216 246 { 247 // create the network monitor after all the init work and before there is any connection handlings 248 if( this->networkMonitor == NULL) 249 this->networkMonitor = new NetworkMonitor(this); 250 251 217 252 int tick = SDL_GetTicks(); 218 253 219 currentState++; 220 221 if ( this->type == NET_SERVER ) 222 { 254 this->currentState++; 255 // there was a wrap around 256 if( this->currentState < 0) 257 { 258 PRINTF(1)("A wrap around in the state variable as occured. The server was running so long? Pls restart server or write a mail to the supporters!\n"); 259 } 260 261 if ( this->pInfo->nodeType == NET_MASTER_SERVER ) 262 { 263 // execute everytthing the master server shoudl do 223 264 if ( serverSocket ) 224 265 serverSocket->update(); 225 266 226 267 this->updateConnectionList(); 268 } 269 else if( this->pInfo->nodeType == NET_PROXY_SERVER_ACTIVE) 270 { 271 // execute everything the proxy server should do 227 272 } 228 273 else … … 249 294 cleanUpOldSyncList(); 250 295 handleHandshakes(); 296 297 // update the network monitor 298 this->networkMonitor->process(); 251 299 252 300 // order of up/downstream is important!!!! … … 258 306 259 307 /** 260 * if we are a server update the connection list to accept new connections (clients)261 * also start the handsake for the new clients308 * if we are a NET_MASTER_SERVER or NET_PROXY_SERVER_ACTIVE update the connection list to accept new 309 * connections (clients) also start the handsake for the new clients 262 310 */ 263 311 void NetworkStream::updateConnectionList( ) … … 276 324 clientId = freeSocketSlots.back(); 277 325 freeSocketSlots.pop_back(); 278 peers[clientId].socket = tempNetworkSocket;279 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() );280 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId );281 peers[clientId].handshake->setUniqueID(clientId);282 peers[clientId].userId = clientId;283 peers[clientId].isServer = false;284 326 } 285 327 else … … 290 332 if ( it->first >= clientId ) 291 333 clientId = it->first + 1; 292 293 peers[clientId].socket = tempNetworkSocket; 294 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); 295 peers[clientId].handshake->setUniqueID(clientId); 296 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); 297 peers[clientId].userId = clientId; 298 peers[clientId].isServer = false; 299 300 PRINTF(0)("num sync: %d\n", synchronizeables.size()); 301 } 302 303 // check if there are too many clients connected 304 if ( clientId > MAX_CONNECTIONS ) 305 { 306 peers[clientId].handshake->doReject( "too many connections" ); 307 PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); 308 } 309 else 310 { 311 PRINTF(0)("New Client: %d\n", clientId); 312 } 313 314 //this->connectSynchronizeable(*handshakes[clientId]); 334 } 335 336 peers[clientId].socket = tempNetworkSocket; 337 // create new handshake and init its variables 338 peers[clientId].handshake = new Handshake(this->pInfo->nodeType, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); 339 peers[clientId].handshake->setUniqueID(clientId); 340 341 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); 342 peers[clientId].userId = clientId; 343 344 PRINTF(0)("num sync: %d\n", synchronizeables.size()); 345 346 // get the proxy server informations and write them to the handshake, if any (proxy) 347 assert( this->networkMonitor != NULL); 348 PeerInfo* pi = this->networkMonitor->getFirstChoiceProxy(); 349 if( pi != NULL) 350 { 351 peers[clientId].handshake->setProxy1Address( pi->ip); 352 PRINTF(0)("proxy1 ip set to: %s\n", pi->ip.ipString().c_str()); 353 } 354 355 pi = this->networkMonitor->getSecondChoiceProxy(); 356 if( pi != NULL) 357 peers[clientId].handshake->setProxy2Address( pi->ip); 358 359 // check if the connecting client should reconnect to a proxy server 360 peers[clientId].handshake->setRedirect(this->networkMonitor->isReconnectNextClient()); 361 362 // the connecting node of course is a client 363 peers[clientId].nodeType = NET_CLIENT; 364 peers[clientId].ip = peers[clientId].socket->getRemoteAddress(); 365 366 367 // check if there are too many clients connected (DEPRECATED: new: the masterserver sends a list of proxy servers) 368 // if ( clientId > SharedNetworkData::getInstance()->getMaxPlayer() ) 369 // { 370 // // peers[clientId].handshake->setRedirect(true); 371 // // 372 // // peers[clientId].handshake->doReject( "too many connections" ); 373 // PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); 374 // } 375 // else 376 // { 377 // PRINTF(0)("New Client: %d\n", clientId); 378 // } 379 PRINTF(0)("New Client: %d\n", clientId); 380 381 315 382 } 316 383 … … 373 440 void NetworkStream::debug() 374 441 { 375 if( this->is Server())376 PRINT(0)(" Host ist Server with ID: %i\n", this-> myHostId);442 if( this->isMasterServer()) 443 PRINT(0)(" Host ist Server with ID: %i\n", this->pInfo->userId); 377 444 else 378 PRINT(0)(" Host ist Client with ID: %i\n", this-> myHostId);445 PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); 379 446 380 447 PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); … … 385 452 (*it)->getUniqueID(), (*it)->beSynchronized()); 386 453 } 387 PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS);454 PRINT(0)(" Maximal Connections: %i\n", SharedNetworkData::getInstance()->getMaxPlayer() ); 388 455 389 456 } … … 414 481 if ( it->second.handshake ) 415 482 { 483 // handshake finished 416 484 if ( it->second.handshake->completed() ) 417 485 { 486 //handshake is correct 418 487 if ( it->second.handshake->ok() ) 419 488 { 420 489 if ( !it->second.handshake->allowDel() ) 421 490 { 422 if ( t ype != NET_SERVER)491 if ( this->pInfo->nodeType == NET_CLIENT ) 423 492 { 424 493 SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); 425 myHostId = SharedNetworkData::getInstance()->getHostID(); 426 494 this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); 495 496 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 497 it->second.ip = it->second.socket->getRemoteAddress(); 498 // add the new server to the nodes list (it can be a NET_MASTER_SERVER or NET_PROXY_SERVER) 499 this->networkMonitor->addNode(&it->second); 500 // get proxy 1 address and add it 501 this->networkMonitor->addNode(it->second.handshake->getProxy1Address(), NET_PROXY_SERVER_ACTIVE); 502 PRINTF(0)("proxy1 ip read to: %s\n", it->second.handshake->getProxy1Address().ipString().c_str()); 503 // get proxy 2 address and add it 504 this->networkMonitor->addNode(it->second.handshake->getProxy2Address(), NET_PROXY_SERVER_ACTIVE); 505 506 // now check if the server accepted the connection 507 if( it->second.handshake->redirect()) 508 this->handleReconnect( it->second.userId); 509 510 // create the new network game manager and init it 427 511 this->networkGameManager = NetworkGameManager::getInstance(); 428 512 this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); 513 // init the new message manager 429 514 MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); 430 515 } … … 437 522 else 438 523 { 524 // handshake finished registring new player 439 525 if ( it->second.handshake->canDel() ) 440 526 { 441 if ( t ype == NET_SERVER )527 if ( this->pInfo->nodeType == NET_MASTER_SERVER ) 442 528 { 443 handleNewClient( it->second.userId ); 529 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 530 it->second.ip = it->second.socket->getRemoteAddress(); 531 532 this->networkMonitor->addNode(&it->second); 533 534 this->handleNewClient( it->second.userId ); 444 535 445 536 if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) … … 468 559 469 560 /** 561 * this functions handles a reconnect event received from the a NET_MASTER_SERVER or NET_PROXY_SERVER 562 */ 563 void NetworkStream::handleReconnect(int userId) 564 { 565 PRINTF(0)("===============================================\n"); 566 PRINTF(0)("Client is redirected to the other proxy servers\n"); 567 PRINTF(0)("===============================================\n"); 568 569 PeerInfo* pInfo = &this->peers[userId]; 570 571 // reject the server 572 pInfo->handshake->doReject( "redirected to different server"); 573 574 // flush the old synchronization states, since the numbering could be completely different 575 pInfo->lastAckedState = 0; 576 pInfo->lastRecvedState = 0; 577 // not sure if this works as expected 578 if( pInfo->handshake) 579 delete pInfo->handshake; 580 581 // disconnect from the current server and reconnect to proxy server 582 pInfo->socket->reconnectToServer( pInfo->handshake->getProxy1Address().ipString(), pInfo->handshake->getProxy1Address().port()); 583 584 // and restart the handshake 585 this->startHandshake(); 586 } 587 588 589 /** 470 590 * handle upstream network traffic 471 591 */ … … 513 633 514 634 // if we are a server and this is not our handshake 515 if ( is Server() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId )635 if ( isMasterServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) 516 636 continue; 517 637 … … 524 644 525 645 // server fakes uniqueid == 0 for handshake 526 if ( this->is Server() && sync.getUniqueID() < MAX_CONNECTIONS- 1 )646 if ( this->isMasterServer() && sync.getUniqueID() < SharedNetworkData::getInstance()->getMaxPlayer() - 1 ) 527 647 n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); 528 648 else … … 573 693 // now compress the data with the zip library 574 694 int compLength = 0; 575 if ( this->is Server() )695 if ( this->isMasterServer() ) 576 696 compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); 577 697 else … … 637 757 assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); 638 758 assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); 639 //NETPRINTF(n)("ackedstate: %d\n", ackedState);640 759 offset = 4*INTSIZE; 641 760 642 761 peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState ); 643 762 644 //NETPRINTF(n)("got packet: %d, %d\n", length, packetLength); 645 646 //if this is an old state drop it 763 764 //if this is an old state drop it 647 765 if ( state <= peer->second.lastRecvedState ) 648 766 continue; … … 688 806 } 689 807 690 if ( !peers[peer->second.userId].is Server)808 if ( !peers[peer->second.userId].isMasterServer() ) 691 809 { 692 810 offset += syncDataLength; … … 779 897 /** 780 898 * is executed when a handshake has finished 781 * @todo create playable for new user782 899 */ 783 900 void NetworkStream::handleNewClient( int userId ) 784 901 { 902 // init and assign the message manager 785 903 MessageManager::getInstance()->initUser( userId ); 786 904 // do all game relevant stuff here 787 905 networkGameManager->signalNewPlayer( userId ); 788 } 906 907 // register the new client at the network monitor 908 // this->networkMonitor->addClient(); 909 } 910 789 911 790 912 /** -
branches/proxy/src/lib/network/network_stream.h
r9246 r9347 14 14 #include "server_socket.h" 15 15 #include "handshake.h" 16 #include " connection_monitor.h"16 #include "monitor/connection_monitor.h" 17 17 #include "udp_server_socket.h" 18 #include "peer_info.h" 19 20 #include "shared_network_data.h" 18 21 19 22 class Synchronizeable; … … 21 24 class ServerSocket; 22 25 class NetworkGameManager; 26 class NetworkMonitor; 23 27 24 //!< this structure contains informations about the network node25 class PeerInfo26 {27 public:28 PeerInfo() { clear(); }29 void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; connectionMonitor = NULL; }30 31 32 public:33 int userId;34 bool isServer;35 NetworkSocket * socket;36 Handshake * handshake;37 ConnectionMonitor * connectionMonitor;38 int lastAckedState;39 int lastRecvedState;40 };41 28 42 29 typedef std::list<Synchronizeable*> SynchronizeableList; … … 58 45 void startHandshake(); 59 46 47 /* synchronizeable interface */ 60 48 void connectSynchronizeable(Synchronizeable& sync); 61 49 void disconnectSynchronizeable(Synchronizeable& sync); 62 50 63 inline bool isServer() const { return (this->type == NET_SERVER)? true:false; } 64 inline bool isActive() const { return this->bActive; } 51 /* functions for the localhost settings */ 52 inline bool isMasterServer() const { return (this->pInfo->nodeType == NET_MASTER_SERVER)? true:false; } 53 inline bool isProxyServer() const { return (this->pInfo->nodeType == NET_PROXY_SERVER_ACTIVE)? true:false; } 54 inline bool isClient() const { return (this->pInfo->nodeType == NET_CLIENT)? true:false; } 55 // inline bool isActive() const { return this->bActive; } 56 inline int getMaxConnections(){ return SharedNetworkData::getInstance()->getMaxPlayer(); } 65 57 66 inline int getMaxConnections(){ return MAX_CONNECTIONS; } 58 /* functions for the peerInfo information retreival */ 59 inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } 60 inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer(); } 61 inline bool isUserProxyServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServer(); } 62 inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient(); } 67 63 64 /* peering interface */ 65 inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } 66 inline PeerInfo* getPeerInfo() { return this->pInfo; } 67 inline PeerList getPeerInfoList() { return this->peers; } 68 69 /* data processing*/ 68 70 virtual void processData(); 71 72 /* debugging */ 73 void debug(); 74 75 76 private: 69 77 70 78 inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } 71 79 inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } 80 void cleanUpOldSyncList(); 72 81 int getSyncCount(); 73 82 74 inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); }75 inline bool isUserServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isServer; }76 77 void debug();78 79 inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; }80 81 82 private:83 83 void updateConnectionList(); 84 /* handle processes */ 84 85 void handleHandshakes(); 85 86 void handleUpstream( int tick ); 86 87 void handleDownstream(int tick ); 88 89 /* handle events*/ 87 90 void handleNewClient( int userId ); 88 void cleanUpOldSyncList();91 void handleReconnect( int userId ); 89 92 90 93 void writeToNewDict( byte * data, int length, bool upstream ); … … 92 95 93 96 private: 94 SynchronizeableList synchronizeables; 95 PeerList peers; 96 ServerSocket* serverSocket; 97 int type; 98 bool bActive; 99 std::list<int> freeSocketSlots; 97 PeerList peers; //!< list of the network node informations 100 98 101 int myHostId;99 PeerInfo* pInfo; //!< the info about the local peer node (not in the PeerList) 102 100 101 std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) 103 102 int currentState; //!< current state id 104 103 105 NetworkGameManager* networkGameManager; 104 NetworkMonitor* networkMonitor; //!< the network monitor 105 NetworkGameManager* networkGameManager; //!< reference to the network game manager 106 ServerSocket* serverSocket; //!< the listening socket of the server 106 107 107 108 std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them 109 SynchronizeableList synchronizeables; //!< list of the synchronizeables 108 110 109 111 byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream 110 112 byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream 111 112 113 int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict 113 114 114 int dictServer;115 int dictClient;115 int dictServer; //!< the zip dict for the server 116 int dictClient; //!< the zip dict for the client 116 117 }; 117 118 #endif /* _NETWORK_STREAM */ -
branches/proxy/src/lib/network/player_stats.cc
r9235 r9347 38 38 { 39 39 init(); 40 40 41 41 this->userId = userId; 42 42 } … … 62 62 this->nickName = "Player"; 63 63 this->oldNickName = "Player"; 64 64 65 65 userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) ); 66 66 teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) ); … … 71 71 modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) ); 72 72 nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) ); 73 73 74 74 MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL ); 75 75 76 76 PRINTF(0)("PlayerStats created\n"); 77 77 } … … 96 96 { 97 97 this->setPlayableUniqueId( this->playableUniqueId ); 98 98 99 99 PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID()); 100 100 } 101 101 102 102 if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() ) 103 103 { … … 115 115 { 116 116 const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); 117 117 118 118 if ( !list ) 119 119 { 120 120 return NULL; 121 121 } 122 122 123 123 for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) 124 124 { … … 128 128 } 129 129 } 130 130 131 131 return NULL; 132 132 } … … 138 138 { 139 139 const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); 140 140 141 141 if ( !list ) 142 142 { … … 144 144 return; 145 145 } 146 146 147 147 this->playable = NULL; 148 148 for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) … … 156 156 } 157 157 } 158 158 159 159 if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() ) 160 160 { 161 161 State::getPlayer()->setPlayable( this->playable ); 162 162 } 163 163 164 164 this->playableUniqueId = uniqueId; 165 165 } … … 173 173 if ( playable ) 174 174 return playable; 175 175 176 176 assert( playableUniqueId > 0 ); 177 177 178 178 setPlayableUniqueId( playableUniqueId ); 179 179 180 180 assert( playable ); 181 181 182 182 return playable; 183 183 } … … 189 189 void PlayerStats::setNickName( std::string nick ) 190 190 { 191 if ( SharedNetworkData::getInstance()->is GameServer() )191 if ( SharedNetworkData::getInstance()->isMasterServer() ) 192 192 { 193 193 this->nickName = nick; … … 199 199 { 200 200 byte * data = new byte[nick.length()+INTSIZE]; 201 201 202 202 assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE ); 203 203 204 204 MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH ); 205 205 return; … … 211 211 std::string newNick; 212 212 int res = Converter::byteArrayToString( data, newNick, dataLength ); 213 213 214 214 if ( res != dataLength ) 215 215 { … … 217 217 newNick = "invalid"; 218 218 } 219 219 220 220 if ( PlayerStats::getStats( userId ) ) 221 221 PlayerStats::getStats( userId )->setNickName( newNick ); 222 222 223 223 return true; 224 224 } … … 228 228 if ( getStats( SharedNetworkData::getInstance()->getHostID() ) ) 229 229 getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick ); 230 230 231 231 Preferences::getInstance()->setString( "multiplayer", "nickname", newNick ); 232 232 } … … 237 237 { 238 238 const std::list<BaseObject*> * list; 239 239 240 240 while ( (list = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() ) 241 241 delete *list->begin(); … … 247 247 { 248 248 ScoreList result; 249 249 250 250 const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); 251 251 252 252 if ( !list ) 253 253 { 254 254 return result; 255 255 } 256 256 257 257 for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) 258 258 { 259 259 PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); 260 260 261 261 TeamScoreList::iterator it = result[stats.getTeamId()].begin(); 262 262 263 263 while ( it != result[stats.getTeamId()].end() && stats.score > it->score ) 264 264 { 265 265 it++; 266 266 } 267 267 268 268 PlayerScore score; 269 269 score.name = stats.getNickName(); 270 270 score.score = stats.getScore(); 271 271 272 272 result[stats.getTeamId()].insert(it, score); 273 273 } 274 274 275 275 return result; 276 276 } -
branches/proxy/src/lib/network/shared_network_data.cc
r9110 r9347 35 35 this->hostID = -1; 36 36 this->defaultSyncStream = NULL; 37 this->newUniqueID = MAX_CONNECTIONS + 2;38 this-> bGameServer = false;37 this->newUniqueID = NET_MAX_CONNECTIONS + 2; 38 this->nodeType = NET_CLIENT; 39 39 } 40 40 -
branches/proxy/src/lib/network/shared_network_data.h
r6695 r9347 8 8 9 9 #include "base_object.h" 10 #include "netdefs.h" 11 #include "proxy/proxy_settings.h" 10 12 11 13 12 14 class NetworkStream; 13 15 class Synchronizeable; 14 template<typename> 15 class tList; 16 16 17 17 18 /* and here is the class itsself*/ … … 25 26 26 27 /** @returns the next uniqueID free for an object */ 27 inline int getNewUniqueID() { return ( this-> bGameServer)?this->newUniqueID++:-1; }28 inline int getNewUniqueID() { return ( this->nodeType != NET_CLIENT)?this->newUniqueID++:-1; } 28 29 29 /** @returns true is this node is a game server */ 30 inline bool isGameServer() { return this->bGameServer; } 30 /** @returns true is this node is a master server */ 31 inline bool isMasterServer() { return this->nodeType == NET_MASTER_SERVER; } 32 /** @returns true is this node is a proxy server */ 33 inline bool isProxyServer() { return this->nodeType == NET_PROXY_SERVER_ACTIVE; } 34 /** @returns true is this node is a client*/ 35 inline bool isClient() { return this->nodeType == NET_CLIENT; } 31 36 /** sets the game server flag @param bGameServer true if it is a game server */ 32 inline void setGameServer(bool bGameServer) { this->bGameServer = bGameServer; } 37 inline void setNodeType(int nodeType) { this->nodeType = nodeType; } 38 39 /** @returns the maximum number of players for this server */ 40 inline int getMaxPlayer() { return ProxySettings::getInstance()->getMaxPlayer(); } 33 41 34 42 /** @returns the hostID of the node */ … … 49 57 private: 50 58 int newUniqueID; //!< next uniqueID 51 bool bGameServer; //!< true if it is a server 59 int nodeType; //!< saves the node type here 60 52 61 int hostID; //!< The Host-ID of the Manager 53 62 NetworkStream* defaultSyncStream; //!< default synchronize NetworkStream -
branches/proxy/src/lib/network/synchronizeable.cc
r9110 r9347 38 38 this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); 39 39 this->owner = 0; 40 this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0);40 // this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0); 41 41 this->uniqueID = NET_UID_UNASSIGNED; 42 42 this->networkStream = NULL; 43 43 this->bSynchronize = false; 44 44 45 45 if( State::isOnline()) 46 46 { … … 54 54 assert( syncVarList.size() == 0 ); 55 55 mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId" ) ); 56 56 57 57 this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) ); 58 58 this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) ); … … 69 69 { 70 70 this->networkStream->disconnectSynchronizeable(*this); 71 72 if ( this->isServer() && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) )71 72 if ( SharedNetworkData::getInstance()->isMasterServer() && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) ) 73 73 NetworkGameManager::getInstance()->removeSynchronizeable( this->getUniqueID() ); 74 74 } 75 75 76 76 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 77 77 { … … 79 79 } 80 80 syncVarList.clear(); 81 81 82 82 for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ ) 83 83 { … … 93 93 94 94 } 95 95 96 96 for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ ) 97 97 { … … 105 105 delete *it2; 106 106 } 107 108 } 109 } 110 111 /** 112 * Sets the server flag to a given value 113 * @param isServer: the boolean value which the server flag is to set to 114 */ 115 void Synchronizeable::setIsServer(bool isServer) 116 { 117 if( isServer ) 118 this->state = this->state | STATE_SERVER; 119 else 120 this->state = this->state & (~STATE_SERVER); 121 } 122 123 124 /** 125 * Determines if the server flag is set 126 * @return true, if the server flag is true, false else 127 */ 128 bool Synchronizeable::isServer() 129 { 130 return (this->state & STATE_SERVER) >0; 107 } 131 108 } 132 109 … … 169 146 (*it2)->data = NULL; 170 147 } 171 148 172 149 delete *it2; 173 150 } … … 181 158 while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId ) 182 159 it++; 183 184 // if ( getLeafClassID() == CL_SPACE_SHIP )185 // {186 // PRINTF(0)("getStateDiff:SpaceShip from: %d stateId: %d\n", (it == sentStates[userId].end())?-1:fromStateId, stateId);187 // }188 160 189 161 if ( it == sentStates[userId].end() ) … … 196 168 197 169 stateFrom = initialEntry; 198 170 199 171 sentStates[userId].push_back( stateFrom ); 200 172 } … … 205 177 206 178 sentStates[userId].push_back( stateTo ); 207 179 208 180 stateTo->stateId = stateId; 209 181 stateTo->dataLength = neededSize; … … 214 186 int i = 0; 215 187 int n; 216 188 217 189 bool hasPermission; 218 190 bool sizeChanged = false; … … 222 194 { 223 195 hasPermission = ( 224 this->isServer() && (*it)->checkPermission( PERMISSION_SERVER ) ||196 SharedNetworkData::getInstance()->isMasterServer() && (*it)->checkPermission( PERMISSION_MASTER_SERVER ) || 225 197 this->owner == SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) || 226 this->isServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ) ||227 (*it)->checkPermission( PERMISSION_ALL ) 198 SharedNetworkData::getInstance()->isMasterServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ) || 199 (*it)->checkPermission( PERMISSION_ALL ) 228 200 ); 229 230 if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() ) 201 202 if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() ) 231 203 sizeChanged = true; 232 204 233 205 if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeChanged ) 234 206 { … … 303 275 it++; 304 276 305 277 306 278 // if ( getLeafClassID() == CL_SPACE_SHIP ) 307 279 // { … … 318 290 319 291 stateFrom = initialEntry; 320 292 321 293 recvStates[userId].push_back( stateFrom ); 322 294 } 323 295 else 324 296 stateFrom = (*it); 325 297 326 298 //apply diff 327 299 for ( int i = 0; i<length; i++ ) … … 331 303 else 332 304 stateTo->data[i] = data[i]; 333 334 } 335 305 306 } 307 336 308 //add state to state history 337 309 recvStates[userId].push_back( stateTo ); 338 310 339 311 int i = 0; 340 312 int n = 0; 341 313 std::list<int> changes; 342 314 343 315 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 344 316 { 345 317 if ( 346 (*it)->checkPermission( PERMISSION_ SERVER ) && networkStream->isUserServer( userId ) ||318 (*it)->checkPermission( PERMISSION_MASTER_SERVER ) && networkStream->isUserMasterServer( userId ) || 347 319 (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId || 348 networkStream->isUser Server( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) ||349 (*it)->checkPermission( PERMISSION_ALL ) 320 networkStream->isUserMasterServer( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) || 321 (*it)->checkPermission( PERMISSION_ALL ) 350 322 ) 351 323 { … … 361 333 else 362 334 { 363 // PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_ SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId );335 // PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_MASTER_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId ); 364 336 n = (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i ); 365 337 //NETPRINTF(0)("%s::setvar %s %d\n", getClassName(), (*it)->getName().c_str(), n); … … 370 342 371 343 this->varChangeHandler( changes ); 372 344 373 345 return i; 374 346 } … … 423 395 (*it)->data = NULL; 424 396 } 425 397 426 398 delete *it; 427 399 } 428 400 recvStates[userId].clear(); 429 401 } 430 402 431 403 if ( sentStates.size() > userId ) 432 404 { 433 405 434 406 for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) 435 407 { … … 439 411 (*it)->data = NULL; 440 412 } 441 413 442 414 delete *it; 443 415 } … … 448 420 /** 449 421 * this function is called after recieving a state. 450 * @param userId 451 * @param stateId 452 * @param fromStateId 422 * @param userId 423 * @param stateId 424 * @param fromStateId 453 425 */ 454 426 void Synchronizeable::handleRecvState( int userId, int stateId, int fromStateId ) … … 457 429 if ( recvStates.size() <= userId ) 458 430 recvStates.resize( userId+1 ); 459 431 460 432 //remove old states 461 433 StateHistory::iterator it = recvStates[userId].begin(); … … 485 457 StateHistory::iterator delIt = it; 486 458 it ++; 487 459 488 460 if ( (*delIt)->data ) 489 461 { … … 493 465 delete *delIt; 494 466 recvStates[userId].erase( delIt ); 495 467 496 468 continue; 497 469 } 498 470 it++; 499 471 } 500 472 501 473 StateHistory::iterator fromState = recvStates[userId].end(); 502 474 StateHistory::iterator toState = recvStates[userId].end(); 503 475 504 476 for ( it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ ) 505 477 { … … 508 480 if ( (*it)->stateId == fromStateId ) 509 481 fromState = it; 510 482 511 483 if ( fromState != recvStates[userId].end() && toState != recvStates[userId].end() ) 512 484 break; 513 485 } 514 486 515 487 // setStateDiff was not called and i know fromStateId 516 488 if ( fromState != recvStates[userId].end() && toState == recvStates[userId].end() ) 517 489 { 518 490 StateHistoryEntry * entry = new StateHistoryEntry; 519 491 520 492 entry->dataLength = (*fromState)->dataLength; 521 493 if ( entry->dataLength > 0 ) 522 494 { 523 495 entry->data = new byte[entry->dataLength]; 524 496 525 497 assert( (*fromState)->data ); 526 498 memcpy( entry->data, (*fromState)->data, entry->dataLength ); … … 528 500 else 529 501 entry->data = NULL; 530 502 531 503 entry->sizeList = (*fromState)->sizeList; 532 504 entry->stateId = stateId; 533 505 534 506 recvStates[userId].push_back(entry); 535 507 } … … 538 510 /** 539 511 * this function is called after sending a state 540 * @param userId 541 * @param stateId 542 * @param fromStateId 512 * @param userId 513 * @param stateId 514 * @param fromStateId 543 515 */ 544 516 void Synchronizeable::handleSentState( int userId, int stateId, int fromStateId ) … … 557 529 StateHistory::iterator delIt = it; 558 530 it ++; 559 531 560 532 if ( (*delIt)->data ) 561 533 { … … 565 537 delete *delIt; 566 538 sentStates[userId].erase( delIt ); 567 539 568 540 continue; 569 541 } … … 571 543 } 572 544 573 545 574 546 StateHistory::iterator fromState = sentStates[userId].end(); 575 547 StateHistory::iterator toState = sentStates[userId].end(); 576 548 577 549 for ( it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) 578 550 { … … 581 553 if ( (*it)->stateId == fromStateId ) 582 554 fromState = it; 583 555 584 556 if ( fromState != sentStates[userId].end() && toState != sentStates[userId].end() ) 585 557 break; 586 558 } 587 559 588 560 589 561 // getStateDiff was not called and i know fromStateId 590 562 if ( fromState != sentStates[userId].end() && toState == sentStates[userId].end() ) 591 563 { 592 564 StateHistoryEntry * entry = new StateHistoryEntry; 593 565 594 566 entry->dataLength = (*fromState)->dataLength; 595 567 if ( entry->dataLength > 0 ) 596 568 { 597 569 entry->data = new byte[entry->dataLength]; 598 570 599 571 assert( (*fromState)->data ); 600 572 memcpy( entry->data, (*fromState)->data, entry->dataLength ); … … 602 574 else 603 575 entry->data = NULL; 604 576 605 577 entry->sizeList = (*fromState)->sizeList; 606 578 entry->stateId = stateId; 607 579 608 580 sentStates[userId].push_back(entry); 609 581 } 610 611 } 612 613 614 582 583 } 584 585 586 -
branches/proxy/src/lib/network/synchronizeable.h
r8708 r9347 20 20 #include "synchronizeable_var/synchronizeable_bool.h" 21 21 #include "synchronizeable_var/synchronizeable_uint.h" 22 #include "synchronizeable_var/synchronizeable_ip.h" 22 23 23 24 … … 51 52 virtual ~Synchronizeable(); 52 53 53 void setIsServer( bool isServer );54 bool isServer();54 // void setIsServer( bool isServer ); 55 // bool isServer(); 55 56 56 57 virtual void varChangeHandler( std::list<int> & id ); … … 61 62 virtual void handleSentState( int userId, int stateId, int fromStateId ); 62 63 virtual void handleRecvState( int userId, int stateId, int fromStateId ); 63 64 64 65 void registerVar( SynchronizeableVar * var ); 65 66 int registerVarId( SynchronizeableVar * var ); … … 82 83 protected: 83 84 NetworkStream* networkStream; //!< reference network stream we are connected to 84 int state;85 // int state; 85 86 86 87 private: -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_bool.h
r7954 r9347 12 12 13 13 public: 14 SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );14 SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableBool(); 16 16 -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_float.h
r7954 r9347 12 12 13 13 public: 14 SynchronizeableFloat( float * ptrIn, float * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );14 SynchronizeableFloat( float * ptrIn, float * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableFloat(); 16 16 -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_int.h
r7954 r9347 12 12 13 13 public: 14 SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );14 SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableInt(); 16 16 -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_quaternion.h
r7954 r9347 14 14 15 15 public: 16 SynchronizeableQuaternion( Quaternion * ptrIn, Quaternion * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );16 SynchronizeableQuaternion( Quaternion * ptrIn, Quaternion * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 17 17 virtual ~SynchronizeableQuaternion(); 18 18 -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_string.h
r7954 r9347 15 15 16 16 public: 17 SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );17 SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 18 18 virtual ~SynchronizeableString(); 19 19 -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_uint.h
r7954 r9347 12 12 13 13 public: 14 SynchronizeableUInt( unsigned int * ptrIn, unsigned int * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );14 SynchronizeableUInt( unsigned int * ptrIn, unsigned int * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableUInt(); 16 16 -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_var.h
r7954 r9347 12 12 13 13 enum { 14 PERMISSION_SERVER = 1, 15 PERMISSION_OWNER = 2, 16 PERMISSION_ALL = 4 14 PERMISSION_MASTER_SERVER = 1, 15 PERMISSION_PROXY_SERVER = 2, 16 PERMISSION_OWNER = 4, 17 PERMISSION_ALL = 8 17 18 }; 18 19 … … 20 21 21 22 public: 22 SynchronizeableVar( void * ptrIn, void * ptrOut, std::string name, int length, int permission = PERMISSION_ SERVER, int priority = 0 );23 SynchronizeableVar( void * ptrIn, void * ptrOut, std::string name, int length, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 23 24 virtual ~SynchronizeableVar(); 24 25 … … 28 29 */ 29 30 inline bool beWatched(){ return this->bWatched; } 30 31 /** 31 32 /** 32 33 * set flag if synchronizeable wants to be informed on changes 33 34 */ … … 96 97 */ 97 98 inline void resetPriority() { this->priority = this->realPriority; } 98 99 99 100 /** 100 101 * reads actual size from buffer. this is used when size is not constant … … 104 105 */ 105 106 virtual inline int getSizeFromBuf( byte * buf, int maxLength ){ return this->getSize(); } 106 107 107 108 /** 108 109 * set variable id … … 110 111 */ 111 112 inline void setVarId( int id ){ this->varId = id; } 112 113 /** 113 114 /** 114 115 * get variable id 115 116 * @return variable id 116 117 */ 117 118 inline int getVarId(){ return varId; } 118 119 119 120 /** 120 121 * set hasChanged … … 122 123 */ 123 124 inline void setHasChanged( bool changed ){ this->changed = changed; } 124 125 /** 125 126 /** 126 127 * get hasChanged 127 128 * @return variable id 128 129 */ 129 130 inline bool getHasChanged(){ return changed; } 130 131 131 132 /** 132 133 * print out variable value … … 137 138 private: 138 139 bool bWatched; //!< true if synchronizeable wants to be informed on changes 139 140 140 141 int permission; //!< who is allowed to change this var 141 142 int priority; //!< priority assigned to var 142 143 int realPriority; //!< priority assigned to var, increased every time not sent 143 144 int varId; //!< id to identify varables 144 145 145 146 bool changed; //!< true if last readFromBuf changed anything 146 147 -
branches/proxy/src/lib/network/synchronizeable_var/synchronizeable_vector.h
r7954 r9347 14 14 15 15 public: 16 SynchronizeableVector( Vector * ptrIn, Vector * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );16 SynchronizeableVector( Vector * ptrIn, Vector * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 17 17 virtual ~SynchronizeableVector(); 18 18 -
branches/proxy/src/lib/network/tcp_server_socket.cc
r7954 r9347 136 136 _isListening = false; 137 137 } 138 -
branches/proxy/src/lib/network/tcp_server_socket.h
r7954 r9347 28 28 virtual void close(); 29 29 virtual void update() {}; 30 30 31 31 private: 32 32 TCPsocket listenSocket; … … 36 36 37 37 void init(); 38 38 39 39 }; 40 40 -
branches/proxy/src/lib/network/tcp_socket.h
r7954 r9347 1 1 /*! 2 * @file network_socket.h3 * Main interface for the network module. Manages all the modules2 * @file tcp_socket.h 3 * tcp socket network interface 4 4 5 5 */ … … 47 47 48 48 virtual void connectToServer( std::string host, int port ); 49 49 50 50 virtual void disconnectServer(); 51 virtual void reconnectToServer( std::string host, int port); 52 virtual void reconnectToServerSoft( std::string host, int port); 51 53 52 54 virtual bool writePacket(byte * data, int length); -
branches/proxy/src/lib/network/udp_socket.cc
r8802 r9347 74 74 { 75 75 this->init(); 76 this->ip = ip; 76 77 this->serverSocket = serverSocket; 77 78 this->userId = userId; … … 105 106 assert( serverSocket == NULL ); 106 107 107 IPaddress ip;108 109 108 this->randomByte = generateNewRandomByte(); 110 109 111 110 PRINTF(0)("connect to server %s on port %d\n", host.c_str(), port); 112 111 113 if ( SDLNet_ResolveHost( & ip, host.c_str(), port ) != 0 )112 if ( SDLNet_ResolveHost( &this->ip, host.c_str(), port ) != 0 ) 114 113 { 115 114 PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError() ); … … 126 125 } 127 126 128 int channel = SDLNet_UDP_Bind(socket, 1, & ip);127 int channel = SDLNet_UDP_Bind(socket, 1, &this->ip); 129 128 if ( channel == -1 ) 130 129 { … … 147 146 bOk = false; 148 147 socket = NULL; 149 } 148 149 this->ip.host = 0; 150 this->ip.port = 0; 151 } 152 153 154 /** 155 * reconnects to 156 * @param host new server address 157 * @param port new port number 158 * 159 * this terminates the current connection and starts a new connection to the new server 160 */ 161 void UdpSocket::reconnectToServer( std::string host, int port) 162 { 163 // first disconnect the old server 164 this->disconnectServer(); 165 166 // now connect to the new 167 this->connectToServer( host, port); 168 } 169 170 171 /** 172 * reconnects to 173 * @param host new server address 174 * @param port new port number 175 * 176 * this terminates the current connection and starts a new connection to the new server 177 */ 178 void UdpSocket::reconnectToServerSoft( std::string host, int port) 179 {} 180 150 181 151 182 /** … … 179 210 180 211 byte udpCmd = 0; 181 212 182 213 if ( networkPacket.length > 0 ) 183 214 { … … 189 220 else 190 221 return 0; 191 222 192 223 if ( !checkRandomByte( networkPacket.data[0] ) ) 193 224 return 0; … … 198 229 networkPacket.data = NULL; 199 230 } 200 231 201 232 if ( !checkUdpCmd( udpCmd ) ) 202 233 return 0; … … 207 238 { 208 239 int numrecv = SDLNet_UDP_Recv( socket, packet); 209 240 210 241 byte udpCmd = 0; 211 242 … … 218 249 else 219 250 return 0; 220 251 221 252 if ( !checkRandomByte( packet->data[0] ) ) 222 253 return 0; 223 254 224 255 if ( !checkUdpCmd( udpCmd ) ) 225 256 return 0; … … 283 314 return false; 284 315 } 285 316 286 317 if ( !this->serverSocket && ( udpCmd & UDPCMD_INVALIDRNDBYTE ) ) 287 318 { … … 292 323 return false; 293 324 } 294 325 295 326 return true; 296 327 } … … 300 331 srand( SDL_GetTicks() ); 301 332 byte res = ( rand() & 0xFC ); 302 333 303 334 PRINTF(0)("generated random byte: %x\n", res); 304 335 305 336 return res; 306 337 } -
branches/proxy/src/lib/network/udp_socket.h
r8802 r9347 31 31 32 32 virtual void connectToServer( std::string host, int port ); 33 34 33 virtual void disconnectServer(); 35 34 35 virtual void reconnectToServer( std::string host, int port); 36 virtual void reconnectToServerSoft( std::string host, int port); 37 36 38 virtual bool writePacket(byte * data, int length ); 39 virtual int readPacket(byte * data, int maxLength); 37 40 38 virtual int readPacket(byte * data, int maxLength); 39 41 40 42 private: 41 43 UdpServerSocket * serverSocket; //!< will get packets here 42 44 int userId; //!< user id used by serverSocket 43 IPaddress ip; //!< host,port44 45 UDPsocket socket; //!< socket used to send/recieve 45 46 UDPpacket * packet; 46 47 47 48 byte randomByte; //!< contains random bytes & 0xFC 48 49 49 50 bool writeRawPacket( byte * data, int length ); 50 51 bool checkUdpCmd( byte udpCmd ); 51 52 bool checkRandomByte( byte rndByte ); 52 53 byte generateNewRandomByte(); 53 54 54 55 void init(); 55 56
Note: See TracChangeset
for help on using the changeset viewer.