如何"prepare "字符数组来提升::asio::streambuf::mutable_buffers_type缓冲区?
How can I "prepare " char arrays to boost::asio::streambuf::mutable_buffers_type buffers?
在我的项目中,我将各种文件读取到不同的缓冲区中,然后希望使用boost::asio::streambuf将这些缓冲区连接在一起以加快进程。我在中读过这个例子http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/reference/streambuf.html。
boost::asio::streambuf b;
// reserve 512 bytes in output sequence
boost::asio::streambuf::mutable_buffers_type bufs = b.prepare(512);
size_t n = sock.receive(bufs);
// received data is "committed" from output sequence to input sequence
b.commit(n);
std::istream is(&b);
std::string s;
is >> s;
我的问题是,在我"准备"了bufs之后,如何手动将多个char数组(char buf[512])按顺序分配给boost::asio::streambuf::mutable_buffers_type bufs?例如,我想知道是否有一种方法可以做到:
some_copy_func(bufs, array1,copy_size1);
some_copy_func(bufs, array2,copy_size2);
some_copy_func(bufs, array3,copy_size3);
在这些复制之后,array1、array2和array3将按相同的顺序复制到bufs。在我打电话之后b.commit(copy_size 1+copy_size 2+copy_size 1),这些数组中的所有缓冲区都将在b的istream端可用。
我试图搜索boost文档,但找不到任何方法。我知道有boost::asio::buffer_copy()可以将外部缓冲区复制到"bufs",但这个buffer_copy()会破坏bufs的旧数据。有人在这个问题上有类似的经历吗?谢谢
ostream
最简单的方法是按预期使用streambuf。。。作为流:
boost::asio::streambuf sb;
{
char a[512], b[512], c[512];
std::ostream os(&sb);
os.write(a, sizeof(a));
os.write(b, sizeof(b));
os.write(c, sizeof(c));
}
copy
boost::asio::streambuf sb;
{
boost::asio::streambuf::mutable_buffers_type bufs = sb.prepare(3 * 512);
auto it = buffers_begin(bufs);
char a[512], b[512], c[512];
it = std::copy_n(a, sizeof(a), it);
it = std::copy_n(b, sizeof(b), it);
it = std::copy_n(c, sizeof(c), it);
sb.commit(sizeof(a) + sizeof(b) + sizeof(c));
}
相关文章:
- DirectX11 IASetVertexBuffers with nullptrs or empty buffers
- 如何创建一个 cl::sycl::buffers 数组?
- 修改Boost多索引项的非索引字段的最佳方法:modify vs mutable
- 为什么"static mutable int n;"不允许进入C++?
- 为什么'mutable' lambda 函数属性,而不是捕获类型?
- Visual Studio 2010 C++ /w Google Protocol Buffers.找不到 60 个外部
- 这是"mutable" C++的一个很好的用例吗?
- 用于构建C++ Google Protocol Buffers Project的生成文件
- 将Google Protocol Buffers.proto文件集成到Visual C++2010中
- 如何用XML格式化Google Protocol Buffers对象并从XML进行解析
- 为什么我不能声明对可变对象的引用?( "reference cannot be declared mutable" )
- C++ Lambdas:"mutable"和按引用捕获之间的区别
- 使用Protocol Buffers检索带有字段描述符的父消息中的字段名
- 使用Protocol Buffers描述符对象从.proto文件中读取注释
- Const and mutable