Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9508 in orxonox.OLD for branches/proxy/src/lib


Ignore:
Timestamp:
Jul 27, 2006, 3:37:11 PM (18 years ago)
Author:
patrick
Message:

message handler: changed some variable names - standardisations to make it better readable. added some documentation

Location:
branches/proxy/src/lib/network
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/proxy/src/lib/network/message_manager.cc

    r9504 r9508  
    4444MessageManager::~MessageManager ()
    4545{
    46   for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ )
     46  for ( MessageQueue::iterator it = outgoingMessageQueue.begin(); it != outgoingMessageQueue.end(); it++ )
    4747  {
    4848    for ( std::list<NetworkMessage>::iterator it2 = it->second.messages.begin(); it2 != it->second.messages.end(); it2++ )
     
    5959  }
    6060
    61   messageQueue.clear();
     61  outgoingMessageQueue.clear();
    6262
    6363  this->messageHandlerMap.clear();
     
    8585  int n;
    8686
    87   n = Converter::intToByteArray( messageQueue[userId].toAck.size(), data + i, maxLength );
     87  n = Converter::intToByteArray( outgoingMessageQueue[userId].toAck.size(), data + i, maxLength );
    8888  i += n;
    8989  assert( n == INTSIZE );
    9090
    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++)
    9292  {
    9393    n = Converter::intToByteArray( *it, data + i, maxLength );
     
    9696  }
    9797
    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 );
    101101  i += n;
    102102  assert( n == INTSIZE );
    103103
    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++ )
    105105  {
    106106    n = Converter::intToByteArray( it->length, data + i, maxLength );
     
    112112    assert( n == INTSIZE );
    113113
    114     n = Converter::intToByteArray( it->messageId, data + i, maxLength );
     114    n = Converter::intToByteArray( it->messageType, data + i, maxLength );
    115115    i += n;
    116116    assert( n == INTSIZE );
     
    168168  i += n;
    169169
    170   int messageLength, messageId;
    171 
     170  int messageLength, messageType;
     171
     172  // now go through all messages
    172173  for ( int j = 0; j < nMessages; j++ )
    173174  {
     
    183184
    184185    assert( i + INTSIZE <= length );
    185     n = Converter::byteArrayToInt( data + i, &messageId );
     186    n = Converter::byteArrayToInt( data + i, &messageType );
    186187    assert( n == INTSIZE );
    187188    i += n;
    188189
    189190    if ( number > 0 )
    190       messageQueue[userId].toAck.push_back( number );
     191      outgoingMessageQueue[userId].toAck.push_back( number );
    191192
    192193    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 ) )
    197202      {
    198203        NetworkMessage msg;
     
    201206        memcpy( msg.data, data + i, messageLength );
    202207        msg.length = messageLength;
    203         msg.messageId = (MessageId)messageId;
     208        msg.messageType = (MessageType)messageType;
    204209        msg.number = userId;
    205210
    206         incomingMessageBuffer.push_back( msg );
     211        incomingMessageQueue.push_back( msg );
    207212      }
    208       messageQueue[userId].recievedMessages.push_back( number );
     213      outgoingMessageQueue[userId].recievedMessages.push_back( number );
    209214    }
    210215    i += messageLength;
     
    213218
    214219  //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 = incomingMessageBuffer.begin(); it != incomingMessageBuffer.end();  )
    216   {
    217     if ( (*(messageHandlerMap[it->messageId].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 ) )
    218223    {
    219224      std::list<NetworkMessage>::iterator delIt = it;
     
    221226        delete it->data;
    222227      it++;
    223       incomingMessageBuffer.erase( delIt );
     228      incomingMessageQueue.erase( delIt );
    224229      continue;
    225230    }
     
    228233
    229234  //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();  )
    231236  {
    232237    if ( std::find( acks.begin(), acks.end(), it->number) != acks.end() )
     
    234239      std::list<NetworkMessage>::iterator delIt = it;
    235240      it++;
    236       messageQueue[userId].messages.erase( delIt );
     241      outgoingMessageQueue[userId].messages.erase( delIt );
    237242      continue;
    238243    }
     
    241246
    242247  //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() );
    247252  }
    248253
     
    258263void MessageManager::cleanUpUser( int userId )
    259264{
    260   if ( messageQueue.find( userId ) == messageQueue.end() )
     265  if ( outgoingMessageQueue.find( userId ) == outgoingMessageQueue.end() )
    261266    return;
    262267
    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++ )
    264269  {
    265270    if ( it->data )
     
    268273  }
    269274
    270   messageQueue[userId].toAck.clear();
    271 
    272   messageQueue.erase( userId );
    273 }
    274 
    275 /**
    276  * registers function to handle messages with id messageId. someData is passed to callbackfuntion
    277  * @param messageId message id to handle
     275  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
    278283 * @param cb function pointer to callback function
    279284 * @param someData this pointer is passed to callback function without modification
    280285 * @return true on success
    281286 */
    282 bool MessageManager::registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData )
     287bool MessageManager::registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData )
    283288{
    284289  MessageHandler messageHandler;
    285290
    286291  messageHandler.cb = cb;
    287   messageHandler.messageId = messageId;
     292  messageHandler.messageType = messageType;
    288293  messageHandler.someData = someData;
    289294
    290   messageHandlerMap[messageId] = messageHandler;
     295  messageHandlerMap[messageType] = messageHandler;
    291296
    292297  return true;
     
    300305{
    301306  // 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 );
    304309}
    305310
     
    311316 *               RT_NOT_USER send to all but reciever
    312317 *
    313  * @param messageId message id
     318 * @param messageType message id
    314319 * @param data pointer to data
    315320 * @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 */
     324void 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++ )
    322328  {
    323329    if (
     
    335341      memcpy( msg.data, data, dataLength );
    336342      msg.length = dataLength;
    337       msg.messageId = messageId;
     343      msg.messageType = messageType;
    338344      msg.number = newNumber++;
    339345      msg.priority = messagePriority;
     
    350356    memcpy( msg.data, data, dataLength );
    351357    msg.length = dataLength;
    352     msg.messageId = messageId;
     358    msg.messageType = messageType;
    353359    msg.number = SharedNetworkData::getInstance()->getHostID();
    354360    msg.priority = messagePriority;
    355361
    356     incomingMessageBuffer.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  
    2020      int length
    2121      int number
    22       int MessageId
     22      int MessageType
    2323      byte * data
    2424    )[1..nmsg]
     
    2727
    2828//!< different message ids
    29 enum MessageId
     29enum MessageType
    3030{
    3131  TESTMESSAGEID                  = 1,         //!< for testing purposes
     
    4040
    4141
    42 typedef bool (*MessageCallback)( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
     42typedef bool (*MessageCallback)( MessageType messageType, byte * data, int dataLength, void * someData, int userId );
    4343
    4444enum RecieverType
     
    6161struct NetworkMessage
    6262{
    63   MessageId        messageId;
     63  MessageType        messageType;
    6464  byte *           data;
    6565  int              length;
     
    7777typedef std::map<int,MessageUserQueue> MessageQueue;
    7878
     79
     80
    7981struct MessageHandler
    8082{
    8183  MessageCallback cb;
    82   MessageId       messageId;
     84  MessageType       messageType;
    8385  void *          someData;
    8486};
    8587
    86 typedef std::map<MessageId,MessageHandler> MessageHandlerMap;
     88typedef std::map<MessageType,MessageHandler> MessageHandlerMap;
    8789
    8890//! A class for sending messages over network
     
    9597   virtual ~MessageManager();
    9698
    97    bool registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData );
     99   bool registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData );
    98100
    99    void sendMessage( MessageId 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 );
    100102
     103   void initUser( int userId );
     104
     105
     106  private:
    101107   virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH );
    102108   virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId );
     
    105111   virtual void handleRecvState( int userId, int stateId, int fromStateId ){}
    106112
    107    void initUser( int userId );
    108 
    109113
    110114 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
    113119   MessageHandlerMap         messageHandlerMap;      //!< contains handlers for messages
    114120
    115121   int                       newNumber;              //!< used to create unique message numbers
    116    std::list<NetworkMessage> incomingMessageBuffer;
     122
    117123
    118124};
  • branches/proxy/src/lib/network/network_game_manager.cc

    r9507 r9508  
    159159/**
    160160 * handler for remove synchronizeable messages
    161  * @param messageId
     161 * @param messageType
    162162 * @param data
    163163 * @param dataLength
     
    166166 * @return true on successfull handling else handler will be called again
    167167 */
    168 bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     168bool NetworkGameManager::delSynchronizeableHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId )
    169169{
    170170
     
    224224/**
    225225 * handler for MSGID_PREFEREDTEAM message
    226  * @param messageId
     226 * @param messageType
    227227 * @param data
    228228 * @param dataLength
     
    231231 * @return
    232232 */
    233 bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     233bool NetworkGameManager::preferedTeamHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId )
    234234{
    235235  assert( SharedNetworkData::getInstance()->isMasterServer() /*||  SharedNetworkData::getInstance()->isProxyServerActive()*/);
     
    314314
    315315
    316 bool NetworkGameManager::chatMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     316bool NetworkGameManager::chatMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId )
    317317{
    318318  PRINTF(0)("NetworkGameManager::chatMessageHandler %d %d\n", userId, SharedNetworkData::getInstance()->getHostID() );
    319319  if ( (SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/) && userId !=  SharedNetworkData::getInstance()->getHostID() )
    320320  {
    321     MessageManager::getInstance()->sendMessage( messageId, data, dataLength, RT_ALL_BUT_ME, 0, MP_HIGHBANDWIDTH );
     321    MessageManager::getInstance()->sendMessage( messageType, data, dataLength, RT_ALL_BUT_ME, 0, MP_HIGHBANDWIDTH );
    322322  }
    323323
     
    334334  }
    335335
    336   int messageType = 0;
    337   Converter::byteArrayToInt( data, &messageType );
     336  int chatType = 0;
     337  Converter::byteArrayToInt( data, &chatType);
    338338  int senderUserId = 0;
    339339  Converter::byteArrayToInt( data+INTSIZE, &senderUserId );
     
    341341  Converter::byteArrayToString( data+2*INTSIZE, message, dataLength-2*INTSIZE );
    342342
    343   rules.handleChatMessage( senderUserId, message, messageType );
     343  rules.handleChatMessage( senderUserId, message, chatType);
    344344
    345345  return true;
  • branches/proxy/src/lib/network/network_game_manager.h

    r9406 r9508  
    7070    NetworkGameManager();
    7171
    72     static bool delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
    73     static bool preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
    74     static bool chatMessageHandler( MessageId 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 );
    7575
    7676    void setPreferedTeam( int userId, int teamId );
  • branches/proxy/src/lib/network/player_stats.cc

    r9507 r9508  
    212212}
    213213
    214 bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     214bool PlayerStats::changeNickHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId )
    215215{
    216216  std::string newNick;
  • branches/proxy/src/lib/network/player_stats.h

    r9507 r9508  
    6666    inline std::string getNickName(){ return this->nickName; }
    6767    void setNickName( std::string nick );
    68     static bool changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
     68    static bool changeNickHandler( MessageType messageType, byte * data, int dataLength, void * someData, int userId );
    6969    void shellNick( const std::string&  newNick );
    7070
Note: See TracChangeset for help on using the changeset viewer.