Changeset 8802 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Jun 26, 2006, 4:46:25 PM (18 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/netdefs.h
r7954 r8802 19 19 #define MAX_CONNECTIONS 1000 20 20 21 #define NETWORK_FREQUENCY 3321 #define NETWORK_FREQUENCY 66 22 22 23 23 -
trunk/src/lib/network/udp_server_socket.cc
r8623 r8802 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 … … 173 170 * @param ip users host / port 174 171 */ 175 void UdpServerSocket::initUser( int userId, IPaddress ip )172 void UdpServerSocket::initUser( int userId, IPaddress ip, byte randomByte ) 176 173 { 177 174 int channel = SDLNet_UDP_Bind( socket, userId, &ip ); … … 192 189 userList.resize( userId + 1 ); 193 190 194 userList[ userId ] = ip; 191 userList[ userId ].addr = ip; 192 userList[ userId ].randomByte = randomByte; 195 193 } 196 194 … … 206 204 return; 207 205 208 userList[userId]. host = 0;209 userList[userId]. port = 0;206 userList[userId].addr.host = 0; 207 userList[userId].addr.port = 0; 210 208 211 209 SDLNet_UDP_Unbind( socket, userId ); … … 234 232 return false; 235 233 } 236 234 237 235 return true; 238 236 } … … 251 249 bool isNewConnection = false; 252 250 251 if ( packet->len <= 0 ) 252 continue; 253 253 254 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 ) ) 255 256 break; 256 257 257 258 if ( userId >= (int)userList.size() ) 258 259 { 259 260 260 newConn++; 261 261 isNewConnection = true; … … 268 268 269 269 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 ) 271 271 break; 272 272 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 ); 275 275 newSocketList.push_back( sock ); 276 276 PRINTF(0)("NEW CONNECTION %x\n", packet->address.host ); … … 292 292 memcpy( networkPacket.data, packet->data, packet->len ); 293 293 packetBuffer[userId].push_back( networkPacket ); 294 295 294 } 296 295 -
trunk/src/lib/network/udp_server_socket.h
r7954 r8802 33 33 typedef std::vector<NetworkPacketList> PacketBuffer; 34 34 35 typedef std::vector<IPaddress> UserList; 35 struct UserInfo 36 { 37 IPaddress addr; 38 byte randomByte; 39 }; 40 41 typedef std::vector<UserInfo> UserList; 36 42 37 43 typedef std::list<UdpSocket*> UdpSocketList; … … 57 63 bool sendPacket( NetworkPacket networkPacket, int userId ); 58 64 int getPacketCount( int childId ); 59 void initUser( int childId, IPaddress ip );65 void initUser( int childId, IPaddress ip, byte randomByte ); 60 66 61 67 private: -
trunk/src/lib/network/udp_socket.cc
r8362 r8802 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 -
trunk/src/lib/network/udp_socket.h
r7954 r8802 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.