wsTimerThread.cpp 2.3 KB

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