forked from cafeberry/cafeberry
LCEMP commit: prepare code for dedicated server support
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include <time.h>
|
||||
#include "..\..\Minecraft.World\net.minecraft.world.entity.item.h"
|
||||
#include "..\..\Minecraft.World\net.minecraft.world.entity.player.h"
|
||||
#include "..\..\Minecraft.World\net.minecraft.world.level.tile.entity.h"
|
||||
@@ -236,6 +238,26 @@ void CMinecraftApp::DebugPrintf(const char *szFormat, ...)
|
||||
vsnprintf(buf, sizeof(buf), szFormat, ap);
|
||||
va_end(ap);
|
||||
OutputDebugStringA(buf);
|
||||
#ifdef _DEDICATED_SERVER
|
||||
bool hasContent = false;
|
||||
for (const char *p = buf; *p; p++) {
|
||||
if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' && *p != '=') {
|
||||
hasContent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasContent)
|
||||
{
|
||||
size_t len = strlen(buf);
|
||||
while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r'))
|
||||
buf[--len] = '\0';
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm t;
|
||||
localtime_s(&t, &now);
|
||||
printf("[%02d:%02d:%02d] [Server thread/INFO]: %s\n", t.tm_hour, t.tm_min, t.tm_sec, buf);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -298,6 +320,9 @@ LPCWSTR CMinecraftApp::GetString(int iID)
|
||||
{
|
||||
//return L"Değişiklikler ve Yenilikler";
|
||||
//return L"ÕÕÕÕÖÖÖÖ";
|
||||
#ifdef _DEDICATED_SERVER
|
||||
if (!app.m_stringTable) return L"";
|
||||
#endif
|
||||
return app.m_stringTable->getString(iID);
|
||||
}
|
||||
|
||||
|
||||
@@ -873,12 +873,14 @@ int CGameNetworkManager::JoinFromInvite_SignInReturned(void *pParam,bool bContin
|
||||
|
||||
void CGameNetworkManager::UpdateAndSetGameSessionData(INetworkPlayer *pNetworkPlayerLeaving)
|
||||
{
|
||||
#ifndef _DEDICATED_SERVER
|
||||
Minecraft *pMinecraft = Minecraft::GetInstance();
|
||||
TexturePack *tPack = pMinecraft->skins->getSelected();
|
||||
s_pPlatformNetworkManager->SetSessionTexturePackParentId( tPack->getDLCParentPackId() );
|
||||
s_pPlatformNetworkManager->SetSessionSubTexturePackId( tPack->getDLCSubPackId() );
|
||||
|
||||
s_pPlatformNetworkManager->UpdateAndSetGameSessionData( pNetworkPlayerLeaving );
|
||||
#endif
|
||||
}
|
||||
|
||||
void CGameNetworkManager::SendInviteGUI(int quadrant)
|
||||
@@ -1540,6 +1542,7 @@ void CGameNetworkManager::CreateSocket( INetworkPlayer *pNetworkPlayer, bool loc
|
||||
// Add this user to the game server if the game is started already
|
||||
if( g_NetworkManager.IsHost() && g_NetworkManager.IsInGameplay() )
|
||||
{
|
||||
app.DebugPrintf("Adding incoming socket for smallId=%d\n", pNetworkPlayer->GetSmallId());
|
||||
Socket::addIncomingSocket(socket);
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -167,6 +167,7 @@ public:
|
||||
static SOCKET GetSocketForSmallId(BYTE smallId);
|
||||
|
||||
static void HandleDataReceived(BYTE fromSmallId, BYTE toSmallId, unsigned char *data, unsigned int dataSize);
|
||||
static void FlushPendingData();
|
||||
|
||||
static bool PopDisconnectedSmallId(BYTE *outSmallId);
|
||||
static void PushFreeSmallId(BYTE smallId);
|
||||
@@ -235,6 +236,9 @@ private:
|
||||
|
||||
static CRITICAL_SECTION s_freeSmallIdLock;
|
||||
static std::vector<BYTE> s_freeSmallIds;
|
||||
|
||||
static CRITICAL_SECTION s_earlyDataLock;
|
||||
static std::vector<BYTE> s_earlyDataBuffers[NETWORK_LAN_MAX_CLIENTS + 1];
|
||||
};
|
||||
|
||||
extern bool g_MultiplayerHost;
|
||||
|
||||
Reference in New Issue
Block a user