wsTimerThread.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "wsTimerThread.h"
  2. #include <iostream>
  3. #include "wsClientMgr.h"
  4. #include "constdef.h"
  5. #include "log.h"
  6. namespace YA
  7. {
  8. wsTimerThread::wsTimerThread()
  9. {
  10. __Reset();
  11. }
  12. wsTimerThread::~wsTimerThread()
  13. {
  14. Stop();
  15. }
  16. void wsTimerThread::Start()
  17. {
  18. __Running = true;
  19. __Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
  20. //__Thread->detach();
  21. }
  22. void wsTimerThread::Stop()
  23. {
  24. if(__Thread==nullptr)
  25. {
  26. return;
  27. }
  28. __Thread->interrupt();
  29. __Running = false;
  30. std::cout << "wsTimerThread::Stop() begin" << std::endl;
  31. __Enable = false;
  32. if ( __Running )
  33. {
  34. boost::unique_lock<boost::mutex> lock( __ExitMutex );
  35. __ExitCond.wait( lock );
  36. }
  37. __Reset();
  38. __Thread=nullptr;
  39. std::cout << "wsTimerThread::Stop() end" << std::endl;
  40. }
  41. void wsTimerThread::__Reset()
  42. {
  43. __Enable = true;
  44. __Running = false;
  45. }
  46. void wsTimerThread::__ChkConfig()
  47. {
  48. if ( __Config.SendInterval < MIN_SEND_INTERVAL )
  49. {
  50. __Config.SendInterval = MIN_SEND_INTERVAL;
  51. }
  52. }
  53. void wsTimerThread::__SendCardPos()
  54. {
  55. std::map<uint64_t, _CARD_POS_> CardPosList;
  56. if(__CardPosList.empty())
  57. return;
  58. __CardPosList.copy( CardPosList );
  59. std::string jsCardPos = __jsBuilder.BuildCardPos( CardPosList );
  60. log_info("[pos_map]%s",jsCardPos.c_str());
  61. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  62. }
  63. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  64. {
  65. while ( pOwner->__Enable )
  66. {
  67. std::time_t t = time( 0 );
  68. int seconds = (int)std::difftime( t, __LastSendTime );
  69. if ( seconds >= pOwner->__Config.SendInterval )
  70. {
  71. pOwner->__SendCardPos();
  72. __LastSendTime = t;
  73. }
  74. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  75. }
  76. pOwner->__ExitCond.notify_one();
  77. }
  78. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  79. {
  80. __Config = Config;
  81. __ChkConfig();
  82. }
  83. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  84. {
  85. //如果已存在就更新,否则插入
  86. uint64_t type = pos.Type;
  87. uint64_t id=type<<32|pos.ID;
  88. if ( __CardPosList.seek( id ) )
  89. {
  90. __CardPosList.update( id, pos );
  91. }
  92. else
  93. {
  94. __CardPosList.insert( id, pos );
  95. }
  96. }
  97. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  98. {
  99. uint64_t type = pos.Type;
  100. uint64_t id=type<<32|pos.ID;
  101. __CardPosList.erase( id );
  102. }
  103. }