LCEMP commit: prepare code for dedicated server support

This commit is contained in:
2026-08-14 13:32:11 +03:00
parent 67a8111974
commit f404e4b4e4
13 changed files with 150 additions and 23 deletions
@@ -61,6 +61,9 @@ std::vector<BYTE> NetworkSocketLayer::s_pendingJoinSmallIds;
CRITICAL_SECTION NetworkSocketLayer::s_freeSmallIdLock;
std::vector<BYTE> NetworkSocketLayer::s_freeSmallIds;
CRITICAL_SECTION NetworkSocketLayer::s_earlyDataLock;
std::vector<BYTE> NetworkSocketLayer::s_earlyDataBuffers[NETWORK_LAN_MAX_CLIENTS + 1];
// only goes true on a successful Initialize(), and a failed one gets retried
static bool s_locksCreated = false;
#if defined _WINDOWS64
@@ -88,6 +91,7 @@ bool NetworkSocketLayer::Initialize()
InitializeCriticalSection(&s_disconnectLock);
InitializeCriticalSection(&s_pendingJoinLock);
InitializeCriticalSection(&s_freeSmallIdLock);
InitializeCriticalSection(&s_earlyDataLock);
for (int i = 0; i < NETWORK_LAN_MAX_CLIENTS + 1; i++)
{
@@ -801,6 +805,9 @@ bool NetworkSocketLayer::JoinGame(const char *ip, int port)
}
s_localSmallId = assignedSmallId;
DWORD noTimeout = 0;
setsockopt(s_hostConnectionSocket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&noTimeout, sizeof(noTimeout));
app.DebugPrintf("LAN: Connected to %s:%d, assigned smallId=%d\n", ip, port, s_localSmallId);
s_active = true;
@@ -926,9 +933,13 @@ void NetworkSocketLayer::HandleDataReceived(BYTE fromSmallId, BYTE toSmallId, un
if (pPlayerFrom == NULL || pPlayerTo == NULL)
{
// dropping here is silent and looks identical to the peer never sending
app.DebugPrintf("LAN: DROPPED %u bytes - from smallId=%d(%s) to smallId=%d(%s)\n",
dataSize, fromSmallId, pPlayerFrom ? "ok" : "NULL", toSmallId, pPlayerTo ? "ok" : "NULL");
if (s_isHost && fromSmallId > 0 && fromSmallId < NETWORK_LAN_MAX_CLIENTS + 1)
{
EnterCriticalSection(&s_earlyDataLock);
s_earlyDataBuffers[fromSmallId].insert(
s_earlyDataBuffers[fromSmallId].end(), data, data + dataSize);
LeaveCriticalSection(&s_earlyDataLock);
}
return;
}
@@ -937,6 +948,13 @@ void NetworkSocketLayer::HandleDataReceived(BYTE fromSmallId, BYTE toSmallId, un
::Socket *pSocket = pPlayerFrom->GetSocket();
if (pSocket != NULL)
pSocket->pushDataToQueue(data, dataSize, false);
else
{
EnterCriticalSection(&s_earlyDataLock);
s_earlyDataBuffers[fromSmallId].insert(
s_earlyDataBuffers[fromSmallId].end(), data, data + dataSize);
LeaveCriticalSection(&s_earlyDataLock);
}
}
else
{
@@ -946,6 +964,26 @@ void NetworkSocketLayer::HandleDataReceived(BYTE fromSmallId, BYTE toSmallId, un
}
}
void NetworkSocketLayer::FlushPendingData()
{
EnterCriticalSection(&s_earlyDataLock);
for (int i = 1; i < NETWORK_LAN_MAX_CLIENTS + 1; i++)
{
if (s_earlyDataBuffers[i].empty()) continue;
INetworkPlayer *pPlayer = g_NetworkManager.GetPlayerBySmallId((BYTE)i);
if (pPlayer == NULL) continue;
::Socket *pSocket = pPlayer->GetSocket();
if (pSocket == NULL) continue;
pSocket->pushDataToQueue(s_earlyDataBuffers[i].data(),
(DWORD)s_earlyDataBuffers[i].size(), false);
s_earlyDataBuffers[i].clear();
}
LeaveCriticalSection(&s_earlyDataLock);
}
int NetworkSocketLayer::AcceptThreadProc(LPVOID param)
{
while (s_active)
@@ -1172,6 +1210,11 @@ void NetworkSocketLayer::CloseConnectionBySmallId(BYTE smallId)
app.DebugPrintf("Win64 LAN: Force-closed TCP connection for smallId=%d\n", smallId);
}
LeaveCriticalSection(&s_connectionsLock);
EnterCriticalSection(&s_earlyDataLock);
if (smallId < NETWORK_LAN_MAX_CLIENTS + 1)
s_earlyDataBuffers[smallId].clear();
LeaveCriticalSection(&s_earlyDataLock);
}
int NetworkSocketLayer::ClientRecvThreadProc(LPVOID param)