| 436 | */ |
| 437 | |
| 438 | void RUdpConnection::OnPacketReceived(const IpAddress& peerIp, NetPacket&& packet) |
| 439 | { |
| 440 | UInt16 protocolBegin; |
| 441 | UInt16 protocolEnd; |
| 442 | SequenceIndex sequenceId; |
| 443 | SequenceIndex lastAck; |
| 444 | UInt32 ackBits; |
| 445 | |
| 446 | packet.GetStream()->SetCursorPos(packet.GetSize() - MessageFooter); |
| 447 | packet >> protocolEnd; |
| 448 | |
| 449 | packet.GetStream()->SetCursorPos(NetPacket::HeaderSize); |
| 450 | packet >> protocolBegin; |
| 451 | |
| 452 | UInt32 protocolId = static_cast<UInt32>(protocolEnd) << 16 | protocolBegin; |
| 453 | if (protocolId != m_protocol) |
| 454 | return; ///< Ignore |
| 455 | |
| 456 | packet >> sequenceId >> lastAck >> ackBits; |
| 457 | |
| 458 | auto it = m_peerByIP.find(peerIp); |
| 459 | if (it == m_peerByIP.end()) |
| 460 | { |
| 461 | switch (packet.GetNetCode()) |
| 462 | { |
| 463 | case NetCode_RequestConnection: |
| 464 | { |
| 465 | UInt64 token; |
| 466 | packet >> token; |
| 467 | |
| 468 | NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Received NetCode_RequestConnection from " + peerIp.ToString() + ": " + String::Number(token)); |
| 469 | if (!m_shouldAcceptConnections) |
| 470 | return; //< Ignore |
| 471 | |
| 472 | OnClientRequestingConnection(peerIp, sequenceId, token); |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | default: |
| 477 | return; //< Ignore |
| 478 | } |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | PeerData& peer = m_peers[it->second]; |
| 483 | peer.lastPacketTime = m_currentTime; |
| 484 | |
| 485 | if (peer.receivedQueue.find(sequenceId) != peer.receivedQueue.end()) |
| 486 | return; //< Ignore |
| 487 | |
| 488 | if (m_isSimulationEnabled && m_packetLossProbability(s_randomGenerator)) |
| 489 | { |
| 490 | NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Lost packet " + String::Number(sequenceId) + " from " + peerIp.ToString() + " for simulation purpose"); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | ///< Receiving a packet from an acknowledged client means the connection works in both ways |
| 495 | if (peer.state == PeerState_Aknowledged && packet.GetNetCode() != NetCode_RequestConnection) |
nothing calls this directly
no test coverage detected