Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8802 in orxonox.OLD for trunk/src/lib/network


Ignore:
Timestamp:
Jun 26, 2006, 4:46:25 PM (18 years ago)
Author:
patrick
Message:

merged the network branche back to trunk

Location:
trunk/src/lib/network
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/netdefs.h

    r7954 r8802  
    1919#define MAX_CONNECTIONS 1000
    2020
    21 #define NETWORK_FREQUENCY 33
     21#define NETWORK_FREQUENCY 66
    2222
    2323
  • trunk/src/lib/network/udp_server_socket.cc

    r8623 r8802  
    147147    res.length = packetBuffer[userId].front().length;
    148148    packetBuffer[userId].pop_front();
    149 
    150     if ( res.length == 0 )
    151       res.length = -1;
    152149  }
    153150
     
    173170 * @param ip users host / port
    174171 */
    175 void UdpServerSocket::initUser( int userId, IPaddress ip )
     172void UdpServerSocket::initUser( int userId, IPaddress ip, byte randomByte )
    176173{
    177174  int channel = SDLNet_UDP_Bind( socket, userId, &ip );
     
    192189    userList.resize( userId + 1 );
    193190
    194   userList[ userId ] = ip;
     191  userList[ userId ].addr = ip;
     192  userList[ userId ].randomByte = randomByte;
    195193}
    196194
     
    206204    return;
    207205
    208   userList[userId].host = 0;
    209   userList[userId].port = 0;
     206  userList[userId].addr.host = 0;
     207  userList[userId].addr.port = 0;
    210208
    211209  SDLNet_UDP_Unbind( socket, userId );
     
    234232    return false;
    235233  }
    236 
     234 
    237235  return true;
    238236}
     
    251249    bool isNewConnection = false;
    252250
     251    if ( packet->len <= 0 )
     252      continue;
     253       
    253254    for ( userId =0; userId < (int)userList.size(); userId++ )
    254       if ( userList[userId].host == packet->address.host && userList[userId].port == packet->address.port )
     255      if ( userList[userId].addr.host == packet->address.host && userList[userId].addr.port == packet->address.port && userList[userId].randomByte == ( packet->data[0] & 0xFC ) )
    255256        break;
    256257
    257258    if ( userId >= (int)userList.size() )
    258259    {
    259 
    260260      newConn++;
    261261      isNewConnection = true;
     
    268268
    269269      for ( userId =0; userId < (int)userList.size(); userId++ )
    270         if ( userList[userId].host == 0 && userList[userId].port == 0 )
     270        if ( userList[userId].addr.host == 0 && userList[userId].addr.port == 0 )
    271271          break;
    272272
    273       initUser( userId, packet->address );
    274       UdpSocket * sock = new UdpSocket( this, packet->address, userId );
     273      initUser( userId, packet->address, packet->data[0] & 0xFC );
     274      UdpSocket * sock = new UdpSocket( this, packet->address, userId, packet->data[0] & 0xFC );
    275275      newSocketList.push_back( sock );
    276276      PRINTF(0)("NEW CONNECTION %x\n", packet->address.host );
     
    292292    memcpy( networkPacket.data, packet->data, packet->len );
    293293    packetBuffer[userId].push_back( networkPacket );
    294 
    295294  }
    296295
  • trunk/src/lib/network/udp_server_socket.h

    r7954 r8802  
    3333typedef std::vector<NetworkPacketList> PacketBuffer;
    3434
    35 typedef std::vector<IPaddress> UserList;
     35struct UserInfo
     36{
     37  IPaddress addr;
     38  byte randomByte;
     39};
     40
     41typedef std::vector<UserInfo> UserList;
    3642
    3743typedef std::list<UdpSocket*> UdpSocketList;
     
    5763    bool sendPacket( NetworkPacket networkPacket, int userId );
    5864    int getPacketCount( int childId );
    59     void initUser( int childId, IPaddress ip );
     65    void initUser( int childId, IPaddress ip, byte randomByte );
    6066   
    6167  private:
  • trunk/src/lib/network/udp_socket.cc

    r8362 r8802  
    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
  • trunk/src/lib/network/udp_socket.h

    r7954 r8802  
    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.