中间件底层,websocket

Scheduler.cpp 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "stdafx.h"
  2. #include "Scheduler.h"
  3. CScheduler::CScheduler(boost::asio::io_service& ios):timer_(ios, std::chrono::milliseconds(0)),m_DuratTime(1000), m_IsStop(true), m_FunCB(nullptr), m_TimeId(0)
  4. {
  5. }
  6. CScheduler::~CScheduler()
  7. {
  8. killTimer();
  9. }
  10. void CScheduler::setTimer(const std::int32_t& nIDEvent, const std::int32_t& uElapse)
  11. {
  12. if (!m_IsStop.load()) return;
  13. m_TimeId = nIDEvent;
  14. m_DuratTime = uElapse;
  15. m_IsStop.store(false);
  16. timer_.expires_from_now(std::chrono::milliseconds(m_DuratTime));
  17. //timer_.expires_at(timer_.expires_at() + std::chrono::milliseconds(m_DuratTime));
  18. timer_.async_wait(std::bind(&this_type::__onTimer, this));
  19. }
  20. void CScheduler::killTimer()
  21. {
  22. if (!m_IsStop)
  23. m_IsStop.store(true);
  24. timer_.cancel();
  25. }
  26. void CScheduler::setOnTimeFuncCB(FimeFunCB fun)
  27. {
  28. if (fun != nullptr&&this->m_FunCB == nullptr) {
  29. this->m_FunCB = fun;
  30. }
  31. }
  32. void CScheduler::__onTimer()
  33. {
  34. if (this->m_FunCB && !m_IsStop.load()) {
  35. this->m_FunCB(m_TimeId);
  36. }
  37. if (!m_IsStop.load()) {
  38. timer_.expires_from_now(std::chrono::milliseconds(m_DuratTime));
  39. //timer_.expires_at(timer_.expires_at() + std::chrono::milliseconds(m_DuratTime));
  40. if (!m_IsStop.load())
  41. timer_.async_wait(std::bind(&this_type::__onTimer, this));
  42. }
  43. }