wsTimerThread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @brief
  3. websocket定时发送线程类
  4. * @version
  5. V 1.0.0
  6. * @author
  7. 王益俊
  8. * @date
  9. 创建时间: 2018-08-17\n
  10. * @note
  11. 2018-08-17 创建类。\n
  12. * @warning
  13. * @bug
  14. */
  15. #pragma once
  16. #include <boost/thread.hpp>
  17. #include <boost/bind.hpp>
  18. #include <boost/atomic.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/thread/condition.hpp>
  21. #include <boost/serialization/singleton.hpp>
  22. #include <ctime>
  23. #include "thread_safe_map.h"
  24. #include "ws_common.h"
  25. #include "jsonBuilder.h"
  26. namespace YA
  27. {
  28. class wsTimerThread
  29. {
  30. private:
  31. boost::shared_ptr<boost::thread> __Thread;//线程
  32. boost::atomic<bool> __Enable;//线程是否继续运行的标志
  33. boost::atomic<bool> __Running;//线程是否正在运行的标志
  34. boost::condition_variable_any __ExitCond;//条件变量
  35. boost::mutex __ExitMutex;//锁(配合__ExitCond)
  36. _THREAD_CONFIG_ __Config;//线程配置
  37. thread_safe_map<uint64_t, _CARD_POS_> __CardPosList;//卡位置列表
  38. std::time_t __LastSendTime{0};//上一次发送的时间
  39. jsonBuilder __jsBuilder;//json构造器类
  40. private:
  41. /**
  42. * @brief
  43. 重置函数。
  44. * @param 无\n
  45. * @return 无\n
  46. * @note
  47. * @warning
  48. * @bug
  49. */
  50. void __Reset();
  51. /**
  52. * @brief
  53. 检查线程配置是否合法函数。
  54. * @param 无\n
  55. * @return 无\n
  56. * @note
  57. * @warning
  58. * @bug
  59. */
  60. void __ChkConfig();
  61. /**
  62. * @brief
  63. 发送卡位置函数。
  64. * @param 无\n
  65. * @return 无\n
  66. * @note
  67. * @warning
  68. * @bug
  69. */
  70. void __SendCardPos();
  71. void send_card_pos();
  72. protected:
  73. /**
  74. * @brief
  75. 线程函数。
  76. * @param [in] wsTimerThread* pOwner 线程所属者\n
  77. * @return 无\n
  78. * @note
  79. * @warning
  80. * @bug
  81. */
  82. void _ThreadFunc( wsTimerThread* pOwner );
  83. public:
  84. wsTimerThread();
  85. ~wsTimerThread();
  86. /**
  87. * @brief
  88. 启动线程函数。
  89. * @param 无\n
  90. * @return 无\n
  91. * @note
  92. * @warning
  93. * @bug
  94. */
  95. void Start();
  96. /**
  97. * @brief
  98. 停止线程函数。
  99. * @param 无\n
  100. * @return 无\n
  101. * @note
  102. * @warning
  103. * @bug
  104. */
  105. void Stop();
  106. /**
  107. * @brief
  108. 初始化函数。
  109. * @param [in] const _THREAD_CONFIG_& Config 线程配置\n
  110. * @return 无\n
  111. * @note
  112. * @warning
  113. * @bug
  114. */
  115. void Init( const _THREAD_CONFIG_& Config );
  116. /**
  117. * @brief
  118. 更新卡位置函数。
  119. * @param [in] const _CARD_POS_& pos 卡位置结构体\n
  120. * @return 无\n
  121. * @note
  122. * @warning
  123. * @bug
  124. */
  125. void upt_card_pos( const _CARD_POS_& pos );
  126. void real_upt_card_pos(const _CARD_POS_& pos);
  127. /**
  128. * @brief
  129. 删除卡位置函数。
  130. * @param [in] const _CARD_POS_& pos 卡位置结构体\n
  131. * @return 无\n
  132. * @note
  133. * @warning
  134. * @bug
  135. */
  136. void del_card_pos( const _CARD_POS_& pos );
  137. };
  138. }
  139. //单件定义
  140. typedef boost::serialization::singleton<YA::wsTimerThread> singleton_wsTimerThread;
  141. #define swsTimerThrd singleton_wsTimerThread::get_mutable_instance()
  142. #define swsTimerThrd_const singleton_wsTimerThread::get_const_instance()