且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在没有回调的情况下提升asio非阻塞IO

更新时间:2022-11-03 14:29:32

是,使用

Yes, using the non_blocking() method to put the socket into Asio non-blocking mode:

template<typename SyncWriteStream,
         typename ConstBufferSequence>
std::size_t write_nonblock(
    SyncWriteStream & s,
    const ConstBufferSequence & buffers)
{
    s.non_blocking(true);
    boost::system::error_code ec;
    auto bytes = s.send(buffers, 0, ec);
    if (bytes == 0 && !(ec == boost::asio::error::would_block))
        throw boost::system::system_error(ec, "write_nonblock send");
    return bytes;
}