Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.