- Timestamp:
- May 18, 2006, 2:32:47 PM (19 years ago)
- Location:
- branches/network/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/handshake.cc
r7613 r7681 24 24 #include <cassert> 25 25 26 Handshake::Handshake( bool server, int clientId, int networkGameManagerId )26 Handshake::Handshake( bool server, int clientId, int networkGameManagerId, int messageManagerId ) 27 27 : Synchronizeable() 28 28 { … … 33 33 this->localState.hostId = clientId; 34 34 this->localState.networkManagerId = networkGameManagerId; 35 this->localState.messageManagerId = messageManagerId; 35 36 36 37 orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId", PERMISSION_ALL ) ); 37 38 version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version", PERMISSION_ALL ) ); 38 39 netManId_handler = registerVarId( new SynchronizeableInt( &localState.networkManagerId, &remoteState.networkManagerId, "networkManagerId", PERMISSION_ALL ) ); 40 netManId_handler = registerVarId( new SynchronizeableInt( &localState.messageManagerId, &remoteState.messageManagerId, "networkManagerId", PERMISSION_ALL ) ); 39 41 hostId_handler = registerVarId( new SynchronizeableInt( &localState.hostId, &remoteState.hostId, "hostId", PERMISSION_ALL ) ); 40 42 completed_handler = registerVarId( new SynchronizeableInt( &localState.completed, &remoteState.completed, "completed", PERMISSION_ALL ) ); -
branches/network/src/lib/network/handshake.h
r7591 r7681 19 19 20 20 int networkManagerId; 21 int messageManagerId; 21 22 int hostId; 22 23 … … 31 32 { 32 33 public: 33 Handshake( bool server, int clientId = 0, int networkGameManagerId = 0 );34 Handshake( bool server, int clientId = 0, int networkGameManagerId = 0, int messageManagerId = 0 ); 34 35 inline bool completed(){ return localState.completed != 0 && remoteState.completed != 0; } 35 36 inline bool ok(){ return localState.error == 0 && remoteState.error == 0; } 36 37 inline int getHostId(){ return remoteState.hostId; } 37 38 inline int getNetworkGameManagerId(){ return remoteState.networkManagerId; } 39 inline int getMessageManagerId(){ return remoteState.messageManagerId; } 38 40 inline void doReject( std::string reason ){ localState.error = 1; localState.errorString = "the server rejected your connection ["+ reason +"]"; } 39 41 -
branches/network/src/lib/network/message_manager.cc
r7678 r7681 29 29 { 30 30 this->setClassID( CL_MESSAGE_MANAGER, "MessageManager" ); 31 newNumber = 1; 32 setSynchronized( true ); 31 33 } 32 34 … … 180 182 assert( i + messageLength <= length ); 181 183 assert( messageHandlerMap.find( (MessageId)messageId ) != messageHandlerMap.end() ); 182 (*(messageHandlerMap[(MessageId)messageId].cb))( (MessageId)messageId, data + i, messageLength, messageHandlerMap[(MessageId)messageId].someData, userId ); 184 if ( std::find( messageQueue[userId].recievedMessages.begin(), messageQueue[userId].recievedMessages.end(), number )== messageQueue[userId].recievedMessages.end() ) 185 { 186 (*(messageHandlerMap[(MessageId)messageId].cb))( (MessageId)messageId, data + i, messageLength, messageHandlerMap[(MessageId)messageId].someData, userId ); 187 messageQueue[userId].recievedMessages.push_back( number ); 188 } 183 189 i += messageLength; 190 } 191 192 193 //walk throu message queue and remove acked messages 194 for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); ) 195 { 196 if ( std::find( acks.begin(), acks.end(), it->number) != acks.end() ) 197 { 198 std::list<NetworkMessage>::iterator delIt = it; 199 it++; 200 messageQueue[userId].messages.erase( delIt ); 201 continue; 202 } 203 it++; 204 } 205 206 //TODO find bether way. maybe with timestamp 207 if ( messageQueue[userId].recievedMessages.size() > 1000 ) 208 { 209 for ( int j = 0; j < messageQueue[userId].recievedMessages.size() - 1000; j++ ) 210 messageQueue[userId].recievedMessages.erase( messageQueue[userId].recievedMessages.begin() ); 184 211 } 185 212 … … 238 265 assert( messageQueue[userId].messages.size() == 0 ); 239 266 } 267 268 /** 269 * send a message to one or more clients 270 * recieverType: 271 * RT_ALL send to all users. reciever is ignored 272 * RT_USER send only to reciever 273 * RT_NOT_USER send to all but reciever 274 * 275 * @param messageId message id 276 * @param data pointer to data 277 * @param dataLength length of data 278 * @param recieverType 279 * @param reciever 280 */ 281 void MessageManager::sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ) 282 { 283 for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ ) 284 { 285 if ( 286 recieverType == RT_ALL || 287 recieverType == RT_USER && it->first == reciever || 288 recieverType == RT_NOT_USER && it->first != reciever 289 ) 290 { 291 NetworkMessage msg; 292 293 msg.data = new byte[dataLength]; 294 memcpy( msg.data, data, dataLength ); 295 msg.length = dataLength; 296 msg.messageId = messageId; 297 msg.number = newNumber++; 298 msg.priority = messagePriority; 299 300 it->second.messages.push_back( msg ); 301 } 302 } 303 } 304 305 -
branches/network/src/lib/network/message_manager.h
r7678 r7681 28 28 enum MessageId 29 29 { 30 TESTMESSAGEID = 1 30 31 }; 31 32 … … 39 40 }; 40 41 42 //TODO implement priority handling 43 enum MessagePriority 44 { 45 MP_HIGHBANDWIDTH = 1, //!< fast and reliable but uses a lot of bandwidth 46 MP_LOWBANDWIDTH, //!< may take a long time to arrive. reliable 47 MP_UNRELIABLE //!< unreliable. low bandwidth 48 }; 49 41 50 struct NetworkMessage 42 51 { 43 MessageId messageId; 44 byte * data; 45 int length; 46 int number; 52 MessageId messageId; 53 byte * data; 54 int length; 55 int number; 56 MessagePriority priority; 47 57 }; 48 58 … … 51 61 std::list<NetworkMessage> messages; 52 62 std::list<int> toAck; 63 std::list<int> recievedMessages; 53 64 }; 54 65 … … 75 86 bool registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData ); 76 87 77 void sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever );88 void sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority ); 78 89 79 90 virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); … … 89 100 MessageHandlerMap messageHandlerMap; //!< contains handlers for messages 90 101 102 int newNumber; //!< used to create unique message numbers 103 91 104 }; 92 105 -
branches/network/src/lib/network/network_stream.cc
r7671 r7681 134 134 // and one for handshake to reject client maxCon+1 135 135 this->networkGameManager->setUniqueID( MAX_CONNECTIONS + 2 ); 136 136 MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); 137 137 } 138 138 … … 219 219 freeSocketSlots.pop_back(); 220 220 peers[clientId].socket = tempNetworkSocket; 221 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID() );221 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); 222 222 peers[clientId].handshake->setUniqueID(clientId); 223 223 peers[clientId].userId = clientId; … … 232 232 233 233 peers[clientId].socket = tempNetworkSocket; 234 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID() );234 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); 235 235 peers[clientId].handshake->setUniqueID(clientId); 236 236 peers[clientId].userId = clientId; … … 333 333 this->networkGameManager = NetworkGameManager::getInstance(); 334 334 this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); 335 MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); 335 336 } 336 337 … … 446 447 continue; 447 448 448 while ( packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE ) > 0)449 while ( 0 < (packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE )) ) 449 450 { 450 451 -
branches/network/src/subprojects/network/network_unit_test.cc
r7631 r7681 16 16 #include "state.h" 17 17 #include "shared_network_data.h" 18 #include "message_manager.h" 18 19 19 20 #include "simple_sync.h" … … 236 237 } 237 238 238 239 void testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) 240 { 241 printf("GOT MESSAGE: %s\n", data); 242 } 239 243 240 244 /** … … 261 265 ss->setSynchronized( true ); 262 266 267 //MessageManager::getInstance()->initUser( 1 ); 268 MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL ); 263 269 264 270 SDL_Delay(20); … … 266 272 for(;;) 267 273 { 274 MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL, 0, MP_HIGHBANDWIDTH ); 268 275 netMan->synchronize(); 269 276 SDL_Delay(1000); … … 299 306 SimpleSync * ss = NULL; 300 307 308 //MessageManager::getInstance()->initUser( 0 ); 309 MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL ); 310 301 311 for(;;) 302 312 { 303 313 netMan->synchronize(); 314 MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL, 0, MP_HIGHBANDWIDTH ); 304 315 ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) ); 305 316 SDL_Delay(1000);
Note: See TracChangeset
for help on using the changeset viewer.