steam relay networking

This commit is contained in:
2026-03-01 23:06:28 +02:00
parent 03c560c2b7
commit 468d765aa1
16 changed files with 628 additions and 101 deletions

View File

@@ -5,6 +5,11 @@
#include "game.h"
#include "inetworkserver.h"
#include "netprotocol.h"
#ifdef STEAM
#include "steam/isteamgameserver.h"
#include "steam/steam_gameserver.h"
#endif
IFileSystem *filesystem;
IRenderContext *g_pRenderContext;
@@ -13,6 +18,8 @@ static CEngineVars s_vars;
CEngineVars *g_pEngineVars = &s_vars;
EngineConsts_t *g_pEngineConstants;
INetworkBase *g_pClientBridge;
INetworkBase *g_pPublicConnection;
INetworkBase *g_pCurrentConnection;
class CFunnyGameBridge: public IEngineBridge
{
@@ -21,6 +28,9 @@ class CFunnyGameBridge: public IEngineBridge
virtual void Frame( float fDelta ) override;
virtual void Shutdown() override;
virtual void ConnectInterface( const char *psz, void *pInterface ) override;
bool m_bIsConnectedToSteamRelay;
float m_fNetUpdateTimer;
};
@@ -32,9 +42,64 @@ IEngineBridge *EngineBridge()
EXPOSE_INTERFACE_FN(EngineBridge, IEngineBridge, ENGINE_BRIDGE_INTERFACE_VERSION)
uint32_t NET_ServerCallback( NetCallback_t *pCallback )
{
V_printf("hi %u\n", pCallback->m_eType);
if (pCallback->m_eType == NET_SERVER_READY_TO_USE)
{
V_printf("//--- LAUNCHED SERVER AT ---\n");
V_printf("// %llu\n", SteamGameServer()->GetSteamID().ConvertToUint64());
return 0;
}
if (pCallback->m_eType == NET_TRYING_TO_CONNECT)
{
V_printf("user %llu is trying to connect\n", pCallback->m_ullUserID);
return 1;
}
if (pCallback->m_eType == NET_CONNECTED)
{
V_printf("user %llu has connected, hi!\n", pCallback->m_ullUserID);
CBaseEntity *pEntity;
pEntity = EntitySystem()->CreateByClassname("player");
pEntity->Spawn();
return 1;
}
return 0;
}
void CFunnyGameBridge::Init()
{
g_pClientBridge = g_pEngineConstants->LaunchLocalBridge(0);
if (g_pEngineConstants->m_bIsDedicated == false)
g_pClientBridge = g_pEngineConstants->LaunchLocalBridge(0);
g_pPublicConnection = NULL;
g_pCurrentConnection = g_pClientBridge;
#ifdef STEAM
if (g_pEngineConstants->m_bIsSteam)
{
SteamErrMsg err = { 0 };
if (SteamGameServer_InitEx(INADDR_ANY, FUNNY_SECURE_PORT, FUNNY_QUERY_PORT, eServerModeAuthentication, "0.0.0.0", &err));
{
V_printf("SteamGameServer_InitEx: %s", err);
}
SteamNetworkingUtils()->InitRelayNetworkAccess();
}
if (g_pEngineConstants->m_bIsDedicated && g_pEngineConstants->m_bIsSteam)
{
SteamGameServer()->SetModDir("funnygame");
SteamGameServer()->SetProduct("funnygame");
SteamGameServer()->SetGameDescription("not that funny but ok");
SteamGameServer()->LogOnAnonymous();
SteamNetworkingUtils()->InitRelayNetworkAccess();
SteamGameServer()->SetDedicatedServer(true);
SteamGameServer()->SetMaxPlayerCount(128);
SteamGameServer()->SetAdvertiseServerActive(true);
}
if (g_pEngineConstants->m_bIsSteam)
{
}
#endif
m_fNetUpdateTimer = 0;
}
void CFunnyGameBridge::Tick( float fDelta )
@@ -42,34 +107,74 @@ void CFunnyGameBridge::Tick( float fDelta )
}
void NET_ProcessPacket( INetworkBase *pBase )
{
NetPacket_t packet = pBase->PeekPacket();
// discard it
if (packet.uSize < sizeof (EMessageType))
{
pBase->RecievePacket();
return;
}
PlayerPacket_t *pPacket = (PlayerPacket_t*)packet.pData;
CBaseEntity *pEntity;
switch (pPacket->m_eType)
{
case MESSAGE_PLAYER_JOINED:
// online packets don't generate these
if (g_pEngineConstants->m_bIsDedicated)
break;
pBase->RecievePacket();
V_printf("Hi %s\n",pPacket->m_playerJoined.m_szPlayerName);
pEntity = EntitySystem()->CreateByClassname("player");
pEntity->Spawn();
break;
default:
break;
}
pBase->RecievePacket();
}
void CFunnyGameBridge::Frame( float fDelta )
{
g_pEngineVars->m_fTime += fDelta;
g_pEngineVars->m_fDeltaTime = fDelta;
g_pClientBridge->NetThink();
while (g_pClientBridge->BHasUpdates())
#ifdef STEAM
if (g_pEngineConstants->m_bIsSteam && g_pEngineConstants->m_bIsDedicated)
{
NetPacket_t packet = g_pClientBridge->PeekPacket();
// discard it
if (packet.uSize < sizeof (EMessageType))
continue;
PlayerPacket_t *pPacket = (PlayerPacket_t*)packet.pData;
CBaseEntity *pEntity;
switch (pPacket->m_eType)
SteamGameServer_RunCallbacks();
if (m_bIsConnectedToSteamRelay == 0 && SteamNetworkingUtils()->GetRelayNetworkStatus(NULL) == k_ESteamNetworkingAvailability_Current)
{
case MESSAGE_PLAYER_JOINED:
g_pClientBridge->RecievePacket();
V_printf("Hi %s\n",pPacket->m_playerJoined.m_szPlayerName);
pEntity = EntitySystem()->CreateByClassname("player");
pEntity->Spawn();
break;
default:
continue;
m_bIsConnectedToSteamRelay = 1;
g_pPublicConnection = g_pEngineConstants->LaunchServer( FUNNY_SECURE_PORT );
g_pPublicConnection->SetCallback(NET_ServerCallback);
g_pCurrentConnection = g_pPublicConnection;
return;
}
}
#endif
if (g_pCurrentConnection)
{
g_pCurrentConnection->NetThink();
while (g_pCurrentConnection->BHasUpdates())
NET_ProcessPacket(g_pCurrentConnection);
}
float fTickRate = 1.0/60.0;
m_fNetUpdateTimer += fDelta;
while (m_fNetUpdateTimer >= fTickRate)
{
m_fNetUpdateTimer-=fTickRate;
EntitySystem()->Think();
if (g_pCurrentConnection)
{
EntitySystem()->NetThink(g_pCurrentConnection);
}
}
EntitySystem()->Think();
}