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.

139 lines
3.7 KiB

  1. //========= Copyright 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. virtual EServerType GetServerType() { return k_EServerTypeGCClient; }
  25. protected:
  26. CGCClient *m_pGCClient;
  27. bool BYldSendMessageAndGetReply( CGCMsgBase &msgOut, uint nTimeoutSec, CGCMsgBase *pMsgIn, MsgType_t eMsg )
  28. {
  29. IMsgNetPacket *pNetPacket = NULL;
  30. if ( !BYldSendMessageAndGetReply( msgOut, nTimeoutSec, &pNetPacket ) )
  31. return false;
  32. pMsgIn->SetPacket( pNetPacket );
  33. if ( pMsgIn->Hdr().m_eMsg != eMsg )
  34. return false;
  35. return true;
  36. }
  37. bool BYldSendMessageAndGetReply( CGCMsgBase &msgOut, uint nTimeoutSec, IMsgNetPacket **ppNetPacket )
  38. {
  39. msgOut.ExpectingReply( GetJobID() );
  40. if ( !m_pGCClient->BSendMessage( msgOut ) )
  41. return false;
  42. SetJobTimeout( nTimeoutSec );
  43. return BYieldingWaitForMsg( ppNetPacket );
  44. }
  45. enum BYldSendMessageAndGetReply_t
  46. {
  47. BYLDREPLY_SUCCESS,
  48. BYLDREPLY_SEND_FAILED,
  49. BYLDREPLY_TIMEOUT,
  50. BYLDREPLY_MSG_TYPE_MISMATCH,
  51. };
  52. BYldSendMessageAndGetReply_t BYldSendMessageAndGetReplyEx( CProtoBufMsgBase &msgOut, uint nTimeoutSec, CProtoBufMsgBase *pMsgIn, MsgType_t eMsg )
  53. {
  54. IMsgNetPacket *pNetPacket = NULL;
  55. msgOut.ExpectingReply( GetJobID() );
  56. if ( !m_pGCClient->BSendMessage( msgOut ) )
  57. return BYLDREPLY_SEND_FAILED;
  58. SetJobTimeout( nTimeoutSec );
  59. if( !BYieldingWaitForMsg( &pNetPacket ) )
  60. return BYLDREPLY_TIMEOUT;
  61. pMsgIn->InitFromPacket( pNetPacket );
  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. if( BYldSendMessageAndGetReplyEx( msgOut, nTimeoutSec, pMsgIn, eMsg ) != BYLDREPLY_SUCCESS )
  69. return false;
  70. return true;
  71. }
  72. bool BYldSendMessageAndGetReply( CProtoBufMsgBase &msgOut, uint nTimeoutSec, IMsgNetPacket **ppNetPacket )
  73. {
  74. msgOut.ExpectingReply( GetJobID() );
  75. if ( !m_pGCClient->BSendMessage( msgOut ) )
  76. return false;
  77. SetJobTimeout( nTimeoutSec );
  78. return BYieldingWaitForMsg( ppNetPacket );
  79. }
  80. virtual uint32 CHeartbeatsBeforeTimeout() { return m_cHeartbeatsBeforeTimeout; }
  81. void SetJobTimeout( uint nTimeoutSec ) { m_cHeartbeatsBeforeTimeout = 1 + ((nTimeoutSec * k_nMillion) / k_cMicroSecJobHeartbeat); }
  82. private:
  83. virtual bool BYieldingRunJobFromMsg( IMsgNetPacket *pNetPacket )
  84. {
  85. // Protection against a NULL GCClient. Yields so the job is not deleted instantly
  86. if ( !m_pGCClient )
  87. {
  88. BYieldingWaitOneFrame();
  89. return false;
  90. }
  91. return BYieldingRunGCJob( pNetPacket );
  92. }
  93. virtual bool BYieldingRunJob( void *pvStartParam )
  94. {
  95. // Protection against a NULL GCClient. Yields so the job is not deleted instantly
  96. if ( !m_pGCClient )
  97. {
  98. BYieldingWaitOneFrame();
  99. return false;
  100. }
  101. return BYieldingRunGCJob();
  102. }
  103. uint32 m_cHeartbeatsBeforeTimeout;
  104. };
  105. } // namespace GCSDK
  106. #endif // GCCLIENTJOB_H