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

时间:2018-09-12 08:06:42

标签: c++ c++11 boost udp boost-asio

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定义的Boost 1.67.0,WIN10和vs2017 BOOST_ASIO_DISABLE_IOCP和BOOST_ASIO_CANCELIO

我没明白吗?

如果我是正确的,如何解决此问题?

1 个答案:

答案 0 :(得分:1)

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

将代码简化为:

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