Changeset 9406 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Jul 24, 2006, 11:09:47 AM (18 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 3 deleted
- 41 edited
- 19 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/Makefile.am
r8623 r9406 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/network_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 38 47 39 48 40 noinst_HEADERS = synchronizeable.h \ 41 network_manager.h \ 42 shared_network_data.h \ 43 network_socket.h \ 44 connection_monitor.h \ 45 network_stream.h \ 46 data_stream.h \ 47 network_protocol.h \ 48 server_socket.h \ 49 tcp_server_socket.h \ 50 tcp_socket.h \ 51 udp_server_socket.h \ 52 udp_socket.h \ 53 handshake.h \ 54 network_game_manager.h \ 55 converter.h \ 56 netdefs.h \ 57 message_manager.h \ 58 network_log.h \ 59 zip.h \ 60 player_stats.h \ 61 udp_broadcast.h \ 62 \ 63 synchronizeable_var/synchronizeable_var.h \ 64 synchronizeable_var/synchronizeable_vector.h \ 65 synchronizeable_var/synchronizeable_int.h \ 66 synchronizeable_var/synchronizeable_quaternion.h \ 67 synchronizeable_var/synchronizeable_string.h \ 68 synchronizeable_var/synchronizeable_float.h \ 69 synchronizeable_var/synchronizeable_bool.h \ 70 synchronizeable_var/synchronizeable_uint.h 71 72 49 noinst_HEADERS = \ 50 netdefs.h \ 51 nettypes.h \ 52 netincl.h \ 53 \ 54 synchronizeable.h \ 55 network_manager.h \ 56 shared_network_data.h \ 57 network_socket.h \ 58 network_stream.h \ 59 data_stream.h \ 60 network_protocol.h \ 61 server_socket.h \ 62 tcp_server_socket.h \ 63 tcp_socket.h \ 64 udp_server_socket.h \ 65 udp_socket.h \ 66 handshake.h \ 67 network_game_manager.h \ 68 converter.h \ 69 peer_info.h \ 70 netdefs.h \ 71 message_manager.h \ 72 network_log.h \ 73 zip.h \ 74 player_stats.h \ 75 udp_broadcast.h \ 76 \ 77 proxy/network_settings.cc \ 78 \ 79 monitor/connection_monitor.h \ 80 monitor/network_monitor.h \ 81 monitor/network_node.h \ 82 \ 83 synchronizeable_var/synchronizeable_var.h \ 84 synchronizeable_var/synchronizeable_vector.h \ 85 synchronizeable_var/synchronizeable_int.h \ 86 synchronizeable_var/synchronizeable_quaternion.h \ 87 synchronizeable_var/synchronizeable_string.h \ 88 synchronizeable_var/synchronizeable_float.h \ 89 synchronizeable_var/synchronizeable_bool.h \ 90 synchronizeable_var/synchronizeable_uint.h \ 91 synchronizeable_var/synchronizeable_ip.h \ 92 \ 93 ip.h -
trunk/src/lib/network/converter.cc
r8362 r9406 10 10 11 11 ### File Specific: 12 main-programmer: Benjamin Wuest 13 co-programmer: ...12 main-programmer: Benjamin Wuest (bwuest@ee.ethz.ch) 13 co-programmer: Christoph Renner (rennerc@ee.ethz.ch) 14 14 */ 15 15 … … 30 30 31 31 /* using namespace std is default, this needs to be here */ 32 using namespace std; 32 33 33 34 34 /*! … … 38 38 { 39 39 /* set the class id for the base object */ 40 //this->setClassID(CL_ENTITY_MANAGER, "EntityManager");41 40 } 42 41 -
trunk/src/lib/network/data_stream.cc
r8623 r9406 19 19 20 20 /* using namespace std is default, this needs to be here */ 21 using namespace std; 21 22 22 23 23 -
trunk/src/lib/network/handshake.cc
r9235 r9406 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 35 // register all variable handlers 35 36 orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId", PERMISSION_ALL ) ); 36 37 version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version", PERMISSION_ALL ) ); … … 42 43 errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString", PERMISSION_ALL ) ); 43 44 45 this->nodeTypeHandler = registerVarId( new SynchronizeableInt( &localState.nodeType, &remoteState.nodeType, "nodeType", PERMISSION_ALL ) ); 46 44 47 candel_id = registerVarId( new SynchronizeableInt( &localState.canDel, &remoteState.canDel, "canDel", PERMISSION_ALL ) ); 45 48 46 49 registerVar( new SynchronizeableString( &localState.preferedNickName, &remoteState.preferedNickName, "preferedNickName", PERMISSION_ALL ) ); 50 // now synchronize only two of the available proxy server addresses 51 registerVar( new SynchronizeableIP( &this->proxy1, &this->proxy1, "proxy server 1", PERMISSION_MASTER_SERVER ) ); 52 registerVar( new SynchronizeableIP( &this->proxy2, &this->proxy2, "proxy server 2", PERMISSION_MASTER_SERVER ) ); 53 registerVar( new SynchronizeableInt( &this->redirectProxy, &this->redirectProxy, "proxy server redirection flag", PERMISSION_MASTER_SERVER ) ); 47 54 55 56 // init the local state 48 57 localState.completed = 0; 49 58 localState.error = 0; … … 54 63 localState.orxId = _ORXONOX_ID; 55 64 localState.version = _ORXONOX_VERSION; 65 localState.nodeType = nodeType; 56 66 localState.canDel = 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; 67 80 81 82 this->proxy1 = IP(0, 0); 83 this->proxy2 = IP(0, 0); 84 this->redirectProxy = 0; 85 86 87 // activate the synchronization process 68 88 this->setSynchronized(true); 69 89 PRINTF(0)("Handshake created clientId = %d\n", clientId); 70 90 } 91 71 92 72 93 /** … … 78 99 for ( std::list<int>::iterator it = id.begin(); it != id.end(); it++ ) 79 100 { 80 if ( *it == orxId_handler ) 101 // orxonox id handler 102 if ( *it == this->orxId_handler ) 81 103 { 82 104 if ( remoteState.orxId != _ORXONOX_ID ) … … 87 109 continue; 88 110 } 89 90 111 } 91 112 92 if ( *it == version_handler ) 113 // orxonox version handler 114 if ( *it == this->version_handler ) 93 115 { 94 116 if ( remoteState.version != _ORXONOX_VERSION ) … … 99 121 continue; 100 122 } 101 102 123 } 103 124 125 // node type handler: for each node type there is a specific action to be taken 126 if ( *it == this->nodeTypeHandler) 127 { 128 if ( remoteState.nodeType == NET_MASTER_SERVER ) 129 { 130 continue; 131 } 132 else if( remoteState.nodeType == NET_PROXY_SERVER_ACTIVE) 133 { 134 continue; 135 } 136 else if( remoteState.nodeType == NET_CLIENT) 137 { 138 continue; 139 } 140 } 141 142 // cancel 104 143 if ( *it == candel_id ) 105 144 { 106 145 PRINTF(0)("handshake finished candel changed\n"); 107 146 } 108 109 147 } 110 148 111 if (112 113 remoteState.version == _ORXONOX_VERSION114 )149 // handshake completed 150 if ( remoteState.orxId == _ORXONOX_ID && 151 remoteState.version == _ORXONOX_VERSION ) 152 { 115 153 localState.completed = 1; 154 } 116 155 } 117 156 -
trunk/src/lib/network/handshake.h
r9235 r9406 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" 16 #include "shared_network_data.h" 11 17 12 #define _ORXONOX_ID 0xF91337A0 18 //!< a struct to save the handshakes to 19 struct HandshakeState 20 { 21 int orxId; //!< orxonox id 22 int version; //!< network protocol version 13 23 14 #define _ORXONOX_VERSION 1 24 int networkManagerId; //!< unique id of the network manager 25 int messageManagerId; //!< unique id of the message manager 26 int hostId; //!< host id 27 int nodeType; //!< type of the network node 15 28 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 29 int completed; //!< true if completed 30 int canDel; //!< true if marked for deletion 31 32 int error; //!< error number 33 34 std::string errorString; //!< error string 35 31 36 //additional data 32 std::string preferedNickName;37 std::string preferedNickName; //!< prefered nick name 33 38 }; 34 39 40 41 //!< the handshake itself with some interface functions 35 42 class Handshake : public Synchronizeable 36 43 { 44 37 45 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; } 46 Handshake( int nodeType, int clientId = 0, int networkGameManagerId = 0, int messageManagerId = 0 ); 47 48 49 /* functions indicating states of the handshake */ 50 /** @returns true if the handshake is completed */ 51 inline bool completed(){ return localState.completed != 0 && remoteState.completed != 0; } 52 /** @returns true if no error has occured until now */ 53 inline bool ok(){ return localState.error == 0 && remoteState.error == 0; } 54 /** stops the handshake and reject the other side with @param reason: string describing the reason */ 55 inline void doReject( std::string reason ){ localState.error = 1; localState.errorString = "the server rejected your connection ["+ reason +"]"; } 56 /** @returns true if the handshake is finished and the instances can be deleted */ 57 inline bool canDel(){ return localState.canDel == 1 && remoteState.canDel == 1; } 58 /** @returns true if the local state can be removed*/ 59 inline bool allowDel(){ return localState.canDel == 1; } 60 /** marks the handshake to be deleted */ 61 inline void del(){ localState.canDel = 1; } 62 63 64 /* the actual informations exchanged in the handshake */ 65 /** @returns the host id of the remote host */ 66 inline int getHostId(){ return remoteState.hostId; } 67 /** @returns the unique id of the network game manager*/ 68 inline int getNetworkGameManagerId(){ return remoteState.networkManagerId; } 69 /** @returns the unique id of the message manager */ 70 inline int getMessageManagerId(){ return remoteState.messageManagerId; } 71 /** @returns the node type of the remote host */ 72 inline int getRemoteNodeType() { return this->remoteState.nodeType; } 73 74 /** sets @param nick the prefereded nick name */ 75 inline void setPreferedNickName( const std::string & nick ){ localState.preferedNickName = nick; } 76 /** @returns the prefered nick name */ 50 77 inline std::string getPreferedNickName(){ return remoteState.preferedNickName; } 51 78 79 /** @returns if true the local client should reconnect to a proxy server from the proxy server list */ 80 inline bool redirect() { return this->redirectProxy;} 81 /** @param flag: indicating if the client should be redirected */ 82 inline void setRedirect(bool flag) { if( SharedNetworkData::getInstance()->isClient()) return; this->redirectProxy = (int)flag; } 83 84 /** @param address: the address of the proxy server 1 if any */ 85 inline void setProxy1Address(IP address) { if( SharedNetworkData::getInstance()->isClient()) return; this->proxy1 = address; } 86 /** @returns the address of the proxy server 1 if any */ 87 inline IP getProxy1Address() { return this->proxy1; } 88 /** @param address: the address of the proxy server 2 if any */ 89 inline void setProxy2Address(IP address) { if( SharedNetworkData::getInstance()->isClient()) return; this->proxy2 = address; } 90 /** @returns the address of the proxy server 2 if any */ 91 inline IP getProxy2Address() { return this->proxy2; } 92 93 94 /* variable handler function */ 52 95 virtual void varChangeHandler( std::list<int> & id ); 53 96 97 54 98 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; 99 HandshakeState localState; //!< the local handshake state 100 HandshakeState remoteState; //!< the remote handshake state 67 101 102 int orxId_handler; //!< orxonox id handler 103 int version_handler; //!< orxonox version id handler 104 int netManId_handler; //!< network manager handler 105 int msgManId_handler; //!< message manager handler 106 int hostId_handler; //!< host id handler 107 int nodeTypeHandler; //!< node type handler 108 int proxy1Handler; //!< handler for the proxy 109 int proxy2Handler; //!< handler for the proxy 110 111 int completed_handler; //!< handshake completion handler 112 int error_handler; //!< handshake error handler 113 int errorString_handler; //!< handshake error string handler 114 int candel_id; //!< handshake deletion handler 115 int nodeType; //!, the type of the network node 116 117 int redirectProxy; //!< true if the client should reconnect to a proxy server 118 IP proxy1; //!< ip address of the first proxy (0.0.0.0 of not available) 119 IP proxy2; //!< ip address of the second proxy (0.0.0.0 of not available) 68 120 }; 69 121 -
trunk/src/lib/network/message_manager.cc
r9059 r9406 20 20 #include "network_stream.h" 21 21 #include "shared_network_data.h" 22 23 using namespace std; 22 #include "converter.h" 23 #include <cassert> 24 25 24 26 25 27 MessageManager* MessageManager::singletonRef = NULL; … … 52 54 } 53 55 } 54 56 55 57 it->second.messages.clear(); 56 58 it->second.toAck.clear(); 57 59 } 58 60 59 61 messageQueue.clear(); 60 62 61 63 this->messageHandlerMap.clear(); 62 64 63 65 MessageManager::singletonRef = NULL; 64 66 } … … 82 84 int i = 0; 83 85 int n; 84 86 85 87 n = Converter::intToByteArray( messageQueue[userId].toAck.size(), data + i, maxLength ); 86 88 i += n; 87 89 assert( n == INTSIZE ); 88 90 89 91 for ( std::list<int>::iterator it = messageQueue[userId].toAck.begin(); it != messageQueue[userId].toAck.end(); it++) 90 92 { … … 93 95 assert( n == INTSIZE ); 94 96 } 95 97 96 98 messageQueue[userId].toAck.clear(); 97 99 98 100 n = Converter::intToByteArray( messageQueue[userId].messages.size(), data + i, maxLength ); 99 101 i += n; 100 102 assert( n == INTSIZE ); 101 103 102 104 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ ) 103 105 { … … 105 107 i += n; 106 108 assert( n == INTSIZE ); 107 109 108 110 n = Converter::intToByteArray( it->number, data + i, maxLength ); 109 111 i += n; 110 112 assert( n == INTSIZE ); 111 113 112 114 n = Converter::intToByteArray( it->messageId, data + i, maxLength ); 113 115 i += n; 114 116 assert( n == INTSIZE ); 115 117 116 118 assert( i + it->length <= maxLength ); 117 119 memcpy( data + i, it->data, it->length ); 118 120 i += it->length; 119 121 } 120 122 121 123 return i; 122 124 } … … 136 138 int i = 0; 137 139 int n; 138 140 139 141 int nAcks; 140 142 141 143 assert( i + INTSIZE <= length ); 142 144 n = Converter::byteArrayToInt( data + i, &nAcks ); 143 145 assert( n == INTSIZE ); 144 146 i += n; 145 147 146 148 std::list<int> acks; 147 149 148 150 int number; 149 151 150 152 for ( int j = 0; j < nAcks; j++ ) 151 153 { … … 154 156 assert( n == INTSIZE ); 155 157 i += n; 156 158 157 159 acks.push_back( number ); 158 160 } 159 161 160 162 int nMessages; 161 163 162 164 assert( i + INTSIZE <= length ); 163 165 n = Converter::byteArrayToInt( data + i, &nMessages ); … … 166 168 167 169 int messageLength, messageId; 168 170 169 171 for ( int j = 0; j < nMessages; j++ ) 170 172 { … … 173 175 assert( n == INTSIZE ); 174 176 i += n; 175 177 176 178 assert( i + INTSIZE <= length ); 177 179 n = Converter::byteArrayToInt( data + i, &number ); 178 180 assert( n == INTSIZE ); 179 181 i += n; 180 182 181 183 assert( i + INTSIZE <= length ); 182 184 n = Converter::byteArrayToInt( data + i, &messageId ); 183 185 assert( n == INTSIZE ); 184 186 i += n; 185 187 186 188 if ( number > 0 ) 187 189 messageQueue[userId].toAck.push_back( number ); 188 190 189 191 assert( i + messageLength <= length ); 190 192 assert( messageHandlerMap.find( (MessageId)messageId ) != messageHandlerMap.end() ); … … 194 196 { 195 197 NetworkMessage msg; 196 198 197 199 msg.data = new byte[messageLength]; 198 200 memcpy( msg.data, data + i, messageLength ); … … 200 202 msg.messageId = (MessageId)messageId; 201 203 msg.number = userId; 202 204 203 205 incomingMessageBuffer.push_back( msg ); 204 206 } … … 207 209 i += messageLength; 208 210 } 209 210 211 212 211 213 //TODO maybe handle incomingMessage in tick function. else local messages will not be handled if no clients are connected 212 214 for ( std::list<NetworkMessage>::iterator it = incomingMessageBuffer.begin(); it != incomingMessageBuffer.end(); ) … … 223 225 it++; 224 226 } 225 227 226 228 //walk throu message queue and remove acked messages 227 229 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); ) … … 236 238 it++; 237 239 } 238 240 239 241 //TODO find bether way. maybe with timestamp 240 242 if ( messageQueue[userId].recievedMessages.size() > 1000 ) … … 255 257 if ( messageQueue.find( userId ) == messageQueue.end() ) 256 258 return; 257 259 258 260 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ ) 259 261 { … … 262 264 it->data = NULL; 263 265 } 264 266 265 267 messageQueue[userId].toAck.clear(); 266 268 267 269 messageQueue.erase( userId ); 268 270 } … … 270 272 /** 271 273 * registers function to handle messages with id messageId. someData is passed to callbackfuntion 272 * @param messageId message id to handle 274 * @param messageId message id to handle 273 275 * @param cb function pointer to callback function 274 276 * @param someData this pointer is passed to callback function without modification … … 278 280 { 279 281 MessageHandler messageHandler; 280 282 281 283 messageHandler.cb = cb; 282 284 messageHandler.messageId = messageId; 283 285 messageHandler.someData = someData; 284 286 285 287 messageHandlerMap[messageId] = messageHandler; 286 288 287 289 return true; 288 290 } … … 309 311 * @param data pointer to data 310 312 * @param dataLength length of data 311 * @param recieverType 312 * @param reciever 313 * @param recieverType 314 * @param reciever 313 315 */ 314 316 void MessageManager::sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ) … … 316 318 for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ ) 317 319 { 318 if ( 320 if ( 319 321 recieverType == RT_ALL_ME || 320 322 recieverType == RT_ALL_NOT_ME || 321 323 recieverType == RT_USER && it->first == reciever || 322 324 recieverType == RT_NOT_USER && it->first != reciever || 323 recieverType == RT_SERVER && getNetworkStream()->isUser Server( it->first )325 recieverType == RT_SERVER && getNetworkStream()->isUserMasterServer( it->first ) 324 326 ) 325 327 { … … 336 338 } 337 339 } 338 340 339 341 if ( recieverType == RT_ALL_ME ) 340 342 { -
trunk/src/lib/network/netdefs.h
r9246 r9406 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 #ifndef _NETDEFS 10 #define _NETDEFS 24 #ifndef _NETDEFS_H 25 #define _NETDEFS_H 11 26 12 #ifdef HAVE_SDL_NET_H 13 #include <SDL_net.h> 14 #else 15 #include <SDL/SDL_net.h> 16 #endif 27 #include "nettypes.h" 28 #include "netincl.h" 17 29 18 30 19 #define MAX_CONNECTIONS 100020 31 32 //!< the amount of slots used before a proxy server is activated 33 #define NET_CONNECTION_SATURATION 0.75 34 35 //!< network polling frequency 21 36 #define NETWORK_FREQUENCY 66 22 37 23 38 24 typedef unsigned char byte; 39 //!< orxonox protocol id 40 #define _ORXONOX_ID 0xF91337A0 41 //!< orxonox network version identifier 42 #define _ORXONOX_VERSION 1 25 43 26 44 27 45 //!< enum indicating the type of the node 28 46 typedef enum { 29 NET_SERVER, 30 NET_CLIENT 47 NET_MASTER_SERVER, 48 NET_PROXY_SERVER_ACTIVE, 49 NET_PROXY_SERVER_PASSIVE, 50 NET_CLIENT, 51 52 NET_NR_TYPES 53 31 54 } NodeType; 32 55 … … 44 67 } UidType; 45 68 46 #endif /* _NET WORK_MANAGER*/69 #endif /* _NETDEFS_H */ -
trunk/src/lib/network/network_game_manager.cc
r9235 r9406 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 using namespace std; 48 49 49 50 50 NetworkGameManager* NetworkGameManager::singletonRef = NULL; … … 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() || SharedNetworkData::getInstance()->isProxyServer()); 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() || SharedNetworkData::getInstance()->isProxyServer()) 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() || SharedNetworkData::getInstance()->isProxyServer()); 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() || SharedNetworkData::getInstance()->isProxyServer()) 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 { 290 PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClass Name(), (*it)->getOwner() );289 { 290 PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassCName(), (*it)->getOwner() ); 291 291 std::list<Playable*>::iterator delit = it; 292 292 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() || SharedNetworkData::getInstance()->isProxyServer()) && 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() || SharedNetworkData::getInstance()->isProxyServer()) 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 } -
trunk/src/lib/network/network_game_manager.h
r8708 r9406 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 }; -
trunk/src/lib/network/network_manager.cc
r9059 r9406 10 10 11 11 ### File Specific: 12 main-programmer: Patrick Boenzli 13 co-programmer: ...12 main-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) 13 co-programmer: Christoph Renner (rennerc@ee.ethz.ch) 14 14 */ 15 15 … … 34 34 35 35 /* using namespace std is default, this needs to be here */ 36 using namespace std; 36 37 37 38 38 SHELL_COMMAND(debug, NetworkManager, debug); … … 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 } … … 95 95 { 96 96 97 } 98 99 100 101 /** 102 * creates a new NetworkStream of server type 103 * @param port: number of the TCP port 104 */ 105 int NetworkManager::createMasterServer(unsigned int port) 106 { 107 // load the network settings 108 NetworkSettings::getInstance()->loadData(); 109 110 // create the network stream 111 this->networkStream = new NetworkStream(NET_MASTER_SERVER); 112 this->networkStream->createServer( port); 113 114 // start the network game manager 115 this->networkStream->createNetworkGameManager(); 116 117 PRINTF(0)("Created Network Master Server\n"); 118 SDL_Delay(20); 119 return 1; 120 } 121 122 /** 123 * creates a new network stream of proxy server type 124 * @param port: number of the TCP port 125 */ 126 int NetworkManager::createProxyServer(unsigned int port) 127 { 128 // load the network settings 129 NetworkSettings::getInstance()->loadData(); 130 131 // create the network stream 132 this->networkStream = new NetworkStream(NET_PROXY_SERVER_ACTIVE); 133 this->networkStream->createServer( port); 134 // and connect to the master server for synchronization 135 // this->networkStream->connectToMasterServer(NetworkSettings::getInstance()->getMasterAddr()); 136 // and to the other proxy servers 137 138 139 140 // start the network game manager 141 this->networkStream->createNetworkGameManager(); 142 143 144 PRINTF(0)("Created Network Proxy Server\n"); 145 SDL_Delay(20); 146 return 1; 97 147 } 98 148 … … 102 152 * @param hostName: the name of the destination host 103 153 */ 104 int NetworkManager::establishConnection(const std::string & name, unsigned int port) 105 { 106 this->networkStream = new NetworkStream( name, port ); 107 SharedNetworkData::getInstance()->setDefaultSyncStream(this->networkStream); 154 int NetworkManager::createClient(const std::string & name, unsigned int port) 155 { 156 // load the network settings 157 NetworkSettings::getInstance()->loadData(); 158 159 // create the network stream 160 this->networkStream = new NetworkStream(NET_CLIENT); 161 // connect to the master server, if a redirection should occure, this is handled in the NetworkStream itself 162 this->networkStream->connectToMasterServer( name, port); 163 164 // and start the handshake 108 165 this->networkStream->startHandshake(); 166 167 PRINTF(0)("Created Network Client"); 109 168 return 1; 110 169 } … … 112 171 113 172 /** 114 * creates a new NetworkStream of server type 115 * @param port: number of the TCP port 116 */ 117 int NetworkManager::createServer(unsigned int port) 118 { 119 SharedNetworkData::getInstance()->setHostID(0); 120 SharedNetworkData::getInstance()->setGameServer(true); 121 this->networkStream = new NetworkStream(port); 122 SharedNetworkData::getInstance()->setDefaultSyncStream(this->networkStream); 123 this->networkStream->createNetworkGameManager(); 124 PRINTF(0)("CREATE SERVER\n"); 125 SDL_Delay(20); 126 return 1; 127 } 128 129 173 * connects a synchronizeable to the network stream 174 * @param sync: synchronizeable to connect 175 */ 130 176 void NetworkManager::connectSynchronizeable(Synchronizeable& sync) 131 177 { … … 144 190 if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) 145 191 return; 192 146 193 this->elapsedTime = 0.0f; 147 194 148 if ( networkStream )195 if ( this->networkStream ) 149 196 networkStream->processData(); 150 197 151 198 NetworkGameManager::getInstance()->tick( this->elapsedTime ); 152 199 } -
trunk/src/lib/network/network_manager.h
r9059 r9406 36 36 void shutdown(); 37 37 38 int establishConnection( const std::string & name, unsigned int port); 39 int createServer(unsigned int port); 40 41 /** Returns the hostID @return The hostID of the object */ 42 inline int getHostID() { return SharedNetworkData::getInstance()->getHostID(); } 43 inline bool isGameServer() { return SharedNetworkData::getInstance()->isGameServer(); } 44 38 int createClient( const std::string & name, unsigned int port); 39 int createMasterServer( unsigned int port); 40 int createProxyServer( unsigned int port); 45 41 46 42 void connectSynchronizeable(Synchronizeable& sync); … … 56 52 private: 57 53 static NetworkManager* singletonRef; //!< Pointer to the only instance of this Class 58 NetworkStream* networkStream; //!< pointer to network stream54 NetworkStream* networkStream; //!< pointer to network stream 59 55 60 56 float elapsedTime; //!< elapsed time since the last network update -
trunk/src/lib/network/network_protocol.cc
r6341 r9406 33 33 34 34 /* using namespace std is default, this needs to be here */ 35 using namespace std; 35 36 36 37 37 -
trunk/src/lib/network/network_socket.cc
r7954 r9406 33 33 { 34 34 bOk = true; 35 this->ip.host = 0; 36 this->ip.port = 0; 35 37 } 36 38 -
trunk/src/lib/network/network_socket.h
r7954 r9406 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 -
trunk/src/lib/network/network_stream.cc
r9246 r9406 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 */ 48 using namespace std; 52 53 #include "converter.h" 49 54 50 55 … … 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 client 65 * @param host: host name (address) 66 * @param port: port number 67 */ 68 NetworkStream::NetworkStream( std::string host, int port ) 69 { 70 this->type = NET_CLIENT; 67 this->pInfo->nodeType = NET_CLIENT; 68 } 69 70 71 NetworkStream::NetworkStream( int nodeType) 72 { 71 73 this->init(); 72 this->peers[0].socket = new UdpSocket( host, port ); 73 this->peers[0].userId = 0; 74 this->peers[0].isServer = true; 75 this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); 76 } 77 78 79 /** 80 * start as a server 81 * @param port: at this port 82 */ 83 NetworkStream::NetworkStream( int port ) 84 { 85 this->type = NET_SERVER; 86 this->init(); 87 this->serverSocket = new UdpServerSocket(port); 88 this->bActive = true; 89 } 74 75 this->pInfo->nodeType = nodeType; 76 77 switch( nodeType) 78 { 79 case NET_MASTER_SERVER: 80 // init the shared network data 81 SharedNetworkData::getInstance()->setHostID(0); 82 // SharedNetworkData::getInstance()->setNodeType(NET_MASTER_SERVER); 83 break; 84 85 case NET_PROXY_SERVER_ACTIVE: 86 // init the shared network data 87 SharedNetworkData::getInstance()->setHostID(0); 88 // SharedNetworkData::getInstance()->setNodeType(NET_PROXY_SERVER_ACTIVE); 89 break; 90 case NET_PROXY_SERVER_PASSIVE: 91 // init the shared network data 92 SharedNetworkData::getInstance()->setHostID(0); 93 // SharedNetworkData::getInstance()->setNodeType(NET_PROXY_SERVER_PASSIVE); 94 break; 95 case NET_CLIENT: 96 // SharedNetworkData::getInstance()->setNodeType(NET_CLIENT); 97 break; 98 } 99 100 SharedNetworkData::getInstance()->setDefaultSyncStream(this); 101 102 // get the local ip address 103 IPaddress ip; 104 SDLNet_ResolveHost( &ip, NULL, 0); 105 this->pInfo->ip = ip; 106 } 107 90 108 91 109 … … 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; 179 } 180 181 182 /** 183 * establish a connection to a remote master server 184 * @param host: host name 185 * @param port: the port number 186 */ 187 void NetworkStream::connectToMasterServer(std::string host, int port) 188 { 189 int node = this->peers.size(); 190 this->peers[node].socket = new UdpSocket( host, port ); 191 this->peers[node].userId = 0; 192 193 this->peers[node].nodeType = NET_MASTER_SERVER; 194 this->peers[node].connectionMonitor = new ConnectionMonitor( 0 ); 195 this->peers[node].ip = this->peers[node].socket->getRemoteAddress(); 196 } 197 198 199 /** 200 * establish a connection to a remote proxy server 201 * @param host: host name 202 * @param port: the port number 203 */ 204 void NetworkStream::connectToProxyServer(std::string host, int port) 205 { 206 int node = this->peers.size(); 207 this->peers[node].socket = new UdpSocket( host, port ); 208 this->peers[node].userId = 0; 209 210 this->peers[node].nodeType = NET_PROXY_SERVER_ACTIVE; 211 this->peers[node].connectionMonitor = new ConnectionMonitor( 0 ); 212 this->peers[node].ip = this->peers[node].socket->getRemoteAddress(); 213 } 214 215 216 /** 217 * create a server 218 * @param port: interface port for all clients 219 */ 220 void NetworkStream::createServer(int port) 221 { 222 this->serverSocket = new UdpServerSocket(port); 149 223 } 150 224 … … 156 230 { 157 231 this->networkGameManager = NetworkGameManager::getInstance(); 158 // setUniqueID( maxCon+2 ) because we need one id for every handshake 159 // and one for handshake to reject client maxCon+1 232 160 233 this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); 161 234 MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); … … 165 238 /** 166 239 * starts the network handshake 240 * handsakes are always initialized from the client side first. this starts the handshake and therefore is only 241 * executed as client 167 242 */ 168 243 void NetworkStream::startHandshake() 169 244 { 170 Handshake* hs = new Handshake( false);245 Handshake* hs = new Handshake(this->pInfo->nodeType); 171 246 hs->setUniqueID( 0 ); 172 247 assert( peers[0].handshake == NULL ); 173 248 peers[0].handshake = hs; 174 249 250 // set the preferred nick name 175 251 hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) ); 176 252 177 // peers[0].handshake->setSynchronized( true ); 178 //this->connectSynchronizeable(*hs); 179 //this->connectSynchronizeable(*hs); 180 PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); 253 PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getCName()); 181 254 } 182 255 … … 192 265 sync.setNetworkStream( this ); 193 266 194 this->bActive = true;267 // this->bActive = true; 195 268 } 196 269 … … 215 288 void NetworkStream::processData() 216 289 { 290 // create the network monitor after all the init work and before there is any connection handlings 291 if( this->networkMonitor == NULL) 292 this->networkMonitor = new NetworkMonitor(this); 293 294 217 295 int tick = SDL_GetTicks(); 218 296 219 currentState++; 220 221 if ( this->type == NET_SERVER ) 222 { 297 this->currentState++; 298 // there was a wrap around 299 if( this->currentState < 0) 300 { 301 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"); 302 } 303 304 if ( this->pInfo->isMasterServer()) 305 { 306 // execute everytthing the master server shoudl do 307 if ( serverSocket ) 308 serverSocket->update(); 309 310 this->updateConnectionList(); 311 } 312 else if( this->pInfo->isProxyServer()) 313 { 314 // execute everything the proxy server should do 223 315 if ( serverSocket ) 224 316 serverSocket->update(); … … 249 341 cleanUpOldSyncList(); 250 342 handleHandshakes(); 343 344 // update the network monitor 345 this->networkMonitor->process(); 251 346 252 347 // order of up/downstream is important!!!! … … 258 353 259 354 /** 260 * if we are a server update the connection list to accept new connections (clients)261 * also start the handsake for the new clients355 * if we are a NET_MASTER_SERVER or NET_PROXY_SERVER_ACTIVE update the connection list to accept new 356 * connections (clients) also start the handsake for the new clients 262 357 */ 263 358 void NetworkStream::updateConnectionList( ) … … 276 371 clientId = freeSocketSlots.back(); 277 372 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 373 } 285 374 else … … 290 379 if ( it->first >= clientId ) 291 380 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]); 381 } 382 peers[clientId].socket = tempNetworkSocket; 383 384 385 // create new handshake and init its variables 386 peers[clientId].handshake = new Handshake(this->pInfo->nodeType, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); 387 peers[clientId].handshake->setUniqueID(clientId); 388 389 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); 390 peers[clientId].userId = clientId; 391 392 PRINTF(0)("num sync: %d\n", synchronizeables.size()); 393 394 // get the proxy server informations and write them to the handshake, if any (proxy) 395 assert( this->networkMonitor != NULL); 396 PeerInfo* pi = this->networkMonitor->getFirstChoiceProxy(); 397 if( pi != NULL) 398 { 399 peers[clientId].handshake->setProxy1Address( pi->ip); 400 } 401 pi = this->networkMonitor->getSecondChoiceProxy(); 402 if( pi != NULL) 403 peers[clientId].handshake->setProxy2Address( pi->ip); 404 405 // check if the connecting client should reconnect to a proxy server 406 peers[clientId].handshake->setRedirect(/*this->networkMonitor->isReconnectNextClient()*/false); 407 408 // the connecting node of course is a client 409 peers[clientId].nodeType = NET_CLIENT; 410 peers[clientId].ip = peers[clientId].socket->getRemoteAddress(); 411 412 413 // check if there are too many clients connected (DEPRECATED: new: the masterserver sends a list of proxy servers) 414 // if ( clientId > SharedNetworkData::getInstance()->getMaxPlayer() ) 415 // { 416 // // peers[clientId].handshake->setRedirect(true); 417 // // 418 // // peers[clientId].handshake->doReject( "too many connections" ); 419 // PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); 420 // } 421 // else 422 // { 423 // PRINTF(0)("New Client: %d\n", clientId); 424 // } 425 PRINTF(0)("New Client: %d\n", clientId); 426 427 315 428 } 316 429 … … 339 452 it->second.socket = NULL; 340 453 454 // remove the old connectin monitor 341 455 if ( it->second.connectionMonitor ) 342 456 delete it->second.connectionMonitor; 343 457 it->second.connectionMonitor = NULL; 344 458 459 // remove the handshake 345 460 if ( it->second.handshake ) 346 461 delete it->second.handshake; 347 462 it->second.handshake = NULL; 348 463 464 // and cleanup the user infos 349 465 for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) 350 466 { … … 373 489 void NetworkStream::debug() 374 490 { 375 if( this->isServer()) 376 PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); 377 else 378 PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); 491 if( SharedNetworkData::getInstance()->isMasterServer()) { 492 PRINT(0)(" Host ist Master Server with ID: %i\n", this->pInfo->userId); 493 } 494 else if( SharedNetworkData::getInstance()->isProxyServer()) { 495 PRINT(0)(" Host ist Proxy Server with ID: %i\n", this->pInfo->userId); 496 } 497 else { 498 PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); 499 } 379 500 380 501 PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); … … 382 503 { 383 504 if( (*it)->beSynchronized() == true) 384 PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClass Name(), (*it)->getName(),505 PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassCName(), (*it)->getCName(), 385 506 (*it)->getUniqueID(), (*it)->beSynchronized()); 386 507 } 387 PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS);508 PRINT(0)(" Maximal Connections: %i\n", SharedNetworkData::getInstance()->getMaxPlayer() ); 388 509 389 510 } … … 414 535 if ( it->second.handshake ) 415 536 { 537 // handshake finished 416 538 if ( it->second.handshake->completed() ) 417 539 { 540 //handshake is correct 418 541 if ( it->second.handshake->ok() ) 419 542 { 543 // the server gave it free for deletion 420 544 if ( !it->second.handshake->allowDel() ) 421 545 { 422 if ( type != NET_SERVER ) 546 547 if ( this->pInfo->isClient() ) 423 548 { 424 549 SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); 425 myHostId = SharedNetworkData::getInstance()->getHostID(); 426 550 this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); 551 552 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 553 it->second.ip = it->second.socket->getRemoteAddress(); 554 // add the new server to the nodes list (it can be a NET_MASTER_SERVER or NET_PROXY_SERVER) 555 this->networkMonitor->addNode(&it->second); 556 // get proxy 1 address and add it 557 this->networkMonitor->addNode(it->second.handshake->getProxy1Address(), NET_PROXY_SERVER_ACTIVE); 558 // get proxy 2 address and add it 559 this->networkMonitor->addNode(it->second.handshake->getProxy2Address(), NET_PROXY_SERVER_ACTIVE); 560 561 // now check if the server accepted the connection 562 if( it->second.handshake->redirect()) 563 this->handleReconnect( it->second.userId); 564 565 // create the new network game manager and init it 427 566 this->networkGameManager = NetworkGameManager::getInstance(); 428 567 this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); 568 // init the new message manager 429 569 MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); 430 570 } … … 432 572 433 573 PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); 434 435 574 it->second.handshake->del(); 436 575 } 437 576 else 438 577 { 578 // handshake finished registring new player 439 579 if ( it->second.handshake->canDel() ) 440 580 { 441 if ( type == NET_SERVER ) 581 582 if ( this->pInfo->isMasterServer() ) 442 583 { 443 handleNewClient( it->second.userId ); 584 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 585 it->second.ip = it->second.socket->getRemoteAddress(); 586 587 this->networkMonitor->addNode(&it->second); 588 589 this->handleNewClient( it->second.userId ); 590 591 if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) 592 { 593 PlayerStats::getStats( it->second.userId )->setNickName( it->second.handshake->getPreferedNickName() ); 594 } 595 } 596 else if ( this->pInfo->isProxyServer() ) 597 { 598 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 599 it->second.ip = it->second.socket->getRemoteAddress(); 600 601 this->networkMonitor->addNode(&it->second); 602 603 this->handleNewClient( it->second.userId ); 444 604 445 605 if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) … … 468 628 469 629 /** 630 * this functions handles a reconnect event received from the a NET_MASTER_SERVER or NET_PROXY_SERVER 631 */ 632 void NetworkStream::handleReconnect(int userId) 633 { 634 PRINTF(0)("===============================================\n"); 635 PRINTF(0)("Client is redirected to the other proxy servers\n"); 636 PRINTF(0)("===============================================\n"); 637 638 return; 639 640 PeerInfo* pInfo = &this->peers[userId]; 641 642 // reject the server 643 pInfo->handshake->doReject( "redirected to different server"); 644 645 // flush the old synchronization states, since the numbering could be completely different 646 pInfo->lastAckedState = 0; 647 pInfo->lastRecvedState = 0; 648 // not sure if this works as expected 649 if( pInfo->handshake) 650 delete pInfo->handshake; 651 652 // disconnect from the current server and reconnect to proxy server 653 pInfo->socket->reconnectToServer( pInfo->handshake->getProxy1Address().ipString(), pInfo->handshake->getProxy1Address().port()); 654 655 // and restart the handshake 656 this->startHandshake(); 657 } 658 659 660 /** 470 661 * handle upstream network traffic 471 662 */ … … 501 692 for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) 502 693 { 694 503 695 int oldOffset = offset; 504 696 Synchronizeable & sync = **it; 697 505 698 506 699 // do not include synchronizeables with uninit id and syncs that don't want to be synchronized … … 512 705 continue; 513 706 514 // if we are a server and this is not our handshake515 if ( isServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId )707 // if we are a server (both master and proxy servers) and this is not our handshake 708 if ( ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer() ) && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) 516 709 continue; 517 710 … … 521 714 continue; 522 715 716 717 assert( sync.getLeafClassID() != 0); 718 523 719 assert( offset + INTSIZE <= UDP_PACKET_SIZE ); 524 720 525 721 // server fakes uniqueid == 0 for handshake 526 if ( this->isServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 ) 722 if ( ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer() ) && 723 sync.getUniqueID() <= SharedNetworkData::getInstance()->getMaxPlayer() + 1) // plus one to handle one client more than the max to redirect it 527 724 n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); 528 725 else 529 726 n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); 530 727 728 531 729 assert( n == INTSIZE ); 532 730 offset += n; 533 731 534 // make space for size732 // make space for packet size 535 733 offset += INTSIZE; 536 734 … … 573 771 // now compress the data with the zip library 574 772 int compLength = 0; 575 if ( this->isServer())773 if ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()) 576 774 compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); 577 775 else … … 637 835 assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); 638 836 assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); 639 //NETPRINTF(n)("ackedstate: %d\n", ackedState);640 837 offset = 4*INTSIZE; 641 838 642 839 peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState ); 643 840 644 //NETPRINTF(n)("got packet: %d, %d\n", length, packetLength); 645 646 //if this is an old state drop it 841 842 //if this is an old state drop it 647 843 if ( state <= peer->second.lastRecvedState ) 648 844 continue; … … 655 851 } 656 852 657 while ( offset + 2 *INTSIZE < length )853 while ( offset + 2 * INTSIZE < length ) 658 854 { 659 855 assert( offset > 0 ); … … 669 865 Synchronizeable * sync = NULL; 670 866 867 // look for the synchronizeable in question 671 868 for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) 672 869 { 673 //client thinks his handshake has id 0!!!!!870 // client thinks his handshake has id 0!!!!! 674 871 if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) 675 872 { … … 679 876 } 680 877 878 // this synchronizeable does not yet exist! create it 681 879 if ( sync == NULL ) 682 880 { 683 881 PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); 882 883 // if it is an old synchronizeable already removed, ignore it 684 884 if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) 685 885 { … … 688 888 } 689 889 690 if ( !peers[peer->second.userId].isServer ) 890 // if the node we got this unknown sync from is a client we ignore it (since it has no rights to create a new sync) 891 if ( peers[peer->second.userId].isClient() ) 691 892 { 692 893 offset += syncDataLength; … … 705 906 assert( leafClassId != 0 ); 706 907 908 707 909 BaseObject * b = NULL; 708 910 /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ … … 710 912 if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) 711 913 { 712 PRINTF(1)(" Can not create Class with ID %x!\n", (int)leafClassId);914 PRINTF(1)("Don't create Object with ID %x, ignored!\n", (int)leafClassId); 713 915 offset += syncDataLength; 714 916 continue; … … 730 932 sync->setSynchronized(true); 731 933 732 PRINTF(0)("Fabricated %s with id %d\n", sync->getClass Name(), sync->getUniqueID());934 PRINTF(0)("Fabricated %s with id %d\n", sync->getClassCName(), sync->getUniqueID()); 733 935 } 734 936 else … … 744 946 int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); 745 947 offset += n; 746 //NETPRINTF(0)("SSSSSEEEEETTTTT: %s %d\n",sync->getClassName(), n);747 948 748 949 } … … 779 980 /** 780 981 * is executed when a handshake has finished 781 * @todo create playable for new user782 982 */ 783 983 void NetworkStream::handleNewClient( int userId ) 784 984 { 985 // init and assign the message manager 785 986 MessageManager::getInstance()->initUser( userId ); 786 987 // do all game relevant stuff here 787 988 networkGameManager->signalNewPlayer( userId ); 788 989 } 990 789 991 790 992 /** -
trunk/src/lib/network/network_stream.h
r9246 r9406 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; … … 49 36 public: 50 37 NetworkStream(); 51 NetworkStream( std::string host, int port);52 NetworkStream( int port);38 NetworkStream(int nodeType); 39 virtual ~NetworkStream(); 53 40 54 virtual ~NetworkStream();55 41 void init(); 42 43 void connectToMasterServer(std::string host, int port); 44 void connectToProxyServer(std::string host, int port); 45 void createServer(int port); 56 46 57 47 void createNetworkGameManager(); 58 48 void startHandshake(); 59 49 50 /* synchronizeable interface */ 60 51 void connectSynchronizeable(Synchronizeable& sync); 61 52 void disconnectSynchronizeable(Synchronizeable& sync); 62 53 63 inline bool isServer() const { return (this->type == NET_SERVER)? true:false; } 64 inline bool isActive() const { return this->bActive; } 54 inline int getMaxConnections(){ return SharedNetworkData::getInstance()->getMaxPlayer(); } 65 55 66 inline int getMaxConnections(){ return MAX_CONNECTIONS; } 56 /* functions for the peerInfo information retreival */ 57 inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } 58 inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer(); } 59 inline bool isUserProxyServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServer(); } 60 inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient(); } 67 61 62 /* peering interface */ 63 inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } 64 inline PeerInfo* getPeerInfo() { return this->pInfo; } 65 inline PeerList getPeerInfoList() { return this->peers; } 66 67 /* data processing*/ 68 68 virtual void processData(); 69 70 /* debugging */ 71 void debug(); 72 73 74 private: 69 75 70 76 inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } 71 77 inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } 78 void cleanUpOldSyncList(); 72 79 int getSyncCount(); 73 80 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 81 void updateConnectionList(); 82 /* handle processes */ 84 83 void handleHandshakes(); 85 84 void handleUpstream( int tick ); 86 85 void handleDownstream(int tick ); 86 87 /* handle events*/ 87 88 void handleNewClient( int userId ); 88 void cleanUpOldSyncList();89 void handleReconnect( int userId ); 89 90 90 91 void writeToNewDict( byte * data, int length, bool upstream ); … … 92 93 93 94 private: 94 SynchronizeableList synchronizeables; 95 PeerList peers; 96 ServerSocket* serverSocket; 97 int type; 98 bool bActive; 99 std::list<int> freeSocketSlots; 95 PeerList peers; //!< list of the network node informations 100 96 101 int myHostId;97 PeerInfo* pInfo; //!< the info about the local peer node (not in the PeerList) 102 98 99 std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) 103 100 int currentState; //!< current state id 104 101 105 NetworkGameManager* networkGameManager; 102 NetworkMonitor* networkMonitor; //!< the network monitor 103 NetworkGameManager* networkGameManager; //!< reference to the network game manager 104 ServerSocket* serverSocket; //!< the listening socket of the server 106 105 107 106 std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them 107 SynchronizeableList synchronizeables; //!< list of the synchronizeables 108 108 109 109 byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream 110 110 byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream 111 112 111 int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict 113 112 114 int dictServer;115 int dictClient;113 int dictServer; //!< the zip dict for the server 114 int dictClient; //!< the zip dict for the client 116 115 }; 117 116 #endif /* _NETWORK_STREAM */ -
trunk/src/lib/network/player_stats.cc
r9235 r9406 23 23 #include "shared_network_data.h" 24 24 25 #include "converter.h" 26 25 27 #include "preferences.h" 26 28 … … 38 40 { 39 41 init(); 40 42 41 43 this->userId = userId; 42 44 } … … 62 64 this->nickName = "Player"; 63 65 this->oldNickName = "Player"; 64 66 65 67 userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) ); 66 68 teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) ); … … 71 73 modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) ); 72 74 nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) ); 73 75 74 76 MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL ); 75 77 76 78 PRINTF(0)("PlayerStats created\n"); 77 79 } … … 96 98 { 97 99 this->setPlayableUniqueId( this->playableUniqueId ); 98 100 99 101 PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID()); 100 102 } 101 103 102 104 if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() ) 103 105 { … … 115 117 { 116 118 const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); 117 119 118 120 if ( !list ) 119 121 { 120 122 return NULL; 121 123 } 122 124 123 125 for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) 124 126 { … … 128 130 } 129 131 } 130 132 131 133 return NULL; 132 134 } … … 138 140 { 139 141 const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); 140 142 141 143 if ( !list ) 142 144 { … … 144 146 return; 145 147 } 146 148 147 149 this->playable = NULL; 148 150 for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) … … 156 158 } 157 159 } 158 160 159 161 if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() ) 160 162 { 161 163 State::getPlayer()->setPlayable( this->playable ); 162 164 } 163 165 164 166 this->playableUniqueId = uniqueId; 165 167 } … … 173 175 if ( playable ) 174 176 return playable; 175 177 176 178 assert( playableUniqueId > 0 ); 177 179 178 180 setPlayableUniqueId( playableUniqueId ); 179 181 180 182 assert( playable ); 181 183 182 184 return playable; 183 185 } … … 189 191 void PlayerStats::setNickName( std::string nick ) 190 192 { 191 if ( SharedNetworkData::getInstance()->is GameServer() )193 if ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer() ) 192 194 { 193 195 this->nickName = nick; … … 199 201 { 200 202 byte * data = new byte[nick.length()+INTSIZE]; 201 203 202 204 assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE ); 203 205 204 206 MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH ); 205 207 return; … … 211 213 std::string newNick; 212 214 int res = Converter::byteArrayToString( data, newNick, dataLength ); 213 215 214 216 if ( res != dataLength ) 215 217 { … … 217 219 newNick = "invalid"; 218 220 } 219 221 220 222 if ( PlayerStats::getStats( userId ) ) 221 223 PlayerStats::getStats( userId )->setNickName( newNick ); 222 224 223 225 return true; 224 226 } … … 228 230 if ( getStats( SharedNetworkData::getInstance()->getHostID() ) ) 229 231 getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick ); 230 232 231 233 Preferences::getInstance()->setString( "multiplayer", "nickname", newNick ); 232 234 } … … 237 239 { 238 240 const std::list<BaseObject*> * list; 239 241 240 242 while ( (list = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() ) 241 243 delete *list->begin(); … … 247 249 { 248 250 ScoreList result; 249 251 250 252 const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); 251 253 252 254 if ( !list ) 253 255 { 254 256 return result; 255 257 } 256 258 257 259 for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) 258 260 { 259 261 PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); 260 262 261 263 TeamScoreList::iterator it = result[stats.getTeamId()].begin(); 262 264 263 265 while ( it != result[stats.getTeamId()].end() && stats.score > it->score ) 264 266 { 265 267 it++; 266 268 } 267 269 268 270 PlayerScore score; 269 271 score.name = stats.getNickName(); 270 272 score.score = stats.getScore(); 271 273 272 274 result[stats.getTeamId()].insert(it, score); 273 275 } 274 276 275 277 return result; 276 278 } -
trunk/src/lib/network/shared_network_data.cc
r9110 r9406 18 18 #include "shared_network_data.h" 19 19 #include "netdefs.h" 20 #include "state.h" 20 21 22 #include "debug.h" 21 23 22 using namespace std;23 24 24 25 … … 33 34 this->setClassID(CL_SHARED_NETWORK_DATA, "SharedNetworkData"); 34 35 36 this->nodeType = NET_CLIENT; 35 37 this->hostID = -1; 36 38 this->defaultSyncStream = NULL; 37 this->newUniqueID = MAX_CONNECTIONS + 2; 38 this->bGameServer = false; 39 40 // setUniqueID( maxCon+2 ) because we need one id for every handshake 41 // and one for handshake to reject client maxCon+1 42 this->newUniqueID = 2; 39 43 } 40 44 -
trunk/src/lib/network/shared_network_data.h
r6695 r9406 8 8 9 9 #include "base_object.h" 10 #include "netdefs.h" 11 #include "proxy/network_settings.h" 10 12 13 //!< maximal connectinons for the server 14 #define NET_MAX_CONNECTIONS 2 11 15 12 16 class NetworkStream; 13 17 class Synchronizeable; 14 template<typename> 15 class tList; 18 16 19 17 20 /* and here is the class itsself*/ … … 25 28 26 29 /** @returns the next uniqueID free for an object */ 27 inline int getNewUniqueID() { return ( this->bGameServer)?this->newUniqueID++:-1; } 30 inline int getNewUniqueID() { return ( this->nodeType != NET_CLIENT)?this->newUniqueID++:-1; } 31 /** sets the @param newUniqueID: the new offset for the next unique id */ 32 inline void setNewUniqueID(int newUniqueID) { this->newUniqueID = newUniqueID; } 28 33 29 /** @returns true is this node is a game server */ 30 inline bool isGameServer() { return this->bGameServer; } 34 /** @returns true is this node is a master server */ 35 inline bool isMasterServer() { return this->nodeType == NET_MASTER_SERVER; } 36 /** @returns true is this node is a proxy server */ 37 inline bool isProxyServer() { return this->nodeType == NET_PROXY_SERVER_ACTIVE; } 38 /** @returns true is this node is a client*/ 39 inline bool isClient() { return this->nodeType == NET_CLIENT; } 31 40 /** sets the game server flag @param bGameServer true if it is a game server */ 32 inline void setGameServer(bool bGameServer) { this->bGameServer = bGameServer; } 41 inline void setNodeType(int nodeType) { this->nodeType = nodeType; } 42 43 /** @returns the maximum number of players for this server */ 44 inline int getMaxPlayer() { return NetworkSettings::getInstance()->getMaxPlayer(); } 33 45 34 46 /** @returns the hostID of the node */ … … 49 61 private: 50 62 int newUniqueID; //!< next uniqueID 51 bool bGameServer; //!< true if it is a server 63 int nodeType; //!< saves the node type here 64 52 65 int hostID; //!< The Host-ID of the Manager 53 66 NetworkStream* defaultSyncStream; //!< default synchronize NetworkStream -
trunk/src/lib/network/synchronizeable.cc
r9110 r9406 11 11 12 12 ### File Specific: 13 main-programmer: Silvan Nellen14 co-programmer: Benjamin Wuest13 main-programmer: Christoph Renner (rennerc@ee.ethz.ch) 14 co-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) 15 15 */ 16 16 … … 29 29 #include "synchronizeable.h" 30 30 31 #include "converter.h" 32 31 33 32 34 … … 38 40 this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); 39 41 this->owner = 0; 40 this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0);42 // this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0); 41 43 this->uniqueID = NET_UID_UNASSIGNED; 42 44 this->networkStream = NULL; 43 45 this->bSynchronize = false; 44 46 45 47 if( State::isOnline()) 46 48 { … … 54 56 assert( syncVarList.size() == 0 ); 55 57 mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId" ) ); 56 58 57 59 this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) ); 58 60 this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) ); … … 69 71 { 70 72 this->networkStream->disconnectSynchronizeable(*this); 71 72 if ( this->isServer() && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) ) 73 74 if ( (SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer() ) 75 && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) ) 73 76 NetworkGameManager::getInstance()->removeSynchronizeable( this->getUniqueID() ); 74 77 } 75 78 76 79 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 77 80 { … … 79 82 } 80 83 syncVarList.clear(); 81 84 82 85 for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ ) 83 86 { … … 93 96 94 97 } 95 98 96 99 for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ ) 97 100 { … … 105 108 delete *it2; 106 109 } 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; 131 } 132 133 134 110 } 111 } 112 113 114 115 /** 116 * creates a diff image from two states 117 * @param userId: the userid of the user where the image will be sent to 118 * @param data: the binary data array to write to 119 * @param maxLength: maximal length of the data written (length of available space in the array) 120 * @param stateId: the state id that this diff will represent 121 * @param priorityTH: the priority threshold: all syncs below this threshold won't be synchronized 122 * 123 * @todo check for permissions 124 */ 135 125 int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ) 136 126 { … … 169 159 (*it2)->data = NULL; 170 160 } 171 161 172 162 delete *it2; 173 163 } … … 181 171 while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId ) 182 172 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 173 189 174 if ( it == sentStates[userId].end() ) … … 196 181 197 182 stateFrom = initialEntry; 198 183 199 184 sentStates[userId].push_back( stateFrom ); 200 185 } … … 205 190 206 191 sentStates[userId].push_back( stateTo ); 207 192 208 193 stateTo->stateId = stateId; 209 194 stateTo->dataLength = neededSize; … … 214 199 int i = 0; 215 200 int n; 216 217 bool hasPermission ;201 202 bool hasPermission = false; 218 203 bool sizeChanged = false; 219 204 … … 221 206 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 222 207 { 223 hasPermission = ( 224 this->isServer() && (*it)->checkPermission( PERMISSION_SERVER ) || 225 this->owner == SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) || 226 this->isServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ) || 227 (*it)->checkPermission( PERMISSION_ALL ) 228 ); 229 230 if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() ) 208 // DATA PERMISSIONS 209 // check if this synchronizeable has the permissions to write the data 210 211 // first check MASTER_SERVER permissions 212 if( SharedNetworkData::getInstance()->isMasterServer() && (*it)->checkPermission( PERMISSION_MASTER_SERVER )) 213 hasPermission = true; 214 // now check PROXY_SERVER permissions 215 else if( SharedNetworkData::getInstance()->isProxyServer() && (*it)->checkPermission( PERMISSION_MASTER_SERVER )) 216 hasPermission = true; 217 // now check OWNER permissions 218 else if( this->owner == SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER )) 219 hasPermission = true; 220 // now check ALL permissions 221 else if( (*it)->checkPermission( PERMISSION_ALL )) 222 hasPermission = true; 223 // SPECIAL: get write permissions if i am master server and i am able to overwrite the client stuff 224 else if( SharedNetworkData::getInstance()->isMasterServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER )) 225 hasPermission = true; 226 // SPECIAL: get write permissions if i am proxy server and i am able to overwrite the client stuff 227 else if( SharedNetworkData::getInstance()->isProxyServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER )) 228 hasPermission = true; 229 else 230 hasPermission = false; 231 232 233 if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() ) 231 234 sizeChanged = true; 232 235 233 236 if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeChanged ) 234 237 { 235 238 n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i ); 236 239 //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n); 240 //PRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n); 237 241 stateTo->sizeList.push_back( n ); 238 //(*it)->debug(); 242 // this is only for very hardcore debug sessions 243 // (*it)->debug(); 239 244 i += n; 240 245 } … … 257 262 if ( i != neededSize ) 258 263 { 259 PRINTF(0)("strange error: (%s) %d != %d\n", this->getClass Name(), i, neededSize);264 PRINTF(0)("strange error: (%s) %d != %d\n", this->getClassCName(), i, neededSize); 260 265 assert(false); 261 266 } … … 281 286 * @param fromStateId id of the base state id 282 287 * @return number bytes read 288 * 283 289 * @todo check for permissions 284 290 */ … … 299 305 StateHistoryEntry * stateFrom = NULL; 300 306 307 // search the state from wich the diff is made of 301 308 StateHistory::iterator it = recvStates[userId].begin(); 302 309 while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId ) 303 310 it++; 304 311 305 306 // if ( getLeafClassID() == CL_SPACE_SHIP ) 307 // { 308 // PRINTF(0)("setStateDiff:SpaceShip from: %d stateId: %d\n", (it == recvStates[userId].end())?-1:fromStateId, stateId); 309 // } 310 312 // if this is the first state to receive 311 313 if ( it == recvStates[userId].end() ) 312 314 { … … 318 320 319 321 stateFrom = initialEntry; 320 322 321 323 recvStates[userId].push_back( stateFrom ); 322 324 } 323 325 else 324 326 stateFrom = (*it); 325 326 //apply diff 327 328 329 // apply diff 327 330 for ( int i = 0; i<length; i++ ) 328 331 { … … 331 334 else 332 335 stateTo->data[i] = data[i]; 333 334 } 335 336 } 337 336 338 //add state to state history 337 339 recvStates[userId].push_back( stateTo ); 338 340 339 341 int i = 0; 340 342 int n = 0; 341 343 std::list<int> changes; 342 344 bool hasPermission = false; 345 346 // extract the new state for every client 343 347 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 344 348 { 345 if ( 346 (*it)->checkPermission( PERMISSION_SERVER ) && networkStream->isUserServer( userId ) || 347 (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId || 348 networkStream->isUserServer( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) || 349 (*it)->checkPermission( PERMISSION_ALL ) 350 ) 349 // DATA PERMISSIONS 350 // check if this synchronizeable has the permissions to write the data 351 352 // first check MASTER_SERVER permissions 353 if( this->networkStream->isUserMasterServer( userId ) && (*it)->checkPermission( PERMISSION_MASTER_SERVER )) 354 hasPermission = true; 355 // now check PROXY_SERVER permissions 356 else if( this->networkStream->isUserProxyServer( userId ) && (*it)->checkPermission( PERMISSION_MASTER_SERVER )) 357 hasPermission = true; 358 // now check OWNER permissions 359 else if( this->owner == userId && (*it)->checkPermission( PERMISSION_OWNER )) 360 hasPermission = true; 361 // now check ALL permissions 362 else if( (*it)->checkPermission( PERMISSION_ALL )) 363 hasPermission = true; 364 // SPECIAL: get write permissions if im sending to a master server that does not own this sync 365 else if( this->networkStream->isUserMasterServer( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER )) 366 hasPermission = true; 367 // SPECIAL: get write permissions if im sending to a proxy server that does not own this sync 368 else if( this->networkStream->isUserProxyServer( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER )) 369 hasPermission = true; 370 else 371 hasPermission = false; 372 373 374 375 // if it has the permission to write do it 376 if( hasPermission) 351 377 { 352 378 n = (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); 353 379 i += n; 354 //NETPRINTF(0)("%s::setvar %s %d\n", getClassName(), (*it)->getName().c_str(), n); 380 //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); 381 //PRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); 355 382 //(*it)->debug(); 356 383 if ( (*it)->getHasChanged() ) … … 361 388 else 362 389 { 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 );390 // 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 391 n = (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i ); 365 //NETPRINTF(0)("%s::setvar %s %d\n", getClass Name(), (*it)->getName().c_str(), n);392 //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); 366 393 //(*it)->debug(); 367 394 i += n; … … 370 397 371 398 this->varChangeHandler( changes ); 372 399 373 400 return i; 374 401 } … … 423 450 (*it)->data = NULL; 424 451 } 425 452 426 453 delete *it; 427 454 } 428 455 recvStates[userId].clear(); 429 456 } 430 457 431 458 if ( sentStates.size() > userId ) 432 459 { 433 460 434 461 for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) 435 462 { … … 439 466 (*it)->data = NULL; 440 467 } 441 468 442 469 delete *it; 443 470 } … … 448 475 /** 449 476 * this function is called after recieving a state. 450 * @param userId 451 * @param stateId 452 * @param fromStateId 477 * @param userId 478 * @param stateId 479 * @param fromStateId 453 480 */ 454 481 void Synchronizeable::handleRecvState( int userId, int stateId, int fromStateId ) … … 457 484 if ( recvStates.size() <= userId ) 458 485 recvStates.resize( userId+1 ); 459 486 460 487 //remove old states 461 488 StateHistory::iterator it = recvStates[userId].begin(); … … 485 512 StateHistory::iterator delIt = it; 486 513 it ++; 487 514 488 515 if ( (*delIt)->data ) 489 516 { … … 493 520 delete *delIt; 494 521 recvStates[userId].erase( delIt ); 495 522 496 523 continue; 497 524 } 498 525 it++; 499 526 } 500 527 501 528 StateHistory::iterator fromState = recvStates[userId].end(); 502 529 StateHistory::iterator toState = recvStates[userId].end(); 503 530 504 531 for ( it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ ) 505 532 { … … 508 535 if ( (*it)->stateId == fromStateId ) 509 536 fromState = it; 510 537 511 538 if ( fromState != recvStates[userId].end() && toState != recvStates[userId].end() ) 512 539 break; 513 540 } 514 541 515 542 // setStateDiff was not called and i know fromStateId 516 543 if ( fromState != recvStates[userId].end() && toState == recvStates[userId].end() ) 517 544 { 518 545 StateHistoryEntry * entry = new StateHistoryEntry; 519 546 520 547 entry->dataLength = (*fromState)->dataLength; 521 548 if ( entry->dataLength > 0 ) 522 549 { 523 550 entry->data = new byte[entry->dataLength]; 524 551 525 552 assert( (*fromState)->data ); 526 553 memcpy( entry->data, (*fromState)->data, entry->dataLength ); … … 528 555 else 529 556 entry->data = NULL; 530 557 531 558 entry->sizeList = (*fromState)->sizeList; 532 559 entry->stateId = stateId; 533 560 534 561 recvStates[userId].push_back(entry); 535 562 } … … 538 565 /** 539 566 * this function is called after sending a state 540 * @param userId 541 * @param stateId 542 * @param fromStateId 567 * @param userId 568 * @param stateId 569 * @param fromStateId 543 570 */ 544 571 void Synchronizeable::handleSentState( int userId, int stateId, int fromStateId ) … … 557 584 StateHistory::iterator delIt = it; 558 585 it ++; 559 586 560 587 if ( (*delIt)->data ) 561 588 { … … 565 592 delete *delIt; 566 593 sentStates[userId].erase( delIt ); 567 594 568 595 continue; 569 596 } … … 571 598 } 572 599 573 600 574 601 StateHistory::iterator fromState = sentStates[userId].end(); 575 602 StateHistory::iterator toState = sentStates[userId].end(); 576 603 577 604 for ( it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) 578 605 { … … 581 608 if ( (*it)->stateId == fromStateId ) 582 609 fromState = it; 583 610 584 611 if ( fromState != sentStates[userId].end() && toState != sentStates[userId].end() ) 585 612 break; 586 613 } 587 614 588 615 589 616 // getStateDiff was not called and i know fromStateId 590 617 if ( fromState != sentStates[userId].end() && toState == sentStates[userId].end() ) 591 618 { 592 619 StateHistoryEntry * entry = new StateHistoryEntry; 593 620 594 621 entry->dataLength = (*fromState)->dataLength; 595 622 if ( entry->dataLength > 0 ) 596 623 { 597 624 entry->data = new byte[entry->dataLength]; 598 625 599 626 assert( (*fromState)->data ); 600 627 memcpy( entry->data, (*fromState)->data, entry->dataLength ); … … 602 629 else 603 630 entry->data = NULL; 604 631 605 632 entry->sizeList = (*fromState)->sizeList; 606 633 entry->stateId = stateId; 607 634 608 635 sentStates[userId].push_back(entry); 609 636 } 610 611 } 612 613 614 637 638 } 639 640 641 -
trunk/src/lib/network/synchronizeable.h
r8708 r9406 1 1 /*! 2 2 * @file synchronizeable.h 3 \brief interface for all classes that have to be synchronized3 * @brief interface for all classes that have to be synchronized 4 4 */ 5 5 … … 8 8 9 9 #include "base_object.h" 10 #include "netdefs.h" 11 #include "converter.h" 12 #include "vector.h" 13 #include "quaternion.h" 10 14 11 #include "synchronizeable_var/synchronizeable_var.h" 15 12 #include "synchronizeable_var/synchronizeable_vector.h" … … 20 17 #include "synchronizeable_var/synchronizeable_bool.h" 21 18 #include "synchronizeable_var/synchronizeable_uint.h" 22 19 #include "synchronizeable_var/synchronizeable_ip.h" 23 20 24 21 #include <vector> 25 22 #include <list> 26 23 27 //State constants: They have to be of the form 2^n28 #define STATE_SERVER 129 24 30 25 struct StateHistoryEntry … … 51 46 virtual ~Synchronizeable(); 52 47 53 void setIsServer( bool isServer );54 bool isServer();55 56 48 virtual void varChangeHandler( std::list<int> & id ); 57 49 … … 61 53 virtual void handleSentState( int userId, int stateId, int fromStateId ); 62 54 virtual void handleRecvState( int userId, int stateId, int fromStateId ); 63 55 64 56 void registerVar( SynchronizeableVar * var ); 65 57 int registerVarId( SynchronizeableVar * var ); … … 82 74 protected: 83 75 NetworkStream* networkStream; //!< reference network stream we are connected to 84 int state; 76 85 77 86 78 private: -
trunk/src/lib/network/synchronizeable_var/synchronizeable_bool.cc
r7954 r9406 16 16 17 17 #include "synchronizeable_bool.h" 18 #include <cassert> 18 19 19 20 … … 44 45 { 45 46 assert( maxLength >= 1 ); 46 47 47 48 buf[0] = ( *vPtrIn ) ? 1 : 0; 48 49 49 50 return 1; 50 51 } … … 59 60 { 60 61 assert( maxLength >= 1 ); 61 62 62 63 bool oldVal = *vPtrOut; 63 64 64 65 *vPtrOut = buf[0] != 0; 65 66 66 67 setHasChanged( oldVal != *vPtrOut ); 67 68 68 69 return 1; 69 70 } -
trunk/src/lib/network/synchronizeable_var/synchronizeable_bool.h
r7954 r9406 2 2 * @file synchronizeable_int.h 3 3 * @brief Definition of SynchronizeableInt 4 */ 5 6 #include "synchronizeable_var/synchronizeable_var.h" 4 */ 7 5 8 6 #ifndef _SYNCHRONIZEABLE_BOOL_H 9 7 #define _SYNCHRONIZEABLE_BOOL_H 10 8 9 #include "synchronizeable_var.h" 10 11 11 class SynchronizeableBool : public SynchronizeableVar { 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 17 17 virtual int writeToBuf( byte * buf, int maxLength ); 18 18 virtual int readFromBuf( byte * buf, int maxLength ); 19 19 20 20 /** 21 21 * check if writeToBuf will return the same size every time … … 23 23 */ 24 24 virtual bool hasStaticSize(){ return true; }; 25 25 26 26 virtual void debug(); 27 27 28 28 private: 29 29 bool * vPtrIn; //!< pointer to data (read) -
trunk/src/lib/network/synchronizeable_var/synchronizeable_float.cc
r7954 r9406 17 17 #include "synchronizeable_float.h" 18 18 #include "converter.h" 19 #include <cassert> 19 20 20 21 … … 46 47 { 47 48 int res = Converter::floatToByteArray( *vPtrIn, buf, maxLength ); 48 49 49 50 assert( res == FLOATSIZE ); 50 51 51 52 return res; 52 53 } … … 61 62 { 62 63 assert( maxLength >= FLOATSIZE ); 63 64 64 65 float oldVal = *vPtrOut; 65 66 66 67 int res = Converter::byteArrayToFloat( buf, vPtrOut ); 67 68 68 69 setHasChanged( oldVal != *vPtrOut ); 69 70 70 71 assert( res == FLOATSIZE ); 71 72 72 73 return res; 73 74 } -
trunk/src/lib/network/synchronizeable_var/synchronizeable_float.h
r7954 r9406 2 2 * @file synchronizeable_float.h 3 3 * @brief Definition of SynchronizeableFloat 4 */4 */ 5 5 6 #include "synchronizeable_var/synchronizeable_var.h"7 6 8 7 #ifndef _SYNCHRONIZEABLE_FLOAT_H 9 8 #define _SYNCHRONIZEABLE_FLOAT_H 10 9 10 #include "synchronizeable_var.h" 11 11 12 class SynchronizeableFloat : public SynchronizeableVar { 12 13 13 14 public: 14 SynchronizeableFloat( float * ptrIn, float * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );15 SynchronizeableFloat( float * ptrIn, float * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 15 16 virtual ~SynchronizeableFloat(); 16 17 17 18 virtual int writeToBuf( byte * buf, int maxLength ); 18 19 virtual int readFromBuf( byte * buf, int maxLength ); 19 20 20 21 /** 21 22 * check if writeToBuf will return the same size every time … … 23 24 */ 24 25 virtual bool hasStaticSize(){ return true; }; 25 26 26 27 virtual void debug(); 27 28 28 29 private: 29 30 float * vPtrIn; //!< pointer to data (read) -
trunk/src/lib/network/synchronizeable_var/synchronizeable_int.cc
r7954 r9406 18 18 #include "converter.h" 19 19 20 #include <cassert> 20 21 21 22 /** … … 46 47 { 47 48 int res = Converter::intToByteArray( *vPtrIn, buf, maxLength ); 48 49 49 50 assert( res == INTSIZE ); 50 51 51 52 return res; 52 53 } … … 61 62 { 62 63 assert( maxLength >= INTSIZE ); 63 64 64 65 int oldVal = *vPtrOut; 65 66 66 67 int res = Converter::byteArrayToInt( buf, vPtrOut ); 67 68 68 69 setHasChanged( oldVal != *vPtrOut ); 69 70 70 71 assert( res == INTSIZE ); 71 72 72 73 return res; 73 74 } -
trunk/src/lib/network/synchronizeable_var/synchronizeable_int.h
r7954 r9406 4 4 */ 5 5 6 #include "synchronizeable_var/synchronizeable_var.h"7 6 8 7 #ifndef _SYNCHRONIZEABLE_INT_H 9 8 #define _SYNCHRONIZEABLE_INT_H 10 9 10 #include "synchronizeable_var.h" 11 11 12 class SynchronizeableInt : public SynchronizeableVar { 12 13 13 14 public: 14 SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );15 SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 15 16 virtual ~SynchronizeableInt(); 16 17 17 18 virtual int writeToBuf( byte * buf, int maxLength ); 18 19 virtual int readFromBuf( byte * buf, int maxLength ); 19 20 20 21 /** 21 22 * check if writeToBuf will return the same size every time … … 23 24 */ 24 25 virtual bool hasStaticSize(){ return true; }; 25 26 26 27 virtual void debug(); 27 28 28 29 protected: 29 30 int * vPtrIn; //!< pointer to data (read) -
trunk/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc
r7954 r9406 17 17 #include "synchronizeable_quaternion.h" 18 18 #include "converter.h" 19 #include <cassert> 19 20 20 21 … … 98 99 assert( res > 0 ); 99 100 n += res; 100 101 101 102 Quaternion oldVal = *vPtrOut; 102 103 103 104 *vPtrOut = Quaternion( Vector(x, y, z), w ); 104 105 105 106 setHasChanged( ! ( oldVal == *vPtrOut ) ); 106 107 -
trunk/src/lib/network/synchronizeable_var/synchronizeable_quaternion.h
r7954 r9406 4 4 */ 5 5 6 #include "synchronizeable_var/synchronizeable_var.h"7 8 6 #ifndef _SYNCHRONIZEABLE_QUATERNION_H 9 7 #define _SYNCHRONIZEABLE_QUATERNION_H 8 9 #include "synchronizeable_var.h" 10 10 11 11 #include "quaternion.h" … … 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 19 19 virtual int writeToBuf( byte * buf, int maxLength ); 20 20 virtual int readFromBuf( byte * buf, int maxLength ); 21 21 22 22 /** 23 23 * check if writeToBuf will return the same size every time … … 25 25 */ 26 26 virtual bool hasStaticSize(){ return true; }; 27 27 28 28 virtual void debug(); 29 29 30 30 private: 31 31 Quaternion * vPtrIn; //!< pointer to data (read) -
trunk/src/lib/network/synchronizeable_var/synchronizeable_string.cc
r8147 r9406 17 17 #include "synchronizeable_string.h" 18 18 #include "converter.h" 19 #include <cassert> 19 20 20 21 … … 46 47 { 47 48 int res = Converter::stringToByteArray( *vPtrIn, buf, maxLength ); 48 49 49 50 assert( res > 0 ); 50 51 assert( res == vPtrIn->length()+INTSIZE ); 51 52 52 53 return res; 54 } 55 56 int SynchronizeableString::getSize() 57 { 58 return vPtrIn->length()+INTSIZE; 53 59 } 54 60 … … 62 68 { 63 69 std::string oldVal = *vPtrOut; 64 70 65 71 int res = Converter::byteArrayToString( buf, *vPtrOut, maxLength ); 66 72 67 73 setHasChanged( oldVal != *vPtrOut ); 68 74 69 75 if ( res < 0 ) 70 76 { … … 72 78 } 73 79 assert( res > 0 ); 74 80 75 81 return res; 76 82 } … … 94 100 { 95 101 std::string t; 96 102 97 103 int res = Converter::byteArrayToString( buf, t, maxLength ); 98 104 99 105 assert( res > 0 ); 100 106 101 107 return res; 102 108 } -
trunk/src/lib/network/synchronizeable_var/synchronizeable_string.h
r7954 r9406 4 4 */ 5 5 6 #include "synchronizeable_var/synchronizeable_var.h"7 #include "converter.h"8 9 6 #ifndef _SYNCHRONIZEABLE_STRING_H 10 7 #define _SYNCHRONIZEABLE_STRING_H 8 9 #include "synchronizeable_var.h" 11 10 12 11 #include <string> … … 15 14 16 15 public: 17 SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );16 SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 18 17 virtual ~SynchronizeableString(); 19 18 20 19 virtual int writeToBuf( byte * buf, int maxLength ); 21 20 virtual int readFromBuf( byte * buf, int maxLength ); 22 23 21 22 24 23 /** 25 24 * check if writeToBuf will return the same size every time … … 27 26 */ 28 27 virtual bool hasStaticSize(){ return false; }; 29 30 virtual int getSize() { return vPtrIn->length()+INTSIZE; }31 28 29 virtual int getSize(); 30 32 31 virtual int getSizeFromBuf( byte * buf, int maxLength ); 33 32 34 33 virtual void debug(); 35 34 36 35 private: 37 36 std::string * vPtrIn; //!< pointer to data (read) -
trunk/src/lib/network/synchronizeable_var/synchronizeable_uint.h
r7954 r9406 4 4 */ 5 5 6 #include "synchronizeable_var/synchronizeable_int.h"7 6 8 7 #ifndef _SYNCHRONIZEABLE_UINT_H 9 8 #define _SYNCHRONIZEABLE_UINT_H 10 9 10 #include "synchronizeable_int.h" 11 11 12 class SynchronizeableUInt : public SynchronizeableInt { 12 13 13 14 public: 14 SynchronizeableUInt( unsigned int * ptrIn, unsigned int * ptrOut, std::string name, int permission = PERMISSION_ SERVER, int priority = 0 );15 SynchronizeableUInt( unsigned int * ptrIn, unsigned int * ptrOut, std::string name, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 15 16 virtual ~SynchronizeableUInt(); 16 17 17 18 virtual void debug(); 18 19 -
trunk/src/lib/network/synchronizeable_var/synchronizeable_var.cc
r7954 r9406 16 16 17 17 #include "synchronizeable_var.h" 18 18 #include "netdefs.h" 19 #include <cassert> 19 20 20 21 -
trunk/src/lib/network/synchronizeable_var/synchronizeable_var.h
r7954 r9406 7 7 #define _SYNCHRONIZEABLE_VAR_H 8 8 9 #include "nettypes.h" 9 10 #include <string> 10 #include "netdefs.h"11 #include <assert.h>12 11 13 12 enum { 14 PERMISSION_SERVER = 1, 15 PERMISSION_OWNER = 2, 16 PERMISSION_ALL = 4 13 PERMISSION_MASTER_SERVER = 1, 14 PERMISSION_PROXY_SERVER = 2, 15 PERMISSION_OWNER = 4, 16 PERMISSION_ALL = 8 17 17 }; 18 18 … … 20 20 21 21 public: 22 SynchronizeableVar( void * ptrIn, void * ptrOut, std::string name, int length, int permission = PERMISSION_ SERVER, int priority = 0 );22 SynchronizeableVar( void * ptrIn, void * ptrOut, std::string name, int length, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); 23 23 virtual ~SynchronizeableVar(); 24 24 … … 28 28 */ 29 29 inline bool beWatched(){ return this->bWatched; } 30 31 /** 30 31 /** 32 32 * set flag if synchronizeable wants to be informed on changes 33 33 */ … … 96 96 */ 97 97 inline void resetPriority() { this->priority = this->realPriority; } 98 98 99 99 /** 100 100 * reads actual size from buffer. this is used when size is not constant … … 104 104 */ 105 105 virtual inline int getSizeFromBuf( byte * buf, int maxLength ){ return this->getSize(); } 106 106 107 107 /** 108 108 * set variable id … … 110 110 */ 111 111 inline void setVarId( int id ){ this->varId = id; } 112 113 /** 112 113 /** 114 114 * get variable id 115 115 * @return variable id 116 116 */ 117 117 inline int getVarId(){ return varId; } 118 118 119 119 /** 120 120 * set hasChanged … … 122 122 */ 123 123 inline void setHasChanged( bool changed ){ this->changed = changed; } 124 125 /** 124 125 /** 126 126 * get hasChanged 127 127 * @return variable id 128 128 */ 129 129 inline bool getHasChanged(){ return changed; } 130 130 131 131 /** 132 132 * print out variable value … … 137 137 private: 138 138 bool bWatched; //!< true if synchronizeable wants to be informed on changes 139 139 140 140 int permission; //!< who is allowed to change this var 141 141 int priority; //!< priority assigned to var 142 142 int realPriority; //!< priority assigned to var, increased every time not sent 143 143 int varId; //!< id to identify varables 144 144 145 145 bool changed; //!< true if last readFromBuf changed anything 146 146 -
trunk/src/lib/network/synchronizeable_var/synchronizeable_vector.cc
r7954 r9406 17 17 #include "synchronizeable_vector.h" 18 18 #include "converter.h" 19 #include <cassert> 19 20 20 21 … … 79 80 int res; 80 81 int n = 0; 81 82 82 83 Vector oldVec = *vPtrOut; 83 84 … … 95 96 96 97 *vPtrOut = Vector( x, y, z ); 97 98 98 99 setHasChanged( !(*vPtrOut == oldVec) ); 99 100 -
trunk/src/lib/network/synchronizeable_var/synchronizeable_vector.h
r7954 r9406 4 4 */ 5 5 6 #include "synchronizeable_var/synchronizeable_var.h"7 8 6 #ifndef _SYNCHRONIZEABLE_VECTOR_H 9 7 #define _SYNCHRONIZEABLE_VECTOR_H 8 9 #include "synchronizeable_var.h" 10 10 11 11 #include "vector.h" … … 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 19 19 virtual int writeToBuf( byte * buf, int maxLength ); 20 20 virtual int readFromBuf( byte * buf, int maxLength ); 21 21 22 22 /** 23 23 * check if writeToBuf will return the same size every time … … 25 25 */ 26 26 virtual bool hasStaticSize(){ return true; } 27 27 28 28 virtual void debug(); 29 29 30 30 private: 31 31 Vector * vPtrIn; //!< pointer to data (read) -
trunk/src/lib/network/tcp_server_socket.cc
r7954 r9406 136 136 _isListening = false; 137 137 } 138 -
trunk/src/lib/network/tcp_server_socket.h
r7954 r9406 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 -
trunk/src/lib/network/tcp_socket.h
r7954 r9406 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 */ … … 16 16 #define _INCOMING_BUFFER_SIZE 2024000 17 17 #define _OUTGOING_BUFFER_SIZE 2024000 18 #define _LOCAL_BUFFER_SIZE 102418 #define _LOCAL_BUFFER_SIZE 2048 19 19 //sleep if incoming buffer is full 20 20 #define _MSECONDS_SLEEP_FULL_BUFFER 10 … … 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); -
trunk/src/lib/network/udp_socket.cc
r8802 r9406 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 } -
trunk/src/lib/network/udp_socket.h
r8802 r9406 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.