boost :: asio取消或关闭对async_handle不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了boost :: asio取消或关闭对async_handle不起作用相关的知识,希望对你有一定的参考价值。

boost :: asio :: ip :: udp:socket不能取消()async_handle,也不能关闭()。

这是测试程序:

{
    boost::asio::io_context io_core;
    boost::asio::ip::udp::socket udp_socket1(io_core, udp::v4());
    char buff[200];
    boost::asio::ip::udp::endpoint endpoint1(boost::asio::ip::address::from_string("127.0.0.1"), 9999);

    cout << udp_socket1.is_open() << endl;
    udp_socket1.async_send_to(boost::asio::buffer(buff, 200), endpoint1, [&udp_socket1](const boost::system::error_code& ec, size_t wlen) {
        cout << udp_socket1.is_open() << endl;
        //assert(0);
    });
    cout << udp_socket1.is_open() << endl;
    udp_socket1.close();
    udp_socket1.async_send_to(boost::asio::buffer(buff, 200), endpoint1, [&udp_socket1](const boost::system::error_code& ec, size_t wlen) {
        cout << udp_socket1.is_open() << endl;
        //assert(0);
    });
    cout << udp_socket1.is_open() << endl;

    udp_socket1.close();
    io_core.run();
    system("pause");
}

根据documentation定义的升压1.67.0,WIN10,vs2017 BOOST_ASIO_DISABLE_IOCP和BOOST_ASIO_CANCELIO

我没有明白这一点吗?

如果我是对的,如何解决这个问题?

答案

异步操作不会立即完成。您的代码看起来像您期望的那样。

将代码简化为:

Live On Coliru

#include <boost/asio.hpp>
#include <iostream>

using boost::asio::ip::udp;

int main() {
    std::cout << std::boolalpha;

    boost::asio::io_context io;
    boost::asio::ip::udp::socket s {io, udp::v4()};
    boost::asio::ip::udp::endpoint const ep {{}, 9999};

    auto trace = [&s](char const* caption) { std::cout << caption << s.is_open() << std::endl; };
    auto handler = [=](boost::system::error_code, size_t) { trace("handler: "); };

    trace("main #1: ");

    char buff[200] = {};
    s.async_send_to(boost::asio::buffer(buff), ep, handler);

    trace("main #2: ");
    s.close();

    s.async_send_to(boost::asio::buffer(buff), ep, handler);

    trace("main #3: ");
    s.close();

    io.run();
}

有意义的是,处理程序只运行在io.run()之后;显然,套接字只有在首次关闭时才会“打开”:

main #1: true
main #2: true
main #3: false
handler: false
handler: false

这完全是预期的。所以,要么你应该处理错误:

Live On Coliru

auto trace = [&s](char const* caption) { std::cout << caption << (s.is_open()?"open":"closed") << std::endl; };
auto handler = [=](boost::system::error_code ec, size_t) { 
    trace(("handler(" + ec.message() + "): ").c_str()); 
};

打印代替:

main #1: open
main #2: open
main #3: closed
handler(Success): closed
handler(Bad file descriptor): closed

请注意,也许令人惊讶的是,第一次发送操作仍然成功。与我的预期相反,这表明发送实际上是在async_send_to调用时启动的,但是完成延迟到io.run()之后(套接字仍然显示已经关闭)。

也许你根本不想要异步操作:

Live On Coliru

#include <boost/asio.hpp>
#include <iostream>

using boost::asio::ip::udp;

int main() {
    std::cout << std::boolalpha;

    boost::asio::io_context io;
    boost::asio::ip::udp::socket s {io, udp::v4()};
    boost::asio::ip::udp::endpoint const ep {{}, 9999};

    auto trace = [&s](char const* caption) { std::cout << caption << s.is_open() << std::endl; };

    trace("main #1: ");

    char buff[200] = {};
    try {
        /*size_t wlen =*/ s.send_to(boost::asio::buffer(buff), ep);
        trace("sent #1: ");
    } catch(boost::system::system_error const& e) {
        std::cout << "Send #1 failed: " << e.code().message() << std::endl;
    }

    trace("main #2: ");
    s.close();

    try {
        /*size_t wlen =*/ s.send_to(boost::asio::buffer(buff), ep);
        trace("sent #2: ");
    } catch(boost::system::system_error const& e) {
        std::cout << "Send #2 failed: " << e.code().message() << std::endl;
    }

    trace("main #3: ");
    s.close();

    io.run();
}

打印

main #1: true
sent #1: true
main #2: true
Send #2 failed: Bad file descriptor
main #3: false

以上是关于boost :: asio取消或关闭对async_handle不起作用的主要内容,如果未能解决你的问题,请参考以下文章

优雅地取消 boost::asio::async_read

boost::asio - 知道何时必须关闭/关闭连接

boost::asio::async_accept 处理程序未被调用

使用 boost::asio::async_wait_until 和 boost::asio::streambuf

安全地完成读取操作 (Boost.Asio)

如何优雅地关闭 boost asio ssl 客户端?