[英]Read data into a circular buffer
Is it possible to use boost::circular_buffer
with boost::asio
? 是否可以将boost::circular_buffer
与boost::asio
?
Specifically I want to read a fixed number of bytes with boost::asio::async_write
and store them directly in the circular buffer without copying. 具体来说,我想用boost::asio::async_write
读取固定数量的字节,并将它们直接存储在循环缓冲区中而不进行复制。
Some example code would be very nice! 一些示例代码会非常好!
As of right now (Boost 1.66), it is not possible to read data into boost::circular_buffer
because it doesn't expose any way to reserve space in the underlying buffer, which is a requirement for creating a mutable_buffer
needed to call asio::read
. 截至目前(Boost 1.66),不可能将数据读入boost::circular_buffer
因为它不会暴露任何方式来保留底层缓冲区中的空间,这是创建调用asio::read
所需的mutable_buffer
的要求asio::read
。
But it is possible to write from boost::circular_buffer
: 但是可以从boost::circular_buffer
写一下:
boost::circular_buffer<char> cir_buf;
FillBuffer(cir_buf);
// Construct a buffer sequence with either 1 or 2 data chunks
std::vector<boost::asio::const_buffer> buffer_sequence;
auto arr1 = cir_buf.array_one();
buffer_sequence.push_back(boost::asio::buffer(arr1.first, arr1.second));
auto arr2 = cir_buf.array_two();
if (arr2.second != 0) {
buffer_sequence.push_back(boost::asio::buffer(arr2.first, arr2.second));
}
boost::asio::write(socket_, buffer_sequence);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.