Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 10, 2006, 1:13:49 PM (18 years ago)
Author:
rennerc
Message:

reimplemented NetworkStream

File:
1 edited

Legend:

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

    r7556 r7565  
    7272
    7373/**
    74  * get newly connected socket. note: this function will also recieve
    75  * packets and create new sockets
     74 * get newly connected socket. note
    7675 * @return new socket or NULL if no new socket exists
    7776 */
    7877NetworkSocket * UdpServerSocket::getNewSocket( void )
     78{
     79  NetworkSocket * result = NULL;
     80 
     81  if ( newSocketList.size() > 0 )
     82  {
     83    result = newSocketList.front();
     84 
     85    newSocketList.pop_front();
     86  }
     87 
     88  return result;
     89}
     90
     91/**
     92 * stop listening on server
     93 */
     94void UdpServerSocket::close( )
     95{
     96 
     97  for ( int i = 0; i < packetBuffer.size(); i++ )
     98    removeUserPackets( i );
     99 
     100  packetBuffer.clear();
     101  userList.clear();
     102 
     103  SDLNet_UDP_Close( socket );
     104  socket = NULL;
     105}
     106
     107/**
     108 * clean up users buffer
     109 * @param userId users userid
     110 */
     111void UdpServerSocket::removeUserPackets( int userId )
     112{
     113  if ( userId >= packetBuffer.size() )
     114    return;
     115 
     116  for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ )
     117  {
     118    if ( it->data )
     119    {
     120      free( it->data );
     121      it->data = NULL;
     122    }
     123  }
     124 
     125  packetBuffer[userId].clear();
     126}
     127
     128/**
     129 * get next packet for user
     130 * @param userId user id
     131 * @return recieved packet or packet with length 0 if no packet available
     132 */
     133NetworkPacket UdpServerSocket::getPacket( int userId )
     134{
     135  NetworkPacket res;
     136  res.data = NULL;
     137  res.length = 0;
     138 
     139  if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
     140  {
     141    res.data = packetBuffer[userId].front().data;
     142    res.length = packetBuffer[userId].front().length;
     143    packetBuffer[userId].pop_front();
     144  }
     145 
     146  return res;
     147}
     148
     149/**
     150 * get number of packets recieved for user
     151 * @param userId user id
     152 * @return number of packets in buffer
     153 */
     154int UdpServerSocket::getPacketCount( int userId )
     155{
     156  if ( userId >= packetBuffer.size() )
     157    return -1;
     158 
     159  return packetBuffer[userId].size();
     160}
     161
     162/**
     163 * will set user state
     164 * @param userId users id
     165 * @param ip users host / port
     166 */
     167void UdpServerSocket::initUser( int userId, IPaddress ip )
     168{
     169  int channel = SDLNet_UDP_Bind( socket, userId, &ip );
     170  if( channel != userId )
     171  {
     172    PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
     173    assert(false);
     174    return;
     175  }
     176 
     177  if ( userId < packetBuffer.size() )
     178    removeUserPackets( userId );
     179 
     180  if ( packetBuffer.size() <= userId )
     181    packetBuffer.resize( userId + 1 );
     182 
     183  if ( userList.size() <= userId )
     184    userList.resize( userId + 1 );
     185 
     186  userList[ userId ] = ip;
     187}
     188
     189/**
     190 * remove user from list
     191 * @param userId user id
     192 */
     193void UdpServerSocket::removeUser( int userId )
     194{
     195  removeUserPackets( userId );
     196 
     197  if ( userId >= userList.size() )
     198    return;
     199 
     200  userList[userId].host = 0;
     201  userList[userId].port = 0;
     202 
     203  SDLNet_UDP_Unbind( socket, userId );
     204}
     205
     206/**
     207 * send one packet to client associated to userId
     208 * @param networkPacket packet to send
     209 * @param userId users id
     210 * @return true on success
     211 */
     212bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId )
     213{
     214  assert( networkPacket.length <= UDP_PACKET_SIZE );
     215 
     216  memcpy( packet->data, networkPacket.data, networkPacket.length );
     217  packet->len = networkPacket.length;
     218  packet->channel = -1;
     219 
     220  if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 )
     221  {
     222    PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
     223    return false;
     224  }
     225 
     226  return true;
     227}
     228
     229/**
     230 * do periodically things
     231 */
     232void UdpServerSocket::update( )
    79233{
    80234  int res;
     
    84238  {
    85239    int userId;
     240    bool isNewConnection = false;
    86241   
    87242    for ( userId =0; userId < userList.size(); userId++ )
     
    92247    {
    93248      newConn++;
     249      isNewConnection = true;
    94250     
    95251      if ( newConn > MAX_NEW_CONNECTIONS )
     
    118274    }
    119275    else
     276    {
     277      if ( isNewConnection )
     278        continue;
     279     
    120280      networkPacket.data = NULL;
     281    }
    121282    memcpy( networkPacket.data, packet->data, packet->len );
    122283    packetBuffer[userId].push_back( networkPacket );
     
    124285 
    125286  assert( res == 0 );
    126  
    127   NetworkSocket * result = NULL;
    128  
    129   if ( newSocketList.size() > 0 )
    130   {
    131     result = newSocketList.front();
    132  
    133     newSocketList.pop_front();
    134   }
    135  
    136   return result;
    137 }
    138 
    139 /**
    140  * stop listening on server
    141  */
    142 void UdpServerSocket::close( )
    143 {
    144  
    145   for ( int i = 0; i < packetBuffer.size(); i++ )
    146     removeUserPackets( i );
    147  
    148   packetBuffer.clear();
    149   userList.clear();
    150  
    151   SDLNet_UDP_Close( socket );
    152   socket = NULL;
    153 }
    154 
    155 /**
    156  * clean up users buffer
    157  * @param userId users userid
    158  */
    159 void UdpServerSocket::removeUserPackets( int userId )
    160 {
    161   if ( userId >= packetBuffer.size() )
    162     return;
    163  
    164   for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ )
    165   {
    166     if ( it->data )
    167     {
    168       free( it->data );
    169       it->data = NULL;
    170     }
    171   }
    172  
    173   packetBuffer[userId].clear();
    174 }
    175 
    176 /**
    177  * get next packet for user
    178  * @param userId user id
    179  * @return recieved packet or packet with length 0 if no packet available
    180  */
    181 NetworkPacket UdpServerSocket::getPacket( int userId )
    182 {
    183   NetworkPacket res;
    184   res.data = NULL;
    185   res.length = 0;
    186  
    187   if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
    188   {
    189     res.data = packetBuffer[userId].front().data;
    190     res.length = packetBuffer[userId].front().length;
    191     packetBuffer[userId].pop_front();
    192   }
    193  
    194   return res;
    195 }
    196 
    197 /**
    198  * get number of packets recieved for user
    199  * @param userId user id
    200  * @return number of packets in buffer
    201  */
    202 int UdpServerSocket::getPacketCount( int userId )
    203 {
    204   if ( userId >= packetBuffer.size() )
    205     return -1;
    206  
    207   return packetBuffer[userId].size();
    208 }
    209 
    210 /**
    211  * will set user state
    212  * @param userId users id
    213  * @param ip users host / port
    214  */
    215 void UdpServerSocket::initUser( int userId, IPaddress ip )
    216 {
    217   int channel = SDLNet_UDP_Bind( socket, userId, &ip );
    218   if( channel != userId )
    219   {
    220     PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
    221     assert(false);
    222     return;
    223   }
    224  
    225   if ( userId < packetBuffer.size() )
    226     removeUserPackets( userId );
    227  
    228   if ( packetBuffer.size() <= userId )
    229     packetBuffer.resize( userId + 1 );
    230  
    231   if ( userList.size() <= userId )
    232     userList.resize( userId + 1 );
    233  
    234   userList[ userId ] = ip;
    235 }
    236 
    237 /**
    238  * remove user from list
    239  * @param userId user id
    240  */
    241 void UdpServerSocket::removeUser( int userId )
    242 {
    243   removeUserPackets( userId );
    244  
    245   if ( userId >= userList.size() )
    246     return;
    247  
    248   userList[userId].host = 0;
    249   userList[userId].port = 0;
    250  
    251   SDLNet_UDP_Unbind( socket, userId );
    252 }
    253 
    254 /**
    255  * send one packet to client associated to userId
    256  * @param networkPacket packet to send
    257  * @param userId users id
    258  * @return true on success
    259  */
    260 bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId )
    261 {
    262   assert( networkPacket.length <= UDP_PACKET_SIZE );
    263  
    264   memcpy( packet->data, networkPacket.data, networkPacket.length );
    265   packet->len = networkPacket.length;
    266   packet->channel = -1;
    267  
    268   if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 )
    269   {
    270     PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
    271     return false;
    272   }
    273  
    274   return true;
    275 }
     287}
Note: See TracChangeset for help on using the changeset viewer.