fix(sockets): Clear Socket Data Properly for Initialization

Signed-off-by: Collecting <collecting@noreply.localhost>
This commit is contained in:
Collecting
2026-01-11 08:12:40 +00:00
parent 2311ed2bdf
commit 0bec953153

View File

@@ -29,28 +29,39 @@ ProxySocket::~ProxySocket() {
} }
void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) { void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) {
if (protocol != packet.protocol || local_endpoint.portno != packet.remote_endpoint.portno || auto room_member = room_network.GetRoomMember().lock();
closed) { if (!room_member) return;
stats.packets_dropped++;
LOG_DEBUG(Network, "Dropped packet: protocol mismatch or closed socket. Stats: sent={}, recv={}, dropped={}", const auto my_ip = room_member->GetFakeIpAddress();
stats.packets_sent, stats.packets_received, stats.packets_dropped);
// If the sender (local_endpoint) is OUR IP, ignore it.
// We don't want to process our own sent packets.
if (packet.local_endpoint.ip == my_ip) {
return; return;
} }
// Only accept packets meant for us or actual broadcasts.
if (packet.remote_endpoint.ip != my_ip && !packet.broadcast) {
return;
}
// PROTOCOL & PORT CHECK
if (protocol != packet.protocol || local_endpoint.portno != packet.remote_endpoint.portno || closed) {
stats.packets_dropped++;
return;
}
// BROADCAST CHECK
if (!broadcast && packet.broadcast) { if (!broadcast && packet.broadcast) {
stats.packets_dropped++; stats.packets_dropped++;
LOG_DEBUG(Network, "Dropped broadcast packet on non-broadcast socket");
return; return;
} }
// DECOMPRESSION & QUEUEING
auto decompressed = packet; auto decompressed = packet;
decompressed.data = Common::Compression::DecompressDataZSTD(packet.data); decompressed.data = Common::Compression::DecompressDataZSTD(packet.data);
// Check if decompression failed (returns empty vector on error)
if (decompressed.data.empty() && !packet.data.empty()) { if (decompressed.data.empty() && !packet.data.empty()) {
stats.packets_dropped++; stats.packets_dropped++;
LOG_WARNING(Network, "Dropped packet: ZSTD decompression failed. Stats: sent={}, recv={}, dropped={}",
stats.packets_sent, stats.packets_received, stats.packets_dropped);
return; return;
} }
@@ -58,13 +69,6 @@ void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) {
received_packets.push(decompressed); received_packets.push(decompressed);
stats.packets_received++; stats.packets_received++;
stats.bytes_received += decompressed.data.size(); stats.bytes_received += decompressed.data.size();
// Log statistics periodically (every 100 packets)
if (stats.packets_received % 100 == 0) {
LOG_DEBUG(Network, "ProxySocket stats: sent={} ({} bytes), recv={} ({} bytes), dropped={}",
stats.packets_sent, stats.bytes_sent,
stats.packets_received, stats.bytes_received, stats.packets_dropped);
}
} }
template <typename T> template <typename T>
@@ -77,6 +81,24 @@ Errno ProxySocket::Initialize(Domain domain, Type type, Protocol socket_protocol
protocol = socket_protocol; protocol = socket_protocol;
SetSockOpt(fd, SO_TYPE, type); SetSockOpt(fd, SO_TYPE, type);
// Reset all state flags
is_bound = false;
closed = false;
broadcast = false;
// Wipe the endpoint.
// This forces the RoomMember to assign a new/fresh identity if possible
local_endpoint = {};
// Reset stats so the new session starts at 0 bytes
stats = {};
// Clear the queue.
std::lock_guard guard(packets_mutex);
while(!received_packets.empty()) {
received_packets.pop();
}
return Errno::SUCCESS; return Errno::SUCCESS;
} }