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

@@ -128,7 +128,9 @@ BEGIN_DATADESC_NOBASE(C_BaseEntity)
END_DATADESC()
IMPLEMENT_RECV_DT_NOBASE(C_BaseEntity)
NetPropFloat3(m_vPosition)
NetPropFloat3(m_vPosition),
NetPropQuaternion(m_vRotation),
NetPropFloat3(m_vScale)
END_RECV_DT()
IMPLEMENT_EMPTY_SEND_DT_NOBASE(C_BaseEntity)

View File

@@ -33,9 +33,12 @@ DECLARE_BUILD_STAGE(Client)
"../shared",
"../../public",
FUNNYSTDLIB"public",
EXTERNAL"cglm/include"
EXTERNAL"cglm/include",
EXTERNAL"steamworks/public",
};
compileProject.bFPIC = true;
if ( GET_PROJECT_VALUE(config, "steam") == "true" )
compileProject.macros.AppendTail({"STEAM", "TRUE"});
ldProject = ccompiler->Compile(&compileProject);
if ( GET_PROJECT_VALUE(config, "static") == "true" )
@@ -44,6 +47,10 @@ DECLARE_BUILD_STAGE(Client)
}
else
{
if ( GET_PROJECT_VALUE(config, "steam") == "true" ) {
ldProject.libraryDirectories.AppendTail(EXTERNAL"steamworks/redistributable_bin/linux64");
ldProject.libraries.AppendTail("steam_api");
}
ldProject.linkType = ELINK_DYNAMIC_LIBRARY;
ldProject.libraryObjects = {
GET_PROJECT_LIBRARY(tier0, "tier0"),

View File

@@ -1,4 +1,5 @@
#include "tier2/ifilesystem.h"
#include "tier0/commandline.h"
#include "materialsystem/imaterialsystem.h"
#include "enginebridge.h"
#include "worldrender.h"
@@ -9,6 +10,10 @@
#include "cglm/cglm.h"
#include "inetworkclient.h"
#include "netprotocol.h"
#ifdef STEAM
#include "steam/isteamgameserver.h"
#include "steam/steam_gameserver.h"
#endif
IFileSystem *filesystem;
IRenderContext *g_pRenderContext;
@@ -16,7 +21,9 @@ IGameWindow *g_pMainWindow;
static CEngineVars s_vars;
CEngineVars *g_pEngineVars = &s_vars;
EngineConsts_t *g_pEngineConstants;
INetworkBase *g_pServerBridge;
INetworkBase *g_pServerConnection;
class CFunnyGameBridge: public IEngineBridge
{
@@ -25,6 +32,11 @@ class CFunnyGameBridge: public IEngineBridge
virtual void Frame( float fDelta ) override;
virtual void Shutdown() override;
virtual void ConnectInterface( const char *psz, void *pInterface ) override;
void TryToConnectToServer();
bool m_bIsConnectedToSteamRelay;
bool m_bIsConnectedToServer;
};
IEngineBridge *EngineBridge()
@@ -35,16 +47,29 @@ IEngineBridge *EngineBridge()
EXPOSE_INTERFACE_FN(EngineBridge, IEngineBridge, ENGINE_BRIDGE_INTERFACE_VERSION)
void CFunnyGameBridge::Init()
{
g_pWorldRenderer->Init();
g_pServerBridge = g_pEngineConstants->ConnectLocalBridge(0);
#ifdef STEAM
if (g_pEngineConstants->m_bIsSteam)
{
SteamErrMsg err = { 0 };
m_bIsConnectedToSteamRelay = 0;
SteamNetworkingUtils()->InitRelayNetworkAccess();
}
#endif
PlayerJoined_t join = {
MESSAGE_PLAYER_JOINED,
"LocalPlayer"
};
g_pServerBridge->SendPacket({&join, sizeof(join)});
m_bIsConnectedToServer = false;
g_pServerBridge = g_pEngineConstants->ConnectLocalBridge(0);
if (g_pServerBridge)
{
PlayerJoined_t join = {
MESSAGE_PLAYER_JOINED,
"LocalPlayer"
};
g_pServerBridge->SendPacket({&join, sizeof(join)});
}
}
void CFunnyGameBridge::Tick( float fDelta )
@@ -70,55 +95,106 @@ searchIndex:
return (char*)pEntity+pCurrentMap->m_pFields[uCurrentIndex].m_uOffset;
}
void CFunnyGameBridge::TryToConnectToServer()
{
#ifdef STEAM
if (g_pEngineConstants->m_bIsSteam)
{
if (m_bIsConnectedToSteamRelay != 0 )
return;
if ( SteamNetworkingUtils()->GetRelayNetworkStatus(NULL) == k_ESteamNetworkingAvailability_Current)
{
m_bIsConnectedToSteamRelay = 1;
V_printf("%llu\n", SteamUser()->GetSteamID().ConvertToUint64());
if (CommandLine()->ParamValue("-steam-connect"))
{
char *pEnd = NULL;
uint64_t uValue = strtoull(CommandLine()->ParamValue("-steam-connect"), &pEnd, 10);
g_pServerConnection = g_pEngineConstants->ConnectSteamServer(uValue, FUNNY_SECURE_PORT);
if (g_pServerConnection)
{
m_bIsConnectedToServer = true;
}
return;
}
}
}
#endif
}
void CFunnyGameBridge::Frame( float fDelta )
{
g_pEngineVars->m_fTime += fDelta;
g_pEngineVars->m_fDeltaTime = fDelta;
g_pServerBridge->NetThink();
while ( g_pServerBridge->BHasUpdates() )
{
NetPacket_t packet = g_pServerBridge->PeekPacket();
// discard it
if (packet.uSize < sizeof (EMessageType))
continue;
PlayerPacket_t *pPacket = (PlayerPacket_t*)packet.pData;
C_BaseEntity *pEntity;
switch (pPacket->m_eType)
{
case MESSAGE_ENTITY_CLASS_SYNC:
pEntity = EntitySystem()->CreateByClassnameWithIndex(
(char*)pPacket->m_entityClass.m_szEntityName, pPacket->m_entityClass.m_uIndex
);
pEntity->Spawn();
g_pServerBridge->RecievePacket();
break;
case MESSAGE_ENTITY_DATA_SYNC:
pEntity = EntitySystem()->GetEntities()[pPacket->m_entityData.m_uIndex];
union {
void *pData;
char *pcCurrentData;
EntityDataSyncValue_t *pcSyncValue;
};
pData = pPacket;
pcCurrentData += sizeof(EntityDataSync_t);
for ( uint32_t u = 0; u < pPacket->m_entityData.m_uCount; u++ )
{
uint32_t uVariableSize = pcSyncValue->m_uVariableSize;
void *pValueData = (float*)ENT_GetNetMapData(
pEntity,
pEntity->GetRecvMap(),
pcSyncValue->m_uVariableIndex);
TryToConnectToServer();
pcCurrentData += sizeof(EntityDataSyncValue_t);
if (pValueData)
V_memcpy(pValueData, pcCurrentData, uVariableSize);
pcCurrentData += (uVariableSize+7) & ~7;
INetworkBase *pCurrentServer = g_pServerBridge;
pCurrentServer = g_pServerBridge;
if (m_bIsConnectedToServer)
if (g_pServerConnection->BIsActive())
pCurrentServer = g_pServerConnection;
if (pCurrentServer)
{
pCurrentServer->NetThink();
while ( pCurrentServer->BHasUpdates() )
{
NetPacket_t packet = pCurrentServer->PeekPacket();
// discard it
if (packet.uSize < sizeof (EMessageType))
continue;
PlayerPacket_t *pPacket = (PlayerPacket_t*)packet.pData;
C_BaseEntity *pEntity;
switch (pPacket->m_eType)
{
case MESSAGE_ENTITY_CLASS_SYNC:
pEntity = EntitySystem()->CreateByClassnameWithIndex(
(char*)pPacket->m_entityClass.m_szEntityName, pPacket->m_entityClass.m_uIndex
);
if (pEntity == NULL)
{
pCurrentServer->RecievePacket();
continue;
}
pEntity->Spawn();
pCurrentServer->RecievePacket();
break;
case MESSAGE_ENTITY_DATA_SYNC:
pEntity = EntitySystem()->GetEntities()[pPacket->m_entityData.m_uIndex];
if (pEntity == NULL)
{
pCurrentServer->RecievePacket();
continue;
}
union {
void *pData;
char *pcCurrentData;
EntityDataSyncValue_t *pcSyncValue;
};
pData = pPacket;
pcCurrentData += sizeof(EntityDataSync_t);
for ( uint32_t u = 0; u < pPacket->m_entityData.m_uCount; u++ )
{
uint32_t uVariableSize = pcSyncValue->m_uVariableSize;
void *pValueData = (float*)ENT_GetNetMapData(
pEntity,
pEntity->GetRecvMap(),
pcSyncValue->m_uVariableIndex);
pcCurrentData += sizeof(EntityDataSyncValue_t);
if (pValueData)
V_memcpy(pValueData, pcCurrentData, uVariableSize);
pcCurrentData += (uVariableSize+7) & ~7;
}
pCurrentServer->RecievePacket();
break;
default:
pCurrentServer->RecievePacket();
V_printf("worng packet\n");
continue;
}
g_pServerBridge->RecievePacket();
break;
default:
continue;
}
}

View File

@@ -25,6 +25,12 @@ void CBaseEntity::Spawn()
void CBaseEntity::SetAbsAngles( float fPitch, float fYaw, float fRoll )
{
versor q;
glm_euler_yzx_quat((vec3){fPitch, fYaw, fRoll}, q);
m_vRotation.x = q[0];
m_vRotation.y = q[1];
m_vRotation.z = q[2];
m_vRotation.w = q[3];
}
void CBaseEntity::SetAbsOrigin( Vector origin )
@@ -42,7 +48,6 @@ void CBaseEntity::SetScale( float fScale )
QAngle CBaseEntity::GetAbsQAngles( void )
{
}
Quat CBaseEntity::GetAbsAngles( void )
{
@@ -119,7 +124,9 @@ BEGIN_DATADESC_NOBASE(CBaseEntity)
END_DATADESC()
IMPLEMENT_SEND_DT_NOBASE(CBaseEntity)
NetPropFloat3(m_vPosition)
NetPropFloat3(m_vPosition),
NetPropQuaternion(m_vRotation),
NetPropFloat3(m_vScale),
END_SEND_DT()
IMPLEMENT_EMPTY_RECV_DT_NOBASE(CBaseEntity)

View File

@@ -27,9 +27,12 @@ DECLARE_BUILD_STAGE(Server)
".",
"../shared",
FUNNYSTDLIB"public",
EXTERNAL"cglm/include"
EXTERNAL"cglm/include",
EXTERNAL"steamworks/public",
};
compileProject.bFPIC = true;
if ( GET_PROJECT_VALUE(config, "steam") == "true" )
compileProject.macros.AppendTail({"STEAM", "TRUE"});
ldProject = ccompiler->Compile(&compileProject);
if ( GET_PROJECT_VALUE(config, "static") == "true" )
{
@@ -37,6 +40,10 @@ DECLARE_BUILD_STAGE(Server)
}
else
{
if ( GET_PROJECT_VALUE(config, "steam") == "true" ) {
ldProject.libraryDirectories.AppendTail(EXTERNAL"steamworks/redistributable_bin/linux64");
ldProject.libraries.AppendTail("steam_api");
}
ldProject.linkType = ELINK_DYNAMIC_LIBRARY;
ldProject.libraryObjects = {
GET_PROJECT_LIBRARY(tier0, "tier0"),

View File

@@ -92,7 +92,8 @@ CBaseEntity *CEntitySystem::CreateByClassname( const char *szName )
iSelectedSlot,
};
V_strncpy((char*)stClassSync.m_szEntityName, szName, 256);
g_pClientBridge->SendPacket({&stClassSync, sizeof(stClassSync)});
if (g_pCurrentConnection)
g_pCurrentConnection->SendPacket({&stClassSync, sizeof(stClassSync)});
return pEntity;
}
IEntityFactory *CEntitySystem::GetFactoryByClassname( const char *szName )
@@ -113,6 +114,25 @@ void CEntitySystem::Think()
CBaseEntity *pEntity;
int i;
for ( i = 0; i < MAX_EDICTS; i++ )
{
pEntity = m_pEntities[i];
if ( pEntity == NULL )
continue;
if ( !pEntity->m_pfnThink )
continue;
(pEntity->*pEntity->m_pfnThink)(0);
}
}
void CEntitySystem::NetThink( INetworkBase *pBase )
{
CBaseEntity *pEntity;
int i;
uint32_t u;
uint32_t x;
uint32_t uSize;
@@ -125,19 +145,15 @@ void CEntitySystem::Think()
EntityDataSyncValue_t *pValue;
};
for ( i = 0; i < MAX_EDICTS; i++ )
{
pEntity = m_pEntities[i];
if ( pEntity == NULL )
continue;
if ( !pEntity->m_pfnThink )
continue;
(pEntity->*pEntity->m_pfnThink)(0);
pNetMap = pEntity->GetSendMap();
uSize = sizeof(EntityDataSyncValue_t);
x = 0;
@@ -177,9 +193,10 @@ void CEntitySystem::Think()
}
pNetMap = pNetMap->m_pBase;
}
g_pClientBridge->SendPacket({pData, uSize});
if (g_pCurrentConnection)
g_pCurrentConnection->SendPacket({pData, uSize});
V_free(pData);
}
}

View File

@@ -7,6 +7,8 @@
#ifndef ENTITIES_H
#define ENTITIES_H
#include "networkbase.h"
class IEntityFactory;
class CBaseEntity;
@@ -23,6 +25,7 @@ public:
virtual IEntityFactory *GetFactoryByClassname( const char *szName );
virtual void Think();
virtual void NetThink( INetworkBase *pBase );
virtual CBaseEntity **GetEntities();
private:
CBaseEntity *m_pEntities[MAX_EDICTS];

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();
}

View File

@@ -10,8 +10,10 @@ void CMOBAPlayer::Spawn()
void CMOBAPlayer::Think( float fDelta )
{
m_fTimer = g_pEngineVars->m_fTime;
SetAbsOrigin({m_fTimer,0,0});
m_fTimer += 1.0/60.0;
SetAbsOrigin({m_fTimer, 0, 0});
SetAbsAngles(glm_rad(90), m_fTimer, 0);
SetScale(1);
};
LINK_ENTITY_TO_CLASS(player, CMOBAPlayer)

View File

@@ -22,5 +22,7 @@ extern IGameWindow *g_pMainWindow;
extern EngineConsts_t *g_pEngineConstants;
extern INetworkBase *g_pServerBridge;
extern INetworkBase *g_pClientBridge;
extern INetworkBase *g_pPublicConnection;
extern INetworkBase *g_pCurrentConnection;
#endif

View File

@@ -14,4 +14,8 @@ public:
};
extern CEngineVars *g_pEngineVars;
#define FUNNY_SECURE_PORT 27015
#define FUNNY_QUERY_PORT 27016
#endif