Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 25, 2006, 2:39:53 PM (18 years ago)
Author:
patrick
Message:

merged the network branche to the trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/network_game_manager.cc

    r6634 r6695  
    4646 */
    4747NetworkGameManager::NetworkGameManager()
     48  : Synchronizeable()
    4849{
    4950  PRINTF(0)("START\n");
     
    5253  this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager");
    5354
    54   newUniqueID = MAX_CONNECTIONS + 2;
    55 
    5655  hasRequestedWorld = false;
     56  this->setSynchronized(true);
    5757}
    5858
     
    8080    b = data[i++];
    8181
     82    /**************** Commands only processed by servers ****************/
    8283    if ( isServer() )
    8384    {
    84       if ( b == REQUEST_CREATE )
     85      if ( b == NET_REQUEST_CREATE )
    8586      {
    8687        if ( !handleRequestCreate( i, data, length, sender ) )
     
    8889        continue;
    8990      }
    90       if ( b == REQUEST_REMOVE )
     91      else if ( b == NET_REQUEST_REMOVE )
    9192      {
    9293        if ( !handleRequestRemove( i, data, length, sender ) )
     
    9495        continue;
    9596      }
     97      else if ( b == NET_REQUEST_PNODE_PATH )
     98      {
     99        if ( !handleRequestPNodePath( i, data, length, sender ) )
     100          return i;
     101        continue;
     102      }
    96103    }
    97104    else
    98105    {
    99       if ( b == CREATE_ENTITY )
     106      /**************** Commands only processed by clients ****************/
     107      if ( b == NET_CREATE_ENTITY )
    100108      {
    101109        PRINTF(0)("CREATE_ENTITY\n");
     
    104112        continue;
    105113      }
    106       if ( b == REMOVE_ENTITY )
     114      else if ( b == NET_REMOVE_ENTITY )
    107115      {
    108116        if ( !handleRemoveEntity( i, data, length, sender ) )
     
    110118        continue;
    111119      }
    112       if ( b == CREATE_ENTITY_LIST )
     120      else if ( b == NET_CREATE_ENTITY_LIST )
    113121      {
    114122        if ( !handleCreateEntityList( i, data, length, sender ) )
     
    116124        continue;
    117125      }
    118       if ( b == REMOVE_ENTITY_LIST )
     126      else if ( b == NET_REMOVE_ENTITY_LIST )
    119127      {
    120128        if ( !handleRemoveEntityList( i, data, length, sender ) )
     
    122130        continue;
    123131      }
    124       if ( b == YOU_ARE_ENTITY )
     132      else if ( b == NET_YOU_ARE_ENTITY )
    125133      {
    126134        if ( !handleYouAreEntity( i, data, length, sender ) )
     
    130138    }
    131139
    132     if ( b == REQUEST_ENTITY_LIST )
     140    /**************** Commands processed by servers and clients ****************/
     141    if ( b == NET_REQUEST_ENTITY_LIST )
    133142    {
    134143      sendEntityList( sender );
    135144      continue;
    136145    }
    137 
    138     if ( b == REQUEST_SYNC )
     146    else if ( b == NET_REQUEST_SYNC )
    139147    {
    140148      if ( !handleRequestSync( i, data, length, sender ) )
     
    143151    }
    144152
    145     //if we get her something with data is wrong
     153
     154    PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender);
     155    PRINTF(1)("Probably this is because the network protocol has different \n");
     156    PRINTF(1)("versions or there occured an error in the sending algorithm\n");
    146157    PRINTF(1)("Data is not in the right format! i=%d\n", i);
    147158    return i;
     
    156167  {
    157168    SYNCHELP_WRITE_BEGIN();
    158     byte b = REQUEST_ENTITY_LIST;
     169    byte b = NET_REQUEST_ENTITY_LIST;
    159170    SYNCHELP_WRITE_BYTE( b );
    160171    hasRequestedWorld = true;
     
    200211int NetworkGameManager::createEntity( ClassID classID, int owner )
    201212{
    202   if ( this->isServer() )
    203   {
    204     if ( newUniqueID < 0 )
     213  if ( this->isServer())
     214  {
     215    if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 )
    205216    {
    206217      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
    207218      return -1;
    208219    }
    209 
    210     return this->executeCreateEntity( classID, newUniqueID++, owner );
     220    return this->executeCreateEntity( classID, SharedNetworkData::getInstance()->getNewUniqueID(), owner );
    211221  }
    212222  else
     
    227237  if ( this->isServer() )
    228238  {
    229     if ( newUniqueID < 0 )
     239    if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 )
    230240    {
    231241      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
     
    245255    {
    246256      Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
    247       s->setUniqueID( newUniqueID++ );
     257      s->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
    248258      s->setOwner( 0 );
    249       this->networkStream->connectSynchronizeable( *s );
     259      // all entities created via this function are added automaticaly to the synchronizeable list
     260      s->setSynchronized(true);
    250261      return b;
    251262    }
     
    258269  }
    259270  else
     271
    260272  {
    261273    PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value());
     
    295307      continue;
    296308
    297     if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_CREATE ) )
     309    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) )
    298310      return;
    299311    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
     
    313325      continue;
    314326
    315     if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_REMOVE ) )
     327    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) )
    316328      return;
    317329    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
     
    332344      continue;
    333345
    334     if ( !writeToClientBuffer( outBuffer[i], (byte)CREATE_ENTITY ) )
     346    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) )
    335347      return -1;
    336348    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
     
    342354  }
    343355
     356  PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
    344357  doCreateEntity( classID, uniqueID, owner );
    345358
     
    359372      continue;
    360373
    361     if ( !writeToClientBuffer( outBuffer[i], (byte)REMOVE_ENTITY ) )
     374    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) )
    362375      return;
    363376    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
     
    394407  e = this->networkStream->getSyncEnd();
    395408
    396   if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) )
     409  // send the packet header
     410  if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) )
    397411    return;
    398412
    399   // -2 because you must not send network_game_manager and handshake
     413  // send the number of entities: -2 because you must not send network_game_manager and handshake
    400414  if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )
    401415    return;
     
    403417  //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
    404418
    405   int n = 0;
    406 
     419  // first send the NullParent
     420  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getLeafClassID()) )
     421    return;
     422  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getUniqueID()) )
     423    return;
     424  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getOwner()) )
     425    return;
     426
     427  // now send the rest of the entities
    407428  while ( it != e )
    408429  {
    409     PRINTF(5)("SENDING ENTITY %s id %d\n", (*it)->getClassName(), (*it)->getUniqueID() );
    410     if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )
    411       return;
    412 
    413     if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )
    414       return;
    415 
    416     if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )
    417       return;
    418 
     430    if( (*it)->beSynchronized() && (*it) != PNode::getNullParent())
     431    {
     432      PRINTF(0)("SENDING ENTITY %s classid: %x, uniqueid %d\n", (*it)->getClassName(), (*it)->getLeafClassID(), (*it)->getUniqueID() );
     433      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )
     434        return;
     435
     436      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )
     437        return;
     438
     439      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )
     440        return;
     441    }
    419442    it++;
    420443  }
     
    467490BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
    468491{
    469   BaseObject * b = Factory::fabricate( classID );
     492  PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
     493
     494  BaseObject * b;
     495  /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
     496  /* Exception 1: NullParent */
     497  if( classID == CL_NULL_PARENT)
     498  {
     499    b = (BaseObject*)PNode::getNullParent();
     500  }
     501  else
     502    b = Factory::fabricate( classID );
    470503
    471504  if ( !b )
     
    480513    s->setUniqueID( uniqueID );
    481514    s->setOwner( owner );
    482     this->networkStream->connectSynchronizeable( *s );
     515    s->setSynchronized(true);
     516    //this->networkStream->connectSynchronizeable( *s );
    483517    if ( !isServer() )
    484518      s->setIsOutOfSync( true );
     
    646680  if ( userID != 0 )
    647681  {
    648     if ( !writeToClientBuffer( outBuffer[userID], (byte)YOU_ARE_ENTITY ) )
     682    if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) )
    649683      return;
    650684
     
    714748  i += Converter::byteArrayToInt( &data[i], &owner );
    715749
     750  PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
    716751  doCreateEntity( (ClassID)classID, uniqueID, owner );
    717752
     
    779814    {
    780815      BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );
    781 
    782       /*if ( b != NULL )
    783       {
    784         if ( b->isA(CL_WORLD_ENTITY) )
    785         {
    786           int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender );
    787 
    788           i += n;
    789         }
    790     }*/
    791816    }
    792817
     
    854879  return true;
    855880}
     881
     882
     883/**
     884 *  handles the network signal NET_REQUEST_PNODE_PATH
     885 * @param i byte offset in the buffer
     886 * @param data data array
     887 * @param length length of the data arary
     888 * @param sender the sender id
     889 * @return true if process terminated sucessfully
     890 */
     891bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)
     892{
     893  if( INTSIZE > length-i )
     894  {
     895    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
     896    return false;
     897  }
     898  PRINTF(0)("HandleRequestPNodePath:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
     899
     900  int uniqueID1, uniqueID2;
     901  if( INTSIZE > length-i )
     902  {
     903    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     904    return false;
     905  }
     906  i += Converter::byteArrayToInt( &data[i], &uniqueID1 );
     907
     908  if( INTSIZE > length-i )
     909  {
     910    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     911    return false;
     912  }
     913  i += Converter::byteArrayToInt( &data[i], &uniqueID2 );
     914
     915
     916  PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);
     917
     918  return true;
     919}
     920
    856921
    857922bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
     
    898963    resizeBufferVector( user );
    899964
    900   if ( !writeToClientBuffer( outBuffer[user], (byte)REQUEST_SYNC ) )
     965  if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) )
    901966    return;
    902967  if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
Note: See TracChangeset for help on using the changeset viewer.