wsTimerThread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @brief
  3. websocket定时发送线程类
  4. * @version
  5. V 1.0.0
  6. * @author
  7. * @date
  8. 创建时间: 2018-08-17\n
  9. * @note
  10. 2018-08-17 创建类。\n
  11. * @warning
  12. * @bug
  13. */
  14. #pragma once
  15. #include <boost/thread.hpp>
  16. #include <boost/bind.hpp>
  17. #include <boost/atomic.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. #include <boost/thread/condition.hpp>
  20. #include <boost/serialization/singleton.hpp>
  21. #include <ctime>
  22. #include "thread_safe_map.h"
  23. #include "ws_common.h"
  24. #include "jsonBuilder.h"
  25. namespace YA
  26. {
  27. class wsTimerThread
  28. {
  29. private:
  30. boost::shared_ptr<boost::thread> __Thread;//线程
  31. boost::atomic<bool> __Enable;//线程是否继续运行的标志
  32. boost::atomic<bool> __Running;//线程是否正在运行的标志
  33. boost::condition_variable_any __ExitCond;//条件变量
  34. boost::mutex __ExitMutex;//锁(配合__ExitCond)
  35. _THREAD_CONFIG_ __Config;//线程配置
  36. thread_safe_map<uint64_t, _CARD_POS_> __CardPosList;//卡位置列表
  37. std::time_t __LastSendTime{0};//上一次发送的时间
  38. jsonBuilder __jsBuilder;//json构造器类
  39. private:
  40. /**
  41. * @brief
  42. 重置函数。
  43. * @param 无\n
  44. * @return 无\n
  45. * @note
  46. * @warning
  47. * @bug
  48. */
  49. void __Reset();
  50. /**
  51. * @brief
  52. 检查线程配置是否合法函数。
  53. * @param 无\n
  54. * @return 无\n
  55. * @note
  56. * @warning
  57. * @bug
  58. */
  59. void __ChkConfig();
  60. /**
  61. * @brief
  62. 发送卡位置函数。
  63. * @param 无\n
  64. * @return 无\n
  65. * @note
  66. * @warning
  67. * @bug
  68. */
  69. void __SendCardPos();
  70. void send_card_pos();
  71. void temp_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()