| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 |    main-programmer: Christoph Renner | 
|---|
| 13 |    co-programmer: | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #include "udp_server_socket.h" | 
|---|
| 17 | #include "debug.h" | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | /** | 
|---|
| 22 |  * constructor | 
|---|
| 23 |  * @param port port to listen on | 
|---|
| 24 |  */ | 
|---|
| 25 | UdpServerSocket::UdpServerSocket( int port ) : ServerSocket( port ) | 
|---|
| 26 | { | 
|---|
| 27 |   packet = SDLNet_AllocPacket( UDP_PACKET_SIZE ); | 
|---|
| 28 |  | 
|---|
| 29 |   if ( !packet ) | 
|---|
| 30 |   { | 
|---|
| 31 |     PRINTF(1)("SDLNet_AllocPacket: %s\n", SDLNet_GetError()); | 
|---|
| 32 |  | 
|---|
| 33 |     assert( false ); | 
|---|
| 34 |     bOk = false; | 
|---|
| 35 |   } | 
|---|
| 36 |  | 
|---|
| 37 |   memset( packet->data, 0, UDP_PACKET_SIZE ); | 
|---|
| 38 |  | 
|---|
| 39 |   listen( port ); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | /** | 
|---|
| 43 |  * default destructor | 
|---|
| 44 |  */ | 
|---|
| 45 | UdpServerSocket::~UdpServerSocket( ) | 
|---|
| 46 | { | 
|---|
| 47 |   for ( int i = 0; i < (int)packetBuffer.size(); i++ ) | 
|---|
| 48 |     removeUserPackets( i ); | 
|---|
| 49 |  | 
|---|
| 50 |   if ( packet ) | 
|---|
| 51 |     SDLNet_FreePacket( packet ); | 
|---|
| 52 |  | 
|---|
| 53 |   if ( socket ) | 
|---|
| 54 |     SDLNet_UDP_Close( socket ); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | /** | 
|---|
| 58 |  * tell udpServerSocket to recieve packets on port | 
|---|
| 59 |  * @param port port | 
|---|
| 60 |  * @return true on success | 
|---|
| 61 |  */ | 
|---|
| 62 | bool UdpServerSocket::listen( unsigned int port ) | 
|---|
| 63 | { | 
|---|
| 64 |   socket = SDLNet_UDP_Open( port ); | 
|---|
| 65 |  | 
|---|
| 66 |   PRINTF(0)("listening on port: %d\n", port); | 
|---|
| 67 |  | 
|---|
| 68 |   if ( !socket ) | 
|---|
| 69 |   { | 
|---|
| 70 |  | 
|---|
| 71 |     bOk = false; | 
|---|
| 72 |     return false; | 
|---|
| 73 |   } | 
|---|
| 74 |  | 
|---|
| 75 |   return true; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 |  * get newly connected socket. note | 
|---|
| 80 |  * @return new socket or NULL if no new socket exists | 
|---|
| 81 |  */ | 
|---|
| 82 | NetworkSocket * UdpServerSocket::getNewSocket( void ) | 
|---|
| 83 | { | 
|---|
| 84 |   NetworkSocket * result = NULL; | 
|---|
| 85 |  | 
|---|
| 86 |   if ( newSocketList.size() > 0 ) | 
|---|
| 87 |   { | 
|---|
| 88 |     result = newSocketList.front(); | 
|---|
| 89 |  | 
|---|
| 90 |     newSocketList.pop_front(); | 
|---|
| 91 |   } | 
|---|
| 92 |  | 
|---|
| 93 |   return result; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | /** | 
|---|
| 98 |  * stop listening on server | 
|---|
| 99 |  */ | 
|---|
| 100 | void UdpServerSocket::close( ) | 
|---|
| 101 | { | 
|---|
| 102 |  | 
|---|
| 103 |   for ( int i = 0; i < (int)packetBuffer.size(); i++ ) | 
|---|
| 104 |     removeUserPackets( i ); | 
|---|
| 105 |  | 
|---|
| 106 |   packetBuffer.clear(); | 
|---|
| 107 |   userList.clear(); | 
|---|
| 108 |  | 
|---|
| 109 |   SDLNet_UDP_Close( socket ); | 
|---|
| 110 |   socket = NULL; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | /** | 
|---|
| 114 |  * clean up users buffer | 
|---|
| 115 |  * @param userId users userid | 
|---|
| 116 |  */ | 
|---|
| 117 | void UdpServerSocket::removeUserPackets( int userId ) | 
|---|
| 118 | { | 
|---|
| 119 |   if ( userId >= (int)packetBuffer.size() ) | 
|---|
| 120 |     return; | 
|---|
| 121 |  | 
|---|
| 122 |   for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ ) | 
|---|
| 123 |   { | 
|---|
| 124 |     if ( it->data ) | 
|---|
| 125 |     { | 
|---|
| 126 |       free( it->data ); | 
|---|
| 127 |       it->data = NULL; | 
|---|
| 128 |     } | 
|---|
| 129 |   } | 
|---|
| 130 |  | 
|---|
| 131 |   packetBuffer[userId].clear(); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 |  * get next packet for user | 
|---|
| 136 |  * @param userId user id | 
|---|
| 137 |  * @return recieved packet or packet with length 0 if no packet available | 
|---|
| 138 |  */ | 
|---|
| 139 | NetworkPacket UdpServerSocket::getPacket( int userId ) | 
|---|
| 140 | { | 
|---|
| 141 |   NetworkPacket res; | 
|---|
| 142 |   res.data = NULL; | 
|---|
| 143 |   res.length = 0; | 
|---|
| 144 |  | 
|---|
| 145 |   if ( (int)packetBuffer.size() > userId && (int)packetBuffer[userId].size() > 0 ) | 
|---|
| 146 |   { | 
|---|
| 147 |     res.data = packetBuffer[userId].front().data; | 
|---|
| 148 |     res.length = packetBuffer[userId].front().length; | 
|---|
| 149 |     packetBuffer[userId].pop_front(); | 
|---|
| 150 |   } | 
|---|
| 151 |  | 
|---|
| 152 |   return res; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | /** | 
|---|
| 156 |  * get number of packets recieved for user | 
|---|
| 157 |  * @param userId user id | 
|---|
| 158 |  * @return number of packets in buffer | 
|---|
| 159 |  */ | 
|---|
| 160 | int UdpServerSocket::getPacketCount( int userId ) | 
|---|
| 161 | { | 
|---|
| 162 |   if ( userId >= (int)packetBuffer.size() ) | 
|---|
| 163 |     return -1; | 
|---|
| 164 |  | 
|---|
| 165 |   return packetBuffer[userId].size(); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | /** | 
|---|
| 169 |  * will set user state | 
|---|
| 170 |  * @param userId users id | 
|---|
| 171 |  * @param ip users host / port | 
|---|
| 172 |  */ | 
|---|
| 173 | void UdpServerSocket::initUser( int userId, IPaddress ip, byte randomByte ) | 
|---|
| 174 | { | 
|---|
| 175 |   int channel = SDLNet_UDP_Bind( socket, userId, &ip ); | 
|---|
| 176 |   if( channel != userId ) | 
|---|
| 177 |   { | 
|---|
| 178 |     PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError()); | 
|---|
| 179 |     assert(false); | 
|---|
| 180 |     return; | 
|---|
| 181 |   } | 
|---|
| 182 |  | 
|---|
| 183 |   if ( userId < (int)packetBuffer.size() ) | 
|---|
| 184 |     removeUserPackets( userId ); | 
|---|
| 185 |  | 
|---|
| 186 |   if ( (int)packetBuffer.size() <= userId ) | 
|---|
| 187 |     packetBuffer.resize( userId + 1 ); | 
|---|
| 188 |  | 
|---|
| 189 |   if ( (int)userList.size() <= userId ) | 
|---|
| 190 |     userList.resize( userId + 1 ); | 
|---|
| 191 |  | 
|---|
| 192 |   userList[ userId ].addr = ip; | 
|---|
| 193 |   userList[ userId ].randomByte = randomByte; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | /** | 
|---|
| 197 |  * remove user from list | 
|---|
| 198 |  * @param userId user id | 
|---|
| 199 |  */ | 
|---|
| 200 | void UdpServerSocket::removeUser( int userId ) | 
|---|
| 201 | { | 
|---|
| 202 |   removeUserPackets( userId ); | 
|---|
| 203 |  | 
|---|
| 204 |   if ( userId >= (int)userList.size() ) | 
|---|
| 205 |     return; | 
|---|
| 206 |  | 
|---|
| 207 |   userList[userId].addr.host = 0; | 
|---|
| 208 |   userList[userId].addr.port = 0; | 
|---|
| 209 |  | 
|---|
| 210 |   SDLNet_UDP_Unbind( socket, userId ); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | /** | 
|---|
| 214 |  * send one packet to client associated to userId | 
|---|
| 215 |  * @param networkPacket packet to send | 
|---|
| 216 |  * @param userId users id | 
|---|
| 217 |  * @return true on success | 
|---|
| 218 |  */ | 
|---|
| 219 | bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId ) | 
|---|
| 220 | { | 
|---|
| 221 |   if ( !socket ) | 
|---|
| 222 |     return false; | 
|---|
| 223 |  | 
|---|
| 224 |   assert( networkPacket.length <= UDP_PACKET_SIZE ); | 
|---|
| 225 |  | 
|---|
| 226 |   memcpy( packet->data, networkPacket.data, networkPacket.length ); | 
|---|
| 227 |   packet->len = networkPacket.length; | 
|---|
| 228 |   packet->channel = -1; | 
|---|
| 229 |  | 
|---|
| 230 |   if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 ) | 
|---|
| 231 |   { | 
|---|
| 232 |     PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError()); | 
|---|
| 233 |     return false; | 
|---|
| 234 |   } | 
|---|
| 235 |  | 
|---|
| 236 |   return true; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | /** | 
|---|
| 240 |  * do periodically things | 
|---|
| 241 |  */ | 
|---|
| 242 | void UdpServerSocket::update( ) | 
|---|
| 243 | { | 
|---|
| 244 |   int res; | 
|---|
| 245 |   int newConn = 0; | 
|---|
| 246 |  | 
|---|
| 247 |   // iterate through all newly received packets and assign them to the users packet buffer | 
|---|
| 248 |   for ( res = SDLNet_UDP_Recv( socket, packet ); res == 1; res = SDLNet_UDP_Recv( socket, packet ) ) | 
|---|
| 249 |   { | 
|---|
| 250 |     int userId; | 
|---|
| 251 |     bool isNewConnection = false; | 
|---|
| 252 |  | 
|---|
| 253 |     if ( packet->len <= 0 ) | 
|---|
| 254 |       continue; | 
|---|
| 255 |  | 
|---|
| 256 |     // search the user id this backet belongs to | 
|---|
| 257 |     for ( userId = 0; userId < (int)userList.size(); userId++ ) | 
|---|
| 258 |     { | 
|---|
| 259 |       if ( userList[userId].addr.host == packet->address.host && | 
|---|
| 260 |            userList[userId].addr.port == packet->address.port && | 
|---|
| 261 |            userList[userId].randomByte == ( packet->data[0] & 0xFC ) ) | 
|---|
| 262 |         break; | 
|---|
| 263 |     } | 
|---|
| 264 |  | 
|---|
| 265 |     // is it a new packet initializing a new connecion? | 
|---|
| 266 |     if ( userId >= (int)userList.size() ) | 
|---|
| 267 |     { | 
|---|
| 268 |       newConn++; | 
|---|
| 269 |       isNewConnection = true; | 
|---|
| 270 |  | 
|---|
| 271 |       if ( newConn > MAX_NEW_CONNECTIONS ) | 
|---|
| 272 |       { | 
|---|
| 273 |         PRINTF(2)("Too many new connections. Dropping packet\n"); | 
|---|
| 274 |         continue; | 
|---|
| 275 |       } | 
|---|
| 276 |  | 
|---|
| 277 |       for ( userId =0; userId < (int)userList.size(); userId++ ) | 
|---|
| 278 |         if ( userList[userId].addr.host == 0 && userList[userId].addr.port == 0 ) | 
|---|
| 279 |           break; | 
|---|
| 280 |  | 
|---|
| 281 |       this->initUser( userId, packet->address, packet->data[0] & 0xFC ); | 
|---|
| 282 |       UdpSocket * sock = new UdpSocket( this, packet->address, userId, packet->data[0] & 0xFC ); | 
|---|
| 283 |       newSocketList.push_back( sock ); | 
|---|
| 284 |       PRINTF(0)("NEW CONNECTION %x\n", packet->address.host ); | 
|---|
| 285 |     } | 
|---|
| 286 |  | 
|---|
| 287 |  | 
|---|
| 288 |     // add new packet to packetbuffer | 
|---|
| 289 |     NetworkPacket networkPacket; | 
|---|
| 290 |     networkPacket.length = packet->len; | 
|---|
| 291 |     if ( packet->len != 0 ) | 
|---|
| 292 |     { | 
|---|
| 293 |       networkPacket.data = (byte*)malloc( packet->len ); | 
|---|
| 294 |       assert( networkPacket.data ); | 
|---|
| 295 |     } | 
|---|
| 296 |     else | 
|---|
| 297 |     { | 
|---|
| 298 |       networkPacket.data = NULL; | 
|---|
| 299 |     } | 
|---|
| 300 |     memcpy( networkPacket.data, packet->data, packet->len ); | 
|---|
| 301 |     packetBuffer[userId].push_back( networkPacket ); | 
|---|
| 302 |   } | 
|---|
| 303 |  | 
|---|
| 304 |   assert( res == 0 ); | 
|---|
| 305 | } | 
|---|