From b64c70a1355beb940cfb4dea2477af471e933fe7 Mon Sep 17 00:00:00 2001 From: freeeyes Date: Thu, 4 Feb 2021 13:02:10 +0800 Subject: [PATCH] add time seconds delay. --- purenessscopeserver/Common/TimerManager.hpp | 12 +++++++-- purenessscopeserver/Common/define.h | 29 +++++++++++++++++++++ purenessscopeserver/Common/tms.hpp | 4 +-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/purenessscopeserver/Common/TimerManager.hpp b/purenessscopeserver/Common/TimerManager.hpp index e7d0d0d2..214204b0 100644 --- a/purenessscopeserver/Common/TimerManager.hpp +++ b/purenessscopeserver/Common/TimerManager.hpp @@ -45,12 +45,14 @@ namespace brynet { Timer(std::chrono::steady_clock::time_point startTime, std::chrono::nanoseconds lastTime, + std::chrono::seconds delayTime, ENUM_TIMER_TYPE timertype, Callback&& callback) : mCallback(std::move(callback)), mStartTime(startTime), mLastTime(lastTime), + mDelayTime(delayTime), mTimerType(timertype) { } @@ -65,10 +67,12 @@ namespace brynet { return mLastTime; } - std::chrono::nanoseconds getLeftTime() const + std::chrono::nanoseconds getLeftTime() { const auto now = std::chrono::steady_clock::now(); - return getLastTime() - (now - getStartTime()); + auto delayTime = mDelayTime; + mDelayTime = std::chrono::seconds(0); + return getLastTime() - (now - getStartTime()) + delayTime; } void cancel() @@ -114,6 +118,7 @@ namespace brynet { Callback mCallback; std::chrono::steady_clock::time_point mStartTime; std::chrono::nanoseconds mLastTime; + std::chrono::seconds mDelayTime; ENUM_TIMER_TYPE mTimerType = ENUM_TIMER_TYPE::TIMER_TYPE_ONCE; friend class TimerMgr; @@ -126,6 +131,7 @@ namespace brynet { template Timer::WeakPtr addTimer_loop( + std::chrono::seconds deleyTime, std::chrono::nanoseconds timeout, F&& callback, TArgs&& ...args) @@ -133,6 +139,7 @@ namespace brynet { auto timer = std::make_shared( std::chrono::steady_clock::now(), std::chrono::nanoseconds(timeout), + deleyTime, ENUM_TIMER_TYPE::TIMER_TYPE_LOOP, std::bind(std::forward(callback), std::forward(args)...)); mtx_queue.lock(); @@ -154,6 +161,7 @@ namespace brynet { auto timer = std::make_shared( std::chrono::steady_clock::now(), std::chrono::nanoseconds(timeout), + std::chrono::seconds(0), ENUM_TIMER_TYPE::TIMER_TYPE_ONCE, std::bind(std::forward(callback), std::forward(args)...)); diff --git a/purenessscopeserver/Common/define.h b/purenessscopeserver/Common/define.h index 3ffa999c..e9fafbe3 100644 --- a/purenessscopeserver/Common/define.h +++ b/purenessscopeserver/Common/define.h @@ -1587,6 +1587,35 @@ inline vector split_string(const string& s, const char& c) return v; } +inline std::chrono::seconds get_time_delay(std::string date) +{ + std::chrono::seconds delete_seconds; + std::tm tm_; + int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。 +#if PSS_PLATFORM != PLATFORM_WIN + sscanf(date.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second); +#else + sscanf_s(date.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second); +#endif + + tm_.tm_year = year - 1900; + tm_.tm_mon = month - 1; + tm_.tm_mday = day; + tm_.tm_hour = hour; + tm_.tm_min = minute; + tm_.tm_sec = second; + tm_.tm_isdst = 0; // 非夏令时。 + + auto tp_tag = std::chrono::system_clock::from_time_t(mktime(&tm_)); + auto tp_now = std::chrono::system_clock::now(); + + auto duration = std::chrono::duration_cast(tp_tag - tp_now); + + //std::cout << "seconds=" << duration.count() << std::endl; + delete_seconds = std::chrono::seconds(duration.count()); + return delete_seconds; +} + #if PSS_PLATFORM != PLATFORM_WIN //获得当前文件打开数 diff --git a/purenessscopeserver/Common/tms.hpp b/purenessscopeserver/Common/tms.hpp index a86073d3..a53078c6 100644 --- a/purenessscopeserver/Common/tms.hpp +++ b/purenessscopeserver/Common/tms.hpp @@ -179,7 +179,7 @@ class TMS } //添加消息(定时器) - brynet::Timer::WeakPtr AddMessage_loop(uint32 u4LogicID, std::chrono::milliseconds millisecond, task_function func) + brynet::Timer::WeakPtr AddMessage_loop(uint32 u4LogicID, std::chrono::seconds delaytime, std::chrono::milliseconds millisecond, task_function func) { brynet::Timer::WeakPtr timer; auto f = m_mapLogicList.find(u4LogicID); @@ -188,7 +188,7 @@ class TMS auto pLogicMessage = std::make_shared(); pLogicMessage->m_func = func; - timer = m_timerManager.addTimer_loop(millisecond, [this, u4LogicID, pLogicMessage]() { + timer = m_timerManager.addTimer_loop(delaytime, millisecond, [this, u4LogicID, pLogicMessage]() { m_mapLogicList[u4LogicID]->Put(pLogicMessage); //cout << "Timer execute is ok." << endl; });