Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Dec 21, 2005, 9:11:47 AM (19 years ago)
Author:
rennerc
Message:

network_game_manager: implemented some functions

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

Legend:

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

    r6197 r6214  
    2222#include "factory.h"
    2323#include "network_stream.h"
     24#include "converter.h"
    2425
    2526/* include your own header */
     
    3738  /* set the class id for the base object */
    3839  this->setClassID(CL_ENTITY_MANAGER, "EntityManager");
     40
     41  allOutBuffer.length = 0;
     42
     43  allOutBuffer.maxLength = 10*1024;
     44
     45  allOutBuffer.buffer = new byte[10*1024];
     46
     47  newUniqueID = MAX_CONNECTIONS + 2;
    3948}
    4049
     
    4453NetworkGameManager::~NetworkGameManager()
    4554{
    46   for ( int i = 0; i<inBuffer.size(); i++)
    47   {
    48     if ( inBuffer[i].buffer )
    49       delete inBuffer[i].buffer;
     55  for ( int i = 0; i<outBuffer.size(); i++)
     56  {
    5057    if ( outBuffer[i].buffer )
    5158      delete outBuffer[i].buffer;
    5259  }
     60
     61  if ( allOutBuffer.buffer )
     62    delete allOutBuffer.buffer;
    5363}
    5464
     
    5666void NetworkGameManager::writeBytes(const byte* data, int length, int sender)
    5767{
     68  int i = 0;
     69  byte b;
     70
     71  while ( i<length )
     72  {
     73    b = data[i++];
     74
     75    if ( b == REQUEST_CREATE )
     76    {
     77      return;
     78    }
     79    if ( b == REQUEST_REMOVE )
     80    {
     81      return;
     82    }
     83    if ( b == CREATE_ENTITY )
     84    {
     85      return;
     86    }
     87    if ( b == REMOVE_ENTITY )
     88    {
     89      return;
     90    }
     91    if ( b == REQUEST_SYNC )
     92    {
     93      return;
     94    }
     95    if ( b == YOU_ARE_ENTITY )
     96    {
     97      return;
     98    }
     99    if ( b == CREATE_ENTITY_LIST )
     100    {
     101      return;
     102    }
     103    if ( b == REMOVE_ENTITY_LIST )
     104    {
     105      return;
     106    }
     107  }
    58108}
    59109
    60110int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever)
    61111{
     112  for ( int i = 0; i<outBuffer.size(); i++ )
     113  {
     114    if ( outBuffer[i].length>0 )
     115    {
     116      int nbytes = outBuffer[i].length;
     117
     118      if ( nbytes > maxLength )
     119      {
     120        PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength);
     121        return 0;
     122      }
     123
     124      memcpy(data, outBuffer[i].buffer, nbytes);
     125      return nbytes;
     126    }
     127  }
     128
     129  int nbytes = allOutBuffer.length;
     130
     131  if ( nbytes <=0 )
     132    return 0;
     133
     134  if ( nbytes > maxLength )
     135  {
     136    PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.length\n", nbytes, maxLength);
     137    return 0;
     138  }
     139
     140  memcpy( data, allOutBuffer.buffer, nbytes );
     141  return nbytes;
    62142}
    63143
     
    73153/*!
    74154 * Checks whether this is connected to a server or a client
    75  * and afterwards creates the needed entity if possible
     155 * and afterwards creates the needed entity
    76156 * @param classID: The ID of the class of which an entity should be created
    77157 */
    78 void NetworkGameManager::createEntity(int classID)
    79 {
     158void NetworkGameManager::createEntity(ClassID classID)
     159{
     160  if ( this->isServer() )
     161  {
     162    if ( newUniqueID < 0 )
     163    {
     164      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
     165      return;
     166    }
     167
     168    this->executeCreateEntity( classID, newUniqueID++ );
     169  }
     170  else
     171  {
     172    this->requestCreateEntity( classID);
     173  }
    80174}
    81175
     
    87181void NetworkGameManager::removeEntity(int uniqueID)
    88182{
     183  if ( this->isServer() )
     184  {
     185    this->executeRemoveEntity( uniqueID );
     186  }
     187  else
     188  {
     189    this->requestRemoveEntity( uniqueID );
     190  }
    89191}
    90192
     
    95197 * @param classID: The ID of the class of which an entity should be created
    96198 */
    97 void NetworkGameManager::requestCreateEntity(int classID)
    98 {
     199void NetworkGameManager::requestCreateEntity(ClassID classID)
     200{
     201  byte* bp;
     202  byte b = REQUEST_CREATE;
     203  bp = Converter::intToByteArray( classID );
     204
     205  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
     206  {
     207    PRINTF(1)("Could not write to clientBuffer\n");
     208    return;
     209  }
     210
     211  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
     212  {
     213    PRINTF(1)("Could not write to clientBuffer\n");
     214    delete bp;
     215    return;
     216  }
     217  delete bp;
    99218}
    100219
     
    105224void NetworkGameManager::requestRemoveEntity(int uniqueID)
    106225{
     226  byte* bp;
     227  byte b = REQUEST_REMOVE;
     228  bp = Converter::intToByteArray( uniqueID );
     229
     230  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
     231  {
     232    PRINTF(1)("Could not write to clientBuffer\n");
     233    return;
     234  }
     235
     236  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
     237  {
     238    PRINTF(1)("Could not write to clientBuffer\n");
     239    delete bp;
     240    return;
     241  }
     242  delete bp;
    107243}
    108244
     
    112248 * @param classID: The ID of the class of which an entity should be created
    113249 */
    114 void NetworkGameManager::executeCreateEntity(int classID)
    115 {
     250void NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner)
     251{
     252  byte* bp;
     253  byte b = CREATE_ENTITY;
     254
     255  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
     256  {
     257    PRINTF(1)("Could not write to clientBuffer\n");
     258    return;
     259  }
     260
     261  bp = Converter::intToByteArray( uniqueID );
     262  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
     263  {
     264    PRINTF(1)("Could not write to clientBuffer\n");
     265    delete bp;
     266    return;
     267  }
     268  delete bp;
     269
     270  bp = Converter::intToByteArray( uniqueID );
     271  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
     272  {
     273    PRINTF(1)("Could not write to clientBuffer\n");
     274    delete bp;
     275    return;
     276  }
     277  delete bp;
     278
     279  bp = Converter::intToByteArray( owner );
     280  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
     281  {
     282    PRINTF(1)("Could not write to clientBuffer\n");
     283    delete bp;
     284    return;
     285  }
     286  delete bp;
     287
     288  doCreateEntity( classID, uniqueID, owner );
    116289}
    117290
     
    123296void NetworkGameManager::executeRemoveEntity(int uniqueID)
    124297{
     298  byte* bp;
     299  byte b = REMOVE_ENTITY;
     300
     301  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
     302  {
     303    PRINTF(1)("Could not write to clientBuffer\n");
     304    return;
     305  }
     306
     307  bp = Converter::intToByteArray( uniqueID );
     308  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
     309  {
     310    PRINTF(1)("Could not write to clientBuffer\n");
     311    delete bp;
     312    return;
     313  }
     314  delete bp;
     315
     316  doRemoveEntity(uniqueID);
    125317}
    126318
     
    129321 * @return: true if the entity can be created, false otherwise
    130322 */
    131 bool NetworkGameManager::canCreateEntity(int classID)
     323bool NetworkGameManager::canCreateEntity(ClassID classID)
    132324{
    133325  return true;
     
    140332void NetworkGameManager::sendEntityList( int userID )
    141333{
     334  if ( !isServer() )
     335    return;
     336
     337  if ( userID > outBuffer.size() )
     338    resizeBufferVector( userID );
     339
     340  SynchronizeableList::const_iterator it, e;
     341
     342  it = this->networkStream->getSyncBegin();
     343  e = this->networkStream->getSyncEnd();
     344
     345  byte b = CREATE_ENTITY_LIST;
     346  byte* ib;
     347
     348  if ( !writeToClientBuffer( outBuffer[userID], &b, 1 ) )
     349  {
     350    PRINTF(1)("Could not write to clientBuffer\n");
     351    return;
     352  }
     353
     354  ib = Converter::intToByteArray( networkStream->getSyncCount() );
     355  if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
     356  {
     357    PRINTF(1)("Could not write to clientBuffer\n");
     358    delete ib;
     359    return;
     360  }
     361  delete ib;
     362
     363  while ( it != e )
     364  {
     365    ib = Converter::intToByteArray( (*it)->getClassID() );
     366    if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
     367    {
     368      PRINTF(1)("Could not write to clientBuffer\n");
     369      delete ib;
     370      return;
     371    }
     372    delete ib;
     373
     374    ib = Converter::intToByteArray( (*it)->getUniqueID() );
     375    if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
     376    {
     377      PRINTF(1)("Could not write to clientBuffer\n");
     378      delete ib;
     379      return;
     380    }
     381    delete ib;
     382
     383    ib = Converter::intToByteArray( (*it)->getOwner() );
     384    if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
     385    {
     386      PRINTF(1)("Could not write to clientBuffer\n");
     387      delete ib;
     388      return;
     389    }
     390    delete ib;
     391
     392    it++;
     393  }
    142394}
    143395
     
    148400void NetworkGameManager::resizeBufferVector( int n )
    149401{
    150   for ( int i = inBuffer.size(); i<=n; i++)
    151   {
    152     clientBuffer inBuf;
     402  for ( int i = outBuffer.size(); i<=n; i++)
     403  {
    153404    clientBuffer outBuf;
    154405
    155     inBuf.length = 0;
    156406    outBuf.length = 0;
    157407
    158     inBuf.maxLength = 5*1014;
    159408    outBuf.maxLength = 5*1024;
    160409
    161     inBuf.buffer = new byte[5*1014];
    162410    outBuf.buffer = new byte[5*1014];
    163411
    164     inBuffer.push_back(inBuf);
    165412    outBuffer.push_back(outBuf);
    166413  }
     
    175422void NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
    176423{
    177   //BaseObject * b = Factory::fabricate( classID );
    178   BaseObject * b = NULL;
     424  BaseObject * b = Factory::fabricate( classID );
     425
     426  if ( !b )
     427  {
     428    PRINTF(1)("Could not fabricate Object with classID %d\n", classID);
     429    return;
     430  }
    179431
    180432  if ( b->isA(CL_SYNCHRONIZEABLE) )
     
    209461      break;
    210462    }
     463    it++;
    211464  }
    212465}
     
    230483      break;
    231484    }
    232   }
    233 }
     485    it++;
     486  }
     487}
     488
     489/**
     490 * Copies length bytes to the clientBuffer with error checking
     491 * @param clientBuffer: the clientBuffer to write to
     492 * @param data: buffer to the data
     493 * @param length: length of data
     494 * @return false on error true else
     495 */
     496bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )
     497{
     498  if ( length > cb.maxLength-cb.length )
     499  {
     500    PRINTF(1)("No space left in clientBuffer\n");
     501    return false;
     502  }
     503
     504  memcpy( cb.buffer+cb.length, data, length );
     505  return true;
     506}
     507
     508/**
     509 * Reads data from clientBuffer with error checking
     510 * @param clientBuffer: the clientBuffer to read from
     511 * @param data: pointer to the buffer
     512 * @param length:
     513 * @return
     514 */
     515bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )
     516{
     517  if ( cb.length < length )
     518  {
     519    PRINTF(0)("There is not enough data in clientBuffer\n");
     520    return 0;
     521  }
     522
     523  memcpy( data, cb.buffer+cb.length-length, length );
     524  return true;
     525}
     526
     527/**
     528 * Tells this client that he has to control this entity
     529 * @param uniqueID: the entity's uniqeID
     530 */
     531void NetworkGameManager::doYouAre( int uniqueID )
     532{
     533  //TODO: what has to be done
     534}
     535
     536/**
     537 * Tells a remote client that he has to control this entity
     538 * @param uniqueID: the entity's uniqeID
     539 * @param userID: the users ID
     540 */
     541void NetworkGameManager::sendYouAre( int uniqueID, int userID )
     542{
     543  if ( !isServer() )
     544    return;
     545
     546  byte* bp;
     547  byte b = YOU_ARE_ENTITY;
     548
     549  if ( userID != 0 )
     550  {
     551    if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
     552    {
     553      PRINTF(1)("Could not write to clientBuffer\n");
     554      return;
     555    }
     556
     557    bp = Converter::intToByteArray( uniqueID );
     558    if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
     559    {
     560      PRINTF(1)("Could not write to clientBuffer\n");
     561      delete bp;
     562      return;
     563    }
     564    delete bp;
     565  }
     566  else
     567  {
     568    doYouAre(uniqueID);
     569  }
     570}
     571
  • branches/network/src/lib/network/network_game_manager.h

    r6190 r6214  
    2727 *  REQUEST_REMOVE:      UNIQUE_ID
    2828 *
    29  *  REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER]
    30  *  REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]
     29 *  //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER]
     30 *  //REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]
    3131 *
    3232 *  REQUEST_SYNC:        UNIQUE_ID
    33  *  REQUEST_SYNC_LIST:   NUMBER, [UNIQUE_ID][0..NUMBER]
     33 *  //REQUEST_SYNC_LIST:   NUMBER, [UNIQUE_ID][0..NUMBER]
    3434 *
     35 *  YOU_ARE_ENTITY:      UNIQUE_ID
    3536 *
    3637 */
     
    3940  CREATE_ENTITY = 0,
    4041  REMOVE_ENTITY,
     42  CREATE_ENTITY_LIST,
     43  REMOVE_ENTITY_LIST,
    4144  REQUEST_CREATE,
    42   REQUEST_SYNC
     45  REQUEST_REMOVE,
     46  REQUEST_SYNC,
     47  YOU_ARE_ENTITY
    4348};
    4449
     
    6469    virtual void readDebug() const;
    6570
    66     void createEntity(int classID);
     71    void createEntity(ClassID classID);
    6772    void removeEntity(int uniqueID);
     73    void sendYouAre( int uniqueID, int userID );
    6874
    6975    void sync(int uniqueID);
     
    7278
    7379  private:
    74     void requestCreateEntity(int classID);
    75     void executeCreateEntity(int classID);
     80    void requestCreateEntity(ClassID classID);
     81    void executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0);
    7682
    7783    void requestRemoveEntity(int uniqueID);
     
    8187    void doRemoveEntity(int uniqueID);
    8288    void doRequestSync(int uniqueID, int userID);
     89    void doYouAre( int uniqueID );
    8390
    84     bool canCreateEntity(int classID);
     91    bool canCreateEntity(ClassID classID);
    8592
    8693    void resizeBufferVector(int n);
    8794
     95    inline bool writeToClientBuffer( clientBuffer &cb, byte*data, int length );
     96    inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length );
     97
    8898  private:
    89     std::vector<clientBuffer>     inBuffer;
    9099    std::vector<clientBuffer>     outBuffer;
     100    clientBuffer                  allOutBuffer;
     101
     102    int                           newUniqueID;
    91103};
    92104
  • branches/network/src/lib/network/network_stream.cc

    r6190 r6214  
    382382    return;
    383383  }
     384
     385  if ( n > MAX_CONNECTIONS )
     386  {
     387    PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS);
     388    return;
     389  }
     390
    384391  this->maxConnections = n;
    385392  this->networkGameManager->setUniqueID( n+2 );
  • branches/network/src/lib/network/network_stream.h

    r6190 r6214  
    1414#include "server_socket.h"
    1515#include "handshake.h"
     16
     17#define MAX_CONNECTIONS 1000
    1618
    1719class Synchronizeable;
     
    5153    inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); }
    5254    inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); }
     55    inline int getSyncCount(){ return synchronizeables.size(); }
    5356
    5457  private:
Note: See TracChangeset for help on using the changeset viewer.