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.

144 lines
3.8 KiB

  1. //====== Copyright (c), Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef GCCLIENTJOB_H
  7. #define GCCLIENTJOB_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. namespace GCSDK
  12. {
  13. class CGCClient;
  14. //-----------------------------------------------------------------------------
  15. // Purpose: handles a network message job from the client
  16. //-----------------------------------------------------------------------------
  17. class CGCClientJob : public CJob
  18. {
  19. public:
  20. CGCClientJob( CGCClient *pGCClient ) : CJob( pGCClient->GetJobMgr() ), m_pGCClient( pGCClient ), m_cHeartbeatsBeforeTimeout( k_cJobHeartbeatsBeforeTimeoutDefault ) {}
  21. // all GCClient jobs must implement one of these
  22. virtual bool BYieldingRunGCJob( IMsgNetPacket *pNetPacket ) { return false; }
  23. virtual bool BYieldingRunGCJob() { return false; }
  24. protected:
  25. CGCClient *m_pGCClient;
  26. bool BYldSendMessageAndGetReply( CGCMsgBase &msgOut, uint nTimeoutSec, CGCMsgBase *pMsgIn, MsgType_t eMsg )
  27. {
  28. IMsgNetPacket *pNetPacket = NULL;
  29. if ( !BYldSendMessageAndGetReply( msgOut, nTimeoutSec, &pNetPacket ) )
  30. return false;
  31. pMsgIn->SetPacket( pNetPacket );
  32. if ( pMsgIn->Hdr().m_eMsg != eMsg )
  33. return false;
  34. return true;
  35. }
  36. bool BYldSendMessageAndGetReply( CGCMsgBase &msgOut, uint nTimeoutSec, IMsgNetPacket **ppNetPacket )
  37. {
  38. msgOut.ExpectingReply( GetJobID() );
  39. if ( !m_pGCClient->BSendMessage( msgOut ) )
  40. return false;
  41. SetJobTimeout( nTimeoutSec );
  42. return BYieldingWaitForMsg( ppNetPacket );
  43. }
  44. enum BYldSendMessageAndGetReply_t
  45. {
  46. BYLDREPLY_SUCCESS,
  47. BYLDREPLY_SEND_FAILED,
  48. BYLDREPLY_TIMEOUT,
  49. BYLDREPLY_MSG_TYPE_MISMATCH,
  50. };
  51. BYldSendMessageAndGetReply_t BYldSendMessageAndGetReplyEx( CProtoBufMsgBase &msgOut, uint nTimeoutSec, CProtoBufMsgBase *pMsgIn, MsgType_t eMsg )
  52. {
  53. IMsgNetPacket *pNetPacket = NULL;
  54. msgOut.ExpectingReply( GetJobID() );
  55. if ( !m_pGCClient->BSendMessage( msgOut ) )
  56. return BYLDREPLY_SEND_FAILED;
  57. SetJobTimeout( nTimeoutSec );
  58. if( !BYieldingWaitForMsg( &pNetPacket ) )
  59. return BYLDREPLY_TIMEOUT;
  60. if( !pMsgIn->InitFromPacket( pNetPacket ) )
  61. return BYLDREPLY_MSG_TYPE_MISMATCH;
  62. if ( pMsgIn->GetEMsg() != eMsg )
  63. return BYLDREPLY_MSG_TYPE_MISMATCH;
  64. return BYLDREPLY_SUCCESS;
  65. }
  66. bool BYldSendMessageAndGetReply( CProtoBufMsgBase &msgOut, uint nTimeoutSec, CProtoBufMsgBase *pMsgIn, MsgType_t eMsg )
  67. {
  68. BYldSendMessageAndGetReply_t result = BYldSendMessageAndGetReplyEx( msgOut, nTimeoutSec, pMsgIn, eMsg );
  69. if ( result == BYLDREPLY_SUCCESS )
  70. return true;
  71. // Notify the client if the reply times out.
  72. if ( result == BYLDREPLY_TIMEOUT )
  73. m_pGCClient->MessageReplyTimedOut( eMsg, nTimeoutSec );
  74. return false;
  75. }
  76. bool BYldSendMessageAndGetReply( CProtoBufMsgBase &msgOut, uint nTimeoutSec, IMsgNetPacket **ppNetPacket )
  77. {
  78. msgOut.ExpectingReply( GetJobID() );
  79. if ( !m_pGCClient->BSendMessage( msgOut ) )
  80. return false;
  81. SetJobTimeout( nTimeoutSec );
  82. return BYieldingWaitForMsg( ppNetPacket );
  83. }
  84. virtual uint32 CHeartbeatsBeforeTimeout() { return m_cHeartbeatsBeforeTimeout; }
  85. void SetJobTimeout( uint nTimeoutSec ) { m_cHeartbeatsBeforeTimeout = 1 + ((nTimeoutSec * k_nMillion) / k_cMicroSecJobHeartbeat); }
  86. private:
  87. virtual bool BYieldingRunJobFromMsg( IMsgNetPacket *pNetPacket )
  88. {
  89. // Protection against a NULL GCClient. Yields so the job is not deleted instantly
  90. if ( !m_pGCClient )
  91. {
  92. BYieldingWaitOneFrame();
  93. return false;
  94. }
  95. return BYieldingRunGCJob( pNetPacket );
  96. }
  97. virtual bool BYieldingRunJob( void *pvStartParam )
  98. {
  99. // Protection against a NULL GCClient. Yields so the job is not deleted instantly
  100. if ( !m_pGCClient )
  101. {
  102. BYieldingWaitOneFrame();
  103. return false;
  104. }
  105. return BYieldingRunGCJob();
  106. }
  107. uint32 m_cHeartbeatsBeforeTimeout;
  108. };
  109. } // namespace GCSDK
  110. #endif // GCCLIENTJOB_H