如何"prepare "字符数组来提升::asio::streambuf::mutable_buffers_type缓冲区?

How can I "prepare " char arrays to boost::asio::streambuf::mutable_buffers_type buffers?

本文关键字:buffers mutable type 缓冲区 streambuf 如何 字符 prepare 数组 asio      更新时间:2023-10-16

在我的项目中,我将各种文件读取到不同的缓冲区中,然后希望使用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));
}