Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8723 in orxonox.OLD for branches/network


Ignore:
Timestamp:
Jun 22, 2006, 2:58:47 PM (18 years ago)
Author:
rennerc
Message:

added status byte to all packets 2 status bits and 6 random bits

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

Legend:

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

    r8623 r8723  
    147147    res.length = packetBuffer[userId].front().length;
    148148    packetBuffer[userId].pop_front();
    149 
    150     if ( res.length == 0 )
    151       res.length = -1;
    152149  }
    153150
     
    234231    return false;
    235232  }
    236 
     233 
    237234  return true;
    238235}
     
    257254    if ( userId >= (int)userList.size() )
    258255    {
    259 
     256      if ( packet->len <= 0 )
     257        continue;
     258       
    260259      newConn++;
    261260      isNewConnection = true;
     
    272271
    273272      initUser( userId, packet->address );
    274       UdpSocket * sock = new UdpSocket( this, packet->address, userId );
     273      UdpSocket * sock = new UdpSocket( this, packet->address, userId, packet->data[0] & 0xFC );
    275274      newSocketList.push_back( sock );
    276275      PRINTF(0)("NEW CONNECTION %x\n", packet->address.host );
     
    292291    memcpy( networkPacket.data, packet->data, packet->len );
    293292    packetBuffer[userId].push_back( networkPacket );
    294 
    295293  }
    296294
  • branches/network/src/lib/network/udp_socket.cc

    r8362 r8723  
    2525  this->socket = NULL;
    2626  this->packet = NULL;
     27  this->randomByte = 0;
    2728}
    2829
     
    7071 * @param userId userid used by serverSocket
    7172 */
    72 UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId )
     73UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId, byte randomByte )
    7374{
    7475  this->init();
    7576  this->serverSocket = serverSocket;
    7677  this->userId = userId;
     78  this->randomByte = randomByte;
    7779}
    7880
     
    104106
    105107  IPaddress ip;
     108 
     109  this->randomByte = generateNewRandomByte();
    106110
    107111  PRINTF(0)("connect to server %s on port %d\n", host.c_str(), port);
     
    137141{
    138142  PRINTF(0)("disconnect\n");
    139   writePacket( NULL, 0 );
     143  byte cmd = this->randomByte | UDPCMD_DISCONNECT;
     144  writeRawPacket( &cmd, 1 );
    140145  SDLNet_UDP_Unbind( socket, -1 );
    141146  SDLNet_UDP_Close( socket );
     
    152157bool UdpSocket::writePacket( byte * data, int length )
    153158{
     159  byte * buf = new byte[length+1];
     160  if ( length > 0 )
     161    memcpy( buf+1, data, length );
     162  buf[0] = this->randomByte;
     163  return writeRawPacket( buf, length+1 );
     164}
     165
     166/**
     167 * recieve one packet from another host
     168 * @param data pointer to buffer to copy data into
     169 * @param maxLength maximal length of buffer
     170 * @return less than 0 on error, number bytes read else
     171 */
     172int UdpSocket::readPacket( byte * data, int maxLength )
     173{
     174  assert( maxLength <= UDP_PACKET_SIZE );
     175
     176  if ( serverSocket )
     177  {
     178    NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
     179
     180    byte udpCmd = 0;
     181   
     182    if ( networkPacket.length > 0 )
     183    {
     184      assert( maxLength > networkPacket.length );
     185
     186      memcpy( data, networkPacket.data+1, networkPacket.length-1 );
     187      udpCmd = networkPacket.data[0];
     188    }
     189    else
     190      return 0;
     191     
     192    if ( !checkRandomByte( networkPacket.data[0] ) )
     193      return 0;
     194
     195    if ( networkPacket.data )
     196    {
     197      free( networkPacket.data );
     198      networkPacket.data = NULL;
     199    }
     200   
     201    if ( !checkUdpCmd( udpCmd ) )
     202      return 0;
     203
     204    return networkPacket.length-1;
     205  }
     206  else
     207  {
     208    int numrecv = SDLNet_UDP_Recv( socket, packet);
     209   
     210    byte udpCmd = 0;
     211
     212    if ( numrecv > 0)
     213    {
     214      assert( packet->len <= maxLength );
     215
     216      if ( packet->len > 0 )
     217        memcpy( data, packet->data+1, packet->len-1 );
     218      else
     219        return 0;
     220           
     221      if ( !checkRandomByte( packet->data[0] ) )
     222        return 0;
     223             
     224      if ( !checkUdpCmd( udpCmd ) )
     225        return 0;
     226
     227      return packet->len-1;
     228    }
     229    else if ( numrecv < 0 )
     230    {
     231      PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError());
     232      bOk = false;
     233      return -1;
     234    }
     235    else
     236    {
     237      return 0;
     238    }
     239  }
     240
     241  return 0;
     242}
     243
     244bool UdpSocket::writeRawPacket( byte * data, int length )
     245{
    154246  if ( serverSocket )
    155247  {
     
    183275}
    184276
    185 /**
    186  * recieve one packet from another host
    187  * @param data pointer to buffer to copy data into
    188  * @param maxLength maximal length of buffer
    189  * @return less than 0 on error, number bytes read else
    190  */
    191 int UdpSocket::readPacket( byte * data, int maxLength )
    192 {
    193   assert( maxLength <= UDP_PACKET_SIZE );
    194 
    195   if ( serverSocket )
    196   {
    197     NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
    198 
    199     if ( networkPacket.length == -1 )
    200     {
    201       this->disconnectServer();
    202       return 0;
    203     }
    204 
    205     if ( networkPacket.length > 0 )
    206     {
    207       assert( maxLength > networkPacket.length );
    208 
    209       memcpy( data, networkPacket.data, networkPacket.length );
    210     }
    211 
    212     if ( networkPacket.data )
    213     {
    214       free( networkPacket.data );
    215       networkPacket.data = NULL;
    216     }
    217 
    218     return networkPacket.length;
     277bool UdpSocket::checkUdpCmd( byte udpCmd )
     278{
     279  if ( udpCmd & UDPCMD_DISCONNECT )
     280  {
     281    this->disconnectServer();
     282    PRINTF(0)("received disconnect byte\n");
     283    return false;
     284  }
     285 
     286  if ( !this->serverSocket && ( udpCmd & UDPCMD_INVALIDRNDBYTE ) )
     287  {
     288    PRINTF(0)("received invlid random number byte\n");
     289    byte cmd = this->randomByte | UDPCMD_DISCONNECT;
     290    writeRawPacket( &cmd, 1 );
     291    this->randomByte = generateNewRandomByte();
     292    return false;
     293  }
     294 
     295  return true;
     296}
     297
     298byte UdpSocket::generateNewRandomByte( )
     299{
     300  srand( SDL_GetTicks() );
     301  byte res = ( rand() & 0xFC );
     302 
     303  PRINTF(0)("generated random byte: %x\n", res);
     304 
     305  return res;
     306}
     307
     308bool UdpSocket::checkRandomByte( byte rndByte )
     309{
     310  if ( ( rndByte & 0xFC ) == this->randomByte )
     311  {
     312    return true;
    219313  }
    220314  else
    221315  {
    222     int numrecv = SDLNet_UDP_Recv( socket, packet);
    223     if ( numrecv > 0)
    224     {
    225       assert( packet->len <= maxLength );
    226 
    227       if ( packet->len == 0 )
    228       {
    229         this->disconnectServer();
    230         return 0;
    231       }
    232 
    233       memcpy( data, packet->data, packet->len );
    234       return packet->len;
    235     }
    236     else if ( numrecv < 0 )
    237     {
    238       PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError());
    239       bOk = false;
    240       return -1;
    241     }
    242     else
    243     {
    244       return 0;
    245     }
    246   }
    247 
    248   return 0;
    249 }
    250 
    251 
    252 
     316    PRINTF(2)("wrong random byte: %x\n", ( rndByte & 0xFC ));
     317    return false;
     318  }
     319}
     320
     321
     322
  • branches/network/src/lib/network/udp_socket.h

    r7954 r8723  
    1616class UdpServerSocket;
    1717
     18enum
     19{
     20  UDPCMD_DISCONNECT = 1,
     21  UDPCMD_INVALIDRNDBYTE = 2
     22};
     23
    1824class UdpSocket : public NetworkSocket
    1925{
    2026  public:
    2127    UdpSocket();
    22     UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId );
     28    UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId, byte randomByte );
    2329    UdpSocket( std::string host, int port );
    2430    virtual ~UdpSocket();
     
    2834    virtual void disconnectServer();
    2935
    30     virtual bool writePacket(byte * data, int length);
     36    virtual bool writePacket(byte * data, int length );
    3137
    3238    virtual int readPacket(byte * data, int maxLength);
     
    3945    UDPpacket *       packet;
    4046   
     47    byte              randomByte;     //!< contains random bytes & 0xFC
     48   
     49    bool writeRawPacket( byte * data, int length );
     50    bool checkUdpCmd( byte udpCmd );
     51    bool checkRandomByte( byte rndByte );
     52    byte generateNewRandomByte();
     53   
    4154    void init();
    4255
Note: See TracChangeset for help on using the changeset viewer.