当使用async_write_some和async_write时 [英] When use async_write_some and async_write
问题描述
我正在阅读Boost Asio的文档,并说
I was reading the documentation of Boost Asio and it says that
boost::asio::async_write_some
可能不会将所有数据传输到对等方.如果需要确保在异步操作完成之前已写入所有数据,请考虑使用async_write
函数.
boost::asio::async_write_some
may NOT transfer all of the data to the peer. Consider using theasync_write
function if you need to ensure that all data is written before the asynchronous operation completes.
所以这是我的问题,在什么情况下我们应该使用它们,对确保所有数据都被写入非常重要吗?什么时候使用async_write_some只是这个功能在我看来没用?
So here is my question, in which cases should we use them, isn`t VERY important to ensure that all the data is written?! when to use async_write_some just this function seems to me useless?
推荐答案
async_write
在请求-答复模式下很有用. IE.发送邮件后切换到阅读模式时.
async_write
is useful in request-reply mode when you need to send a specific response that fully fits in your buffer. I.e. when you switch to reading mode after the message is sent.
但是,如果您需要发送的数据多于写缓冲区中的数据,那么是否发送整个缓冲区都没有关系,因为您还有更多的数据要发送.在那种情况下,确保整个缓冲区的发送效率将很低,因为最后一个块可能比如果您在循环中仅调用async_write_some
时要小得多,从而在缓冲区中填充了更多的数据.为了获得最佳性能,您需要确保发送缓冲区中始终有足够的数据来填充整个帧.
But if you need to send more data than you have in your write buffer then it doesn't matter if the whole buffer is sent or not, because you still have more data to send. In that case ensuring that the whole buffer is sent is going to be inefficient because the last chunk may be smaller than it could have been if you just called async_write_some
in a loop, filling up the buffer with more data as you go. For optimal performance you want to ensure there is always enough data in the send buffer to fill a full frame.
这篇关于当使用async_write_some和async_write时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!