Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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

finished and tested MessageManager

Location:
branches/network/src
Files:
6 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
  • branches/network/src/subprojects/network/network_unit_test.cc

    r7631 r7681  
    1616#include "state.h"
    1717#include "shared_network_data.h"
     18#include "message_manager.h"
    1819
    1920#include "simple_sync.h"
     
    236237}
    237238
    238 
     239void testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     240{
     241  printf("GOT MESSAGE: %s\n", data);
     242}
    239243
    240244/**
     
    261265  ss->setSynchronized( true );
    262266
     267  //MessageManager::getInstance()->initUser( 1 );
     268  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
    263269 
    264270  SDL_Delay(20);
     
    266272  for(;;)
    267273  {
     274    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
    268275    netMan->synchronize();
    269276    SDL_Delay(1000);
     
    299306  SimpleSync * ss = NULL;
    300307 
     308  //MessageManager::getInstance()->initUser( 0 );
     309  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
     310 
    301311  for(;;)
    302312  {
    303313    netMan->synchronize();
     314    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
    304315    ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
    305316    SDL_Delay(1000);
Note: See TracChangeset for help on using the changeset viewer.