gpt4 book ai didi

c++ - Boost basic_deadline_timer 在几次迭代后停止触发

转载 作者:行者123 更新时间:2023-11-30 05:32:26 25 4
gpt4 key购买 nike

我继承了一个项目,我正在努力思考这一切是如何发生的作品。我已经检查了我在截止日期计时器方面遇到的所有基本陷阱之前。

  • 在调用 io_service.run 之前,我们是否附加了一个工作对象? ☑
  • 对象是否仍然存在并在范围内? ☑
  • 确认我们没有收到错误代码? ☑

*** 新数据 **************

它不仅会在杂草丛中消失,还会恢复正常并恢复正常运行。但它需要大约 5 到 10 分钟的时间。计时器本身似乎是基于滴答计数计时器示例:

http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/timers/tick_count_timer.cpp

在这一点上,我想知道计时器对象本身是否有问题。我一直使用普通的截止日期计时器。我认为它使用 tick_count_timer 来独立于系统时钟,系统时钟通常从属于某处的网络时钟。

这不是计时器重新启动的情况,因为它有一条单独的路径,我没有第二次看到该打印输出。

*** 结束新数据 *********

不用说这让我难住了。

这里是有问题的方法:

void project::on_heartbeatsend_timer_elapsed(const boost::system::error_code& e)
{
if (e != boost::asio::error::operation_aborted)
{
if (this->m_state!=session_state::SessionConnected)
{
stop("Heartbeat SEND lost connection");

#ifdef HEARTBEAT_DEBUG
cout << "Heartbeat SEND lost connection to target: "
<< GetIP().c_str()
<< ":"
<< GetPort()
<< endl;
#endif // HEARTBEAT_DEBUG

}
else
{

size_t size = 0;
packet_buffer_t b(construct_api_packet(SESSION_HEARTBEAT, 0, 0,NULL,size,&size));
write(b, size);

m_timerHeartbeatSend.cancel();

size_t cancelled_stuff = m_timerHeartbeatSend.expires_from_now(m_heartbeatFrequency*1000);

m_timerHeartbeatSend.async_wait(strand_.wrap(boost::bind(&project::on_heartbeatsend_timer_elapsed,
shared_from_this(),
boost::asio::placeholders::error)));
#ifdef HEARTBEAT_DEBUG
cout << "Heartbeat SEND to target "
<< GetIP().c_str()
<< ":"
<< GetPort()
<< " Next Heartbeat in: "
<< m_heartbeatFrequency
<< " seconds. cancelled "
<< cancelled_stuff
<< " pending operation."
<< endl;
#endif // HEARTBEAT_DEBUG
}
}
else if (e.value() != 995)
{
stringstream evalSS;
evalSS << e.value();

std::string error_str = std::string("Error in on_heartbeatsend_timer_elapsed: ") + e.message() + " (" + evalSS.str() + ")";

stop(error_str);

#ifdef HEARTBEAT_DEBUG
cout << "Heartbeat "
<< error_str.c_str()
<< " "
<< GetIP().c_str()
<< ":"
<< GetPort()
<< endl;
#endif // HEARTBEAT_DEBUG
}
else
{
#ifdef HEARTBEAT_DEBUG
cout << "Heartbeat Threading ending to target "
<< GetIP().c_str()
<< ":"
<< GetPort()
<< endl;
#endif // HEARTBEAT_DEBUG
}
#ifdef HEARTBEAT_DEBUG
cout << "finished processing this heartbeat for sending to target "
<< GetIP().c_str()
<< ":"
<< GetPort()
<< endl;
#endif
}

调试的输出如下所示:

Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat SEND to target 127.0.0.1:50647 Next Heartbeat in: 5 seconds. cancelled 0 pending operation.
finished processing this heartbeat for sending to target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat SEND to target 127.0.0.1:50647 Next Heartbeat in: 5 seconds. cancelled 0 pending operation.
finished processing this heartbeat for sending to target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat SEND to target 127.0.0.1:50647 Next Heartbeat in: 5 seconds. cancelled 0 pending operation.
finished processing this heartbeat for sending to target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50647

Heartbeat RECV timer STARTED 消息每次被启动心跳数据包是从 C# 测试器接收到的,用于此的 C# 实现API。

接下来我想做的是在调试器中进一步检查它。我在用着Visual Studio 2013,但我很难做出m_timerHeartbeatSend 对象。

添加了更多调试,以更好地显示我看到的模式。时间戳是滴答计数。

Heartbeat SEND to target 127.0.0.1:50367 Next Heartbeat in: 5 seconds. cancelled
0 pending operation. old experation: 1672369 now expires at: 1677377 current ti
me: 1672377
finished processing this heartbeat for sending to target 127.0.0.1:50367
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1677307
Heartbeat SEND to target 127.0.0.1:50367 Next Heartbeat in: 5 seconds. cancelled
0 pending operation. old experation: 1677377 now expires at: 1682385 current ti
me: 1677385
finished processing this heartbeat for sending to target 127.0.0.1:50367
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1682299
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1687306
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1692314
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1697322
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1702314
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1707321
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1712329
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1717337
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1722344
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1727352
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1732344
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1737351
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1742359
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1747367
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1752374
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1757366
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1762374
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1767382
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1772389
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1777397
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1782405
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1787412
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1792404
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1797412
Heartbeat SEND to target 127.0.0.1:50367 Next Heartbeat in: 5 seconds. cancelled
0 pending operation. old experation: 1682385 now expires at: 1805001 current ti
me: 1800001
finished processing this heartbeat for sending to target 127.0.0.1:50367
Heartbeat RECV timer STARTED (handle_read_header) for target 127.0.0.1:50367 at
1802419
Heartbeat SEND to target 127.0.0.1:50367 Next Heartbeat in: 5 seconds. cancelled
0 pending operation. old experation: 1805001 now expires at: 1810009 current ti
me: 1805009
finished processing this heartbeat for sending to target 127.0.0.1:50367

但现在我又陷入困境了。我觉得我需要以某种方式检查 m_timerHeartBeatSend 的内部结构才能更进一步。

最佳答案

原来问题出在为此计时器定义的 time_traits 结构中。它使用的是无符号数学,当减去的值大于另一个值时,它把整个地方的计时器都搞砸了。最终它会恢复。如果您在代码库中看到类似的问题,请确保您的特征允许负值。

关于c++ - Boost basic_deadline_timer 在几次迭代后停止触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com