@@ -204,7 +204,7 @@ namespace pg
204204 // 2) Process incoming packets
205205 // Process Tcp data
206206
207- auto now = getCurrentTime () ;
207+ std::set< uint32_t > disconnectedClients ;
208208
209209 for (auto & client : clients)
210210 {
@@ -217,6 +217,7 @@ namespace pg
217217 {
218218 if (sendTCPMessage (ci.clientId , ci.token , NetMsgType::Ping, {0 }, ci.tcpSock ))
219219 {
220+ auto now = getCurrentTime ();
220221 LOG_INFO (" NetSys" , " Sent ping to client: " << ci.clientId << " at time: " << now);
221222 ci.lastPingSentMs = 0 ;
222223 }
@@ -241,6 +242,14 @@ namespace pg
241242 handleServerMessage (msg.header , msg.payload );
242243 }
243244 }
245+
246+ if (tcpClosed)
247+ {
248+ LOG_WARNING (" NetSys" , " TCP connection closed by client: " << ci.clientId );
249+
250+ disconnectedClients.insert (ci.clientId );
251+ break ;
252+ }
244253 }
245254
246255 // Process Udp data
@@ -259,6 +268,11 @@ namespace pg
259268 }
260269 // LOG_INFO("NetSys", "Received UDP request from ip: " << ipPortKey(ip));
261270 }
271+
272+ for (auto id : disconnectedClients)
273+ {
274+ disconnectClient (id);
275+ }
262276 }
263277
264278 void NetworkSystem::runClientFrame (float dt)
@@ -363,4 +377,21 @@ namespace pg
363377 handleMessage (header, payload);
364378 }
365379
380+ void NetworkSystem::disconnectClient (uint32_t id)
381+ {
382+ auto it = idToTcp.find (id);
383+
384+ if (it != idToTcp.end ())
385+ {
386+ auto tcpSock = it->second ;
387+
388+ clients.erase (tcpSock);
389+ idToTcp.erase (it);
390+
391+ backend->closeTcp (tcpSock);
392+
393+ LOG_INFO (" NetSys" , " Client " << id << " disconnected and cleaned up" );
394+ }
395+ }
396+
366397} // namespace pg
0 commit comments