BVB Source Codes

CRYENGINE Show GameObjectDispatch.cpp Source code

Return Download CRYENGINE: download GameObjectDispatch.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.    Description: This file implements dispatching RMI calls in C++ to
  8.                 relevant game object code
  9.  
  10.    -------------------------------------------------------------------------
  11.    History:
  12.     - 24 Oct 2005 : Created by Craig Tiller
  13.  
  14. *************************************************************************/
  15.  
  16. #include "StdAfx.h"
  17. #include "GameObject.h"
  18. #include "GameObjectDispatch.h"
  19. #include "Network/GameContext.h"
  20.  
  21. CGameObjectDispatch* CGameObjectDispatch::m_pGOD = NULL;
  22.  
  23. CGameObjectDispatch::CGameObjectDispatch() : m_bSafety(false)
  24. {
  25.         CRY_ASSERT(m_pGOD == NULL);
  26.         m_pGOD = this;
  27. }
  28.  
  29. CGameObjectDispatch::~CGameObjectDispatch()
  30. {
  31.         m_pGOD = NULL;
  32. }
  33.  
  34. void CGameObjectDispatch::RegisterInterface(SGameObjectExtensionRMI* pMessages, size_t nCount)
  35. {
  36.         if (nCount == 0)
  37.                 return;
  38.  
  39.         // watch out for duplicate registrations...
  40.         for (size_t i = 0; i < m_messages.size(); i++)
  41.                 if (m_messages[i]->pBase == pMessages)
  42.                         return;
  43.         // messages already registered
  44.  
  45.         if (m_bSafety)
  46.         {
  47.                 CryFatalError("CGameObjectDispatch::RegisterInterface: occurs too late");
  48.                 return;
  49.         }
  50.  
  51.         // actually build protocol definitions for the messages
  52.         for (size_t i = 0; i < nCount; i++)
  53.         {
  54.                 std::vector<SNetMessageDef>* pMsgDef = pMessages[i].isServerCall ? &m_serverCalls : &m_clientCalls;
  55.  
  56.                 SNetMessageDef md;
  57.                 md.description = pMessages[i].description;
  58.                 md.handler = Trampoline;
  59.                 md.nUser = m_messages.size();
  60.                 switch (pMessages[i].attach)
  61.                 {
  62.                 case eRAT_NoAttach:     // Intentional fall-through
  63.                 case eRAT_Urgent:       // Intentional fall-through
  64.                 case eRAT_Independent:
  65.                         // NoAttach, Urgent and Independent get the reliability specified at RMI declaration
  66.                         md.reliability = pMessages[i].reliability;
  67.                         break;
  68.                 default:
  69.                         // Everything else's reliability/orderedness is based on the object they're attached to
  70.                         md.reliability = eNRT_UnreliableOrdered;
  71.                 }
  72.                 md.parallelFlags = eMPF_BlocksStateChange | eMPF_DiscardIfNoEntity;
  73.                 if (pMessages[i].lowDelay)
  74.                         md.parallelFlags |= eMPF_NoSendDelay;
  75.  
  76.                 pMsgDef->push_back(md);
  77.                 m_messages.push_back(pMessages + i);
  78.         }
  79. }
  80.  
  81. TNetMessageCallbackResult CGameObjectDispatch::Trampoline(
  82.   uint32 userId,
  83.   INetMessageSink* handler,
  84.   TSerialize serialize,
  85.   uint32 curSeq,
  86.   uint32 oldSeq,
  87.   uint32 timeFraction32,
  88.   EntityId* pEntityId,
  89.   INetChannel* pNetChannel)
  90. {
  91.         const SGameObjectExtensionRMI* pRMI = m_pGOD->m_messages[userId];
  92.         IRMIAtSyncItem* pItem = (IRMIAtSyncItem*) pRMI->decoder(serialize, pEntityId, pNetChannel);
  93.         if (pItem)
  94.         {
  95.                 CCryAction* pCryAction = CCryAction::GetCryAction();
  96.                 CGameContext* pGameContext = pCryAction ? pCryAction->GetGameContext() : NULL;
  97.                 INetContext* pNetContext = pGameContext ? pGameContext->GetNetContext() : NULL;
  98.  
  99.                 if (pNetContext)
  100.                         pNetContext->LogCppRMI(*pEntityId, pItem);
  101.         }
  102.         return TNetMessageCallbackResult(pItem != 0, pItem);
  103. }
  104.  
  105. void CGameObjectDispatch::LockSafety()
  106. {
  107.         if (m_bSafety)
  108.                 return;
  109.  
  110.         for (size_t i = 0; i < m_serverCalls.size(); i++)
  111.                 m_messages[m_serverCalls[i].nUser]->pMsgDef = &m_serverCalls[i];
  112.         for (size_t i = 0; i < m_clientCalls.size(); i++)
  113.                 m_messages[m_clientCalls[i].nUser]->pMsgDef = &m_clientCalls[i];
  114.  
  115.         m_bSafety = true;
  116. }
  117.  
  118. bool CGameObjectDispatch::CProtocolDef::IsServer()
  119. {
  120.         return this == &m_pGOD->m_serverDef;
  121. }
  122.  
  123. void CGameObjectDispatch::CProtocolDef::DefineProtocol(IProtocolBuilder* pBuilder)
  124. {
  125.         m_pGOD->LockSafety();
  126.  
  127.         std::vector<SNetMessageDef>* pSending = IsServer() ? &m_pGOD->m_clientCalls : &m_pGOD->m_serverCalls;
  128.         std::vector<SNetMessageDef>* pReceiving = IsServer() ? &m_pGOD->m_serverCalls : &m_pGOD->m_clientCalls;
  129.  
  130.         SNetProtocolDef protoSending;
  131.         protoSending.nMessages = pSending->size();
  132.         protoSending.vMessages = protoSending.nMessages ? &*pSending->begin() : 0;
  133.         SNetProtocolDef protoReceiving;
  134.         protoReceiving.nMessages = pReceiving->size();
  135.         protoReceiving.vMessages = protoReceiving.nMessages ? &*pReceiving->begin() : 0;
  136.  
  137.         pBuilder->AddMessageSink(this, protoSending, protoReceiving);
  138. }
  139.  
  140. bool CGameObjectDispatch::CProtocolDef::HasDef(const SNetMessageDef* pDef)
  141. {
  142.         return
  143.           pDef >= &*m_pGOD->m_serverCalls.begin() && pDef < &*m_pGOD->m_serverCalls.end() ||
  144.           pDef >= &*m_pGOD->m_clientCalls.begin() && pDef < &*m_pGOD->m_clientCalls.end();
  145. }
  146.  
  147. void CGameObjectDispatch::GetMemoryUsage(ICrySizer* s) const
  148. {
  149.         //s->AddObject(m_messages);
  150.         //s->AddObject(m_clientCalls);
  151.         //s->AddObject(m_serverCalls);
  152. }
  153.  
downloadGameObjectDispatch.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