Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6090 in orxonox.OLD


Ignore:
Timestamp:
Dec 13, 2005, 4:29:32 PM (18 years ago)
Author:
rennerc
Message:

handshake: several clients can connect at the same time now

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

Legend:

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

    r6062 r6090  
    3333  this->setOwner(0);
    3434
    35   PRINTF(0)("Handshake created clientId = %d\n", clientId);
     35  PRINTF(5)("Handshake created clientId = %d\n", clientId);
    3636}
    3737
     
    8888  if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
    8989  {
    90     if ( length != 4 )
     90    if ( length != 1 )
    9191    {
    92       PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 4);
     92      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1);
    9393      setState( HS_COMPLETED );
    9494      return;
     
    9898    setState( HS_RECVD_HID );
    9999    this->isOk = true;
    100     memcpy(&(this->newHostId), data, 4);
    101     PRINTF(0)("got my hostID: %d\n", newHostId);
     100    this->newHostId = data[0];
     101
     102    if ( newHostId == 0 )
     103    {
     104      setState( HS_WAS_REJECT );
     105      isOk = false;
     106      PRINTF(0)("Server did not accept handshake!\n");
     107    }
     108    else
     109    {
     110      PRINTF(0)("got my hostID: %d\n", newHostId);
     111    }
    102112    return;
    103113  }
     
    155165    setState( HS_SENT_HID );
    156166    setState( HS_COMPLETED );
    157     isOk = true;
    158     memcpy(data, (byte*)&clientId, 4);
    159     if ( this->isServer() )
    160       *reciever = clientId;
    161     return 4;
     167
     168    if ( hasState( HS_DO_REJECT ) )
     169    {
     170      isOk = false;
     171      //memcpy(data, (byte*)0, 4);
     172      data[0] = 0;
     173    }
     174    else
     175    {
     176      isOk = true;
     177      //memcpy(data, &clientId, 4);
     178      data[0] = (byte)clientId;
     179    }
     180    *reciever = clientId;
     181    return 1;
    162182  }
    163183
  • branches/network/src/lib/network/handshake.h

    r6053 r6090  
    2323  HS_SENT_HID   = 0x00000010,
    2424  HS_RECVD_HID  = 0x00000020,
    25   HS_COMPLETED  = 0X00000040,
     25  HS_COMPLETED  = 0x00000040,
     26
     27  HS_DO_REJECT  = 0x00010000,
     28  HS_WAS_REJECT = 0x00020000,
    2629
    2730  NUM_STATES
     
    3538    inline bool       ok(){ return isOk; }
    3639    inline int        getHostId(){ return newHostId; }
     40
     41    inline void       doReject(){ setState(HS_DO_REJECT); }
    3742
    3843    virtual void      writeBytes(const byte* data, int length);
  • branches/network/src/lib/network/network_protocol.cc

    r6061 r6090  
    6262int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source)
    6363{
    64   PRINTF(0)("create header length = %i, bufferLength = %i\n", length, bufferLength);
     64  PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength);
    6565  //If there isn't enough space for the header return -1
    6666  if (length + this->headerLength > bufferLength)
     
    9090Header NetworkProtocol::extractHeader(byte* data, int length)
    9191{
    92   PRINTF(0)("extract Header\n");
     92  PRINTF(5)("extract Header\n");
    9393  //Test if received data can contain a header
    9494  if (length < headerLength)
    9595  {
    96     PRINTF(0)("Received data is to short; it can't contain a header!\n");
     96    PRINTF(1)("Received data is to short; it can't contain a header!\n");
    9797    Header h;
    9898    h.length = 0;
  • branches/network/src/lib/network/network_stream.cc

    r6062 r6090  
    6060
    6161  Handshake* hs = new Handshake(false);
     62  hs->setUniqueID( 0 );
    6263  this->handshakes.push_back(hs);
    6364  this->connectSynchronizeable(*hs);
     
    7677  this->handshakes.push_back( NULL );
    7778  this->bActive = true;
     79
     80  this->setMaxConnections( 10 );
    7881}
    7982
     
    141144  if ( this->type == NET_SERVER )
    142145    this->updateConnectionList();
     146  else
     147  {
     148    if ( networkSockets[0] && !networkSockets[0]->isOk() )
     149    {
     150      PRINTF(1)("lost connection to server\n");
     151
     152      //delete networkSockets[i];
     153      networkSockets[0]->disconnectServer();
     154      networkSockets[0]->destroy();
     155      networkSockets[0] = NULL;
     156      //TODO: delete handshake from synchronizeable list so i can delete it
     157      if ( handshakes[0] )
     158        delete handshakes[0];
     159      handshakes[0] = NULL;
     160    }
     161  }
    143162
    144163  for (int i = 0; i<handshakes.size(); i++)
     
    164183          PRINT(1)("handshake failed!\n");
    165184          networkSockets[i]->disconnectServer();
     185          delete handshakes[i];
     186          handshakes[i] = NULL;
    166187          //TODO: handle error
    167188        }
     
    192213
    193214        dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
     215
     216        //FIXME: this is a hack, find a better way
     217        Header* header = (Header*)downBuffer;
     218        if ( header->synchronizeableID<100 )
     219          header->synchronizeableID = 0;
    194220
    195221        if ( dataLength<=0 )
     
    240266          continue;
    241267
    242         PRINTF(0)("read %d bytes from socket\n", dataLength);
     268        PRINTF(5)("read %d bytes from socket\n", dataLength);
    243269        header = networkProtocol->extractHeader(upBuffer, dataLength);
    244270        dataLength -= sizeof(header);
     
    250276        }
    251277
     278        if ( header.synchronizeableID == 0 )
     279        {
     280          header.synchronizeableID = i;
     281        }
     282
    252283        for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
    253284        {
     
    259290    }
    260291  }
    261 
    262 #if 0
    263   int dataLength = 0;
    264 
    265   /* DOWNSTREAM */
    266   printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
    267   PRINT(0)("============= DOWNSTREAM:===============\n");
    268   /* first of all read the synchronizeable's data: */
    269   if( this->isServer())
    270     dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
    271 
    272   if( dataLength > 0)
    273   {
    274     /* send the received data to connectionMonitor */
    275     this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
    276 
    277     dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
    278                  *(this->synchronizeables), 12/* some random number (no real id)*/);
    279 
    280     /* pass the data to the network socket */
    281  //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
    282     /* check if there was an error */
    283     if( dataLength == -1)
    284     {
    285       PRINTF(0)("Error in writing data to the NetworkSocket\n");
    286     }
    287   }
    288 
    289 
    290   /* UPSTREAM */
    291   dataLength = 0;
    292   PRINT(0)("============== UPSTREAM:================\n");
    293   /* first of all read the next Orxonox Network Header */
    294 
    295   if( this->state == NET_REC_HEADER)
    296   {
    297 //    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
    298     if( dataLength == sizeof(Header))
    299     {
    300       this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
    301       printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
    302              this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
    303              this->packetHeader.receiverID, this->packetHeader.length);
    304       /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
    305 
    306       this->state = NET_REC_DATA;
    307     }
    308   }
    309   if( this->state == NET_REC_DATA)
    310   {
    311     /* now read the data */
    312 //    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
    313     /* check if the data is available and process it if so */
    314     if( dataLength == this->packetHeader.length)
    315     {
    316       printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);
    317       /* send the received data to connectionMonitor */
    318       this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
    319       /* now pass the data to the sync object */
    320       if( !this->isServer())
    321         this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
    322 
    323       this->state = NET_REC_HEADER;
    324     }
    325   }
    326 
    327 #endif
    328292}
    329293
     
    343307      networkSockets[clientId] = tempNetworkSocket;
    344308      handshakes[clientId] = new Handshake(true, clientId);
     309      handshakes[clientId]->setUniqueID(clientId);
    345310    } else
    346311    {
    347312      clientId = networkSockets.size();
    348313      networkSockets.push_back(tempNetworkSocket);
    349       handshakes.push_back(new Handshake(true, clientId));
    350     }
     314      Handshake* tHs = new Handshake(true, clientId);
     315      tHs->setUniqueID(clientId);
     316      handshakes.push_back(tHs);
     317    }
     318
     319    if ( clientId > this->maxConnections )
     320    {
     321      handshakes[clientId]->doReject();
     322      PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
     323    }
     324    else
    351325
    352326    PRINTF(0)("New Client: %d\n", clientId);
     
    388362}
    389363
    390 
     364void NetworkStream::setMaxConnections( int n )
     365{
     366  if ( !this->isServer() )
     367  {
     368    PRINTF(1)("Cannot set maxConnections because I am no server.\n");
     369  }
     370  if ( this->networkSockets.size() > 1 )
     371  {
     372    PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size());
     373    return;
     374  }
     375  this->maxConnections = n;
     376}
     377
     378
  • branches/network/src/lib/network/network_stream.h

    r6060 r6090  
    4343    inline bool isActive() const { return this->bActive; }
    4444
     45    inline int getMaxConnections(){ return maxConnections; }
     46    void setMaxConnections( int n );
     47
    4548    virtual void processData();
    4649
     
    5861
    5962    int                    myHostId;
     63    int                    maxConnections;
    6064
    6165    void updateConnectionList();
  • branches/network/src/lib/network/synchronizeable.cc

    r6060 r6090  
    3131  owner = 0;
    3232  hostID = NetworkManager::getInstance()->getHostID();
     33  uniqueID = -1;
    3334  //state = ?;
    3435
  • branches/network/src/lib/network/synchronizeable.h

    r6060 r6090  
    3939    bool isServer();
    4040    bool isOutOfSync();
    41     void setUniqueId( int id ){ uniqueID = id; }
     41    void setUniqueID( int id ){ uniqueID = id; }
    4242    int  getUniqueID() const { return uniqueID; };
    4343    void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
     
    4545    inline int getOwner(){ return owner; }
    4646    inline void setOwner(int owner){ this->owner = owner; }
    47    
     47
    4848    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
    4949
     
    5959    int state;
    6060    std::list<int> synchronizeRequests;
    61    
     61
    6262    NetworkStream* networkStream;
    6363
Note: See TracChangeset for help on using the changeset viewer.