steam relay networking
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user