Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6695 in orxonox.OLD for trunk/src/lib/network


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

merged the network branche to the trunk

Location:
trunk/src/lib/network
Files:
13 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/Makefile.am

    r6139 r6695  
    66libORXnet_a_SOURCES = synchronizeable.cc \
    77                      network_manager.cc \
     8                      shared_network_data.cc \
    89                      network_socket.cc \
    910                      connection_monitor.cc \
     
    2122noinst_HEADERS = synchronizeable.h \
    2223                 network_manager.h \
     24                 shared_network_data.h \
    2325                 network_socket.h \
    2426                 connection_monitor.h \
  • trunk/src/lib/network/handshake.cc

    r6341 r6695  
    2323
    2424Handshake::Handshake( bool server, int clientId, int networkGameManagerId )
     25  : Synchronizeable()
    2526{
    2627  /* set the class id for the base object */
     
    3435  this->setOwner(0);
    3536
     37  this->setSynchronized(true);
    3638  PRINTF(5)("Handshake created clientId = %d\n", clientId);
    3739}
  • trunk/src/lib/network/netdefs.h

    r5822 r6695  
    1717
    1818
     19#define MAX_CONNECTIONS 1000
     20
     21
    1922typedef unsigned char byte;
    2023
     
    2528} NodeType;
    2629
     30
     31typedef enum {
     32  NET_UID_UNASSIGNED = -1,
     33
     34  NET_UID_NUMBER
     35} UidType;
     36
    2737#endif /* _NETWORK_MANAGER */
  • 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 ) )
  • trunk/src/lib/network/network_game_manager.h

    r6634 r6695  
    1515
    1616class TiXmlElement;
     17class PNode;
    1718
    1819/**
    1920 * protocol definition
    2021 *
    21  *  CREATE_ENTITY:       CLASS_ID, UNIQUE_ID, OWNER
    22  *  REMOVE_ENTITY:       UNIQUE_ID
     22 *  CREATE_ENTITY:       >> CLASS_ID, UNIQUE_ID, OWNER
     23 *  REMOVE_ENTITY:       >> UNIQUE_ID
    2324 *
    24  *  CREATE_ENTITY_LIST:  NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER]
    25  *  REMOVE_ENTITY_LIST:  NUMBER, [UNIQUE_ID][0..NUMBER]
     25 *  CREATE_ENTITY_LIST:  >> NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER]
     26 *  REMOVE_ENTITY_LIST:  >> NUMBER, [UNIQUE_ID][0..NUMBER]
    2627 *
    27  *  REQUEST_CREATE:      CLASS_ID
    28  *  REQUEST_REMOVE:      UNIQUE_ID
     28 *  REQUEST_CREATE:      >> CLASS_ID
     29 *                       << [Sync Info]
     30 *  REQUEST_REMOVE:      >> UNIQUE_ID
     31 *                       << [Sync Info]
    2932 *
    3033 *  //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER]
     
    3235 *
    3336 *  REQUEST_ENTITY_LIST: //request the whole world :D
    34  *  REQUEST_SYNC:        UNIQUE_ID
     37 *  REQUEST_SYNC:        >> UNIQUE_ID
     38 *                       << [Sync Info]
    3539 *  //REQUEST_SYNC_LIST:   NUMBER, [UNIQUE_ID][0..NUMBER]
    3640 *
    37  *  YOU_ARE_ENTITY:      UNIQUE_ID
     41 *  YOU_ARE_ENTITY:      >> UNIQUE_ID
    3842 *
     43 *  REQUEST_PNODE_PATH   >> UNIQUE_ID_START   UNIQUE_ID_STOP
     44 *                       << UNIQUE_ID_1   UNIQUE_ID_2   UNIQUE_ID_3 ...   UNIQUE_ID_N
     45 *
     46 *  SEND_PNODE_PATH      >> UNIQUE_ID_START   UNIQUE_ID_STOP NUMBER [UNIQUE_ID][0..NUMBER]
    3947 */
    4048
    41 typedef enum NetworkGameManagerProtocol{
    42   CREATE_ENTITY = 0,
    43   REMOVE_ENTITY,
    44   CREATE_ENTITY_LIST,
    45   REMOVE_ENTITY_LIST,
    46   REQUEST_CREATE,
    47   REQUEST_REMOVE,
    48   REQUEST_SYNC,
    49   YOU_ARE_ENTITY,
    50   REQUEST_ENTITY_LIST
     49typedef enum NetworkGameManagerProtocol {
     50  NET_CREATE_ENTITY = 0,
     51  NET_REMOVE_ENTITY,
     52  NET_CREATE_ENTITY_LIST,
     53  NET_REMOVE_ENTITY_LIST,
     54  NET_REQUEST_CREATE,
     55  NET_REQUEST_REMOVE,
     56  NET_REQUEST_SYNC,
     57  NET_YOU_ARE_ENTITY,
     58  NET_REQUEST_ENTITY_LIST,
     59  NET_REQUEST_PNODE_PATH,
     60  NET_SEND_PNODE_PATH,
     61
     62  NET_NUMBER
    5163};
    5264
     
    8395    void sendEntityList(int userID);
    8496
     97
    8598  private:
    8699    NetworkGameManager();
    87100
     101
     102    /* some network signal handlers */
     103    bool handleRequestCreate( int& i, const byte* data, int length, int sender );
     104    bool handleRequestRemove( int& i, const byte* data, int length, int sender );
     105    bool handleCreateEntity( int& i, const byte* data, int length, int sender );
     106    bool handleRemoveEntity( int& i, const byte* data, int length, int sender );
     107    bool handleCreateEntityList( int& i, const byte* data, int length, int sender );
     108    bool handleRemoveEntityList( int& i, const byte* data, int length, int sender );
     109    bool handleYouAreEntity( int& i, const byte* data, int length, int sender );
     110    bool handleRequestSync( int& i, const byte* data, int length, int sender );
     111    bool handleRequestPNodePath(int& i, const byte* data, int length, int sender);
     112    bool handleSendPNodePath(int& i, const byte* data, int length, int sender);
     113
     114
     115    /* some network handlers helper functions */
    88116    void requestCreateEntity(ClassID classID);
    89117    int executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0);
     118    BaseObject* doCreateEntity(ClassID classID, int uniqueID, int owner);
    90119
    91120    void requestRemoveEntity(int uniqueID);
    92121    void executeRemoveEntity(int uniqueID);
     122    void doRemoveEntity(int uniqueID);
    93123
    94124    void executeRequestSync( int uniqueID, int user );
     125    void doRequestSync(int uniqueID, int userID);
    95126
    96     BaseObject* doCreateEntity(ClassID classID, int uniqueID, int owner);
    97     void doRemoveEntity(int uniqueID);
    98     void doRequestSync(int uniqueID, int userID);
    99127    void doYouAre( int uniqueID );
     128
     129    void requestPNodePath(const PNode* node1, const PNode* node2);
     130    void executeRequestPNodePath(const PNode* node2, const PNode* node2);
     131    void doRequestPNodePath(const PNode* node1, const PNode* node2);
    100132
    101133    bool canCreateEntity(ClassID classID);
     
    105137    void resizeBufferVector(int n);
    106138
    107     inline bool writeToClientBuffer( clientBuffer &cb, byte*data, int length );
    108     inline bool writeToClientBuffer( clientBuffer &cb, byte b );
    109     inline bool writeToClientBuffer( clientBuffer &cb, int i );
    110     inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length );
     139    bool writeToClientBuffer( clientBuffer &cb, byte*data, int length );
     140    bool writeToClientBuffer( clientBuffer &cb, byte b );
     141    bool writeToClientBuffer( clientBuffer &cb, int i );
     142    bool readFromClientBuffer( clientBuffer &cb, byte*data, int length );
    111143
    112     //helper functions for writeBytes
    113     inline bool handleRequestCreate( int& i, const byte* data, int length, int sender );
    114     inline bool handleRequestRemove( int& i, const byte* data, int length, int sender );
    115     inline bool handleCreateEntity( int& i, const byte* data, int length, int sender );
    116     inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender );
    117     inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender );
    118     inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender );
    119     inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender );
    120     inline bool handleRequestSync( int& i, const byte* data, int length, int sender );
    121144
    122145  private:
     
    124147    //clientBuffer                  allOutBuffer;
    125148    static NetworkGameManager*    singletonRef;
    126 
    127     int                           newUniqueID;
    128149    bool                          hasRequestedWorld;
    129150};
  • trunk/src/lib/network/network_manager.cc

    r6341 r6695  
    2020#define DEBUG_MODULE_NETWORK
    2121
    22 
    23 #include "network_stream.h"
    2422#include "class_list.h"
    25 
    2623#include "debug.h"
    27 
     24#include "shell_command.h"
    2825
    2926/* include your own header */
    3027#include "network_manager.h"
     28#include "shared_network_data.h"
     29#include "network_stream.h"
    3130
    3231
    3332/* using namespace std is default, this needs to be here */
    3433using namespace std;
     34
     35SHELL_COMMAND(debug, NetworkManager, debug);
    3536
    3637
     
    4950  this->netStreamList = NULL;
    5051  this->syncList = NULL;
    51   this->tmpStream = NULL;
    52 
    53   this->hostID = -1;
    54   this->bGameServer = false;
     52  this->defaultSyncStream = NULL;
     53  this->sharedNetworkData = SharedNetworkData::getInstance();
    5554
    5655  PRINTF(0)("NetworkManager created\n");
     
    9897  }
    9998
    100   this->tmpStream = new NetworkStream(ipAddress);
     99  this->defaultSyncStream = new NetworkStream(ipAddress);
     100  this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
     101  this->defaultSyncStream->startHandshake();
    101102  return 1;
    102103}
     
    109110int NetworkManager::createServer(unsigned int port)
    110111{
    111   this->hostID = 0;
    112   this->bGameServer = true;
    113   this->tmpStream = new NetworkStream(port);
    114   this->bGameServer = true;
     112  this->sharedNetworkData->setHostID(0);
     113  this->sharedNetworkData->setGameServer(true);
     114  this->defaultSyncStream = new NetworkStream(port);
     115  this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
     116  this->defaultSyncStream->createNetworkGameManager();
     117  PRINTF(0)("CREATE SERVER\n");
    115118  SDL_Delay(20);
    116119  return 1;
     
    118121
    119122
    120 /**
    121  *  creates a connection from one object to a host
    122  * @param address: the address of the destination host
    123  * @param synchronizeable: reference to the sync object
    124  */
    125 NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync)
    126 {
    127   /* creating a new network stream, it will register itself automaticaly to the class list */
    128   this->tmpStream = new NetworkStream(address);
    129   this->tmpStream->connectSynchronizeable(sync);
    130 }
    131 
    132 
    133 /**
    134  *  teardown a connection
    135  */
    136 void NetworkManager::shutdownConnection()
    137 {
    138   PRINTF(0)("Shutdown connection\n");
    139 }
    140 
    141 
    142123void NetworkManager::connectSynchronizeable(Synchronizeable& sync)
    143124{
    144   this->tmpStream->connectSynchronizeable(sync);
     125  if( this->defaultSyncStream)
     126    this->defaultSyncStream->connectSynchronizeable(sync);
    145127}
    146128
     
    157139      if( static_cast<NetworkStream*>(*stream)->isActive())
    158140        static_cast<NetworkStream*>(*stream)->processData();
    159 
    160141  }
    161 
    162142}
    163143
     144
     145
    164146/**
    165  * Sets the hostID to a specific number
    166  * @param id: The new ID
     147 * debug output
    167148 */
    168 void NetworkManager::setHostID(int id)
     149 void NetworkManager::debug()
    169150{
    170     this->hostID = id;
     151  PRINT(0)("=================Network::debug()=========\n");
     152  this->defaultSyncStream->debug();
     153  PRINT(0)("===========================================\n");
    171154}
  • trunk/src/lib/network/network_manager.h

    r6341 r6695  
    66/* you will want to add such a a line at your header file also, since it will
    77   prevent c++ from including your code twice*/
    8 #ifndef _NETWORK_MANGER
     8#ifndef _NETWORK_MANAGER
    99#define _NETWORK_MANAGER
    1010
    1111/* include this file, it contains some default definitions */
    1212#include "netdefs.h"
     13#include "shared_network_data.h"
    1314
    1415/* include base_object.h since all classes are derived from this one */
     
    3839    int createServer(unsigned int port);
    3940
    40     NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync);
    41     void shutdownConnection();
    42 
    43 
    44     void setHostID(int id);
    4541    /** Returns the hostID @return The hostID of the object */
    46     inline int getHostID() { return this->hostID; }
    47     inline bool isGameServer() { return this->bGameServer; }
     42    inline int getHostID() { return this->sharedNetworkData->getHostID(); }
     43    inline bool isGameServer() { return this->sharedNetworkData->isGameServer(); }
    4844
    4945
    5046    void connectSynchronizeable(Synchronizeable& sync);
    5147    void synchronize();
     48
     49    void debug();
     50
    5251
    5352  private:
     
    5958    const std::list<BaseObject*>*    syncList;                // list of synchronizeables
    6059    static NetworkManager*           singletonRef;            //!< Pointer to the only instance of this Class
    61     NetworkStream*                   tmpStream;               //!< FIXME: this is only for testing purposes
    62     int                              hostID;                  //!< The Host-ID of the Manager
    63     bool                             bGameServer;             //!< true if it is a server
     60    NetworkStream*                   defaultSyncStream;       //!< FIXME: this is only for testing purposes
    6461
     62    SharedNetworkData*               sharedNetworkData;       //!< reference to the shared data
    6563};
    6664
  • trunk/src/lib/network/network_socket.cc

    r6634 r6695  
    9797/**
    9898 * Default destructor
     99 * dont use this from outside: use destroy() instead!!
    99100 */
    100101NetworkSocket::~NetworkSocket( )
  • trunk/src/lib/network/network_socket.h

    r6139 r6695  
    8181  void init();
    8282
    83   //dont make this public use destroy() instead
    84   ~NetworkSocket();
    85 
    8683public:
    8784
    8885  NetworkSocket();
     86  ~NetworkSocket();
    8987  NetworkSocket(IPaddress ip);
    9088  NetworkSocket(TCPsocket sock);
  • trunk/src/lib/network/network_stream.cc

    r6634 r6695  
    5353}
    5454
     55
    5556NetworkStream::NetworkStream(IPaddress& address)
    5657{
     
    6061  this->networkProtocol = new NetworkProtocol();
    6162  this->connectionMonitor = new ConnectionMonitor();
    62 
    63   Handshake* hs = new Handshake(false);
    64   hs->setUniqueID( 0 );
    65   this->handshakes.push_back(hs);
    66   this->connectSynchronizeable(*hs);
    67   PRINTF(0)("NetworkStream: %s\n", hs->getName());
    6863  this->maxConnections = 1;
    6964}
     
    8176  this->handshakes.push_back( NULL );
    8277  this->bActive = true;
    83   this->networkGameManager = NetworkGameManager::getInstance();
    84   // setUniqueID( maxCon+2 ) because we need one id for every handshake
    85   // and one for handshake to reject client maxCon+1
    86   this->networkGameManager->setUniqueID( this->maxConnections+2 );
    87   this->connectSynchronizeable( *(this->networkGameManager) );
    88 
    89   this->setMaxConnections( 10 );
    9078}
    9179
     
    132120
    133121
     122void NetworkStream::createNetworkGameManager()
     123{
     124  this->networkGameManager = NetworkGameManager::getInstance();
     125  // setUniqueID( maxCon+2 ) because we need one id for every handshake
     126  // and one for handshake to reject client maxCon+1
     127  this->networkGameManager->setUniqueID( this->maxConnections + 2 );
     128  //this->connectSynchronizeable( *(this->networkGameManager) );
     129  this->setMaxConnections( 10 );
     130}
     131
     132
     133void NetworkStream::startHandshake()
     134{
     135  Handshake* hs = new Handshake(false);
     136  hs->setUniqueID( 0 );
     137  this->handshakes.push_back(hs);
     138  //this->connectSynchronizeable(*hs);
     139  PRINTF(0)("NetworkStream: %s\n", hs->getName());
     140}
     141
     142
    134143void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
    135144{
     
    140149    this->bActive = true;
    141150}
     151
    142152
    143153void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync)
     
    184194          if ( type != NET_SERVER )
    185195          {
    186             NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() );
     196            SharedNetworkData::getInstance()->setHostID( handshakes[i]->getHostId() );
    187197            myHostId = NetworkManager::getInstance()->getHostID();
    188198
    189199            this->networkGameManager = NetworkGameManager::getInstance();
    190200            this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() );
    191             this->connectSynchronizeable( *(this->networkGameManager) );
     201            //this->connectSynchronizeable( *(this->networkGameManager) );
    192202          }
    193203          else
     
    216226  /* DOWNSTREAM */
    217227
     228
     229
    218230  int dataLength;
    219231  int reciever;
     
    221233  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
    222234  {
    223     if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ )
     235    if ( (*it)!=NULL && (*it)->beSynchronized() /*&& (*it)->getOwner() == myHostId*/ )
    224236    {
    225237      do {
     
    357369    PRINTF(0)("New Client: %d\n", clientId);
    358370
    359     this->connectSynchronizeable(*handshakes[clientId]);
     371    //this->connectSynchronizeable(*handshakes[clientId]);
    360372  }
    361373
     
    416428
    417429
     430
     431void NetworkStream::debug()
     432{
     433  if( this->isServer())
     434    PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId);
     435  else
     436    PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId);
     437
     438  PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size());
     439  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
     440  {
     441    if( (*it)->beSynchronized() == true)
     442      PRINT(0)("  Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(),
     443               (*it)->getUniqueID(), (*it)->beSynchronized());
     444  }
     445  PRINT(0)(" Maximal Connections: %i\n", this->maxConnections);
     446
     447}
     448
     449
     450int NetworkStream::getSyncCount()
     451{
     452  int n = 0;
     453  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
     454    if( (*it)->beSynchronized() == true)
     455      ++n;
     456
     457  //return synchronizeables.size();
     458  return n;
     459}
     460
     461
     462
     463
     464
     465
  • trunk/src/lib/network/network_stream.h

    r6634 r6695  
    1414#include "server_socket.h"
    1515#include "handshake.h"
    16 
    17 #define MAX_CONNECTIONS 1000
    1816
    1917class Synchronizeable;
     
    4038    void init();
    4139
     40    void createNetworkGameManager();
     41    void startHandshake();
     42
    4243    void connectSynchronizeable(Synchronizeable& sync);
    4344    void disconnectSynchronizeable(Synchronizeable& sync);
     
    5354    inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); }
    5455    inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); }
    55     inline int getSyncCount(){ return synchronizeables.size(); }
     56    int getSyncCount();
    5657
    5758    inline bool isUserIdActive( int userID ) { if (userID>=networkSockets.size()) return false; else return networkSockets[userID]!=NULL; }
    5859
     60    void debug();
     61
     62
    5963  private:
    60     NetworkProtocol*       networkProtocol;
    61     ConnectionMonitor*     connectionMonitor;
    62     SynchronizeableList    synchronizeables;
    63     NetworkSocketVector    networkSockets;
    64     HandshakeVector        handshakes;
    65     ServerSocket*          serverSocket;
    66     int                    type;
    67     Header                 packetHeader;
    68     bool                   bActive;
    69     std::list<int>         freeSocketSlots;
     64    void updateConnectionList();
    7065
    71     int                    myHostId;
    72     int                    maxConnections;
    7366
    74     NetworkGameManager*   networkGameManager;
     67  private:
     68    NetworkProtocol*           networkProtocol;
     69    ConnectionMonitor*         connectionMonitor;
     70    SynchronizeableList        synchronizeables;
     71    NetworkSocketVector        networkSockets;
     72    HandshakeVector            handshakes;
     73    ServerSocket*              serverSocket;
     74    int                        type;
     75    Header                     packetHeader;
     76    bool                       bActive;
     77    std::list<int>             freeSocketSlots;
    7578
    76     void updateConnectionList();
     79    int                        myHostId;
     80    int                        maxConnections;
     81
     82    NetworkGameManager*        networkGameManager;
    7783};
    7884#endif /* _NETWORK_STREAM */
  • trunk/src/lib/network/synchronizeable.cc

    r6424 r6695  
    1717#define DEBUG_MODULE_NETWORK
    1818
     19#include "shared_network_data.h"
     20#include "network_stream.h"
     21#include "netdefs.h"
     22
     23#include "state.h"
     24
     25#include "assert.h"
     26
    1927#include "synchronizeable.h"
    20 #include "netdefs.h"
    21 #include "network_manager.h"
    22 #include "network_stream.h"
     28
    2329
    2430
     
    2935{
    3036  this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable");
    31   owner = 0;
    32   state = 0;
    33   hostID = NetworkManager::getInstance()->getHostID();
     37  this->owner = 0;
     38  this->state = 0;
     39  this->hostID = SharedNetworkData::getInstance()->getHostID();
    3440  this->setIsServer(this->hostID == 0);
    35   uniqueID = -1;
     41  this->uniqueID = NET_UID_UNASSIGNED;
    3642  this->networkStream = NULL;
    3743  this->setRequestedSync( false );
    3844  this->setIsOutOfSync( !(this->isServer()) );
     45  this->bSynchronize = false;
     46
     47  if( State::isOnline())
     48  {
     49    NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream();
     50    assert(nd != NULL);
     51    nd->connectSynchronizeable(*this);
     52    this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID());
     53  }
    3954}
    4055
     
    5065}
    5166
     67
    5268/**
    5369 *  write data to NetworkStream
     
    5773  PRINTF(5)("Synchronizeable::writeBytes was called\n");
    5874}
     75
    5976
    6077/**
     
    87104}
    88105
     106
    89107/**
    90108 * Sets the outofsync flag to a given value
     
    100118}
    101119
     120
    102121/**
    103122 * Determines if the server flag is set
     
    108127  return (this->state & STATE_SERVER) >0;
    109128}
     129
    110130
    111131/**
     
    118138}
    119139
     140
    120141/**
    121142 * Determines if the requestedSync flag is set
     
    126147  return (this->state & STATE_REQUESTEDSYNC) >0;
    127148}
     149
    128150
    129151/**
  • trunk/src/lib/network/synchronizeable.h

    r6341 r6695  
    163163
    164164class Synchronizeable : virtual public BaseObject
    165   {
     165{
     166
    166167  public:
    167168    Synchronizeable();
    168     ~Synchronizeable();
     169    virtual ~Synchronizeable();
    169170
    170171    virtual int       writeBytes(const byte* data, int length, int sender);
     
    179180    bool isOutOfSync();
    180181    bool requestedSync();
     182
    181183    inline void setUniqueID( int id ){ uniqueID = id; }
    182     inline int  getUniqueID() const { return uniqueID; };
     184    inline int  getUniqueID() const { return uniqueID; }
     185    inline int getHostID() { return this->hostID; }
     186
     187    inline int getOwner(){ return owner; }
     188    inline void setOwner(int owner){ this->owner = owner; }
     189
     190    /** @returns true if this Synchronizeable has to be synchronized over network */
     191    inline bool beSynchronized() { return this->bSynchronize; }
     192    /** @param bSynchronize sets the Synchronizeable to be sunchronized or not */
     193    inline void setSynchronized(bool bSynchronize) { this->bSynchronize = bSynchronize; }
     194
    183195    inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
    184196    inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } };
    185     inline int getHostID() { return this->hostID; }
    186 
    187     inline int getOwner(){ return owner; }
    188     inline void setOwner(int owner){ this->owner = owner; }
    189197
    190198    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
     199    inline NetworkStream* getNetworkStream() { return this->networkStream; }
     200
     201
     202  protected:
     203    NetworkStream*    networkStream;
     204    int               state;
     205
    191206
    192207  private:
    193 
    194208    int               uniqueID;
    195 
    196 
    197 
    198     //static std::vector<Synchronizeable*> classList;
    199     int owner;
    200     int hostID;
    201 
    202     std::list<int> synchronizeRequests;
    203 
    204   protected:
    205     NetworkStream* networkStream;
    206     int state;
    207 
    208   };
     209    int               owner;
     210    int               hostID;
     211    bool              bSynchronize;
     212
     213    std::list<int>    synchronizeRequests;
     214
     215};
    209216#endif /* _SYNCHRONIZEABLE_H */
Note: See TracChangeset for help on using the changeset viewer.