chore/fix: clean up NetworkSocketLayer, fix console compile errors

This commit is contained in:
2026-09-13 14:10:24 +01:00
parent 0405a47bd1
commit a2dad85bb1
16 changed files with 781 additions and 296 deletions
@@ -4,6 +4,7 @@
#include "NetworkSocketLayer.h"
#include "..\..\Common\Network\PlatformNetworkManager.h"
#include "..\..\..\Minecraft.World\Socket.h"
#include "SocketManagers\NetworkSocketManager.h"
#if defined(MINECRAFT_SERVER_BUILD)
#include "..\..\..\Minecraft.Server\Access\Access.h"
#include "..\..\..\Minecraft.Server\ServerLogManager.h"
@@ -13,6 +14,7 @@
static bool TryGetNumericRemoteIp(const sockaddr_in &remoteAddress, std::string *outIp);
#endif
NetworkSocketManager *NetworkSocketLayer::s_pPlatformSocketManager;
SOCKET NetworkSocketLayer::s_listenSocket = INVALID_SOCKET;
SOCKET NetworkSocketLayer::s_hostConnectionSocket = INVALID_SOCKET;
SOCKET NetworkSocketLayer::s_advertiseSock = INVALID_SOCKET;
@@ -90,6 +92,8 @@ bool NetworkSocketLayer::Initialize()
{
if (s_initialized) return true;
s_pPlatformSocketManager = new NetworkSocketManager();
// Create the locks before touching the platform network stack.
if (!s_locksCreated)
{
@@ -111,42 +115,7 @@ bool NetworkSocketLayer::Initialize()
s_locksCreated = true;
}
#if defined _WINDOWS64 || defined _XBOX
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0)
{
app.DebugPrintf("WSAStartup failed: %d\n", result);
return false;
}
#elif defined __PS3__
int result = sys_net_initialize_network();
if (result != 0) {
app.DebugPrintf("sys_net_initialize_network failed: %d\n", result);
return false;
}
result = cellNetCtlInit();
if (result < 0) {
app.DebugPrintf("cellNetCtlInit failed: %d\n", result);
sys_net_finalize_network();
return false;
}
#elif defined __PSVITA__
SceNetInitParam param;
static char memory[16 * 1024];
param.memory = memory;
param.size = sizeof(memory);
param.flags = 0;
int result = sceNetInit(&param);
// libnet is brought up before us by the platform layer, which reports EBUSY here.
if (result < 0 && result != SCE_NET_ERROR_EBUSY) {
app.DebugPrintf("sceNetInit failed: 0x%08x\n", result);
return false;
}
#endif
s_pPlatformSocketManager->InitNetwork();
s_initialized = true;
@@ -166,44 +135,26 @@ void NetworkSocketLayer::Shutdown()
s_active = false;
s_connected = false;
#if defined _WINDOWS64 || defined _XBOX
if (s_listenSocket != INVALID_SOCKET)
if (s_pPlatformSocketManager->IsSocketValid(s_listenSocket))
{
closesocket(s_listenSocket);
s_pPlatformSocketManager->CloseSocket(s_listenSocket);
s_listenSocket = INVALID_SOCKET;
}
if (s_hostConnectionSocket != INVALID_SOCKET)
if (s_pPlatformSocketManager->IsSocketValid(s_hostConnectionSocket))
{
closesocket(s_hostConnectionSocket);
s_pPlatformSocketManager->CloseSocket(s_hostConnectionSocket);
s_hostConnectionSocket = INVALID_SOCKET;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (s_listenSocket >= 0)
{
socketclose(s_listenSocket);
s_listenSocket = -1;
}
if (s_hostConnectionSocket >= 0)
{
socketclose(s_hostConnectionSocket);
s_hostConnectionSocket = -1;
}
#endif
EnterCriticalSection(&s_connectionsLock);
for (int i = 0; i < NETWORK_LAN_MAX_CLIENTS + 1; i++)
{
s_connections[i].active = false;
#if defined _WINDOWS64 || defined _XBOX
if (s_connections[i].tcpSocket != INVALID_SOCKET)
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (s_connections[i].tcpSocket >= 0)
#endif
if (s_pPlatformSocketManager->IsSocketValid(s_connections[i].tcpSocket))
{
closesocket(s_connections[i].tcpSocket);
s_pPlatformSocketManager->CloseSocket(s_connections[i].tcpSocket);
s_connections[i].tcpSocket = INVALID_SOCKET;
}
if (s_connections[i].recvThread != NULL)
@@ -256,9 +207,9 @@ bool NetworkSocketLayer::HostGame(int port)
s_connected = false;
s_active = false;
if (s_hostConnectionSocket != INVALID_SOCKET)
if (s_pPlatformSocketManager->IsSocketValid(s_hostConnectionSocket))
{
closesocket(s_hostConnectionSocket);
s_pPlatformSocketManager->CloseSocket(s_hostConnectionSocket);
s_hostConnectionSocket = INVALID_SOCKET;
}
s_nextSmallId = 1;
@@ -268,129 +219,10 @@ bool NetworkSocketLayer::HostGame(int port)
s_freeSmallIds.clear();
LeaveCriticalSection(&s_freeSmallIdLock);
int iResult;
#if defined _WINDOWS64
struct addrinfo hints = {};
struct addrinfo *result = NULL;
s_listenSocket = s_pPlatformSocketManager->MakeHostSocket(port);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
char portStr[16];
sprintf_s(portStr, "%d", port);
iResult = getaddrinfo(NULL, portStr, &hints, &result);
if (iResult != 0)
{
app.DebugPrintf("getaddrinfo failed: %d\n", iResult);
return false;
}
s_listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (s_listenSocket == INVALID_SOCKET)
{
app.DebugPrintf("socket() failed: %d\n", WSAGetLastError());
freeaddrinfo(result);
return false;
}
#elif defined _XBOX
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons((WORD)port);
s_listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s_listenSocket == INVALID_SOCKET)
{
app.DebugPrintf("socket() failed: %d\n", WSAGetLastError());
return false;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
#ifdef __PSVITA__
SceNetSockaddrIn addr = {};
#else
struct sockaddr_in addr = {};
#endif
#ifdef __PSVITA__
addr.sin_len = sizeof(addr);
#endif
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons((uint16_t)port);
s_listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s_listenSocket < 0)
{
app.DebugPrintf("socket() failed: %d\n", s_listenSocket);
return false;
}
#endif
int opt = 1;
setsockopt(s_listenSocket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
#if defined _WINDOWS64
iResult = ::bind(s_listenSocket, result->ai_addr, (int)result->ai_addrlen);
freeaddrinfo(result);
if (iResult == SOCKET_ERROR)
{
app.DebugPrintf("bind() failed: %d\n", WSAGetLastError());
closesocket(s_listenSocket);
s_listenSocket = INVALID_SOCKET;
return false;
}
iResult = listen(s_listenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
app.DebugPrintf("listen() failed: %d\n", WSAGetLastError());
closesocket(s_listenSocket);
s_listenSocket = INVALID_SOCKET;
return false;
}
#elif defined _XBOX
iResult = ::bind(s_listenSocket, (struct sockaddr*)&addr, sizeof(addr));
if (iResult == SOCKET_ERROR)
{
app.DebugPrintf("bind() failed: %d\n", WSAGetLastError());
closesocket(s_listenSocket);
s_listenSocket = INVALID_SOCKET;
return false;
}
iResult = listen(s_listenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
app.DebugPrintf("listen() failed: %d\n", WSAGetLastError());
closesocket(s_listenSocket);
s_listenSocket = INVALID_SOCKET;
return false;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
#ifdef __PSVITA__
iResult = ::bind(s_listenSocket, (const sockaddr*)&addr, sizeof(addr));
#else
iResult = ::bind(s_listenSocket, (struct sockaddr*)&addr, sizeof(addr));
#endif
if (iResult < 0)
{
app.DebugPrintf("bind() failed: %d\n", iResult);
closesocket(s_listenSocket);
s_listenSocket = -1;
return false;
}
iResult = listen(s_listenSocket, 5);
if (iResult < 0)
{
app.DebugPrintf("listen() failed: %d\n", iResult);
closesocket(s_listenSocket);
s_listenSocket = -1;
return false;
}
#endif
s_active = true;
s_connected = true;
@@ -852,11 +684,7 @@ bool NetworkSocketLayer::SendOnSocket(SOCKET sock, const void *data, int dataSiz
while (totalSent < toSend)
{
int sent = send(sock, (const char *)header + totalSent, toSend - totalSent, 0);
#if defined _WINDOWS64 || defined _XBOX
if (sent == SOCKET_ERROR || sent == 0)
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (sent < 0 || sent == 0)
#endif
if (!s_pPlatformSocketManager->IsSentValid(sent) || sent == 0)
return false;
totalSent += sent;
}
@@ -866,11 +694,7 @@ bool NetworkSocketLayer::SendOnSocket(SOCKET sock, const void *data, int dataSiz
{
int sent = send(sock, (const char *)data + totalSent, dataSize - totalSent, 0);
#if defined _WINDOWS64 || defined _XBOX
if (sent == SOCKET_ERROR || sent == 0)
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (sent < 0 || sent == 0)
#endif
if (!s_pPlatformSocketManager->IsSentValid(sent) || sent == 0)
return false;
totalSent += sent;
}
@@ -918,11 +742,7 @@ SOCKET NetworkSocketLayer::GetSocketForSmallId(BYTE smallId)
return sock;
}
LeaveCriticalSection(&s_connectionsLock);
#if defined _WINDOWS64 || defined _XBOX
return INVALID_SOCKET;
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
return -1;
#endif
}
// Send reject handshake: sentinel 0xFF + DisconnectPacket wire format (1 byte id 255 + 4 byte big-endian reason). Then caller closes socket.
@@ -977,14 +797,6 @@ void NetworkSocketLayer::HandleDataReceived(BYTE fromSmallId, BYTE toSmallId, un
INetworkPlayer *pPlayerFrom = g_NetworkManager.GetPlayerBySmallId(fromSmallId);
INetworkPlayer *pPlayerTo = g_NetworkManager.GetPlayerBySmallId(toSmallId);
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");
return;
}
if (s_isHost)
{
::Socket *pSocket = pPlayerFrom->GetSocket();
@@ -1011,24 +823,19 @@ int NetworkSocketLayer::AcceptThreadProc(LPVOID param)
#else
sockaddr_in remoteAddress;
ZeroMemory(&remoteAddress, sizeof(remoteAddress));
#ifdef __PS3__
socklen_t remoteAddressLength = sizeof(remoteAddress);
#else
int remoteAddressLength = sizeof(remoteAddress);
#endif
SOCKET clientSocket = accept(s_listenSocket, (sockaddr*)&remoteAddress, &remoteAddressLength);
#endif
#if defined _WINDOWS64 || defined _XBOX
if (clientSocket == INVALID_SOCKET)
if (!s_pPlatformSocketManager->IsSocketValid(clientSocket))
{
if (s_active)
app.DebugPrintf("accept() failed: %d\n", WSAGetLastError());
app.DebugPrintf("accept() failed: %d\n",s_pPlatformSocketManager->getSocketError(clientSocket));
break;
}
#elif __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (clientSocket < 0)
{
if (s_active)
app.DebugPrintf("accept() failed: %d\n", clientSocket);
break;
}
#endif
int noDelay = 1;
setsockopt(clientSocket, IPPROTO_TCP, TCP_NODELAY, (const char *)&noDelay, sizeof(noDelay));
@@ -1055,7 +862,7 @@ int NetworkSocketLayer::AcceptThreadProc(LPVOID param)
#else
app.DebugPrintf("Win64 LAN: Rejecting connection, game not ready\n");
#endif
closesocket(clientSocket);
s_pPlatformSocketManager->CloseSocket(clientSocket);
continue;
}
@@ -1078,7 +885,7 @@ int NetworkSocketLayer::AcceptThreadProc(LPVOID param)
#else
app.DebugPrintf("Win64 LAN: Server full, rejecting connection\n");
#endif
closesocket(clientSocket);
s_pPlatformSocketManager->CloseSocket(clientSocket);
continue;
}
LeaveCriticalSection(&s_freeSmallIdLock);
@@ -1088,7 +895,7 @@ int NetworkSocketLayer::AcceptThreadProc(LPVOID param)
if (sent != 1)
{
app.DebugPrintf("Failed to send small ID to client\n");
closesocket(clientSocket);
s_pPlatformSocketManager->CloseSocket(clientSocket);
continue;
}
@@ -1193,7 +1000,7 @@ int NetworkSocketLayer::RecvThreadProc(LPVOID param)
s_connections[clientSmallId].active = false;
if (s_connections[clientSmallId].tcpSocket != INVALID_SOCKET)
{
closesocket(s_connections[clientSmallId].tcpSocket);
s_pPlatformSocketManager->CloseSocket(s_connections[clientSmallId].tcpSocket);
s_connections[clientSmallId].tcpSocket = INVALID_SOCKET;
}
LeaveCriticalSection(&s_connectionsLock);
@@ -1237,7 +1044,7 @@ void NetworkSocketLayer::CloseConnectionBySmallId(BYTE smallId)
EnterCriticalSection(&s_connectionsLock);
if (smallId < NETWORK_LAN_MAX_CLIENTS + 1 && s_connections[smallId].active && s_connections[smallId].tcpSocket != INVALID_SOCKET)
{
closesocket(s_connections[smallId].tcpSocket);
s_pPlatformSocketManager->CloseSocket(s_connections[smallId].tcpSocket);
s_connections[smallId].tcpSocket = INVALID_SOCKET;
app.DebugPrintf("Win64 LAN: Force-closed TCP connection for smallId=%d\n", smallId);
}
@@ -1249,11 +1056,7 @@ int NetworkSocketLayer::ClientRecvThreadProc(LPVOID param)
std::vector<BYTE> recvBuf;
recvBuf.resize(NETWORK_LAN_RECV_BUFFER_SIZE);
#if defined _WINDOWS64 || defined _XBOX
while (s_active && s_hostConnectionSocket != INVALID_SOCKET)
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
while (s_active && s_hostConnectionSocket >= 0)
#endif
while (s_active && s_pPlatformSocketManager->IsSocketValid(s_hostConnectionSocket))
{
BYTE header[4];
if (!RecvExact(s_hostConnectionSocket, header, 4))
@@ -1317,19 +1120,11 @@ bool NetworkSocketLayer::StartAdvertising(int gamePort, const wchar_t *hostName,
LeaveCriticalSection(&s_advertiseLock);
s_advertiseSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#if defined _WINDOWS64 || defined _XBOX
if (s_advertiseSock == INVALID_SOCKET)
if (!s_pPlatformSocketManager->IsSocketValid(s_advertiseSock))
{
app.DebugPrintf("Win64 LAN: Failed to create advertise socket: %d\n", WSAGetLastError());
app.DebugPrintf("LAN: Failed to create advertise socket: %d\n", s_pPlatformSocketManager->getSocketError(s_advertiseSock));
return false;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (s_advertiseSock < 0)
{
app.DebugPrintf("PS3 LAN: Failed to create advertise socket: %d\n", s_advertiseSock);
return false;
}
#endif
BOOL broadcast = TRUE;
setsockopt(s_advertiseSock, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast));
@@ -1337,7 +1132,7 @@ bool NetworkSocketLayer::StartAdvertising(int gamePort, const wchar_t *hostName,
s_advertising = true;
s_advertiseThread = new C4JThread(AdvertiseThreadProc, NULL, "AdvertiseThreadProc");
s_advertiseThread->Run();
app.DebugPrintf("Win64 LAN: Started advertising on UDP port %d\n", NETWORK_LAN_DISCOVERY_PORT);
app.DebugPrintf("LAN: Started advertising on UDP port %d\n", NETWORK_LAN_DISCOVERY_PORT);
return true;
}
@@ -1345,19 +1140,11 @@ void NetworkSocketLayer::StopAdvertising()
{
s_advertising = false;
#if defined _WINDOWS64
if (s_advertiseSock != INVALID_SOCKET)
if (s_pPlatformSocketManager->IsSocketValid(s_advertiseSock))
{
closesocket(s_advertiseSock);
s_pPlatformSocketManager->CloseSocket(s_advertiseSock);
s_advertiseSock = INVALID_SOCKET;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (s_advertiseSock >= 0)
{
closesocket(s_advertiseSock);
s_advertiseSock = -1;
}
#endif
if (s_advertiseThread != NULL)
{
@@ -1461,17 +1248,10 @@ int NetworkSocketLayer::AdvertiseThreadProc(LPVOID param)
(struct sockaddr *)&broadcastAddr, sizeof(broadcastAddr));
#endif
#if defined _WINDOWS64 || defined _XBOX
if (sent == SOCKET_ERROR && s_advertising)
if (!s_pPlatformSocketManager->IsSentValid(sent) && s_advertising)
{
app.DebugPrintf("Win64 LAN: Broadcast sendto failed: %d\n", WSAGetLastError());
app.DebugPrintf("LAN: Broadcast sendto failed: %d\n", s_pPlatformSocketManager->getSocketError(s_advertising));
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (sent < 0 && s_advertising)
{
app.DebugPrintf("PS3 LAN: Broadcast sendto failed: %d\n", sent);
}
#endif
C4JThread::Sleep(1000);
}
@@ -1485,21 +1265,11 @@ bool NetworkSocketLayer::StartDiscovery()
if (!s_initialized) return false;
s_discoverySock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#if defined _WINDOWS64 || defined _XBOX
if (s_discoverySock == INVALID_SOCKET)
if (!s_pPlatformSocketManager->IsSocketValid(s_discoverySock))
{
app.DebugPrintf("Win64 LAN: Failed to create discovery socket: %d\n", WSAGetLastError());
app.DebugPrintf("LAN: Failed to create discovery socket: %d\n", s_pPlatformSocketManager->getSocketError(s_discoverySock));
return false;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
app.DebugPrintf("Discovery socket: %d\n", s_discoverySock);
if (s_discoverySock < 0)
{
app.DebugPrintf("PS3 LAN: Failed to create discovery socket: %d\n", s_discoverySock);
terminate_networking();
return false;
}
#endif
BOOL reuseAddr = TRUE;
setsockopt(s_discoverySock, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuseAddr, sizeof(reuseAddr));
@@ -1522,7 +1292,7 @@ bool NetworkSocketLayer::StartDiscovery()
if (::bind(s_discoverySock, (struct sockaddr *)&bindAddr, sizeof(bindAddr)) == SOCKET_ERROR)
{
app.DebugPrintf("Win64 LAN: Discovery bind failed: %d\n", WSAGetLastError());
closesocket(s_discoverySock);
s_pPlatformSocketManager->CloseSocket(s_discoverySock);
s_discoverySock = INVALID_SOCKET;
return false;
}
@@ -1566,19 +1336,11 @@ void NetworkSocketLayer::StopDiscovery()
{
s_discovering = false;
#if defined _WINDOWS64 || defined _XBOX
if (s_discoverySock != INVALID_SOCKET)
if (s_pPlatformSocketManager->IsSocketValid(s_discoverySock))
{
closesocket(s_discoverySock);
s_pPlatformSocketManager->CloseSocket(s_discoverySock);
s_discoverySock = INVALID_SOCKET;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (s_discoverySock >= 0)
{
socketclose(s_discoverySock);
s_discoverySock = -1;
}
#endif
if (s_discoveryThread != NULL)
{
@@ -1629,17 +1391,10 @@ int NetworkSocketLayer::DiscoveryThreadProc(LPVOID param)
(struct sockaddr *)&senderAddr, &senderLen);
#endif
#ifdef _WINDOWS64
if (recvLen == SOCKET_ERROR)
if (!s_pPlatformSocketManager->IsSentValid(recvLen))
{
continue;
}
#elif defined __PS3__ || defined __ORBIS__ || defined __PSVITA__
if (recvLen < 0)
{
continue;
}
#endif
if (recvLen < (int)sizeof(LANBroadcast))
continue;