Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7681 in orxonox.OLD for branches/network/src/lib


Ignore:
Timestamp:
May 18, 2006, 2:32:47 PM (19 years ago)
Author:
rennerc
Message:

finished and tested MessageManager

Location:
branches/network/src/lib/network
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/handshake.cc

    r7613 r7681  
    2424#include <cassert>
    2525
    26 Handshake::Handshake( bool server, int clientId, int networkGameManagerId )
     26Handshake::Handshake( bool server, int clientId, int networkGameManagerId, int messageManagerId )
    2727  : Synchronizeable()
    2828{
     
    3333  this->localState.hostId = clientId;
    3434  this->localState.networkManagerId = networkGameManagerId;
     35  this->localState.messageManagerId = messageManagerId;
    3536 
    3637  orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId", PERMISSION_ALL ) );
    3738  version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version", PERMISSION_ALL ) );
    3839  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 ) );
    3941  hostId_handler = registerVarId( new SynchronizeableInt( &localState.hostId, &remoteState.hostId, "hostId", PERMISSION_ALL ) );
    4042  completed_handler = registerVarId( new SynchronizeableInt( &localState.completed, &remoteState.completed, "completed", PERMISSION_ALL ) );
  • branches/network/src/lib/network/handshake.h

    r7591 r7681  
    1919 
    2020  int networkManagerId;
     21  int messageManagerId;
    2122  int hostId;
    2223 
     
    3132{
    3233  public:
    33     Handshake( bool server, int clientId = 0, int networkGameManagerId = 0 );
     34    Handshake( bool server, int clientId = 0, int networkGameManagerId = 0, int messageManagerId = 0 );
    3435    inline bool       completed(){ return localState.completed != 0 && remoteState.completed != 0; }
    3536    inline bool       ok(){ return localState.error == 0 && remoteState.error == 0; }
    3637    inline int        getHostId(){ return remoteState.hostId; }
    3738    inline int        getNetworkGameManagerId(){ return remoteState.networkManagerId; }
     39    inline int        getMessageManagerId(){ return remoteState.messageManagerId; }
    3840    inline void       doReject( std::string reason ){ localState.error = 1; localState.errorString = "the server rejected your connection ["+ reason +"]"; }
    3941   
  • branches/network/src/lib/network/message_manager.cc

    r7678 r7681  
    2929{
    3030  this->setClassID( CL_MESSAGE_MANAGER, "MessageManager" );
     31  newNumber = 1;
     32  setSynchronized( true );
    3133}
    3234
     
    180182    assert( i + messageLength <= length );
    181183    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    }
    183189    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() );
    184211  }
    185212
     
    238265  assert( messageQueue[userId].messages.size() == 0 );
    239266}
     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 */
     281void 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  
    2828enum MessageId
    2929{
     30  TESTMESSAGEID = 1
    3031};
    3132
     
    3940};
    4041
     42//TODO implement priority handling
     43enum 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
    4150struct NetworkMessage
    4251{
    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;
    4757};
    4858
     
    5161  std::list<NetworkMessage> messages;
    5262  std::list<int>            toAck;
     63  std::list<int>            recievedMessages;
    5364};
    5465
     
    7586   bool registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData );
    7687   
    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 );
    7889
    7990   virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH );
     
    89100   MessageHandlerMap       messageHandlerMap;   //!< contains handlers for messages
    90101
     102   int                     newNumber;           //!< used to create unique message numbers
     103
    91104};
    92105
  • branches/network/src/lib/network/network_stream.cc

    r7671 r7681  
    134134  // and one for handshake to reject client maxCon+1
    135135  this->networkGameManager->setUniqueID( MAX_CONNECTIONS + 2 );
    136 
     136  MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
    137137}
    138138
     
    219219      freeSocketSlots.pop_back();
    220220      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() );
    222222      peers[clientId].handshake->setUniqueID(clientId);
    223223      peers[clientId].userId = clientId;
     
    232232     
    233233      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());
    235235      peers[clientId].handshake->setUniqueID(clientId);
    236236      peers[clientId].userId = clientId;
     
    333333            this->networkGameManager = NetworkGameManager::getInstance();
    334334            this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() );
     335            MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() );
    335336          }
    336337
     
    446447      continue;
    447448
    448     while ( packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE ) > 0 )
     449    while ( 0 < (packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE )) )
    449450    {
    450451
Note: See TracChangeset for help on using the changeset viewer.