[7540] | 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 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * constructor |
---|
| 22 | * @param port port to listen on |
---|
| 23 | */ |
---|
| 24 | UdpServerSocket::UdpServerSocket( int port ) : ServerSocket( port ) |
---|
| 25 | { |
---|
| 26 | packet = SDLNet_AllocPacket( UDP_PACKET_SIZE ); |
---|
| 27 | |
---|
| 28 | if ( !packet ) |
---|
| 29 | { |
---|
| 30 | PRINTF(1)("SDLNet_AllocPacket: %s\n", SDLNet_GetError()); |
---|
| 31 | |
---|
| 32 | assert( false ); |
---|
| 33 | bOk = false; |
---|
| 34 | } |
---|
| 35 | |
---|
[7613] | 36 | memset( packet->data, 0, UDP_PACKET_SIZE ); |
---|
| 37 | |
---|
[7540] | 38 | listen( port ); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * default destructor |
---|
| 43 | */ |
---|
| 44 | UdpServerSocket::~UdpServerSocket( ) |
---|
| 45 | { |
---|
[8228] | 46 | for ( int i = 0; i < (int)packetBuffer.size(); i++ ) |
---|
[7540] | 47 | removeUserPackets( i ); |
---|
[7556] | 48 | |
---|
| 49 | if ( packet ) |
---|
| 50 | SDLNet_FreePacket( packet ); |
---|
| 51 | |
---|
| 52 | if ( socket ) |
---|
| 53 | SDLNet_UDP_Close( socket ); |
---|
[7540] | 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * tell udpServerSocket to recieve packets on port |
---|
| 58 | * @param port port |
---|
| 59 | * @return true on success |
---|
| 60 | */ |
---|
| 61 | bool UdpServerSocket::listen( unsigned int port ) |
---|
| 62 | { |
---|
| 63 | socket = SDLNet_UDP_Open( port ); |
---|
| 64 | |
---|
[7570] | 65 | PRINTF(0)("listening on port: %d\n", port); |
---|
| 66 | |
---|
[7540] | 67 | if ( !socket ) |
---|
| 68 | { |
---|
| 69 | |
---|
| 70 | bOk = false; |
---|
| 71 | return false; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | return true; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
[7565] | 78 | * get newly connected socket. note |
---|
[7540] | 79 | * @return new socket or NULL if no new socket exists |
---|
| 80 | */ |
---|
| 81 | NetworkSocket * UdpServerSocket::getNewSocket( void ) |
---|
| 82 | { |
---|
[7553] | 83 | NetworkSocket * result = NULL; |
---|
[7540] | 84 | |
---|
[7553] | 85 | if ( newSocketList.size() > 0 ) |
---|
| 86 | { |
---|
| 87 | result = newSocketList.front(); |
---|
[7540] | 88 | |
---|
[7553] | 89 | newSocketList.pop_front(); |
---|
| 90 | } |
---|
| 91 | |
---|
[7540] | 92 | return result; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * stop listening on server |
---|
| 97 | */ |
---|
| 98 | void UdpServerSocket::close( ) |
---|
| 99 | { |
---|
| 100 | |
---|
[8228] | 101 | for ( int i = 0; i < (int)packetBuffer.size(); i++ ) |
---|
[7540] | 102 | removeUserPackets( i ); |
---|
| 103 | |
---|
| 104 | packetBuffer.clear(); |
---|
| 105 | userList.clear(); |
---|
| 106 | |
---|
| 107 | SDLNet_UDP_Close( socket ); |
---|
| 108 | socket = NULL; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * clean up users buffer |
---|
| 113 | * @param userId users userid |
---|
| 114 | */ |
---|
| 115 | void UdpServerSocket::removeUserPackets( int userId ) |
---|
| 116 | { |
---|
[8228] | 117 | if ( userId >= (int)packetBuffer.size() ) |
---|
[7540] | 118 | return; |
---|
| 119 | |
---|
| 120 | for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ ) |
---|
| 121 | { |
---|
| 122 | if ( it->data ) |
---|
| 123 | { |
---|
[7556] | 124 | free( it->data ); |
---|
[7540] | 125 | it->data = NULL; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | packetBuffer[userId].clear(); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * get next packet for user |
---|
| 134 | * @param userId user id |
---|
| 135 | * @return recieved packet or packet with length 0 if no packet available |
---|
| 136 | */ |
---|
| 137 | NetworkPacket UdpServerSocket::getPacket( int userId ) |
---|
| 138 | { |
---|
| 139 | NetworkPacket res; |
---|
| 140 | res.data = NULL; |
---|
| 141 | res.length = 0; |
---|
| 142 | |
---|
[8228] | 143 | if ( (int)packetBuffer.size() > userId && (int)packetBuffer[userId].size() > 0 ) |
---|
[7540] | 144 | { |
---|
| 145 | res.data = packetBuffer[userId].front().data; |
---|
| 146 | res.length = packetBuffer[userId].front().length; |
---|
| 147 | packetBuffer[userId].pop_front(); |
---|
[8228] | 148 | |
---|
| 149 | if ( res.length == 0 ) |
---|
| 150 | res.length = -1; |
---|
[7540] | 151 | } |
---|
| 152 | |
---|
| 153 | return res; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * get number of packets recieved for user |
---|
| 158 | * @param userId user id |
---|
| 159 | * @return number of packets in buffer |
---|
| 160 | */ |
---|
| 161 | int UdpServerSocket::getPacketCount( int userId ) |
---|
| 162 | { |
---|
[8228] | 163 | if ( userId >= (int)packetBuffer.size() ) |
---|
[7540] | 164 | return -1; |
---|
| 165 | |
---|
| 166 | return packetBuffer[userId].size(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * will set user state |
---|
| 171 | * @param userId users id |
---|
| 172 | * @param ip users host / port |
---|
| 173 | */ |
---|
| 174 | void UdpServerSocket::initUser( int userId, IPaddress ip ) |
---|
| 175 | { |
---|
| 176 | int channel = SDLNet_UDP_Bind( socket, userId, &ip ); |
---|
[7553] | 177 | if( channel != userId ) |
---|
[7540] | 178 | { |
---|
| 179 | PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError()); |
---|
| 180 | assert(false); |
---|
| 181 | return; |
---|
| 182 | } |
---|
| 183 | |
---|
[8228] | 184 | if ( userId < (int)packetBuffer.size() ) |
---|
[7540] | 185 | removeUserPackets( userId ); |
---|
| 186 | |
---|
[8228] | 187 | if ( (int)packetBuffer.size() <= userId ) |
---|
[7540] | 188 | packetBuffer.resize( userId + 1 ); |
---|
| 189 | |
---|
[8228] | 190 | if ( (int)userList.size() <= userId ) |
---|
[7540] | 191 | userList.resize( userId + 1 ); |
---|
| 192 | |
---|
| 193 | userList[ userId ] = ip; |
---|
| 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 | |
---|
[8228] | 204 | if ( userId >= (int)userList.size() ) |
---|
[7540] | 205 | return; |
---|
| 206 | |
---|
| 207 | userList[userId].host = 0; |
---|
| 208 | userList[userId].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 | assert( networkPacket.length <= UDP_PACKET_SIZE ); |
---|
| 222 | |
---|
| 223 | memcpy( packet->data, networkPacket.data, networkPacket.length ); |
---|
| 224 | packet->len = networkPacket.length; |
---|
[7556] | 225 | packet->channel = -1; |
---|
[7540] | 226 | |
---|
| 227 | if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 ) |
---|
| 228 | { |
---|
| 229 | PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError()); |
---|
| 230 | return false; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | return true; |
---|
| 234 | } |
---|
[7565] | 235 | |
---|
| 236 | /** |
---|
| 237 | * do periodically things |
---|
| 238 | */ |
---|
| 239 | void UdpServerSocket::update( ) |
---|
| 240 | { |
---|
| 241 | int res; |
---|
| 242 | int newConn = 0; |
---|
| 243 | |
---|
| 244 | for ( res = SDLNet_UDP_Recv( socket, packet ); res == 1; res = SDLNet_UDP_Recv( socket, packet ) ) |
---|
| 245 | { |
---|
| 246 | int userId; |
---|
| 247 | bool isNewConnection = false; |
---|
| 248 | |
---|
[8228] | 249 | for ( userId =0; userId < (int)userList.size(); userId++ ) |
---|
[7565] | 250 | if ( userList[userId].host == packet->address.host && userList[userId].port == packet->address.port ) |
---|
| 251 | break; |
---|
| 252 | |
---|
[8228] | 253 | if ( userId >= (int)userList.size() ) |
---|
[7565] | 254 | { |
---|
[7854] | 255 | |
---|
[7565] | 256 | newConn++; |
---|
| 257 | isNewConnection = true; |
---|
| 258 | |
---|
| 259 | if ( newConn > MAX_NEW_CONNECTIONS ) |
---|
| 260 | { |
---|
| 261 | PRINTF(2)("Too many new connections. Dropping packet\n"); |
---|
| 262 | continue; |
---|
| 263 | } |
---|
| 264 | |
---|
[8228] | 265 | for ( userId =0; userId < (int)userList.size(); userId++ ) |
---|
[7565] | 266 | if ( userList[userId].host == 0 && userList[userId].port == 0 ) |
---|
| 267 | break; |
---|
| 268 | |
---|
| 269 | initUser( userId, packet->address ); |
---|
| 270 | UdpSocket * sock = new UdpSocket( this, packet->address, userId ); |
---|
| 271 | newSocketList.push_back( sock ); |
---|
| 272 | PRINTF(0)("NEW CONNECTION %x\n", packet->address.host ); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | //add new packet to packetbuffer |
---|
[7854] | 276 | |
---|
[7565] | 277 | NetworkPacket networkPacket; |
---|
| 278 | networkPacket.length = packet->len; |
---|
| 279 | if ( packet->len != 0 ) |
---|
| 280 | { |
---|
| 281 | networkPacket.data = (byte*)malloc( packet->len ); |
---|
| 282 | assert( networkPacket.data ); |
---|
| 283 | } |
---|
| 284 | else |
---|
| 285 | { |
---|
| 286 | networkPacket.data = NULL; |
---|
| 287 | } |
---|
| 288 | memcpy( networkPacket.data, packet->data, packet->len ); |
---|
| 289 | packetBuffer[userId].push_back( networkPacket ); |
---|
[7854] | 290 | |
---|
[7565] | 291 | } |
---|
| 292 | |
---|
| 293 | assert( res == 0 ); |
---|
| 294 | } |
---|