- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
单线程应用程序。
它不会每次都发生,只有在高负载1.5小时后才会发生。
最佳答案
正如Igor在评论中提到的那样,完成处理程序已经排队。
这种情况是操作执行与调用处理程序之间时间间隔分开的结果。 io_service::run()
, io_service::run_one()
, io_service::poll()
和 io_service::poll_one()
的文档专用于提及处理程序,而不是操作。在这种情况下,socket::async_connect()
操作和deadline_timer::async_wait()
操作在同一事件循环迭代中完成。这导致两个处理程序都以未指定的顺序添加到io_service
中以进行延迟的调用。
考虑以下突出场景的代码段:
void handle_wait(const boost::system::error_code& error)
{
if (error) return;
socket_.close();
}
timer_.expires_from_now(boost::posix_time::seconds(30));
timer_.async_wait(&handle_wait);
socket_.async_connect(endpoint_, handle_connect);
boost::this_thread::sleep(boost::posix_time::seconds(60));
io_service_.run_one();
io_service_.run_one()
时,
socket::async_connect()
和
deadline_timer::async_wait()
操作可能都已完成,导致
handle_wait
和
handle_connect
准备好以未指定的顺序从
io_service
内部调用。为了正确处理此未指定的顺序,需要在
handle_wait()
和
handle_connect()
中发生附加逻辑以查询当前状态,并确定是否已调用其他处理程序,而不是仅取决于操作的状态(
error_code
)。
handle_connect()
中,检查套接字是否仍通过 is_open()
打开。如果套接字仍处于打开状态,则尚未调用handle_timer()
。向handle_timer()
指示handle_connect()
已经运行的一种干净方法是更新到期时间。 handle_timer()
中,检查是否已过期。如果是这样,则handle_connect()
尚未运行,因此请关闭套接字。 void handle_wait(const boost::system::error_code& error)
{
// On error, return early.
if (error) return;
// If the timer expires in the future, then connect handler must have
// first.
if (timer_.expires_at() > deadline_timer::traits_type::now()) return;
// Timeout has occurred, so close the socket.
socket_.close();
}
void handle_connect(const boost::system::error_code& error)
{
// The async_connect() function automatically opens the socket at the start
// of the asynchronous operation. If the socket is closed at this time then
// the timeout handler must have run first.
if (!socket_.is_open()) return;
// On error, return early.
if (error) return;
// Otherwise, a connection has been established. Update the timer state
// so that the timeout handler does not close the socket.
timer_.expires_at(boost::posix_time::pos_infin);
}
关于c++ - 关闭后 boost asio async_connect成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14586695/
我正在尝试检查 boost::asio::async_connect 是否成功,但我没有收到错误,即使服务器未启动。如何正确检查是否成功?谢谢! main.cpp #include #include
我正在尝试为我的项目从 boost TCP 客户端示例创建一个客户端类,并且我注意到有时在连接到不存在的主机时不会调用 handle_connect。 我在堆栈上读过类似的问题,人们忘记运行 io_s
短篇小说:socket.async_connect(...) 之后的调用 socket.cancel() 完美运行,即调用了完成处理程序;但是对于 asio::async_connect(socket
我想知道传递给上述方法的对象的生命周期是多少。 异步解析 ip::basic_resolver::async_resolve(const query & q, ResolveHandler handl
我在修改 asio 的阻塞套接字时没有遇到任何问题,但我对异步套接字的深入研究一直是一场噩梦。也许我只是累坏了: using boost::asio::ip::tcp; boost::asio
我偶尔会遇到这个问题。我们有一个使用 boost::asio 和 deadline_timer 实现的 http 客户端来处理连接/请求超时。 async_connect 被调用。 deadline_
我正在编写一个使用 boost asio 连接到远程套接字的小型客户端类。如果初始连接失败,它应该能够尝试重新连接。 在测试该场景时,即没有打开的远程套接字,第一次正确调用了 async_connec
我正在使用 asio 构建一个可以通过 TCP 或 unix 套接字连接到远程系统的网络库。我使用 asio::generic::stream_protocol::socket,它同时具有 conne
我正在尝试实现带有超时的 async_connect()。 async_connect_with_timeout(socket_type & s, std::function
我想检查 Boost Asio 是否真的执行异步连接。根据the diagrams corresponding to the asynchronous calls发表在Asio的基础知识中,当io_s
我有一个服务器应用程序,其中我只接受一个带有 boost::asio::ip::tcp::acceptor 和 async_accept 的客户端。 我运行客户端应用程序并使用 boost::asio
我已经关注了 documentation and examples由 boost asio 实现提供,但在将我的客户端连接到服务器后没有任何运气。无论成功还是失败,处理程序都不会被调用。我已验证服务器
我正在学习使用 Boost ASIO。这是我的代码改编自 Boost 文档的 chat example . class AsioCommunicationService { AsioCommunica
我成功地实现了 async_connect,使用它作为一个自由函数和一个 lambda 作为 connect_handler,如下所示: auto self(shared_from_this());
我刚刚通过 TCPView 发现使用 boost::asio 建立的一个 TCP 连接创建了两个连接。其中之一使用本地端口,第二个使用所需的远程端口。为什么?这种行为正常吗?有没有办法只创建一个? 这
我正在尝试运行以下代码: some_sock.async_connect(...); // handle_connect() sets the 'condition' flag boost::asio
我是一名优秀的程序员,十分优秀!