Counter Strike : Global Offensive Source Code
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.

157 lines
3.2 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef NETCONSOLE_H
  9. #define NETCONSOLE_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #ifdef _GAMECONSOLE
  14. #define SUPPORT_NET_CONSOLE 0
  15. #else
  16. #define SUPPORT_NET_CONSOLE 1
  17. #endif
  18. #if SUPPORT_NET_CONSOLE
  19. #include "tier2/socketcreator.h"
  20. #define MAX_NETCONSOLE_INPUT_LEN 2048
  21. class CConnectedNetConsoleData
  22. {
  23. public:
  24. SocketHandle_t m_hSocket;
  25. int m_nCharsInCommandBuffer;
  26. char m_pszInputCommandBuffer[MAX_NETCONSOLE_INPUT_LEN];
  27. bool m_bAuthorized; // for password protection
  28. bool m_bInputOnly; // if set, don't send spew to this netconsole
  29. CConnectedNetConsoleData( SocketHandle_t hSocket = -1 )
  30. {
  31. m_nCharsInCommandBuffer = 0;
  32. m_bAuthorized = false;
  33. m_hSocket = hSocket;
  34. m_bInputOnly = false;
  35. }
  36. };
  37. class CNetConsoleMgr : public ISocketCreatorListener
  38. {
  39. CSocketCreator m_Socket;
  40. char m_pPassword[256]; // if set
  41. netadr_t m_Address;
  42. bool m_bActive;
  43. bool m_bPasswordProtected;
  44. CConnectedNetConsoleData m_ParentConnection;
  45. bool ShouldAcceptSocket( SocketHandle_t hSocket, const netadr_t &netAdr )
  46. {
  47. return true;
  48. }
  49. void OnSocketAccepted( SocketHandle_t hSocket, const netadr_t &netAdr, void** ppData )
  50. {
  51. CConnectedNetConsoleData *pData = new CConnectedNetConsoleData( hSocket );
  52. if ( ! m_bPasswordProtected )
  53. {
  54. pData->m_bAuthorized = true; // no password, auto-auth
  55. }
  56. *ppData = pData;
  57. }
  58. void OnSocketClosed( SocketHandle_t hSocket, const netadr_t &netAdr, void* pData )
  59. {
  60. if ( pData )
  61. delete (CConnectedNetConsoleData*)pData;
  62. }
  63. void Execute( CConnectedNetConsoleData *pData );
  64. void HandleInputChars( char const *pChars, int nNumChars, CConnectedNetConsoleData *pData );
  65. int NumConnectedSockets( void )
  66. {
  67. int nRet = m_Socket.GetAcceptedSocketCount();
  68. if ( m_ParentConnection.m_hSocket != -1 )
  69. {
  70. ++nRet;
  71. }
  72. return nRet;
  73. }
  74. CConnectedNetConsoleData *GetConnection( int nIdx )
  75. {
  76. if ( m_ParentConnection.m_hSocket != -1 )
  77. {
  78. if ( nIdx == 0 )
  79. {
  80. return &m_ParentConnection;
  81. }
  82. nIdx--;
  83. }
  84. CConnectedNetConsoleData *pData = ( CConnectedNetConsoleData * ) m_Socket.GetAcceptedSocketData( nIdx );
  85. return pData;
  86. }
  87. void CloseConnection( int nIdx )
  88. {
  89. if ( m_ParentConnection.m_hSocket != -1 )
  90. {
  91. if ( nIdx == 0 )
  92. {
  93. return; // don't really close
  94. }
  95. nIdx--;
  96. }
  97. m_Socket.CloseAcceptedSocket( nIdx );
  98. }
  99. public:
  100. void RunFrame( void );
  101. void SendStringToNetConsoles( char const *pString );
  102. CNetConsoleMgr( void ); // initialize from command line arguments
  103. CNetConsoleMgr( int nPort ); // init from expicity port number
  104. bool IsActive() const { return m_bActive; }
  105. const netadr_t& GetAddress() const { return m_Address; }
  106. };
  107. extern CNetConsoleMgr *g_pNetConsoleMgr;
  108. #endif // support_netconsole
  109. FORCEINLINE void SendStringToNetConsoles( char const *pMsg )
  110. {
  111. #if SUPPORT_NET_CONSOLE
  112. if ( g_pNetConsoleMgr )
  113. g_pNetConsoleMgr->SendStringToNetConsoles( pMsg );
  114. #endif
  115. }
  116. #if SUPPORT_NET_CONSOLE
  117. void InitNetConsole( void );
  118. #else
  119. FORCEINLINE void InitNetConsole( void )
  120. {
  121. }
  122. #endif
  123. #endif // if NETCONSOLE