Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7565 in orxonox.OLD for branches/network


Ignore:
Timestamp:
May 10, 2006, 1:13:49 PM (19 years ago)
Author:
rennerc
Message:

reimplemented NetworkStream

Location:
branches/network/src
Files:
14 edited

Legend:

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

    r7444 r7565  
    4141  error_handler = registerVarId( new SynchronizeableInt( &localState.error, &remoteState.error, "error" ) );
    4242  errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString" ) );
    43 
     43 
     44  localState.completed = false;
     45  localState.error = 0;
     46  localState.errorString = "";
     47  localState.hostId = clientId;
     48  localState.networkManagerId = networkGameManagerId;
     49  localState.orxId = _ORXONOX_ID;
     50  localState.version = _ORXONOX_VERSION;
     51 
     52  remoteState.completed = false;
     53  remoteState.error = 0;
     54  remoteState.errorString = "";
     55  remoteState.hostId = -1;
     56  remoteState.networkManagerId = -1;
     57  remoteState.orxId = 0;
     58  remoteState.version = 0;
    4459
    4560  this->setSynchronized(true);
     
    5368void Handshake::varChangeHandler( std::list< int > & id )
    5469{
     70  for ( std::list<int>::iterator it = id.begin(); it != id.end(); it++ )
     71  {
     72    if ( *it == orxId_handler )
     73    {
     74      if ( remoteState.orxId != _ORXONOX_ID )
     75      {
     76        localState.error = 1;
     77        localState.completed = true;
     78        localState.errorString = "Seems not to be orxonox!";
     79        return;
     80      }
     81    }
     82     
     83    if ( *it == version_handler )
     84    {
     85      if ( remoteState.version != _ORXONOX_VERSION )
     86      {
     87        localState.error = 2;
     88        localState.completed = true;
     89        localState.errorString = "Versions of server and client do not match!";
     90        return;
     91      }
     92    }
     93   
     94  }
    5595}
    5696
  • branches/network/src/lib/network/handshake.h

    r7444 r7565  
    1010#include "synchronizeable.h"
    1111
    12 #define _INITIAL_DATA 0xF91337A0
     12#define _ORXONOX_ID 0xF91337A0
    1313
    1414#define _ORXONOX_VERSION 1
  • branches/network/src/lib/network/network_stream.cc

    r7540 r7565  
    2323#include "base_object.h"
    2424#include "network_protocol.h"
    25 #include "tcp_socket.h"
    26 #include "tcp_server_socket.h"
     25#include "udp_socket.h"
     26#include "udp_server_socket.h"
    2727#include "connection_monitor.h"
    2828#include "synchronizeable.h"
    2929#include "network_game_manager.h"
    3030#include "shared_network_data.h"
     31
     32#include "lib/util/loading/factory.h"
    3133
    3234#include "debug.h"
     
    5961  this->type = NET_CLIENT;
    6062  this->init();
    61   this->networkSockets.push_back(new TcpSocket( host, port ));
     63  this->peers[0].socket = new UdpSocket( host, port );
    6264  this->networkProtocol = new NetworkProtocol();
    6365  this->connectionMonitor = new ConnectionMonitor();
    64   this->maxConnections = 1;
    65 }
    66 
    67 
    68 NetworkStream::NetworkStream( int port)
     66  this->maxConnections = MAX_CONNECTIONS;
     67}
     68
     69
     70NetworkStream::NetworkStream( int port )
    6971{
    7072  this->type = NET_SERVER;
    7173  this->init();
    72   this->serverSocket = new TcpServerSocket(port);
     74  this->serverSocket = new UdpServerSocket(port);
    7375  this->networkProtocol = new NetworkProtocol();
    7476  this->connectionMonitor = new ConnectionMonitor();
    75   this->networkSockets.push_back( NULL );
    76   this->networkSockets[0] = NULL; //TODO: remove this
    77   this->handshakes.push_back( NULL );
    7877  this->bActive = true;
    7978}
     
    8887  this->networkGameManager = NULL;
    8988  myHostId = 0;
     89  currentState = 0;
    9090}
    9191
     
    9999  }
    100100
    101   for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++)
    102   {
    103     if ( *i )
    104     {
    105       (*i)->disconnectServer();
    106     }
    107   }
    108 
    109   for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++)
    110   {
    111     if ( *i )
    112     {
    113       delete (*i);
    114     }
     101  for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++)
     102  {
     103    if ( i->second.socket )
     104    {
     105      i->second.socket->disconnectServer();
     106      delete i->second.socket;
     107      i->second.socket = NULL;
     108    }
     109   
     110    if ( i->second.handshake )
     111    {
     112      delete i->second.handshake;
     113      i->second.handshake = NULL;
     114    }
     115  }
     116 
     117  if ( serverSocket )
     118  {
     119    delete serverSocket;
     120    serverSocket = NULL;
    115121  }
    116122
     
    126132  // and one for handshake to reject client maxCon+1
    127133  this->networkGameManager->setUniqueID( this->maxConnections + 2 );
    128   //this->connectSynchronizeable( *(this->networkGameManager) );
    129   this->setMaxConnections( 10 );
     134
    130135}
    131136
     
    135140  Handshake* hs = new Handshake(false);
    136141  hs->setUniqueID( 0 );
    137   this->handshakes.push_back(hs);
     142  assert( peers[0].handshake == NULL );
     143  peers[0].handshake = hs;
    138144  //this->connectSynchronizeable(*hs);
    139145  PRINTF(0)("NetworkStream: %s\n", hs->getName());
     
    146152  sync.setNetworkStream( this );
    147153
    148   if( this->networkSockets.size()>0 )
    149     this->bActive = true;
     154  this->bActive = true;
    150155}
    151156
     
    158163    this->synchronizeables.erase(disconnectSynchro);
    159164
    160   if( this->networkSockets.size()<=0 )
    161     this->bActive = false;
     165  this->bActive = false;
    162166}
    163167
     
    165169void NetworkStream::processData()
    166170{
     171  currentState++;
     172 
    167173  if ( this->type == NET_SERVER )
    168174    this->updateConnectionList();
    169175  else
    170176  {
    171     if ( networkSockets[0] && !networkSockets[0]->isOk() )
     177    if ( peers[0].socket && !peers[0].socket->isOk() )
    172178    {
    173179      PRINTF(1)("lost connection to server\n");
    174180
    175       //delete networkSockets[i];
    176       networkSockets[0]->disconnectServer();
    177       networkSockets[0] = NULL;
    178 
    179       if ( handshakes[0] )
    180         delete handshakes[0];
    181       handshakes[0] = NULL;
    182     }
    183   }
    184 
    185   for (int i = 0; i<handshakes.size(); i++)
    186   {
    187     if ( handshakes[i] )
    188     {
    189       if ( handshakes[i]->completed() )
    190       {
    191         if ( handshakes[i]->ok() )
    192         {
    193           if ( type != NET_SERVER )
    194           {
    195             SharedNetworkData::getInstance()->setHostID( handshakes[i]->getHostId() );
    196             myHostId = SharedNetworkData::getInstance()->getHostID();
    197 
    198             this->networkGameManager = NetworkGameManager::getInstance();
    199             this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() );
    200             //this->connectSynchronizeable( *(this->networkGameManager) );
    201           }
    202           else
    203           {
    204 
    205           }
    206           PRINT(0)("handshake finished id=%d\n", handshakes[i]->getNetworkGameManagerId());
    207 
    208 
    209           delete handshakes[i];
    210           handshakes[i] = NULL;
    211         }
    212         else
    213         {
    214           PRINT(1)("handshake failed!\n");
    215           networkSockets[i]->disconnectServer();
    216           delete handshakes[i];
    217           handshakes[i] = NULL;
    218           //TODO: handle error
    219         }
    220       }
    221     }
    222   }
     181      peers[0].socket->disconnectServer();
     182      delete peers[0].socket;
     183      peers[0].socket = NULL;
     184
     185      if ( peers[0].handshake )
     186        delete peers[0].handshake;
     187      peers[0].handshake = NULL;
     188    }
     189  }
     190
     191
    223192
    224193
    225194  /* DOWNSTREAM */
    226 
     195#if 0
    227196
    228197
     
    341310        {
    342311#warning fix this
    343 #if 0
     312
    344313          if ( *it && (*it)->getUniqueID()==header.synchronizeableID )
    345314          {
     
    351320            continue;
    352321          }
     322
     323        }
     324
     325      } while ( dataLength>0 );
     326    }
     327
     328  }
    353329#endif
    354         }
    355 
    356       } while ( dataLength>0 );
    357     }
    358   }
    359330}
    360331
     
    372343      clientId = freeSocketSlots.back();
    373344      freeSocketSlots.pop_back();
    374       networkSockets[clientId] = tempNetworkSocket;
    375       handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
    376       handshakes[clientId]->setUniqueID(clientId);
     345      peers[clientId].socket = tempNetworkSocket;
     346      peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
     347      peers[clientId].handshake->setUniqueID(clientId);
     348      peers[clientId].userId = clientId;
    377349    } else
    378350    {
    379       clientId = networkSockets.size();
    380       networkSockets.push_back(tempNetworkSocket);
    381       Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
    382       tHs->setUniqueID(clientId);
    383       handshakes.push_back(tHs);
     351      clientId = 0;
     352     
     353      for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
     354        if ( it->first >= clientId )
     355          clientId = it->first + 1;
     356     
     357      peers[clientId].socket = tempNetworkSocket;
     358      peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
     359      peers[clientId].handshake->setUniqueID(clientId);
     360      peers[clientId].userId = clientId;
    384361    }
    385362
    386363    if ( clientId > this->maxConnections )
    387364    {
    388       handshakes[clientId]->doReject( "too many connections" );
     365      peers[clientId].handshake->doReject( "too many connections" );
    389366      PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
    390367    }
     
    398375
    399376  //check if connections are ok else remove them
    400   for ( int i = 1; i<networkSockets.size(); i++)
    401   {
    402     if ( networkSockets[i] && !networkSockets[i]->isOk() )
    403     {
    404       //TODO: tell EntityManager that this player left the game
    405       PRINTF(0)("Client is gone: %d\n", i);
    406 
    407       //delete networkSockets[i];
    408       networkSockets[i]->disconnectServer();
    409       networkSockets[i] = NULL;
    410 
    411       if ( handshakes[i] )
    412         delete handshakes[i];
    413       handshakes[i] = NULL;
    414 
    415 
    416       NetworkGameManager::getInstance()->signalLeftPlayer(i);
    417 
    418       if ( i == networkSockets.size()-1 )
    419       {
    420         networkSockets.pop_back();
    421         handshakes.pop_back();
    422       }
    423       else
    424       {
    425         freeSocketSlots.push_back(i);
    426       }
    427     }
    428   }
    429 
    430 
    431 }
    432 
    433 void NetworkStream::setMaxConnections( int n )
    434 {
    435   if ( !this->isServer() )
    436   {
    437     PRINTF(1)("Cannot set maxConnections because I am no server.\n");
    438   }
    439   if ( this->networkSockets.size() > 1 )
    440   {
    441     PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size());
    442     return;
    443   }
    444 
    445   if ( n > MAX_CONNECTIONS )
    446   {
    447     PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS);
    448     return;
    449   }
    450 
    451   this->maxConnections = n;
    452   this->networkGameManager->setUniqueID( n+2 );
    453 }
    454 
    455 
     377  for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
     378  {
     379    if ( it->second.socket && !it->second.socket->isOk() )
     380    {
     381      PRINTF(0)("Client is gone: %d\n", it->second.userId);
     382
     383      it->second.socket->disconnectServer();
     384      delete it->second.socket;
     385      it->second.socket = NULL;
     386
     387      if ( it->second.handshake )
     388        delete it->second.handshake;
     389      it->second.handshake = NULL;
     390
     391
     392      NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId);
     393
     394      freeSocketSlots.push_back( it->second.userId );
     395
     396    }
     397  }
     398
     399
     400}
    456401
    457402void NetworkStream::debug()
     
    485430}
    486431
    487 
    488 
    489 
    490 
    491 
     432/**
     433 * check if handshakes completed
     434 */
     435void NetworkStream::handleHandshakes( )
     436{
     437  for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
     438  {
     439    if ( it->second.handshake )
     440    {
     441      if ( it->second.handshake->completed() )
     442      {
     443        if ( it->second.handshake->ok() )
     444        {
     445          if ( type != NET_SERVER )
     446          {
     447            SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() );
     448            myHostId = SharedNetworkData::getInstance()->getHostID();
     449
     450            this->networkGameManager = NetworkGameManager::getInstance();
     451            this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() );
     452          }
     453
     454          PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId());
     455
     456
     457          delete it->second.handshake;
     458          it->second.handshake = NULL;
     459        }
     460        else
     461        {
     462          PRINT(1)("handshake failed!\n");
     463          it->second.socket->disconnectServer();
     464        }
     465      }
     466    }
     467  }
     468}
     469
     470/**
     471 * handle upstream network traffic
     472 */
     473void NetworkStream::handleUpstream( )
     474{
     475  byte buf[UDP_PACKET_SIZE];
     476  int offset;
     477  int n;
     478 
     479  for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ )
     480  {
     481    offset = INTSIZE; //make already space for length
     482   
     483    if ( peer->second.socket )
     484      continue;
     485   
     486    n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset );
     487    assert( n == INTSIZE );
     488    offset += n;
     489   
     490    n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset );
     491    assert( n == INTSIZE );
     492    offset += n;
     493   
     494    n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset );
     495    assert( n == INTSIZE );
     496    offset += n;
     497   
     498    for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
     499    {
     500      Synchronizeable & sync = **it;
     501     
     502      assert( offset + INTSIZE <= UDP_PACKET_SIZE );
     503     
     504      n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset );
     505      assert( n == INTSIZE );
     506      offset += n;
     507     
     508      offset += sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, 0 );
     509    }
     510   
     511    assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE );
     512   
     513    assert( peer->second.socket->writePacket( buf, offset ) );
     514  }
     515}
     516
     517/**
     518 * handle downstream network traffic
     519 */
     520void NetworkStream::handleDownstream( )
     521{
     522  byte buf[UDP_PACKET_SIZE];
     523  int offset = 0;
     524 
     525  int length = 0;
     526  int packetLength = 0;
     527  int uniqueId = 0;
     528  int state = 0;
     529  int ackedState = 0;
     530  int fromState = 0;
     531 
     532  for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ )
     533  {
     534    packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE );
     535   
     536    assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE );
     537    assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE );
     538    assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE );
     539    assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE );
     540   
     541    //if this is an old state drop it
     542    if ( state <= peer->second.lastRecvedState )
     543      continue;
     544   
     545    if ( packetLength != length )
     546    {
     547      PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length);
     548      peer->second.socket->disconnectServer();
     549      continue;
     550    }
     551   
     552    offset = 4*INTSIZE;
     553   
     554    while ( offset < length )
     555    {
     556      assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE );
     557      offset += INTSIZE;
     558     
     559      Synchronizeable * sync = NULL;
     560     
     561      for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
     562      {
     563        if ( (*it)->getUniqueID() == uniqueId )
     564        {
     565          sync = *it;
     566          break;
     567        }
     568      }
     569     
     570      if ( sync == NULL )
     571      {
     572        //TODO dont accept new object from all peers (probably only servers)
     573        int leafClassId;
     574        if ( INTSIZE > length - offset )
     575          break;
     576       
     577        Converter::byteArrayToInt( buf + offset, &leafClassId );
     578       
     579        BaseObject * b;
     580        /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
     581        /* Exception 1: NullParent */
     582        if( leafClassId == CL_NULL_PARENT)
     583        {
     584          PRINTF(1)("Can not create Class with ID %x!", (int)leafClassId);
     585          break;
     586        }
     587        else
     588          b = Factory::fabricate( (ClassID)leafClassId );
     589
     590        if ( !b )
     591        {
     592          PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId);
     593          break;
     594        }
     595       
     596        if ( b->isA(CL_SYNCHRONIZEABLE) )
     597        {
     598          sync = dynamic_cast<Synchronizeable*>(b);
     599          sync->setUniqueID( uniqueId );
     600          sync->setSynchronized(true);
     601 
     602          PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID());
     603        }
     604        else
     605        {
     606          PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)leafClassId);
     607          delete b;
     608          break;
     609        }
     610      }
     611     
     612      offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState );
     613    }
     614   
     615    if ( offset != length )
     616    {
     617      peer->second.socket->disconnectServer();
     618    }
     619   
     620    peer->second.lastAckedState = ackedState;
     621  }
     622}
     623
     624
     625
     626
     627
     628
  • branches/network/src/lib/network/network_stream.h

    r7540 r7565  
    99#include <vector>
    1010#include <list>
     11#include <map>
    1112
    1213#include "data_stream.h"
     
    2223class NetworkGameManager;
    2324
     25class PeerInfo
     26{
     27  public:
     28    PeerInfo() { clear(); }
     29    void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; }
     30    int             userId;
     31    bool            isServer;
     32    NetworkSocket * socket;
     33    Handshake *     handshake;
     34    int             lastAckedState;
     35    int             lastRecvedState;
     36};
     37
    2438typedef std::list<Synchronizeable*>  SynchronizeableList;
    25 typedef std::vector<NetworkSocket*>  NetworkSocketVector;
    26 typedef std::vector<Handshake*>      HandshakeVector;
     39typedef std::map<int,PeerInfo>       PeerList;
    2740
    2841
     
    4861
    4962    inline int getMaxConnections(){ return maxConnections; }
    50     void setMaxConnections( int n );
    5163
    5264    virtual void processData();
     
    5668    int getSyncCount();
    5769
    58     inline bool isUserIdActive( int userID ) { if (userID>=networkSockets.size()) return false; else return networkSockets[userID]!=NULL; }
     70    inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); }
    5971
    6072    void debug();
     73   
     74    inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; }
    6175
    6276
    6377  private:
    6478    void updateConnectionList();
     79    void handleHandshakes();
     80    void handleUpstream();
     81    void handleDownstream();
    6582
    6683
     
    6986    ConnectionMonitor*         connectionMonitor;
    7087    SynchronizeableList        synchronizeables;
    71     NetworkSocketVector        networkSockets;
    72     HandshakeVector            handshakes;
     88    PeerList                   peers;
    7389    ServerSocket*              serverSocket;
    7490    int                        type;
     
    7995    int                        myHostId;
    8096    int                        maxConnections;
     97   
     98    int                        currentState;
    8199
    82100    NetworkGameManager*        networkGameManager;
  • branches/network/src/lib/network/server_socket.h

    r7540 r7565  
    2727    virtual NetworkSocket* getNewSocket( void ) = 0;
    2828    virtual void close() = 0;
     29    virtual void update() = 0;
    2930    virtual bool isOk() { return this->bOk; };
    3031
  • branches/network/src/lib/network/synchronizeable.cc

    r7559 r7565  
    4141  this->networkStream = NULL;
    4242  this->bSynchronize = false;
     43  this->mLeafClassId = getLeafClassID();
    4344
    4445  if( State::isOnline())
     
    5051  }
    5152
     53  /* make sure loadClassId is first synced var because this is read by networkStream */
     54  assert( syncVarList.size() == 0 );
     55  this->registerVar( new SynchronizeableInt( &this->mLeafClassId, &this->mLeafClassId, "leafClassId" ) );
     56 
     57  this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) );
    5258  this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) );
    5359}
     
    175181      i += n;
    176182    }
     183    else if ( ! (
     184                 this->isServer() && (*it)->checkPremission( PERMISSION_SERVER ) ||
     185                 this->owner == this->hostID && (*it)->checkPremission( PERMISSION_OWNER ) ||
     186                 (*it)->checkPremission( PERMISSION_ALL )
     187                ) )
     188    {
     189      for ( int j = 0; j < (*it)->getSize(); it++ )
     190      {
     191        stateTo->data[i] = 0;
     192        i++;
     193      }
     194    }
    177195    else
    178196    {
     
    212230 * @param fromStateId id of the base state id
    213231 * @return true on success
     232 * @todo check for permissions
    214233 */
    215234bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId )
     
    264283    stateFrom = (*it);
    265284
     285  std::list<int> changes;
     286 
    266287  //apply diff
    267288  for ( int i = 0; i<length; i++ )
     
    271292    else
    272293      stateTo->data[i] = data[i];
     294   
     295    if ( data[i] != 0 )
     296      changes.push_back(i);
    273297  }
    274298
     
    282306    i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i );
    283307  }
     308 
     309  this->varChangeHandler( changes );
    284310
    285311  assert( i == length -1 );
  • branches/network/src/lib/network/synchronizeable.h

    r7559 r7565  
    4141
    4242enum {
    43   PERMISSION_OWNER = 1,
    44   PERMISSION_ALL   = 2
     43  PERMISSION_SERVER = 1,
     44  PERMISSION_OWNER  = 2,
     45  PERMISSION_ALL    = 4
    4546};
    4647
     
    8990  private:
    9091    int               uniqueID;       //!< unique id assigned to synchronizeable
     92    int               mLeafClassId;   //!< store leafClassId to send via states
    9193    int               owner;          //!< hostId of owner ( 0 if none / server )
    9294    int               hostID;         //!< my own host id
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc

    r7559 r7565  
    8787  n += res;
    8888
    89   res += Converter::byteArrayToFloat( buf + n, &x );
     89  res += Converter::byteArrayToFloat( buf + n, &y );
    9090  assert( res > 0 );
    9191  n += res;
    9292
    93   res += Converter::byteArrayToFloat( buf + n, &x );
     93  res += Converter::byteArrayToFloat( buf + n, &z );
    9494  assert( res > 0 );
    9595  n += res;
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_vector.cc

    r7559 r7565  
    8484  n += res;
    8585
    86   res += Converter::byteArrayToFloat( buf + n, &x );
     86  res += Converter::byteArrayToFloat( buf + n, &y );
    8787  assert( res > 0 );
    8888  n += res;
    8989
    90   res += Converter::byteArrayToFloat( buf + n, &x );
     90  res += Converter::byteArrayToFloat( buf + n, &z );
    9191  assert( res > 0 );
    9292  n += res;
  • branches/network/src/lib/network/tcp_server_socket.h

    r7541 r7565  
    2727    virtual NetworkSocket* getNewSocket( void );
    2828    virtual void close();
     29    virtual void update() {};
    2930   
    3031  private:
  • branches/network/src/lib/network/udp_server_socket.cc

    r7556 r7565  
    7272
    7373/**
    74  * get newly connected socket. note: this function will also recieve
    75  * packets and create new sockets
     74 * get newly connected socket. note
    7675 * @return new socket or NULL if no new socket exists
    7776 */
    7877NetworkSocket * UdpServerSocket::getNewSocket( void )
     78{
     79  NetworkSocket * result = NULL;
     80 
     81  if ( newSocketList.size() > 0 )
     82  {
     83    result = newSocketList.front();
     84 
     85    newSocketList.pop_front();
     86  }
     87 
     88  return result;
     89}
     90
     91/**
     92 * stop listening on server
     93 */
     94void UdpServerSocket::close( )
     95{
     96 
     97  for ( int i = 0; i < packetBuffer.size(); i++ )
     98    removeUserPackets( i );
     99 
     100  packetBuffer.clear();
     101  userList.clear();
     102 
     103  SDLNet_UDP_Close( socket );
     104  socket = NULL;
     105}
     106
     107/**
     108 * clean up users buffer
     109 * @param userId users userid
     110 */
     111void UdpServerSocket::removeUserPackets( int userId )
     112{
     113  if ( userId >= packetBuffer.size() )
     114    return;
     115 
     116  for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ )
     117  {
     118    if ( it->data )
     119    {
     120      free( it->data );
     121      it->data = NULL;
     122    }
     123  }
     124 
     125  packetBuffer[userId].clear();
     126}
     127
     128/**
     129 * get next packet for user
     130 * @param userId user id
     131 * @return recieved packet or packet with length 0 if no packet available
     132 */
     133NetworkPacket UdpServerSocket::getPacket( int userId )
     134{
     135  NetworkPacket res;
     136  res.data = NULL;
     137  res.length = 0;
     138 
     139  if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
     140  {
     141    res.data = packetBuffer[userId].front().data;
     142    res.length = packetBuffer[userId].front().length;
     143    packetBuffer[userId].pop_front();
     144  }
     145 
     146  return res;
     147}
     148
     149/**
     150 * get number of packets recieved for user
     151 * @param userId user id
     152 * @return number of packets in buffer
     153 */
     154int UdpServerSocket::getPacketCount( int userId )
     155{
     156  if ( userId >= packetBuffer.size() )
     157    return -1;
     158 
     159  return packetBuffer[userId].size();
     160}
     161
     162/**
     163 * will set user state
     164 * @param userId users id
     165 * @param ip users host / port
     166 */
     167void UdpServerSocket::initUser( int userId, IPaddress ip )
     168{
     169  int channel = SDLNet_UDP_Bind( socket, userId, &ip );
     170  if( channel != userId )
     171  {
     172    PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
     173    assert(false);
     174    return;
     175  }
     176 
     177  if ( userId < packetBuffer.size() )
     178    removeUserPackets( userId );
     179 
     180  if ( packetBuffer.size() <= userId )
     181    packetBuffer.resize( userId + 1 );
     182 
     183  if ( userList.size() <= userId )
     184    userList.resize( userId + 1 );
     185 
     186  userList[ userId ] = ip;
     187}
     188
     189/**
     190 * remove user from list
     191 * @param userId user id
     192 */
     193void UdpServerSocket::removeUser( int userId )
     194{
     195  removeUserPackets( userId );
     196 
     197  if ( userId >= userList.size() )
     198    return;
     199 
     200  userList[userId].host = 0;
     201  userList[userId].port = 0;
     202 
     203  SDLNet_UDP_Unbind( socket, userId );
     204}
     205
     206/**
     207 * send one packet to client associated to userId
     208 * @param networkPacket packet to send
     209 * @param userId users id
     210 * @return true on success
     211 */
     212bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId )
     213{
     214  assert( networkPacket.length <= UDP_PACKET_SIZE );
     215 
     216  memcpy( packet->data, networkPacket.data, networkPacket.length );
     217  packet->len = networkPacket.length;
     218  packet->channel = -1;
     219 
     220  if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 )
     221  {
     222    PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
     223    return false;
     224  }
     225 
     226  return true;
     227}
     228
     229/**
     230 * do periodically things
     231 */
     232void UdpServerSocket::update( )
    79233{
    80234  int res;
     
    84238  {
    85239    int userId;
     240    bool isNewConnection = false;
    86241   
    87242    for ( userId =0; userId < userList.size(); userId++ )
     
    92247    {
    93248      newConn++;
     249      isNewConnection = true;
    94250     
    95251      if ( newConn > MAX_NEW_CONNECTIONS )
     
    118274    }
    119275    else
     276    {
     277      if ( isNewConnection )
     278        continue;
     279     
    120280      networkPacket.data = NULL;
     281    }
    121282    memcpy( networkPacket.data, packet->data, packet->len );
    122283    packetBuffer[userId].push_back( networkPacket );
     
    124285 
    125286  assert( res == 0 );
    126  
    127   NetworkSocket * result = NULL;
    128  
    129   if ( newSocketList.size() > 0 )
    130   {
    131     result = newSocketList.front();
    132  
    133     newSocketList.pop_front();
    134   }
    135  
    136   return result;
    137 }
    138 
    139 /**
    140  * stop listening on server
    141  */
    142 void UdpServerSocket::close( )
    143 {
    144  
    145   for ( int i = 0; i < packetBuffer.size(); i++ )
    146     removeUserPackets( i );
    147  
    148   packetBuffer.clear();
    149   userList.clear();
    150  
    151   SDLNet_UDP_Close( socket );
    152   socket = NULL;
    153 }
    154 
    155 /**
    156  * clean up users buffer
    157  * @param userId users userid
    158  */
    159 void UdpServerSocket::removeUserPackets( int userId )
    160 {
    161   if ( userId >= packetBuffer.size() )
    162     return;
    163  
    164   for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ )
    165   {
    166     if ( it->data )
    167     {
    168       free( it->data );
    169       it->data = NULL;
    170     }
    171   }
    172  
    173   packetBuffer[userId].clear();
    174 }
    175 
    176 /**
    177  * get next packet for user
    178  * @param userId user id
    179  * @return recieved packet or packet with length 0 if no packet available
    180  */
    181 NetworkPacket UdpServerSocket::getPacket( int userId )
    182 {
    183   NetworkPacket res;
    184   res.data = NULL;
    185   res.length = 0;
    186  
    187   if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
    188   {
    189     res.data = packetBuffer[userId].front().data;
    190     res.length = packetBuffer[userId].front().length;
    191     packetBuffer[userId].pop_front();
    192   }
    193  
    194   return res;
    195 }
    196 
    197 /**
    198  * get number of packets recieved for user
    199  * @param userId user id
    200  * @return number of packets in buffer
    201  */
    202 int UdpServerSocket::getPacketCount( int userId )
    203 {
    204   if ( userId >= packetBuffer.size() )
    205     return -1;
    206  
    207   return packetBuffer[userId].size();
    208 }
    209 
    210 /**
    211  * will set user state
    212  * @param userId users id
    213  * @param ip users host / port
    214  */
    215 void UdpServerSocket::initUser( int userId, IPaddress ip )
    216 {
    217   int channel = SDLNet_UDP_Bind( socket, userId, &ip );
    218   if( channel != userId )
    219   {
    220     PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
    221     assert(false);
    222     return;
    223   }
    224  
    225   if ( userId < packetBuffer.size() )
    226     removeUserPackets( userId );
    227  
    228   if ( packetBuffer.size() <= userId )
    229     packetBuffer.resize( userId + 1 );
    230  
    231   if ( userList.size() <= userId )
    232     userList.resize( userId + 1 );
    233  
    234   userList[ userId ] = ip;
    235 }
    236 
    237 /**
    238  * remove user from list
    239  * @param userId user id
    240  */
    241 void UdpServerSocket::removeUser( int userId )
    242 {
    243   removeUserPackets( userId );
    244  
    245   if ( userId >= userList.size() )
    246     return;
    247  
    248   userList[userId].host = 0;
    249   userList[userId].port = 0;
    250  
    251   SDLNet_UDP_Unbind( socket, userId );
    252 }
    253 
    254 /**
    255  * send one packet to client associated to userId
    256  * @param networkPacket packet to send
    257  * @param userId users id
    258  * @return true on success
    259  */
    260 bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId )
    261 {
    262   assert( networkPacket.length <= UDP_PACKET_SIZE );
    263  
    264   memcpy( packet->data, networkPacket.data, networkPacket.length );
    265   packet->len = networkPacket.length;
    266   packet->channel = -1;
    267  
    268   if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 )
    269   {
    270     PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
    271     return false;
    272   }
    273  
    274   return true;
    275 }
     287}
  • branches/network/src/lib/network/udp_server_socket.h

    r7540 r7565  
    5050    virtual void close();
    5151   
     52    virtual void update();
     53   
    5254    void removeUserPackets( int userId );
    5355    void removeUser( int userId );
  • branches/network/src/lib/network/udp_socket.cc

    r7556 r7565  
    121121    return;
    122122  }
     123 
     124  writePacket( NULL, 0 );
    123125}
    124126
     
    130132  SDLNet_UDP_Unbind( socket, -1 );
    131133  SDLNet_UDP_Close( socket );
     134  bOk = false;
    132135  socket = NULL;
    133136}
     
    180183int UdpSocket::readPacket( byte * data, int maxLength )
    181184{
     185  assert( maxLength <= UDP_PACKET_SIZE );
     186 
    182187  if ( serverSocket )
    183188  {
  • branches/network/src/subprojects/network/network_unit_test.cc

    r7556 r7565  
    4242  NetworkSocket* client1 = new UdpSocket("localhost", 9999);
    4343
    44   client1->writePacket( (byte*)"test", 5 );
    4544  NetworkSocket* server1 = NULL;
    4645  while ( server1 == NULL )
     46  {
     47    server.update();
    4748    server1 = server.getNewSocket();
     49  }
    4850 
    4951  assert( server1->isOk() );
     
    5153  NetworkSocket* client2 = new UdpSocket("localhost", 9999);
    5254
    53   client2->writePacket( (byte*)"test", 5 );
    5455  NetworkSocket* server2 = NULL;
    5556  while ( server2 == NULL )
     57  {
     58    server.update();
    5659    server2 = server.getNewSocket();
     60  }
    5761
    5862  char buf[1024];
     
    7781  n = server2->writePacket((byte*)str4, strlen(str4)+1);
    7882  printf("%d bytes send from server2\n", n);
    79   SDL_Delay(1000);
     83  SDL_Delay(10);
     84 
     85  server.update();
     86
     87  printf("read from server1\n");
     88  n = server1->readPacket((byte*)buf, 1024);
     89  printf("read %d bytes\n", n);
     90  if (n<0)
     91    return -1;
     92
     93  printf("data: '%s'\n", buf);
     94
     95  printf("read from server2\n");
     96  n = server2->readPacket((byte*)buf, 1024);
     97  printf("read %d bytes\n", n);
     98  if (n<0)
     99    return -1;
     100
     101  printf("data: '%s'\n", buf);
     102
     103  printf("read from client1\n");
     104  n = client1->readPacket((byte*)buf, 1024);
     105  printf("read %d bytes\n", n);
     106  if (n<0)
     107    return -1;
     108
     109  printf("data: '%s'\n", buf);
     110
     111  printf("read from client2\n");
     112  n = client2->readPacket((byte*)buf, 1024);
     113  printf("read %d bytes\n", n);
     114  if (n<0)
     115    return -1;
     116
     117  printf("data: '%s'\n", buf);
     118 
     119 
     120  //22222222222222222222222222222222222222222
     121  n = client1->writePacket((byte*)str1, strlen(str1)+1);
     122  printf("%d bytes send from client1\n", n);
     123  n = server1->writePacket((byte*)str2, strlen(str2)+1);
     124  printf("%d bytes send from server1\n", n);
     125  n = client2->writePacket((byte*)str3, strlen(str3)+1);
     126  printf("%d bytes send from client2\n", n);
     127  n = server2->writePacket((byte*)str4, strlen(str4)+1);
     128  printf("%d bytes send from server2\n", n);
     129  SDL_Delay(10);
     130 
     131  server.update();
    80132
    81133  printf("read from server1\n");
Note: See TracChangeset for help on using the changeset viewer.