Changeset 9508 in orxonox.OLD
- Timestamp:
- Jul 27, 2006, 3:37:11 PM (18 years ago)
- Location:
- branches/proxy/src
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/proxy/src/lib/network/message_manager.cc
r9504 r9508 44 44 MessageManager::~MessageManager () 45 45 { 46 for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ )46 for ( MessageQueue::iterator it = outgoingMessageQueue.begin(); it != outgoingMessageQueue.end(); it++ ) 47 47 { 48 48 for ( std::list<NetworkMessage>::iterator it2 = it->second.messages.begin(); it2 != it->second.messages.end(); it2++ ) … … 59 59 } 60 60 61 messageQueue.clear();61 outgoingMessageQueue.clear(); 62 62 63 63 this->messageHandlerMap.clear(); … … 85 85 int n; 86 86 87 n = Converter::intToByteArray( messageQueue[userId].toAck.size(), data + i, maxLength );87 n = Converter::intToByteArray( outgoingMessageQueue[userId].toAck.size(), data + i, maxLength ); 88 88 i += n; 89 89 assert( n == INTSIZE ); 90 90 91 for ( std::list<int>::iterator it = messageQueue[userId].toAck.begin(); it != messageQueue[userId].toAck.end(); it++)91 for ( std::list<int>::iterator it = outgoingMessageQueue[userId].toAck.begin(); it != outgoingMessageQueue[userId].toAck.end(); it++) 92 92 { 93 93 n = Converter::intToByteArray( *it, data + i, maxLength ); … … 96 96 } 97 97 98 messageQueue[userId].toAck.clear();99 100 n = Converter::intToByteArray( messageQueue[userId].messages.size(), data + i, maxLength );98 outgoingMessageQueue[userId].toAck.clear(); 99 100 n = Converter::intToByteArray( outgoingMessageQueue[userId].messages.size(), data + i, maxLength ); 101 101 i += n; 102 102 assert( n == INTSIZE ); 103 103 104 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ )104 for ( std::list<NetworkMessage>::iterator it = outgoingMessageQueue[userId].messages.begin(); it != outgoingMessageQueue[userId].messages.end(); it++ ) 105 105 { 106 106 n = Converter::intToByteArray( it->length, data + i, maxLength ); … … 112 112 assert( n == INTSIZE ); 113 113 114 n = Converter::intToByteArray( it->message Id, data + i, maxLength );114 n = Converter::intToByteArray( it->messageType, data + i, maxLength ); 115 115 i += n; 116 116 assert( n == INTSIZE ); … … 168 168 i += n; 169 169 170 int messageLength, messageId; 171 170 int messageLength, messageType; 171 172 // now go through all messages 172 173 for ( int j = 0; j < nMessages; j++ ) 173 174 { … … 183 184 184 185 assert( i + INTSIZE <= length ); 185 n = Converter::byteArrayToInt( data + i, &message Id);186 n = Converter::byteArrayToInt( data + i, &messageType ); 186 187 assert( n == INTSIZE ); 187 188 i += n; 188 189 189 190 if ( number > 0 ) 190 messageQueue[userId].toAck.push_back( number );191 outgoingMessageQueue[userId].toAck.push_back( number ); 191 192 192 193 assert( i + messageLength <= length ); 193 assert( messageHandlerMap.find( (MessageId)messageId ) != messageHandlerMap.end() ); 194 if ( std::find( messageQueue[userId].recievedMessages.begin(), messageQueue[userId].recievedMessages.end(), number )== messageQueue[userId].recievedMessages.end() ) 195 { 196 if ( !(*(messageHandlerMap[(MessageId)messageId].cb))( (MessageId)messageId, data + i, messageLength, messageHandlerMap[(MessageId)messageId].someData, userId ) ) 194 // make sure there is a message handler for this message type 195 assert( messageHandlerMap.find( (MessageType)messageType ) != messageHandlerMap.end() ); 196 197 198 if ( std::find( outgoingMessageQueue[userId].recievedMessages.begin(), outgoingMessageQueue[userId].recievedMessages.end(), number ) == 199 outgoingMessageQueue[userId].recievedMessages.end() ) 200 { 201 if ( !(*(messageHandlerMap[(MessageType)messageType].cb))( (MessageType)messageType, data + i, messageLength, messageHandlerMap[(MessageType)messageType].someData, userId ) ) 197 202 { 198 203 NetworkMessage msg; … … 201 206 memcpy( msg.data, data + i, messageLength ); 202 207 msg.length = messageLength; 203 msg.message Id = (MessageId)messageId;208 msg.messageType = (MessageType)messageType; 204 209 msg.number = userId; 205 210 206 incomingMessage Buffer.push_back( msg );211 incomingMessageQueue.push_back( msg ); 207 212 } 208 messageQueue[userId].recievedMessages.push_back( number );213 outgoingMessageQueue[userId].recievedMessages.push_back( number ); 209 214 } 210 215 i += messageLength; … … 213 218 214 219 //TODO maybe handle incomingMessage in tick function. else local messages will not be handled if no clients are connected 215 for ( std::list<NetworkMessage>::iterator it = incomingMessage Buffer.begin(); it != incomingMessageBuffer.end(); )216 { 217 if ( (*(messageHandlerMap[it->message Id].cb))( it->messageId, it->data, it->length, messageHandlerMap[it->messageId].someData, it->number ) )220 for ( std::list<NetworkMessage>::iterator it = incomingMessageQueue.begin(); it != incomingMessageQueue.end(); ) 221 { 222 if ( (*(messageHandlerMap[it->messageType].cb))( it->messageType, it->data, it->length, messageHandlerMap[it->messageType].someData, it->number ) ) 218 223 { 219 224 std::list<NetworkMessage>::iterator delIt = it; … … 221 226 delete it->data; 222 227 it++; 223 incomingMessage Buffer.erase( delIt );228 incomingMessageQueue.erase( delIt ); 224 229 continue; 225 230 } … … 228 233 229 234 //walk throu message queue and remove acked messages 230 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); )235 for ( std::list<NetworkMessage>::iterator it = outgoingMessageQueue[userId].messages.begin(); it != outgoingMessageQueue[userId].messages.end(); ) 231 236 { 232 237 if ( std::find( acks.begin(), acks.end(), it->number) != acks.end() ) … … 234 239 std::list<NetworkMessage>::iterator delIt = it; 235 240 it++; 236 messageQueue[userId].messages.erase( delIt );241 outgoingMessageQueue[userId].messages.erase( delIt ); 237 242 continue; 238 243 } … … 241 246 242 247 //TODO find bether way. maybe with timestamp 243 if ( messageQueue[userId].recievedMessages.size() > 1000 )244 { 245 for ( int j = 0; j < messageQueue[userId].recievedMessages.size() - 1000; j++ )246 messageQueue[userId].recievedMessages.erase( messageQueue[userId].recievedMessages.begin() );248 if ( outgoingMessageQueue[userId].recievedMessages.size() > 1000 ) 249 { 250 for ( int j = 0; j < outgoingMessageQueue[userId].recievedMessages.size() - 1000; j++ ) 251 outgoingMessageQueue[userId].recievedMessages.erase( outgoingMessageQueue[userId].recievedMessages.begin() ); 247 252 } 248 253 … … 258 263 void MessageManager::cleanUpUser( int userId ) 259 264 { 260 if ( messageQueue.find( userId ) == messageQueue.end() )265 if ( outgoingMessageQueue.find( userId ) == outgoingMessageQueue.end() ) 261 266 return; 262 267 263 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ )268 for ( std::list<NetworkMessage>::iterator it = outgoingMessageQueue[userId].messages.begin(); it != outgoingMessageQueue[userId].messages.end(); it++ ) 264 269 { 265 270 if ( it->data ) … … 268 273 } 269 274 270 messageQueue[userId].toAck.clear();271 272 messageQueue.erase( userId );273 } 274 275 /** 276 * registers function to handle messages with id message Id. someData is passed to callbackfuntion277 * @param message Idmessage id to handle275 outgoingMessageQueue[userId].toAck.clear(); 276 277 outgoingMessageQueue.erase( userId ); 278 } 279 280 /** 281 * registers function to handle messages with id messageType. someData is passed to callbackfuntion 282 * @param messageType message id to handle 278 283 * @param cb function pointer to callback function 279 284 * @param someData this pointer is passed to callback function without modification 280 285 * @return true on success 281 286 */ 282 bool MessageManager::registerMessageHandler( Message Id messageId, MessageCallback cb, void * someData )287 bool MessageManager::registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData ) 283 288 { 284 289 MessageHandler messageHandler; 285 290 286 291 messageHandler.cb = cb; 287 messageHandler.message Id = messageId;292 messageHandler.messageType = messageType; 288 293 messageHandler.someData = someData; 289 294 290 messageHandlerMap[message Id] = messageHandler;295 messageHandlerMap[messageType] = messageHandler; 291 296 292 297 return true; … … 300 305 { 301 306 // just do something so map creates a new entry 302 messageQueue[userId].toAck.clear();303 //assert( messageQueue[userId].messages.size() == 0 );307 outgoingMessageQueue[userId].toAck.clear(); 308 //assert( outgoingMessageQueue[userId].messages.size() == 0 ); 304 309 } 305 310 … … 311 316 * RT_NOT_USER send to all but reciever 312 317 * 313 * @param message Idmessage id318 * @param messageType message id 314 319 * @param data pointer to data 315 320 * @param dataLength length of data 316 * @param recieverType 317 * @param reciever 318 */ 319 void MessageManager::sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ) 320 { 321 for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ ) 321 * @param recieverType type of the receiver 322 * @param reciever the userId of the receiver if needed (depends on the ReceiverType) 323 */ 324 void MessageManager::sendMessage( MessageType messageType, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ) 325 { 326 327 for ( MessageQueue::iterator it = this->outgoingMessageQueue.begin(); it != this->outgoingMessageQueue.end(); it++ ) 322 328 { 323 329 if ( … … 335 341 memcpy( msg.data, data, dataLength ); 336 342 msg.length = dataLength; 337 msg.message Id = messageId;343 msg.messageType = messageType; 338 344 msg.number = newNumber++; 339 345 msg.priority = messagePriority; … … 350 356 memcpy( msg.data, data, dataLength ); 351 357 msg.length = dataLength; 352 msg.message Id = messageId;358 msg.messageType = messageType; 353 359 msg.number = SharedNetworkData::getInstance()->getHostID(); 354 360 msg.priority = messagePriority; 355 361 356 incomingMessage Buffer.push_back( msg );357 } 358 } 359 360 362 incomingMessageQueue.push_back( msg ); 363 } 364 } 365 366 -
branches/proxy/src/lib/network/message_manager.h
r9507 r9508 20 20 int length 21 21 int number 22 int Message Id22 int MessageType 23 23 byte * data 24 24 )[1..nmsg] … … 27 27 28 28 //!< different message ids 29 enum Message Id29 enum MessageType 30 30 { 31 31 TESTMESSAGEID = 1, //!< for testing purposes … … 40 40 41 41 42 typedef bool (*MessageCallback)( Message Id messageId, byte * data, int dataLength, void * someData, int userId );42 typedef bool (*MessageCallback)( MessageType messageType, byte * data, int dataLength, void * someData, int userId ); 43 43 44 44 enum RecieverType … … 61 61 struct NetworkMessage 62 62 { 63 Message Id messageId;63 MessageType messageType; 64 64 byte * data; 65 65 int length; … … 77 77 typedef std::map<int,MessageUserQueue> MessageQueue; 78 78 79 80 79 81 struct MessageHandler 80 82 { 81 83 MessageCallback cb; 82 Message Id messageId;84 MessageType messageType; 83 85 void * someData; 84 86 }; 85 87 86 typedef std::map<Message Id,MessageHandler> MessageHandlerMap;88 typedef std::map<MessageType,MessageHandler> MessageHandlerMap; 87 89 88 90 //! A class for sending messages over network … … 95 97 virtual ~MessageManager(); 96 98 97 bool registerMessageHandler( Message Id messageId, MessageCallback cb, void * someData );99 bool registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData ); 98 100 99 void sendMessage( Message Id messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority );101 void sendMessage( MessageType messageType, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ); 100 102 103 void initUser( int userId ); 104 105 106 private: 101 107 virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); 102 108 virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ); … … 105 111 virtual void handleRecvState( int userId, int stateId, int fromStateId ){} 106 112 107 void initUser( int userId );108 109 113 110 114 private: 111 static MessageManager * singletonRef; 112 MessageQueue messageQueue; //!< stores messages to send 115 static MessageManager * singletonRef; //!< the singleton reference 116 117 std::list<NetworkMessage> incomingMessageQueue; //!< the incoming message buffer 118 MessageQueue outgoingMessageQueue; //!< stores messages to send 113 119 MessageHandlerMap messageHandlerMap; //!< contains handlers for messages 114 120 115 121 int newNumber; //!< used to create unique message numbers 116 std::list<NetworkMessage> incomingMessageBuffer; 122 117 123 118 124 }; -
branches/proxy/src/lib/network/network_game_manager.cc
r9507 r9508 159 159 /** 160 160 * handler for remove synchronizeable messages 161 * @param message Id161 * @param messageType 162 162 * @param data 163 163 * @param dataLength … … 166 166 * @return true on successfull handling else handler will be called again 167 167 */ 168 bool NetworkGameManager::delSynchronizeableHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId )168 bool NetworkGameManager::delSynchronizeableHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ) 169 169 { 170 170 … … 224 224 /** 225 225 * handler for MSGID_PREFEREDTEAM message 226 * @param message Id226 * @param messageType 227 227 * @param data 228 228 * @param dataLength … … 231 231 * @return 232 232 */ 233 bool NetworkGameManager::preferedTeamHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId )233 bool NetworkGameManager::preferedTeamHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ) 234 234 { 235 235 assert( SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/); … … 314 314 315 315 316 bool NetworkGameManager::chatMessageHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId )316 bool NetworkGameManager::chatMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ) 317 317 { 318 318 PRINTF(0)("NetworkGameManager::chatMessageHandler %d %d\n", userId, SharedNetworkData::getInstance()->getHostID() ); 319 319 if ( (SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/) && userId != SharedNetworkData::getInstance()->getHostID() ) 320 320 { 321 MessageManager::getInstance()->sendMessage( message Id, data, dataLength, RT_ALL_BUT_ME, 0, MP_HIGHBANDWIDTH );321 MessageManager::getInstance()->sendMessage( messageType, data, dataLength, RT_ALL_BUT_ME, 0, MP_HIGHBANDWIDTH ); 322 322 } 323 323 … … 334 334 } 335 335 336 int messageType = 0;337 Converter::byteArrayToInt( data, & messageType);336 int chatType = 0; 337 Converter::byteArrayToInt( data, &chatType); 338 338 int senderUserId = 0; 339 339 Converter::byteArrayToInt( data+INTSIZE, &senderUserId ); … … 341 341 Converter::byteArrayToString( data+2*INTSIZE, message, dataLength-2*INTSIZE ); 342 342 343 rules.handleChatMessage( senderUserId, message, messageType);343 rules.handleChatMessage( senderUserId, message, chatType); 344 344 345 345 return true; -
branches/proxy/src/lib/network/network_game_manager.h
r9406 r9508 70 70 NetworkGameManager(); 71 71 72 static bool delSynchronizeableHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId );73 static bool preferedTeamHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId );74 static bool chatMessageHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId );72 static bool delSynchronizeableHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ); 73 static bool preferedTeamHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ); 74 static bool chatMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ); 75 75 76 76 void setPreferedTeam( int userId, int teamId ); -
branches/proxy/src/lib/network/player_stats.cc
r9507 r9508 212 212 } 213 213 214 bool PlayerStats::changeNickHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId )214 bool PlayerStats::changeNickHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ) 215 215 { 216 216 std::string newNick; -
branches/proxy/src/lib/network/player_stats.h
r9507 r9508 66 66 inline std::string getNickName(){ return this->nickName; } 67 67 void setNickName( std::string nick ); 68 static bool changeNickHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId );68 static bool changeNickHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ); 69 69 void shellNick( const std::string& newNick ); 70 70 -
branches/proxy/src/subprojects/network/network_unit_test.cc
r9494 r9508 238 238 } 239 239 240 bool testCB( Message Id messageId, byte * data, int dataLength, void * someData, int userId )240 bool testCB( MessageType messageType, byte * data, int dataLength, void * someData, int userId ) 241 241 { 242 242 printf("GOT MESSAGE: %s\n", data); -
branches/proxy/src/world_entities/spawning_point.cc
r9494 r9508 216 216 } 217 217 218 bool SpawningPoint::respawnMessageHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId )218 bool SpawningPoint::respawnMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ) 219 219 { 220 220 if ( SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/) -
branches/proxy/src/world_entities/spawning_point.h
r9494 r9508 70 70 71 71 void sendRespawnMessage( int uniqueId ); 72 static bool respawnMessageHandler( Message Id messageId, byte * data, int dataLength, void * someData, int userId );72 static bool respawnMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId ); 73 73 74 74
Note: See TracChangeset
for help on using the changeset viewer.