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.

180 lines
6.4 KiB

  1. //====== Copyright (c), Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef GCJOB_H
  7. #define GCJOB_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "gcwebapikey.h"
  12. namespace GCSDK
  13. {
  14. //-----------------------------------------------------------------------------
  15. // Purpose: handles a network message job from the client
  16. //-----------------------------------------------------------------------------
  17. class CGCJob : public CJob
  18. {
  19. public:
  20. CGCJob( CGCBase *pGC ) : CJob( pGC->GetJobMgr() ), m_pGC( pGC ), m_cHeartbeatsBeforeTimeout( k_cJobHeartbeatsBeforeTimeoutDefault ) {}
  21. // all GC jobs must implement one of these
  22. virtual bool BYieldingRunGCJob( IMsgNetPacket *pNetPacket ) { return false; }
  23. virtual bool BYieldingRunGCJob() { return false; }
  24. virtual EServerType GetServerType() { return k_EServerTypeGC; }
  25. bool BYldSendMessageAndGetReply( CSteamID &steamIDTarget, CGCMsgBase &msgOut, uint nTimeoutSec, CGCMsgBase *pMsgIn, MsgType_t eMsg );
  26. bool BYldSendMessageAndGetReply( CSteamID &steamIDTarget, CGCMsgBase &msgOut, uint nTimeoutSec, IMsgNetPacket **ppNetPacket );
  27. bool BYldSendMessageAndGetReply( CSteamID &steamIDTarget, IProtoBufMsg &msgOut, uint nTimeoutSec, IProtoBufMsg *pMsgIn, MsgType_t eMsg );
  28. bool BYldSendMessageAndGetReply( CSteamID &steamIDTarget, IProtoBufMsg &msgOut, uint nTimeoutSec, IMsgNetPacket **ppNetPacket );
  29. virtual uint32 CHeartbeatsBeforeTimeout() { return m_cHeartbeatsBeforeTimeout; }
  30. void SetJobTimeout( uint nTimeoutSec ) { m_cHeartbeatsBeforeTimeout = 1 + ((nTimeoutSec * k_nMillion) / k_cMicroSecJobHeartbeat); }
  31. protected:
  32. CGCBase *m_pGC;
  33. private:
  34. virtual bool BYieldingRunJobFromMsg( IMsgNetPacket *pNetPacket )
  35. {
  36. return BYieldingRunGCJob( pNetPacket );
  37. }
  38. virtual bool BYieldingRunJob( void *pvStartParam )
  39. {
  40. return BYieldingRunGCJob();
  41. }
  42. uint32 m_cHeartbeatsBeforeTimeout;
  43. };
  44. //-----------------------------------------------------------------------------
  45. // CGCWGJob - A job invoked from forwarded WG messages
  46. //-----------------------------------------------------------------------------
  47. class CGCWGJob : public CGCJob
  48. {
  49. public:
  50. CGCWGJob( CGCBase *pGCBase );
  51. ~CGCWGJob();
  52. bool BYieldingRunGCJob( IMsgNetPacket * pNetPacket ); // invokes BYieldingRunJobFromRequest
  53. virtual bool BYieldingRunJobFromRequest( KeyValues *pkvRequest, KeyValues *pkvResponse ) = 0;
  54. virtual bool BVerifyParams( const CGCMsg<MsgGCWGRequest_t> & msg, KeyValues *pkvRequest, const WebApiFunc_t * pWebApiFunc );
  55. void SetWebApiFunc( const WebApiFunc_t * pWebApiFunc ) { m_pWebApiFunc = pWebApiFunc; }
  56. void SetErrorMessage( KeyValues *pkvErr, const char *pchErrorMsg, int32 nResult );
  57. protected:
  58. KeyValues *m_pkvResponse;
  59. CUtlBuffer m_bufRequest; // packed binary form of the request
  60. const WebApiFunc_t * m_pWebApiFunc;
  61. // requester auth info, like WGRequestContext
  62. CSteamID m_steamID;
  63. };
  64. //-----------------------------------------------------------------------------
  65. // CGCJobVerifySession - A job that asks steam if a given user is still connected
  66. // and cleans up the session if the user is gone
  67. //-----------------------------------------------------------------------------
  68. class CGCJobVerifySession : public CGCJob
  69. {
  70. public:
  71. CGCJobVerifySession( CGCBase *pGC, const CSteamID &steamID ) : CGCJob( pGC ), m_steamID( steamID ) { }
  72. virtual bool BYieldingRunGCJob();
  73. private:
  74. CSteamID m_steamID;
  75. };
  76. //-----------------------------------------------------------------------------
  77. // CWebAPIJob - A job invoked from a forwarded WebAPI request
  78. //-----------------------------------------------------------------------------
  79. class CWebAPIJob : public CGCJob
  80. {
  81. public:
  82. CWebAPIJob( CGCBase *pGC, EWebAPIOutputFormat eDefaultOutputFormat = k_EWebAPIOutputFormat_JSON );
  83. ~CWebAPIJob();
  84. // Called by jobmgr, and then invokes appropriate run function for specific API version requested
  85. bool BYieldingRunJobFromMsg( IMsgNetPacket * pNetPacket );
  86. // Implemented by each individual WebAPI job, should be declared using BEGIN_WEBAPI_JOB_VERSION_ADAPTERS and related macros.
  87. virtual bool BYieldingRunJobFromAPIRequest( const char *pchInterface, const char *pchMethod, uint32 unVersion, CHTTPRequest *pRequest, CHTTPResponse *pResponse, CWebAPIResponse *pWebAPIResponse ) = 0;
  88. protected:
  89. static void AddLocalizedString( CWebAPIValues *pOutDefn, const char *pchFieldName, const char *pchKeyName, ELanguage eLang, bool bReturnTokenIfNotFound = true );
  90. CHTTPRequest *m_pRequest;
  91. CHTTPResponse *m_pResponse;
  92. CWebAPIKey m_webAPIKey;
  93. EWebAPIOutputFormat m_eDefaultOutputFormat;
  94. };
  95. #define BEGIN_WEBAPI_JOB_VERSION_ADAPTERS( interface, method ) \
  96. bool BYieldingRunJobFromAPIRequest( const char *pchInterface, const char *pchMethod, uint32 unVersion, CHTTPRequest *pRequest, CHTTPResponse *pResponse, CWebAPIResponse *pWebAPIResponse ) \
  97. { \
  98. if ( Q_strnicmp( pchInterface, interface, Q_strlen( interface ) ) != 0 ) \
  99. { \
  100. AssertMsg2( false, "WebAPIJob recieved request for unexpected interface (got %s, expected %s)!", pchInterface, interface ); \
  101. pResponse->SetStatusCode( k_EHTTPStatusCode500InternalServerError ); \
  102. return false; \
  103. } \
  104. \
  105. if ( Q_stricmp( pchMethod, method ) != 0 ) \
  106. { \
  107. AssertMsg2( false, "WebAPIJob received request fo unexpected method (got %s, expected %s)!", pchMethod, method ); \
  108. pResponse->SetStatusCode( k_EHTTPStatusCode500InternalServerError ); \
  109. return false; \
  110. } \
  111. \
  112. \
  113. bool bFoundVersion = false; \
  114. bool bResult = false; \
  115. switch( unVersion ) \
  116. {
  117. #define WEBAPI_JOB_VERSION_ADAPTER( version, funcname ) \
  118. case version: \
  119. bFoundVersion = true; \
  120. { \
  121. VPROF_BUDGET( #funcname, VPROF_BUDGETGROUP_JOBS_COROUTINES ); \
  122. bResult = this->##funcname( pRequest, pWebAPIResponse ); \
  123. } \
  124. break;
  125. #define WEBAPI_JOB_VERSION_ADAPTER_EXTENDED_ARRAYS( version, funcname ) \
  126. case version: \
  127. bFoundVersion = true; \
  128. pWebAPIResponse->SetExtendedArrays( true ); \
  129. { \
  130. VPROF_BUDGET( #funcname, VPROF_BUDGETGROUP_JOBS_COROUTINES ); \
  131. bResult = this->##funcname( pRequest, pWebAPIResponse ); \
  132. } \
  133. break;
  134. #define END_WEBAPI_JOB_VERSION_ADAPTERS() \
  135. default: \
  136. break; \
  137. } \
  138. AssertMsg3( bFoundVersion, "WebAPIJob for %s/%s received unhandled version %d", pchInterface, pchMethod, unVersion ); \
  139. return bResult; \
  140. }
  141. } // namespace GCSDK
  142. #endif // GCJOB_H