简体   繁体   中英

Does Asio's write_some really return immediately?

According to https://www.boost.org/doc/libs/1_73_0/doc/html/boost_asio/reference/basic_stream_socket/write_some/overload1.html ,

The function call (write_some) will block until one or more bytes of the data has been written successfully, or until an error occurs.

Here's the function:

template<
    typename ConstBufferSequence>
std::size_t write_some(
    const ConstBufferSequence & buffers);

as we see, a reference to the buffer is passed, which means that the implementation of write_some must consume the buffer immediately and entirely. It cannot borrow the buffer for itself to write (to a file descriptor) later.

However, the explanation in the page suggests that it does exactly that: once the first byte is written it can return and continue to write the remaining bytes. How is it possible? The reference to buffer may be destructed after the call.

basic_stream_socket::write_some is equivalent of Berkley socket send() ( or write() ) function.

Normally send() blocks until all bytes has been sent. But in rare cases, it can be interrupted by SIGNAL handler or timeout SO_SNDTIMEO at the moment when only part of the data has been transmitted. In this case, send returns the number of bytes sent ( one or more bytes ). And one should advance in the buffer and send the remaining bytes later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM