Changeset 8723 in orxonox.OLD for branches/network
- Timestamp:
- Jun 22, 2006, 2:58:47 PM (18 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/udp_server_socket.cc
r8623 r8723 147 147 res.length = packetBuffer[userId].front().length; 148 148 packetBuffer[userId].pop_front(); 149 150 if ( res.length == 0 )151 res.length = -1;152 149 } 153 150 … … 234 231 return false; 235 232 } 236 233 237 234 return true; 238 235 } … … 257 254 if ( userId >= (int)userList.size() ) 258 255 { 259 256 if ( packet->len <= 0 ) 257 continue; 258 260 259 newConn++; 261 260 isNewConnection = true; … … 272 271 273 272 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 ); 275 274 newSocketList.push_back( sock ); 276 275 PRINTF(0)("NEW CONNECTION %x\n", packet->address.host ); … … 292 291 memcpy( networkPacket.data, packet->data, packet->len ); 293 292 packetBuffer[userId].push_back( networkPacket ); 294 295 293 } 296 294 -
branches/network/src/lib/network/udp_socket.cc
r8362 r8723 25 25 this->socket = NULL; 26 26 this->packet = NULL; 27 this->randomByte = 0; 27 28 } 28 29 … … 70 71 * @param userId userid used by serverSocket 71 72 */ 72 UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId )73 UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId, byte randomByte ) 73 74 { 74 75 this->init(); 75 76 this->serverSocket = serverSocket; 76 77 this->userId = userId; 78 this->randomByte = randomByte; 77 79 } 78 80 … … 104 106 105 107 IPaddress ip; 108 109 this->randomByte = generateNewRandomByte(); 106 110 107 111 PRINTF(0)("connect to server %s on port %d\n", host.c_str(), port); … … 137 141 { 138 142 PRINTF(0)("disconnect\n"); 139 writePacket( NULL, 0 ); 143 byte cmd = this->randomByte | UDPCMD_DISCONNECT; 144 writeRawPacket( &cmd, 1 ); 140 145 SDLNet_UDP_Unbind( socket, -1 ); 141 146 SDLNet_UDP_Close( socket ); … … 152 157 bool UdpSocket::writePacket( byte * data, int length ) 153 158 { 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 */ 172 int 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 244 bool UdpSocket::writeRawPacket( byte * data, int length ) 245 { 154 246 if ( serverSocket ) 155 247 { … … 183 275 } 184 276 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; 277 bool 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 298 byte 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 308 bool UdpSocket::checkRandomByte( byte rndByte ) 309 { 310 if ( ( rndByte & 0xFC ) == this->randomByte ) 311 { 312 return true; 219 313 } 220 314 else 221 315 { 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 16 16 class UdpServerSocket; 17 17 18 enum 19 { 20 UDPCMD_DISCONNECT = 1, 21 UDPCMD_INVALIDRNDBYTE = 2 22 }; 23 18 24 class UdpSocket : public NetworkSocket 19 25 { 20 26 public: 21 27 UdpSocket(); 22 UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId );28 UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId, byte randomByte ); 23 29 UdpSocket( std::string host, int port ); 24 30 virtual ~UdpSocket(); … … 28 34 virtual void disconnectServer(); 29 35 30 virtual bool writePacket(byte * data, int length );36 virtual bool writePacket(byte * data, int length ); 31 37 32 38 virtual int readPacket(byte * data, int maxLength); … … 39 45 UDPpacket * packet; 40 46 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 41 54 void init(); 42 55
Note: See TracChangeset
for help on using the changeset viewer.