Skip to content

Commit

Permalink
add time seconds delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
freeeyes committed Feb 4, 2021
1 parent e3be70e commit b64c70a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
12 changes: 10 additions & 2 deletions purenessscopeserver/Common/TimerManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -126,13 +131,15 @@ namespace brynet {

template<typename F, typename ...TArgs>
Timer::WeakPtr addTimer_loop(
std::chrono::seconds deleyTime,
std::chrono::nanoseconds timeout,
F&& callback,
TArgs&& ...args)
{
auto timer = std::make_shared<Timer>(
std::chrono::steady_clock::now(),
std::chrono::nanoseconds(timeout),
deleyTime,
ENUM_TIMER_TYPE::TIMER_TYPE_LOOP,
std::bind(std::forward<F>(callback), std::forward<TArgs>(args)...));
mtx_queue.lock();
Expand All @@ -154,6 +161,7 @@ namespace brynet {
auto timer = std::make_shared<Timer>(
std::chrono::steady_clock::now(),
std::chrono::nanoseconds(timeout),
std::chrono::seconds(0),
ENUM_TIMER_TYPE::TIMER_TYPE_ONCE,
std::bind(std::forward<F>(callback), std::forward<TArgs>(args)...));

Expand Down
29 changes: 29 additions & 0 deletions purenessscopeserver/Common/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,35 @@ inline vector<string> 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<std::chrono::seconds>(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

//获得当前文件打开数
Expand Down
4 changes: 2 additions & 2 deletions purenessscopeserver/Common/tms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -188,7 +188,7 @@ class TMS
auto pLogicMessage = std::make_shared<CLogicMessage>();
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;
});
Expand Down

0 comments on commit b64c70a

Please sign in to comment.