BVB Source Codes

CRYENGINE Show GameServerNub.cpp Source code

Return Download CRYENGINE: download GameServerNub.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.    - 24:8:2004   10:54 : Created by M谩rcio Martins
  11.  
  12. *************************************************************************/
  13. #include "StdAfx.h"
  14. #include "GameServerNub.h"
  15. #include "GameServerChannel.h"
  16. #include "CryAction.h"
  17. #include "GameContext.h"
  18. #include "GameRulesSystem.h"
  19.  
  20. ICVar* CGameServerNub::sv_timeout_disconnect = 0;
  21.  
  22. CGameServerNub::CGameServerNub()
  23.         : m_genId(0)
  24.         , m_pGameContext(0)
  25.         , m_maxPlayers(0)
  26.         , m_allowRemoveChannel(false)
  27. {
  28.         sv_timeout_disconnect = gEnv->pConsole->GetCVar("sv_timeout_disconnect");
  29. }
  30.  
  31. CGameServerNub::~CGameServerNub()
  32. {
  33.         // Delete server channels.
  34.         for (TServerChannelMap::iterator ch_it = m_channels.begin(); ch_it != m_channels.end(); )
  35.         {
  36.                 TServerChannelMap::iterator next = ch_it;
  37.                 ++next;
  38.                 delete ch_it->second;
  39.                 ch_it = next;
  40.         }
  41.         m_channels.clear();
  42. }
  43.  
  44. //------------------------------------------------------------------------
  45. void CGameServerNub::Release()
  46. {
  47.         delete this;
  48. }
  49.  
  50. //------------------------------------------------------------------------
  51. SCreateChannelResult CGameServerNub::CreateChannel(INetChannel* pChannel, const char* pRequest)
  52. {
  53.         CRY_ASSERT(m_maxPlayers);
  54.  
  55.         CGameServerChannel* pNewChannel;
  56.  
  57.         if (!pRequest)
  58.         {
  59.                 CRY_ASSERT(false);
  60.                 SCreateChannelResult res(eDC_GameError);
  61.                 return res;
  62.         }
  63.  
  64.         SParsedConnectionInfo info = m_pGameContext->ParseConnectionInfo(pRequest);
  65.         if (!info.allowConnect)
  66.         {
  67.                 GameWarning("Not allowed to connect to server: %s", info.errmsg.c_str());
  68.                 SCreateChannelResult res(info.cause);
  69.                 cry_strcpy(res.errorMsg, info.errmsg.c_str());
  70.                 return res;
  71.         }
  72.  
  73.         if (int(m_channels.size()) >= m_maxPlayers)
  74.         {
  75.                 SCreateChannelResult res(eDC_ServerFull);
  76.                 cry_strcpy(res.errorMsg, string().Format("Disallowing more than %d players", m_maxPlayers).c_str());
  77.                 return res;
  78.         }
  79.  
  80.         if (info.isMigrating && CCryAction::GetCryAction()->IsGameSessionMigrating())
  81.         {
  82.                 pChannel->SetMigratingChannel(true);
  83.         }
  84.  
  85.         pNewChannel = GetOnHoldChannelFor(pChannel);
  86.         if (!pNewChannel)
  87.         {
  88.                 pNewChannel = new CGameServerChannel(pChannel, m_pGameContext, this);
  89.  
  90.                 if (pChannel->GetSession() != CrySessionInvalidHandle)
  91.                 {
  92.                         // There is a valid CrySessionHandle created by the lobby so use this as the channel id
  93.                         // as it contains information that can identify the connection in the lobby.
  94.                         pNewChannel->SetChannelId(pChannel->GetSession());
  95.                 }
  96.                 else
  97.                 {
  98.                         // No valid CrySessionHandle so create an id here.
  99.                         pNewChannel->SetChannelId(++m_genId);
  100.                 }
  101.  
  102.                 if (m_channels.find(pNewChannel->GetChannelId()) != m_channels.end())
  103.                 {
  104.                         CryFatalError("CGameServerNub::CreateChannel: Trying to create channel with duplicate id %u", pNewChannel->GetChannelId());
  105.                 }
  106.  
  107.                 m_channels.insert(TServerChannelMap::value_type(pNewChannel->GetChannelId(), pNewChannel));
  108.         }
  109.         else
  110.         {
  111.                 pNewChannel->SetNetChannel(pChannel);
  112. #if !NEW_BANDWIDTH_MANAGEMENT
  113.                 pNewChannel->SetupNetChannel(pChannel);
  114. #endif // NEW_BANDWIDTH_MANAGEMENT
  115.         }
  116.  
  117.         ICVar* pPass = gEnv->pConsole->GetCVar("sv_password");
  118.         if (pPass && gEnv->bMultiplayer)
  119.         {
  120.                 pChannel->SetPassword(pPass->GetString());
  121.         }
  122.         pChannel->SetNickname(info.playerName.c_str());
  123.  
  124.         // Host migration
  125.         if (info.isMigrating && CCryAction::GetCryAction()->IsGameSessionMigrating())
  126.         {
  127.                 // Enable the game rules to find the migrating player details by channel id
  128.                 IGameFramework* pGameFramework = gEnv->pGameFramework;
  129.                 IGameRules* pGameRules = pGameFramework->GetIGameRulesSystem()->GetCurrentGameRules();
  130.  
  131.                 if (pGameRules)
  132.                 {
  133.                         EntityId playerID = pGameRules->SetChannelForMigratingPlayer(info.playerName.c_str(), pNewChannel->GetChannelId());
  134.  
  135.                         if (playerID)
  136.                         {
  137.                                 CryLog("CGameServerNub::CreateChannel() assigning actor %u '%s' to channel %u", playerID, info.playerName.c_str(), pNewChannel->GetChannelId());
  138.                                 pNewChannel->SetPlayerId(playerID);
  139.                         }
  140.                         else
  141.                         {
  142.                                 CryLog("CGameServerNub::CreateChannel() failed to assign actor '%s' to channel %u", info.playerName.c_str(), pNewChannel->GetChannelId());
  143.                         }
  144.                 }
  145.                 else
  146.                 {
  147.                         CryLog("[host migration] terminating because game rules is NULL, game session migrating %d session 0x%08x", CCryAction::GetCryAction()->IsGameSessionMigrating(), pChannel->GetSession());
  148.                         gEnv->pNetwork->TerminateHostMigration(pChannel->GetSession());
  149.                 }
  150.         }
  151.  
  152.         m_netchannels.insert(TNetServerChannelMap::value_type(pChannel, pNewChannel->GetChannelId()));
  153.  
  154.         return SCreateChannelResult(pNewChannel);
  155. }
  156.  
  157. //------------------------------------------------------------------------
  158. void CGameServerNub::Update()
  159. {
  160.         const int timeout = MAX(0, sv_timeout_disconnect->GetIVal());
  161.  
  162.         CTimeValue now = gEnv->pTimer->GetFrameStartTime();
  163.  
  164.         THoldChannelMap::iterator it = m_onhold.begin();
  165.         THoldChannelMap::iterator end = m_onhold.end();
  166.  
  167.         for (; it != end; )
  168.         {
  169.                 THoldChannelMap::iterator next = it;
  170.                 ++next;
  171.                 if ((now - it->second.time).GetSeconds() >= timeout)
  172.                         RemoveOnHoldChannel(GetChannel(it->second.channelId), false);
  173.                 it = next;
  174.         }
  175. }
  176.  
  177. void CGameServerNub::FailedActiveConnect(EDisconnectionCause cause, const char* description)
  178. {
  179.         CRY_ASSERT(false && "Shouldn't be called from here");
  180. }
  181.  
  182. //------------------------------------------------------------------------
  183. CGameServerChannel* CGameServerNub::GetChannel(uint16 channelId)
  184. {
  185.         TServerChannelMap::iterator it = m_channels.find(channelId);
  186.  
  187.         if (it != m_channels.end())
  188.                 return it->second;
  189.         return 0;
  190. }
  191.  
  192. //------------------------------------------------------------------------
  193. CGameServerChannel* CGameServerNub::GetChannel(INetChannel* pNetChannel)
  194. {
  195.         TNetServerChannelMap::iterator it = m_netchannels.find(pNetChannel);
  196.  
  197.         if (it != m_netchannels.end())
  198.                 return GetChannel(it->second);
  199.         return 0;
  200. }
  201.  
  202. //------------------------------------------------------------------------
  203. INetChannel* CGameServerNub::GetLocalChannel()
  204. {
  205.         TNetServerChannelMap::iterator it = m_netchannels.begin();
  206.         for (; it != m_netchannels.end(); ++it)
  207.         {
  208.                 if (it->first->IsLocal())
  209.                 {
  210.                         return it->first;
  211.                 }
  212.         }
  213.  
  214.         return 0;
  215. }
  216.  
  217. //------------------------------------------------------------------------
  218. uint16 CGameServerNub::GetChannelId(INetChannel* pNetChannel) const
  219. {
  220.         TNetServerChannelMap::const_iterator it = m_netchannels.find(pNetChannel);
  221.  
  222.         if (it != m_netchannels.end())
  223.                 return it->second;
  224.         return 0;
  225. }
  226.  
  227. //------------------------------------------------------------------------
  228. void CGameServerNub::RemoveChannel(uint16 channelId)
  229. {
  230.         if (stl::member_find_and_erase(m_channels, channelId))
  231.         {
  232.                 for (TNetServerChannelMap::iterator it = m_netchannels.begin(); it != m_netchannels.end(); ++it)
  233.                 {
  234.                         if (it->second == channelId)
  235.                         {
  236.                                 m_netchannels.erase(it);
  237.                                 break;
  238.                         }
  239.                 }
  240.         }
  241. }
  242.  
  243. //------------------------------------------------------------------------
  244. void CGameServerNub::SetMaxPlayers(int maxPlayers)
  245. {
  246.         CRY_ASSERT(maxPlayers);
  247.         m_maxPlayers = maxPlayers;
  248.  
  249.         if (m_maxPlayers < int(m_channels.size()))
  250.         {
  251.                 int kick = int(m_channels.size()) - m_maxPlayers;
  252.                 GameWarning("Kicking %d players due to maximum player count reduction",
  253.                             kick);
  254.                 for (TServerChannelMap::const_reverse_iterator it = m_channels.rbegin();
  255.                      kick != 0; ++it)
  256.                 {
  257.                         it->second->GetNetChannel()->Disconnect(eDC_ServerFull, "");
  258.                         kick--;
  259.                 }
  260.         }
  261. }
  262.  
  263. //------------------------------------------------------------------------
  264. bool CGameServerNub::IsPreordered(uint16 channelId)
  265. {
  266.         for (TNetServerChannelMap::const_iterator nit = m_netchannels.begin(); nit != m_netchannels.end(); ++nit)
  267.                 if (nit->second == channelId)
  268.                 {
  269.                         return nit->first->IsPreordered();
  270.                 }
  271.  
  272.         return false;
  273. }
  274.  
  275. //------------------------------------------------------------------------
  276. bool CGameServerNub::PutChannelOnHold(CGameServerChannel* pServerChannel)
  277. {
  278.         uint32 userId = pServerChannel->GetNetChannel()->GetProfileId();
  279.         if (userId)
  280.         {
  281.                 for (TNetServerChannelMap::iterator it = m_netchannels.begin(); it != m_netchannels.end(); ++it)
  282.                 {
  283.                         if (it->second == pServerChannel->GetChannelId())
  284.                         {
  285.                                 m_netchannels.erase(it);
  286.                                 break;
  287.                         }
  288.                 }
  289.  
  290.                 m_onhold.insert(THoldChannelMap::value_type(userId, SHoldChannel(pServerChannel->GetChannelId(), gEnv->pTimer->GetFrameStartTime())));
  291.  
  292.                 pServerChannel->SetNetChannel(0);
  293.                 pServerChannel->SetOnHold(true);
  294.  
  295.                 return true;
  296.         }
  297.  
  298.         return false;
  299. }
  300.  
  301. //------------------------------------------------------------------------
  302. void CGameServerNub::RemoveOnHoldChannel(CGameServerChannel* pServerChannel, bool renewed)
  303. {
  304.         for (THoldChannelMap::iterator it = m_onhold.begin(); it != m_onhold.end(); ++it)
  305.         {
  306.                 if (it->second.channelId == pServerChannel->GetChannelId())
  307.                 {
  308.                         m_onhold.erase(it);
  309.                         if (!renewed)//let gamerules know we should get rid of it...
  310.                         {
  311.                                 IGameRules* pGameRules = CCryAction::GetCryAction()->GetIGameRulesSystem()->GetCurrentGameRules();
  312.                                 pGameRules->OnClientDisconnect(pServerChannel->GetChannelId(), eDC_Timeout, "OnHold timed out", false);
  313.                         }
  314.                         break;
  315.                 }
  316.         }
  317.  
  318.         if (!renewed)
  319.         {
  320.                 stl::member_find_and_erase(m_channels, pServerChannel->GetChannelId());
  321.  
  322.                 delete pServerChannel;
  323.         }
  324. }
  325.  
  326. //------------------------------------------------------------------------
  327. void CGameServerNub::ResetOnHoldChannels()
  328. {
  329.         for (THoldChannelMap::iterator it = m_onhold.begin(); it != m_onhold.end(); ++it)
  330.         {
  331.                 IGameRules* pGameRules = CCryAction::GetCryAction()->GetIGameRulesSystem()->GetCurrentGameRules();
  332.                 pGameRules->OnClientDisconnect(it->first, eDC_Timeout, "OnHold reseted", false);
  333.                 TServerChannelMap::iterator ch_it = m_channels.find(it->first);
  334.                 if (ch_it != m_channels.end())
  335.                 {
  336.                         delete ch_it->second;
  337.                         m_channels.erase(ch_it);
  338.                 }
  339.         }
  340.         m_onhold.clear();
  341. }
  342.  
  343. //------------------------------------------------------------------------
  344. CGameServerChannel* CGameServerNub::GetOnHoldChannelFor(INetChannel* pNetChannel)
  345. {
  346.         int userId = pNetChannel->GetProfileId();
  347.         if (userId)
  348.         {
  349.                 THoldChannelMap::iterator it = m_onhold.find(userId);
  350.                 if (it != m_onhold.end())
  351.                 {
  352.                         CGameServerChannel* pChannel = GetChannel(it->second.channelId);
  353.                         RemoveOnHoldChannel(pChannel, true);
  354.                         return pChannel;
  355.                 }
  356.         }
  357.  
  358.         return 0;
  359. }
  360.  
  361. void CGameServerNub::BanPlayer(uint16 channelId, const char* reason)
  362. {
  363.         int userId = 0;
  364.         int ip = 0;
  365.         for (TNetServerChannelMap::const_iterator nit = m_netchannels.begin(); nit != m_netchannels.end(); ++nit)
  366.                 if (nit->second == channelId)
  367.                 {
  368.                         userId = nit->first->GetProfileId();
  369.                         nit->first->Disconnect(eDC_Kicked, reason);
  370.                         break;
  371.                 }
  372.         int timeout = 30;
  373.         if (ICVar* pV = gEnv->pConsole->GetCVar("ban_timeout"))
  374.                 timeout = pV->GetIVal();
  375.         if (userId)
  376.         {
  377.                 m_banned.push_back(SBannedPlayer(userId, 0, gEnv->pTimer->GetFrameStartTime() + CTimeValue(timeout * 60.0f)));
  378.         }
  379.         else if (ip)
  380.         {
  381.                 m_banned.push_back(SBannedPlayer(0, ip, gEnv->pTimer->GetFrameStartTime() + CTimeValue(timeout * 60.0f)));
  382.         }
  383. }
  384.  
  385. bool CGameServerNub::CheckBanned(INetChannel* pNetChannel)
  386. {
  387.         int userId = pNetChannel->GetProfileId();
  388.         if (!userId)
  389.                 return false;
  390.         for (int i = 0; i < m_banned.size(); ++i)
  391.         {
  392.                 if (m_banned[i].profileId == userId)
  393.                 {
  394.                         if (m_banned[i].time < gEnv->pTimer->GetFrameStartTime()) // ban timed out
  395.                         {
  396.                                 m_banned.erase(m_banned.begin() + i);
  397.                                 return false;
  398.                         }
  399.                         else
  400.                         {
  401.                                 pNetChannel->Disconnect(eDC_Banned, "You're banned from server.");
  402.                                 return true;
  403.                         }
  404.                 }
  405.         }
  406.         return false;
  407. }
  408.  
  409. void CGameServerNub::BannedStatus()
  410. {
  411.         CryLogAlways("-----------------------------------------");
  412.         CryLogAlways("Banned players : ");
  413.         for (int i = 0; i < m_banned.size(); ++i)
  414.         {
  415.                 if (m_banned[i].time < gEnv->pTimer->GetFrameStartTime())
  416.                 {
  417.                         m_banned.erase(m_banned.begin() + i);
  418.                         --i;
  419.                 }
  420.                 else
  421.                 {
  422.                         int left = int((m_banned[i].time - gEnv->pTimer->GetFrameStartTime()).GetSeconds() + 0.5f);
  423.                         CryLogAlways("profile : %d, time left : %d:%02d", m_banned[i].profileId, left / 60, left % 60);
  424.                 }
  425.         }
  426.         CryLogAlways("-----------------------------------------");
  427. }
  428.  
  429. void CGameServerNub::UnbanPlayer(int profileId)
  430. {
  431.         for (int i = 0; i < m_banned.size(); ++i)
  432.         {
  433.                 if (m_banned[i].profileId == profileId)
  434.                 {
  435.                         m_banned.erase(m_banned.begin() + i);
  436.                         break;
  437.                 }
  438.         }
  439. }
  440.  
  441. //------------------------------------------------------------------------
  442. void CGameServerNub::GetMemoryUsage(ICrySizer* s) const
  443. {
  444.         s->Add(*this);
  445.         s->AddObject(m_channels);
  446.         //s->AddObject(m_netchannels);
  447. }
  448.  
downloadGameServerNub.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