Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
2.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: defines a RCon class used to send rcon commands to remote servers
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "ServerPing.h"
  8. #include "Iresponse.h"
  9. #include "ServerPingMsgHandler.h"
  10. #include "Socket.h"
  11. #include "proto_oob.h"
  12. extern void v_strncpy(char *dest, const char *src, int bufsize);
  13. CServerPing::CServerPing(IResponse *target,serveritem_t &server) {
  14. memcpy(&m_Server, &server,sizeof(serveritem_t));
  15. m_pResponseTarget=target;
  16. m_bIsRefreshing=false;
  17. int bytecode = S2A_INFO_DETAILED;
  18. m_pQuery = new CSocket("internet server ping", -1);
  19. m_pQuery->AddMessageHandler(new CServerPingMsgHandlerDetails(this, CMsgHandler::MSGHANDLER_ALL, &bytecode));
  20. m_fSendTime=0;
  21. }
  22. CServerPing::~CServerPing() {
  23. delete m_pQuery;
  24. }
  25. //-----------------------------------------------------------------------------
  26. // Purpose: sends a status query packet to a single server
  27. //-----------------------------------------------------------------------------
  28. void CServerPing::Query()
  29. {
  30. CMsgBuffer *buffer = m_pQuery->GetSendBuffer();
  31. assert( buffer );
  32. if ( !buffer )
  33. {
  34. return;
  35. }
  36. m_bIsRefreshing=true;
  37. m_bRefreshed=false;
  38. netadr_t adr;
  39. adr.ip[0] = m_Server.ip[0];
  40. adr.ip[1] = m_Server.ip[1];
  41. adr.ip[2] = m_Server.ip[2];
  42. adr.ip[3] = m_Server.ip[3];
  43. adr.port = (m_Server.port & 0xff) << 8 | (m_Server.port & 0xff00) >> 8;
  44. adr.type = NA_IP;
  45. // Create query message
  46. buffer->Clear();
  47. // Write control sequence
  48. buffer->WriteLong(0xffffffff);
  49. // Write query string
  50. buffer->WriteString("ping");
  51. // Sendmessage
  52. m_pQuery->SendMessage( &adr );
  53. m_fSendTime = CSocket::GetClock();
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose:
  57. //-----------------------------------------------------------------------------
  58. void CServerPing::RunFrame()
  59. {
  60. /* float curtime = CSocket::GetClock();
  61. if(m_fSendTime!=0 && (curtime-m_fSendTime)> 5.0f) // 10 seconds timeout
  62. {
  63. m_fSendTime = 0;
  64. m_pResponseTarget->ServerFailedToRespond();
  65. }
  66. */
  67. if (m_pQuery)
  68. {
  69. m_pQuery->Frame();
  70. }
  71. }
  72. void CServerPing::UpdateServer(float recvTime)
  73. {
  74. //A2A_ACK
  75. int ping = (int)((recvTime - m_fSendTime) * 1000);
  76. if (ping > 10000 || ping < 0)
  77. {
  78. // make sure ping is valid
  79. ping = 1200;
  80. }
  81. m_Server.ping = ping;
  82. m_bIsRefreshing=false;
  83. m_bRefreshed=true;
  84. m_fSendTime=0;
  85. // notify the UI of the new server info
  86. m_pResponseTarget->ServerResponded();
  87. }
  88. void CServerPing::Refresh()
  89. {
  90. Query();
  91. }
  92. bool CServerPing::IsRefreshing()
  93. {
  94. return m_bIsRefreshing;
  95. }
  96. serveritem_t &CServerPing::GetServer()
  97. {
  98. return m_Server;
  99. }
  100. bool CServerPing::Refreshed()
  101. {
  102. bool val = m_bRefreshed;
  103. m_bRefreshed=false;
  104. return val;
  105. }