feat: implement multiplayer networking improvements for reduced latency

Major networking enhancements to improve multiplayer performance and
reduce desync issues in games like Mario Kart 8 Deluxe:

Network Performance:
- Add socket connection pooling in BSD service to reduce overhead
- Implement unreliable packet delivery for latency-sensitive game data
- Add packet reliability control for both ProxyPacket and LDNPacket
- Use ENET_PACKET_FLAG_UNSEQUENCED for small UDP packets (<1200 bytes)

Monitoring & Debugging:
- Add PacketStatistics struct to track sent/received/dropped packets
- Enhanced logging for proxy packet handling and socket lifecycle
- Periodic stats logging every 100 packets for diagnostics

Configuration:
- Update lobby_api_url and web_api_url to https://api.ynet-fun.xyz
- Add lobby API URL configuration support

Socket Management:
- Implement SocketPoolKey for efficient socket reuse
- Store domain/type/protocol info in FileDescriptor
- Max pool size limit (8 sockets per type) to prevent memory bloat
- Return closed sockets to pool when room is still connected

Protocol Updates:
- Add 'reliable' field to ProxyPacket and LDNPacket structures
- Update room.cpp packet handlers to respect reliability flags
- Maintain backward compatibility with default reliable=true

These changes significantly reduce packet latency for real-time game
traffic while maintaining reliability for control packets.

Signed-off-by: Zephyron <zephyron@citron-emu.org>
This commit is contained in:
Zephyron
2025-11-05 18:52:06 +10:00
parent ee124b44e3
commit 5117ff3702
9 changed files with 154 additions and 21 deletions

View File

@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
@@ -831,10 +832,14 @@ void Room::RoomImpl::HandleProxyPacket(const ENetEvent* event) {
bool broadcast;
in_packet.Read(broadcast); // Broadcast
bool reliable;
in_packet.Read(reliable); // Reliability flag
Packet out_packet;
out_packet.Append(event->packet->data, event->packet->dataLength);
const u32 enet_flags = reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED;
ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
ENET_PACKET_FLAG_RELIABLE);
enet_flags);
const auto& destination_address = remote_ip;
if (broadcast) { // Send the data to everyone except the sender
@@ -885,10 +890,14 @@ void Room::RoomImpl::HandleLdnPacket(const ENetEvent* event) {
bool broadcast;
in_packet.Read(broadcast); // Broadcast
bool reliable;
in_packet.Read(reliable); // Reliability flag
Packet out_packet;
out_packet.Append(event->packet->data, event->packet->dataLength);
const u32 enet_flags = reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED;
ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
ENET_PACKET_FLAG_RELIABLE);
enet_flags);
const auto& destination_address = remote_ip;
if (broadcast) { // Send the data to everyone except the sender