Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6053 in orxonox.OLD for branches/network


Ignore:
Timestamp:
Dec 11, 2005, 6:01:04 PM (19 years ago)
Author:
rennerc
Message:

handshake works now

Location:
branches/network/src
Files:
11 edited

Legend:

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

    r6043 r6053  
    2727  this->setClassID(CL_HANDSHAKE, "Handshake");
    2828
    29   this->server = server;
     29  this->setIsServer(server);
    3030  this->clientId = clientId;
    31   this->state = HS_SEND_INIT;
     31  this->state = 0;
    3232  this->isOk = false;
     33  this->setOwner(0);
     34
     35  PRINTF(0)("Handshake created clientId = %d\n", clientId);
    3336}
    3437
    3538void Handshake::writeBytes( const byte * data, int length )
    3639{
    37   if ( state == HS_RECV_INIT )
     40  PRINTF(0)("Handshake::writeBytes states = %d %d %d %d (%d)\n", hasState( HS_RECVD_INIT ), hasState( HS_RECVD_VER ), hasState( HS_RECVD_HID ), hasState( HS_COMPLETED ), state);
     41
     42  if ( hasState( HS_COMPLETED ) )
     43       return;
     44
     45  if ( !hasState( HS_RECVD_INIT ) )
    3846  {
    3947    if ( length != _INITIAL_DATA_LENGTH )
    4048    {
    41       PRINTF(1)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
    42       state = HS_COMPLETED;
     49      PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
     50      setState( HS_COMPLETED );
    4351      return;
    4452    }
     
    4654    if ( strncmp((char*)data, _INITIAL_DATA, length) )
    4755    {
    48       PRINTF(1)("initial packed does not match\n");
    49       state = HS_COMPLETED;
     56      PRINTF(0)("initial packed does not match\n");
     57      setState( HS_COMPLETED );
    5058      return;
    5159    }
    5260
    53     state = HS_SEND_VERSION;
     61    setState( HS_RECVD_INIT );
    5462    PRINTF(0)("got valid initial packet from client %d\n", clientId);
    5563    return;
    5664  }
    5765
    58   if ( state == HS_RECV_VERSION )
     66  if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) )
    5967  {
    6068    if ( length != _ORXONOX_VERSION_LENGTH )
    6169    {
    62       PRINTF(1)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
    63       state = HS_COMPLETED;
     70      PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
     71      setState( HS_COMPLETED );
    6472      return;
    6573    }
     
    6775    if ( strncmp((char*)data, _ORXONOX_VERSION, length) )
    6876    {
    69       PRINTF(1)("versions do not match\n");
    70       state = HS_COMPLETED;
     77      PRINTF(0)("versions do not match\n");
     78      setState( HS_COMPLETED );
    7179      return;
    7280    }
    7381
    74     if ( this->isServer() )
    75       state = HS_SEND_ID;
    76     else
    77       state = HS_RECV_ID;
     82    setState( HS_RECVD_VER );
    7883
    7984    PRINTF(0)("client %d's version does match\n", clientId);
     
    8186  }
    8287
    83   if ( state == HS_RECV_ID )
     88  if ( hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
    8489  {
    85     if ( length != _ORXONOX_VERSION_LENGTH )
     90    if ( length != 4 )
    8691    {
    87       PRINTF(1)("hostID packet has wrong size %d instead of %d\n", length, 4);
    88       state = HS_COMPLETED;
     92      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 4);
     93      setState( HS_COMPLETED );
    8994      return;
    9095    }
    9196
    92     this->state = HS_COMPLETED;
     97    setState( HS_COMPLETED );
     98    setState( HS_RECVD_HID );
    9399    this->isOk = true;
    94100    memcpy(&(this->newHostId), data, 4);
     
    99105}
    100106
    101 int Handshake::readBytes( byte * data, int maxLength, int & reciever )
     107int Handshake::readBytes( byte * data, int maxLength, int * reciever )
    102108{
    103   if ( state == HS_SEND_INIT )
     109  PRINTF(0)("Handshake::readBytes states = %d %d %d %d (%d)\n", hasState( HS_SENT_INIT ), hasState( HS_SENT_VER ), hasState( HS_SENT_HID ), hasState( HS_COMPLETED ), state);
     110
     111  if ( hasState( HS_COMPLETED ) )
     112    return 0;
     113
     114  if ( !hasState( HS_SENT_INIT ) )
    104115  {
    105116    if ( maxLength < _INITIAL_DATA_LENGTH )
    106117    {
    107       PRINTF(1)("buffer too small for _INITIAL_DATA");
    108       state = HS_COMPLETED;
     118      PRINTF(0)("buffer too small for _INITIAL_DATA");
     119      setState( HS_COMPLETED );
    109120      return 0;
    110121    }
    111122
    112     state = HS_RECV_INIT;
     123    setState( HS_SENT_INIT );
    113124    memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);
    114125    if ( this->isServer() )
    115       reciever = clientId;
     126      *reciever = clientId;
    116127    return _INITIAL_DATA_LENGTH;
    117128  }
    118129
    119   if ( state == HS_SEND_VERSION )
     130  if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )
    120131  {
    121132    if ( maxLength < _ORXONOX_VERSION_LENGTH )
    122133    {
    123       PRINTF(1)("buffer too small for version number");
    124       state = HS_COMPLETED;
     134      PRINTF(0)("buffer too small for version number");
     135      setState( HS_COMPLETED );
    125136      return 0;
    126137    }
    127138
    128     state = HS_RECV_VERSION;
     139    setState( HS_SENT_VER );
    129140    memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);
    130141    if ( this->isServer() )
    131       reciever = clientId;
     142      *reciever = clientId;
    132143    return _ORXONOX_VERSION_LENGTH;
    133144  }
    134145
    135   if ( state == HS_SEND_ID )
     146  if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
    136147  {
    137148    if ( maxLength < 4 )
    138149    {
    139       PRINTF(1)("buffer too small for ID");
    140       state = HS_COMPLETED;
     150      PRINTF(0)("buffer too small for ID");
     151      setState( HS_COMPLETED );
    141152      return 0;
    142153    }
    143154
    144     state = HS_COMPLETED;
     155    setState( HS_SENT_HID );
     156    setState( HS_COMPLETED );
     157    isOk = true;
    145158    memcpy(data, (byte*)&clientId, 4);
    146159    if ( this->isServer() )
    147       reciever = clientId;
     160      *reciever = clientId;
    148161    return 4;
    149162  }
  • branches/network/src/lib/network/handshake.h

    r6043 r6053  
    1717
    1818typedef enum HandshakeState {
    19   HS_SEND_INIT = 0,
    20   HS_RECV_INIT,
    21   HS_SEND_VERSION,
    22   HS_RECV_VERSION,
    23   HS_SEND_ID,
    24   HS_RECV_ID,
    25   HS_COMPLETED,
     19  HS_SENT_INIT  = 0x00000001,
     20  HS_RECVD_INIT = 0x00000002,
     21  HS_SENT_VER   = 0x00000004,
     22  HS_RECVD_VER  = 0x00000008,
     23  HS_SENT_HID   = 0x00000010,
     24  HS_RECVD_HID  = 0x00000020,
     25  HS_COMPLETED  = 0X00000040,
    2626
    2727  NUM_STATES
     
    3232  public:
    3333    Handshake(bool server, int clientId = 0);
    34     inline bool       completed(){ return state == HS_COMPLETED; }
     34    inline bool       completed(){ return hasState( HS_COMPLETED ); }
    3535    inline bool       ok(){ return isOk; }
    3636    inline int        getHostId(){ return newHostId; }
    3737
    3838    virtual void      writeBytes(const byte* data, int length);
    39     virtual int       readBytes(byte* data, int maxLength, int& reciever);
     39    virtual int       readBytes(byte* data, int maxLength, int * reciever);
    4040    virtual void      writeDebug() const;
    4141    virtual void      readDebug() const;
     
    4343  private:
    4444    int               state;
    45     int               server;
    4645    int               clientId;
    4746    int               newHostId;
    4847    bool              isOk;
     48
     49    inline bool       hasState( int a ){ return (state & a) == a; }
     50    inline void       setState( int a ){ state = state | a; }
     51    inline void       unsetState( int a ){ state = state & (~a); }
    4952
    5053};
  • branches/network/src/lib/network/network_stream.cc

    r6043 r6053  
    2929#include "list.h"
    3030#include "debug.h"
     31#include "class_list.h"
    3132
    3233/* include your own header */
     
    7374  this->connectionMonitor = new ConnectionMonitor();
    7475  this->networkSockets.push_back( NULL );
     76  this->handshakes.push_back( NULL );
    7577  this->bActive = true;
    7678}
     
    128130void NetworkStream::processData()
    129131{
    130   //PRINTF(0)("processData()\n");
     132  PRINTF(0)("processData()\n");
    131133  if ( this->type == NET_SERVER )
    132134    this->updateConnectionList();
    133135
    134   for (HandshakeVector::iterator it = handshakes.begin(); it!=handshakes.end(); it++)
    135   {
    136     if ( *it )
    137     {
    138       if ( (*it)->completed() )
     136  for (int i = 0; i<handshakes.size(); i++)
     137  {
     138    if ( handshakes[i] )
     139    {
     140      if ( handshakes[i]->completed() )
    139141      {
    140         if ( (*it)->ok() )
    141         {
    142           NetworkManager::getInstance()->setHostID( (*it)->getHostId() );
    143           myHostId = NetworkManager::getInstance()->getHostID();
    144           delete *it;
    145           *it = NULL;
     142        if ( handshakes[i]->ok() )
     143        {
     144          if ( type != NET_SERVER )
     145          {
     146            NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() );
     147            myHostId = NetworkManager::getInstance()->getHostID();
     148          }
    146149          PRINT(0)("handshake finished\n");
    147150        }
     
    149152        {
    150153          PRINT(1)("handshake failed!\n");
    151           delete *it;
    152           *it = NULL;
     154          networkSockets[i]->disconnectServer();
    153155          //TODO: handle error
    154156        }
     
    165167  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
    166168  {
    167     if ( (*it)->getOwner() == myHostId )
    168     {
    169       PRINTF(0)("sync: %s\n", (*it)->getName());
    170       reciever = 0;
     169    if ( (*it) && (*it)->getOwner() == myHostId )
     170    {
    171171      do {
    172         dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, reciever);
     172        reciever = 0;
     173        dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever);
     174
     175        PRINTF(0)("reciever = %d\n", reciever);
    173176
    174177        if ( dataLength<=0 ){
     
    179182        PRINTF(0)("read %d bytes\n", dataLength);
    180183
    181         dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
     184        if (ClassList::exists((*it)))
     185        {
     186          PRINTF(0)("before cast\n");
     187          dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
     188          PRINTF(0)("after cast\n");
     189        }
     190        else
     191        {
     192          PRINTF(0)("instance does not exist anymore\n");
     193          //synchronizeables.remove(it);
     194        }
    182195
    183196        if ( dataLength<=0 )
     
    188201          if ( networkSockets[reciever] )
    189202          {
    190             PRINTF(0)("write %d bytes to socket\n", dataLength);
     203            PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever);
    191204            networkSockets[reciever]->writePacket(downBuffer, dataLength);
    192205          }
     
    198211        else
    199212        {
    200           for ( int i = 1; i<networkSockets.size(); i++)
     213          for ( int i = 0; i<networkSockets.size(); i++)
    201214          {
    202215            if ( networkSockets[i] )
     216            {
     217              PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever);
    203218              networkSockets[i]->writePacket(downBuffer, dataLength);
     219            }
    204220          }
    205221        }
     
    211227  /* UPSTREAM */
    212228
    213   for ( int i = 1; i<networkSockets.size(); i++)
     229  for ( int i = 0; i<networkSockets.size(); i++)
    214230  {
    215231    if ( networkSockets[i] )
  • branches/network/src/lib/network/synchronizeable.cc

    r6043 r6053  
    1414   co-programmer: Benjamin Wuest
    1515*/
     16
     17#define DEBUG_MODULE_NETWORK
    1618
    1719#include "synchronizeable.h"
     
    5153 */
    5254void Synchronizeable::writeBytes(const byte* data, int length)
    53 {}
     55{
     56  PRINTF(1)("Synchronizeable::writeBytes was called\n");
     57}
    5458
    5559/**
    5660 *  read data from NetworkStream
    5761 */
    58 int Synchronizeable::readBytes(byte* data, int maxLength, int& reciever)
    59 {}
     62int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever)
     63{
     64  PRINTF(1)("Synchronizeable::readBytes was called\n");
     65}
    6066
    6167
  • branches/network/src/lib/network/synchronizeable.h

    r6043 r6053  
    2929
    3030    virtual void      writeBytes(const byte* data, int length);
    31     virtual int       readBytes(byte* data, int maxLength, int& reciever);
     31    virtual int       readBytes(byte* data, int maxLength, int * reciever);
    3232    virtual void      writeDebug() const;
    3333    virtual void      readDebug() const;
  • branches/network/src/subprojects/network/read_sync.cc

    r6043 r6053  
    8080 *  read data from Synchronizeable
    8181 */
    82 int ReadSync::readBytes(byte* data, int maxLength, int& reciever)
     82int ReadSync::readBytes(byte* data, int maxLength, int * reciever)
    8383{
    8484  return 0;
  • branches/network/src/subprojects/network/read_sync.h

    r6043 r6053  
    1717
    1818    virtual void writeBytes(const byte* data, int length);
    19     virtual int readBytes(byte* data, int maxLength, int& reciever);
     19    virtual int readBytes(byte* data, int maxLength, int * reciever);
    2020
    2121
  • branches/network/src/subprojects/network/simple_sync.cc

    r6043 r6053  
    8686 *  read data from Synchronizeable
    8787 */
    88 int SimpleSync::readBytes(byte* data, int maxLength, int& reciever)
     88int SimpleSync::readBytes(byte* data, int maxLength, int * reciever)
    8989{
    9090  PRINTF(0)("SimpleSync: sent %i bytes of data\n", this->outLength);
  • branches/network/src/subprojects/network/simple_sync.h

    r6043 r6053  
    1717
    1818    virtual void writeBytes(const byte* data, int length);
    19     virtual int readBytes(byte* data, int maxLength, int& reciever);
     19    virtual int readBytes(byte* data, int maxLength, int * reciever);
    2020
    2121
  • branches/network/src/subprojects/network/write_sync.cc

    r6043 r6053  
    8686 *  read data from Synchronizeable
    8787 */
    88 int WriteSync::readBytes(byte* data, int maxLength, int& reciever)
     88int WriteSync::readBytes(byte* data, int maxLength, int * reciever)
    8989{
    9090  PRINTF(0)("WriteSync: sent %i bytes of data\n", this->outLength);
  • branches/network/src/subprojects/network/write_sync.h

    r6043 r6053  
    1717
    1818    virtual void writeBytes(const byte* data, int length);
    19     virtual int readBytes(byte* data, int maxLength, int& reciever);
     19    virtual int readBytes(byte* data, int maxLength, int * reciever);
    2020
    2121
Note: See TracChangeset for help on using the changeset viewer.