BVB Source Codes

CRYENGINE Show GameServerChannel.cpp Source code

Return Download CRYENGINE: download GameServerChannel.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.  
  8.    -------------------------------------------------------------------------
  9.    History:
  10.    - 11:8:2004   11:40 : Created by M谩rcio Martins
  11.  
  12. *************************************************************************/
  13. #include "StdAfx.h"
  14. #include "GameServerChannel.h"
  15. #include "GameClientChannel.h"
  16. #include "GameServerNub.h"
  17. #include "GameContext.h"
  18. #include "CryAction.h"
  19. #include "GameRulesSystem.h"
  20.  
  21. ICVar* CGameServerChannel::sv_timeout_disconnect = 0;
  22.  
  23. CGameServerChannel::CGameServerChannel(INetChannel* pNetChannel, CGameContext* pGameContext, CGameServerNub* pServerNub)
  24.         : m_pServerNub(pServerNub), m_channelId(0), m_hasLoadedLevel(false), m_onHold(false)
  25. {
  26.         Init(pNetChannel, pGameContext);
  27.         CRY_ASSERT(pNetChannel);
  28.  
  29. #if NEW_BANDWIDTH_MANAGEMENT
  30.         pNetChannel->SetServer(GetGameContext()->GetNetContext());
  31. #else
  32.         SetupNetChannel(pNetChannel);
  33. #endif // NEW_BANDWIDTH_MANAGEMENT
  34.  
  35.         if (!sv_timeout_disconnect)
  36.                 sv_timeout_disconnect = gEnv->pConsole->GetCVar("sv_timeout_disconnect");
  37.  
  38.         gEnv->pConsole->AddConsoleVarSink(this);
  39.         CCryAction::GetCryAction()->OnActionEvent(SActionEvent(eAE_channelCreated, 1));
  40. }
  41.  
  42. CGameServerChannel::~CGameServerChannel()
  43. {
  44.         gEnv->pConsole->RemoveConsoleVarSink(this);
  45.         CCryAction::GetCryAction()->OnActionEvent(SActionEvent(eAE_channelDestroyed, 1));
  46.  
  47.         Cleanup();
  48. }
  49.  
  50. #if !NEW_BANDWIDTH_MANAGEMENT
  51. void CGameServerChannel::SetupNetChannel(INetChannel* pNetChannel)
  52. {
  53.         pNetChannel->SetServer(GetGameContext()->GetNetContext(), true);
  54.         INetChannel::SPerformanceMetrics pm;
  55.         if (!gEnv->bMultiplayer)
  56.                 pm.pPacketRateDesired = gEnv->pConsole->GetCVar("g_localPacketRate");
  57.         else
  58.                 pm.pPacketRateDesired = gEnv->pConsole->GetCVar("sv_packetRate");
  59.         pm.pBitRateDesired = gEnv->pConsole->GetCVar("sv_bandwidth");
  60.         pNetChannel->SetPerformanceMetrics(&pm);
  61. }
  62. #endif // !NEW_BANDWIDTH_MANAGEMENT
  63.  
  64. void CGameServerChannel::Release()
  65. {
  66.         if (GetNetChannel())
  67.                 delete this;
  68. }
  69.  
  70. bool CGameServerChannel::OnBeforeVarChange(ICVar* pVar, const char* sNewValue)
  71. {
  72.         // This code is useful for debugging issues with networked cvars, but it's
  73.         // very spammy so #if'ing out for now.
  74. #if 0 // LOG_CVAR_USAGE
  75.         int flags = pVar->GetFlags();
  76.         bool netSynced = ((flags & VF_NET_SYNCED) != 0);
  77.  
  78.         CryLog("[CVARS]: [CHANGED] CGameServerChannel::OnBeforeVarChange(): variable [%s] (%smarked VF_NET_SYNCED) with a value of [%s]; SERVER changing to [%s]",
  79.                pVar->GetName(),
  80.                (netSynced) ? "" : "not ",
  81.                pVar->GetString(),
  82.                sNewValue);
  83. #endif // LOG_CVAR_USAGE
  84.  
  85.         return true;
  86. }
  87.  
  88. void CGameServerChannel::OnAfterVarChange(ICVar* pVar)
  89. {
  90.         if (pVar->GetFlags() & VF_NET_SYNCED)
  91.         {
  92.                 if (GetNetChannel() && !GetNetChannel()->IsLocal())
  93.                 {
  94.                         SClientConsoleVariableParams params(pVar->GetName(), pVar->GetString());
  95. #if FAST_CVAR_SYNC
  96.                         SSendableHandle& id = GetConsoleStreamId(params.key);
  97.                         INetSendablePtr pSendable = new CSimpleNetMessage<SClientConsoleVariableParams>(params, CGameClientChannel::SetConsoleVariable);
  98.                         pSendable->SetGroup('cvar');
  99.                         GetNetChannel()->SubstituteSendable(pSendable, 1, &id, &id);
  100. #else
  101.                         INetSendablePtr pSendable = new CSimpleNetMessage<SClientConsoleVariableParams>(params, CGameClientChannel::SetConsoleVariable);
  102.                         pSendable->SetGroup('cvar');
  103.                         GetNetChannel()->AddSendable(pSendable, 1, &m_consoleVarSendable, &m_consoleVarSendable);
  104. #endif
  105.                 }
  106.         }
  107. }
  108.  
  109. void CGameServerChannel::OnDisconnect(EDisconnectionCause cause, const char* description)
  110. {
  111.         //CryLogAlways("CGameServerChannel::OnDisconnect(%d, '%s')", cause, description?description:"");
  112.         CCryAction::GetCryAction()->OnActionEvent(SActionEvent(eAE_clientDisconnected, int(cause), description));
  113.  
  114.         IGameRules* pGameRules = CCryAction::GetCryAction()->GetIGameRulesSystem()->GetCurrentGameRules();
  115.  
  116.         if (pGameRules && IsOnHold())
  117.         {
  118.                 pGameRules->OnClientDisconnect(GetChannelId(), cause, description, false);
  119.                 Cleanup();
  120.  
  121.                 return;
  122.         }
  123.  
  124.         if (sv_timeout_disconnect && pGameRules && sv_timeout_disconnect->GetIVal() > 0 && pGameRules->ShouldKeepClient(GetChannelId(), cause, description))
  125.         {
  126.                 if (m_pServerNub->PutChannelOnHold(this))
  127.                 {
  128.                         pGameRules->OnClientDisconnect(GetChannelId(), cause, description, true);
  129.                         m_hasLoadedLevel = false;
  130.                         return;
  131.                 }
  132.         }
  133.  
  134.         if (pGameRules)
  135.                 pGameRules->OnClientDisconnect(GetChannelId(), cause, description, false);
  136.         Cleanup();
  137. }
  138.  
  139. void CGameServerChannel::Cleanup()
  140. {
  141.         m_pServerNub->RemoveChannel(GetChannelId());
  142.  
  143.         if (GetPlayerId())
  144.         {
  145.                 gEnv->pEntitySystem->RemoveEntity(GetPlayerId(), true);
  146.         }
  147. }
  148.  
  149. void CGameServerChannel::DefineProtocol(IProtocolBuilder* pBuilder)
  150. {
  151.         pBuilder->AddMessageSink(this, CGameClientChannel::GetProtocolDef(), CGameServerChannel::GetProtocolDef());
  152.         CCryAction* cca = CCryAction::GetCryAction();
  153.         if (cca->GetIGameObjectSystem())
  154.                 cca->GetIGameObjectSystem()->DefineProtocol(true, pBuilder);
  155.         if (cca->GetGameContext())
  156.                 cca->GetGameContext()->DefineContextProtocols(pBuilder, true);
  157. }
  158.  
  159. void CGameServerChannel::SetPlayerId(EntityId playerId)
  160. {
  161.         //check for banned status here
  162.         if (m_pServerNub->CheckBanned(GetNetChannel()))
  163.                 return;
  164.  
  165.         if (CGameServerChannel* pServerChannel = m_pServerNub->GetOnHoldChannelFor(GetNetChannel()))
  166.         {
  167.                 //cleanup onhold channel if it was not associated with us
  168.                 //normally it should be taken while creating channel, but for now, this doesn't happen
  169.                 m_pServerNub->RemoveOnHoldChannel(pServerChannel, false);
  170.         }
  171.  
  172.         CGameChannel::SetPlayerId(playerId);
  173.         if (GetNetChannel()->IsLocal())
  174.                 CGameClientChannel::SendSetPlayerId_LocalOnlyWith(playerId, GetNetChannel());
  175. }
  176.  
  177. bool CGameServerChannel::CheckLevelLoaded() const
  178. {
  179.         return m_hasLoadedLevel;
  180. }
  181.  
  182. void CGameServerChannel::AddUpdateLevelLoaded(IContextEstablisher* pEst)
  183. {
  184.         if (!m_hasLoadedLevel)
  185.                 AddSetValue(pEst, eCVS_InGame, &m_hasLoadedLevel, true, "AllowChaining");
  186. }
  187.  
  188. #ifndef OLD_VOICE_SYSTEM_DEPRECATED
  189. NET_IMPLEMENT_SIMPLE_ATSYNC_MESSAGE(CGameServerChannel, MutePlayer, eNRT_ReliableUnordered, eMPF_BlocksStateChange)
  190. {
  191.         if (GetNetChannel()->IsLocal())
  192.                 return true;
  193.  
  194.         CCryAction::GetCryAction()->GetGameContext()->GetNetContext()->GetVoiceContext()->Mute(param.requestor, param.id, param.mute);
  195.  
  196.         return true;
  197. }
  198. #endif
  199.  
  200. #if defined(GAME_CHANNEL_SYNC_CLIENT_SERVER_TIME)
  201. NET_IMPLEMENT_SIMPLE_ATSYNC_MESSAGE(CGameServerChannel, SyncTimeServer, eNRT_ReliableOrdered, eMPF_NoSendDelay)
  202. {
  203.         CTimeValue value = gEnv->pTimer->GetAsyncTime();
  204.         INetSendablePtr msg = new CSimpleNetMessage<SSyncTimeClient>(SSyncTimeClient(param.id, param.clientTime, param.serverTime, value.GetValue()), CGameClientChannel::SyncTimeClient);
  205.         GetNetChannel()->AddSendable(msg, 0, NULL, NULL);
  206.  
  207.         return true;
  208. }
  209. #endif
  210.  
downloadGameServerChannel.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top