简体   繁体   English

Asio 的 write_some 真的会立即返回吗?

[英]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 ,根据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. function 调用 (write_some) 将阻塞,直到成功写入一个或多个字节的数据,或者直到发生错误。

Here's the function:这是 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.如我们所见,传递了对缓冲区的引用,这意味着write_some的实现必须立即完全消耗缓冲区。 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. basic_stream_socket::write_some等效于 Berkley 套接字send() (或write() )function。

Normally send() blocks until all bytes has been sent.通常send()会阻塞,直到所有字节都发送完毕。 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.但在极少数情况下,它可能会在仅传输部分数据的那一刻被SIGNAL处理程序或超时SO_SNDTIMEO中断。 In this case, send returns the number of bytes sent ( one or more bytes ).在这种情况下, send返回发送的字节数( one or more bytes )。 And one should advance in the buffer and send the remaining bytes later.并且应该在缓冲区中前进并稍后发送剩余的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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