BVB Source Codes

CRYENGINE Show GameQueryListener.cpp Source code

Return Download CRYENGINE: download GameQueryListener.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 listener looks for running servers in the LAN and
  8.    is keeping a list of all available servers.
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 19/06/2006   : Implemented by Jan M眉ller
  12. *************************************************************************/
  13.  
  14. #include "StdAfx.h"
  15. #include "GameQueryListener.h"
  16. #include "CryAction.h"
  17.  
  18. CGameQueryListener::CGameQueryListener()
  19.         : m_pNetListener(nullptr)
  20.         , m_iServers(50)
  21. {
  22.         for (int iServer = 0; iServer < m_iServers; iServer++)
  23.         {
  24.                 m_astrServers[iServer] = new char[256];
  25.         }
  26. }
  27.  
  28. CGameQueryListener::~CGameQueryListener()
  29. {
  30.         for (int iServer = 0; iServer < m_iServers; iServer++)
  31.         {
  32.                 delete[] m_astrServers[iServer];
  33.         }
  34. }
  35.  
  36. void CGameQueryListener::Update()
  37. {
  38.         CleanUpServers();
  39. }
  40.  
  41. void CGameQueryListener::AddServer(const char* description, const char* target, const char* additionalText, uint32 ping)
  42. {
  43.         SGameServer server(target, description, additionalText, ping);
  44.         //compare messages in showcase and increment if fitting
  45.         std::vector<SGameServer>::iterator sIT;
  46.         for (sIT = m_servers.begin(); sIT != m_servers.end(); ++sIT)
  47.         {
  48.                 if ((*sIT).Compare(server))
  49.                 {
  50.                         (*sIT).Inc(server);
  51.                         return;
  52.                 }
  53.         }
  54.  
  55.         //add server to list
  56.         m_servers.push_back(server);
  57.  
  58.         //ping the servers when they are added ...
  59.         ILanQueryListener* pLQL = static_cast<ILanQueryListener*>(m_pNetListener);
  60.         pLQL->SendPingTo(server.m_target.c_str());
  61. }
  62.  
  63. void CGameQueryListener::RemoveServer(string address)
  64. {
  65.         std::vector<SGameServer>::iterator it = m_servers.begin();
  66.         for (; it != m_servers.end(); ++it)
  67.         {
  68.                 if ((*it).m_target.compare(address) == 0)
  69.                 {
  70.                         m_servers.erase(it);
  71.                         return;
  72.                 }
  73.         }
  74. }
  75.  
  76. void CGameQueryListener::OnReceiveGameState(const char* fromAddress, XmlNodeRef xmlData)
  77. {
  78.         CRY_ASSERT(m_pNetListener);
  79.         CryLogAlways("Game running at: %s", fromAddress);
  80.         CryLogAlways("%s", xmlData->getXML().c_str());
  81. }
  82.  
  83. void CGameQueryListener::Release()
  84. {
  85.         delete this;
  86. }
  87.  
  88. void CGameQueryListener::SetNetListener(INetQueryListener* pListener)
  89. {
  90.         CRY_ASSERT(!m_pNetListener);
  91.         m_pNetListener = pListener;
  92.         CRY_ASSERT(m_pNetListener);
  93. }
  94.  
  95. void CGameQueryListener::Complete()
  96. {
  97.         CRY_ASSERT(m_pNetListener);
  98.         m_pNetListener->DeleteNetQueryListener();
  99. }
  100.  
  101. void CGameQueryListener::AddPong(string address, uint32 ping)
  102. {
  103.         std::vector<SGameServer>::iterator it = m_servers.begin();
  104.         for (; it != m_servers.end(); ++it)
  105.         {
  106.                 if ((*it).m_target.compare(address) == 0)
  107.                 {
  108.                         (*it).IncPing(ping);
  109.                         break;
  110.                 }
  111.         }
  112. }
  113.  
  114. void CGameQueryListener::GetServer(int number, char** server, char** data, int& ping)
  115. {
  116.         FUNCTION_PROFILER(GetISystem(), PROFILE_NETWORK);
  117.         if (number >= m_servers.size())
  118.         {
  119.                 server = NULL;
  120.                 return;
  121.         }
  122.  
  123.         //number = m_servers.size()-1 - number; //list is slowest to fastest ...
  124.  
  125.         *server = (char*)(m_servers[number].m_target.c_str());
  126.         *data = (char*)(m_servers[number].m_data.c_str());
  127.         ping = m_servers[number].m_ping;
  128. }
  129.  
  130. void CGameQueryListener::GetCurrentServers(char*** pastrServers, int& o_amount)
  131. {
  132.         FUNCTION_PROFILER(GetISystem(), PROFILE_NETWORK);
  133.         if (m_servers.size() == 0)
  134.         {
  135.                 o_amount = 0;
  136.                 return;
  137.         }
  138.         o_amount = m_servers.size();
  139.  
  140.         if (o_amount > m_iServers)// || o_amount < 0.1f*m_iServers)
  141.         {
  142.                 for (int iServer = 0; iServer < m_iServers; iServer++)
  143.                 {
  144.                         delete[] m_astrServers[iServer];
  145.                 }
  146.                 m_iServers = std::max(o_amount, int(120));
  147.                 for (int iServer = 0; iServer < m_iServers; iServer++)
  148.                 {
  149.                         m_astrServers[iServer] = new char[256];
  150.                 }
  151.         }
  152.  
  153.         //servers = new char*[m_servers.size()];
  154.         *pastrServers = m_astrServers;
  155.  
  156.         std::vector<SGameServer>::iterator it = m_servers.begin();
  157.         for (int s = 0; it != m_servers.end(); ++it, ++s)    //copy server address to return-vector
  158.         {
  159.                 strcpy(m_astrServers[s], (*it).m_target.c_str());
  160.         }
  161. }
  162.  
  163. void CGameQueryListener::CleanUpServers()
  164. {
  165.         if (m_servers.size() == 0)
  166.                 return;
  167.  
  168.         CTimeValue now = gEnv->pTimer->GetFrameStartTime();
  169.  
  170.         std::vector<SGameServer>::iterator it;
  171.         //kill old servers
  172.         for (int i = 0; i < m_servers.size(); ++i)
  173.         {
  174.                 uint32 seconds = (uint32)(now.GetSeconds() - m_servers[i].m_lastTime.GetSeconds());
  175.                 if (!seconds)
  176.                         continue;
  177.                 if (seconds > 10)
  178.                 {
  179.                         it = m_servers.begin();
  180.                         for (int inc = 0; inc < i; ++inc)
  181.                                 ++it;
  182.                         m_servers.erase(it);
  183.                         --i;
  184.                 }
  185.         }
  186. }
  187.  
  188. const char* CGameQueryListener::GetServerData(const char* server, int& o_ping)
  189. {
  190.         o_ping = -1;
  191.  
  192.         std::vector<SGameServer>::iterator it = m_servers.begin();
  193.         for (; it != m_servers.end(); ++it)
  194.         {
  195.                 if ((*it).m_target.find(string(server)) != string::npos)
  196.                 {
  197.                         o_ping = (*it).m_ping;
  198.                         return (*it).m_data.c_str();
  199.                 }
  200.         }
  201.         return NULL;
  202. }
  203.  
  204. void CGameQueryListener::RefreshPings()
  205. {
  206.         FUNCTION_PROFILER(GetISystem(), PROFILE_NETWORK);
  207.  
  208.         std::vector<SGameServer>::iterator it = m_servers.begin();
  209.         ILanQueryListener* pLQL = static_cast<ILanQueryListener*>(m_pNetListener);
  210.         for (; it != m_servers.end(); ++it)
  211.                 pLQL->SendPingTo((*it).m_target.c_str());
  212. }
  213.  
  214. CGameQueryListener::SGameServer* CGameQueryListener::FindServer(const char* server)
  215. {
  216.         std::vector<SGameServer>::iterator it = m_servers.begin();
  217.         for (; it != m_servers.end(); ++it)
  218.                 if (!strcmp((*it).m_target, server))
  219.                         return &(*it);
  220.         return NULL;
  221. }
  222.  
  223. void CGameQueryListener::ConnectToServer(const char* server)
  224. {
  225.         //first check the version of the server ...
  226.         char myVersion[32];
  227.         GetISystem()->GetProductVersion().ToShortString(myVersion);
  228.         string version(myVersion);
  229.  
  230.         SGameServer* targetServer = FindServer(server);
  231.         if (!targetServer)
  232.         {
  233.                 CryWarning(VALIDATOR_MODULE_NETWORK, VALIDATOR_WARNING, "Selected server not found in list!");
  234.                 return;
  235.         }
  236.  
  237.         if (version.compare(targetServer->GetServerGameVersion()) != 0)
  238.         {
  239.                 CryWarning(VALIDATOR_MODULE_NETWORK, VALIDATOR_WARNING, "Game versions differ - not connecting!");
  240.                 return;
  241.         }
  242.  
  243.         string addr(server);
  244.         string port;
  245.         size_t pos = addr.find(":");
  246.         if (pos != string::npos) //space for port
  247.         {
  248.                 port = addr.substr(pos + 1, addr.size() - pos);
  249.                 addr.erase(pos, addr.size() - pos);
  250.         }
  251.  
  252.         IConsole* pConsole = gEnv->pConsole;
  253.  
  254.         pConsole->GetCVar("cl_serveraddr")->Set(addr.c_str());
  255.         if (port.size() > 0)
  256.                 pConsole->GetCVar("cl_serverport")->Set(port.c_str());
  257.  
  258.         string tempHost = pConsole->GetCVar("cl_serveraddr")->GetString();
  259.  
  260.         SGameStartParams params;  //this would connect to a server
  261.         params.flags = eGSF_Client;
  262.         params.hostname = tempHost.c_str();
  263.         params.pContextParams = NULL;
  264.         params.port = pConsole->GetCVar("cl_serverport")->GetIVal();
  265.  
  266.         CCryAction* action = (CCryAction*) (gEnv->pGameFramework);
  267.         if (action)
  268.         {
  269.                 gEnv->pConsole->ExecuteString("net_lanbrowser 0");
  270.                 action->StartGameContext(&params);
  271.         }
  272. }
  273.  
  274. void CGameQueryListener::GetValuesFromData(char* strData, SServerData* pServerData)
  275. {
  276.         string strSTD = strData;
  277.  
  278.         // Map
  279.         int iFirst = strSTD.find_first_of("\"") + 1;
  280.         int iSecond = strSTD.find_first_of("\"", iFirst);
  281.         pServerData->strMap = strSTD.substr(iFirst, iSecond - iFirst);
  282.  
  283.         // Num players
  284.         iFirst = strSTD.find_first_of("\"", iSecond + 1) + 1;
  285.         iSecond = strSTD.find_first_of("\"", iFirst);
  286.         pServerData->iNumPlayers = atoi(strSTD.substr(iFirst, iSecond - iFirst));
  287.  
  288.         // GameMode
  289.         iFirst = strSTD.find_first_of("\"", iSecond + 1) + 1;
  290.         iSecond = strSTD.find_first_of("\"", iFirst);
  291.         pServerData->strMode = strSTD.substr(iFirst, iSecond - iFirst);
  292.  
  293.         // Version
  294.         iFirst = strSTD.find_first_of("\"", iSecond + 1) + 1;
  295.         iSecond = strSTD.find_first_of("\"", iFirst);
  296.         pServerData->strVersion = strSTD.substr(iFirst, iSecond - iFirst);
  297.  
  298.         // Max players
  299.         iFirst = strSTD.find_first_of("\"", iSecond + 1) + 1;
  300.         iSecond = strSTD.find_first_of("\"", iFirst);
  301.         pServerData->iMaxPlayers = atoi(strSTD.substr(iFirst, iSecond - iFirst));
  302. }
  303.  
  304. void CGameQueryListener::GetMemoryStatistics(ICrySizer* s)
  305. {
  306.         s->Add(*this);
  307.         s->AddContainer(m_servers);
  308.         for (size_t i = 0; i < m_servers.size(); i++)
  309.         {
  310.                 s->Add(m_servers[i].m_target);
  311.                 s->Add(m_servers[i].m_description);
  312.                 s->Add(m_servers[i].m_data);
  313.         }
  314.         s->AddObject(m_astrServers, m_iServers * 256);
  315. }
  316.  
downloadGameQueryListener.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