升压ASIO流缓冲 [英] Boost ASIO streambuf

查看:89
本文介绍了升压ASIO流缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑的升压ASIO ::流缓冲类的输入序列和输出序列。

I am confused about the input sequence and output sequence in boost asio::streambuf classes.

根据本code实施例(用于发送数据)的文档中似乎缓冲器重新presenting输入序列被用于书面方式向插座和一个重新presenting输出序列是用于读取。

According to the code examples (for sending data) in the documentation it seems that the buffer representing the input sequence is used for writting to socket and the one representing the output sequence is used for reading.

示例 -

boost::asio::streambuf b;
std::ostream os(&b);
os << "Hello, World!\n";
// try sending some data in input sequence
size_t n = sock.send(b.data());
b.consume(n); // sent data is removed from input sequence

现在,有一个术语问题?

Now, is there a nomenclature problem?

推荐答案

有关的 的boost ::支持ASIO ::流缓冲 是相似的,其中在C ++标准定义,并在各种使用在标准模板库,其中的数据被写入一个输出流和数据类从输入流中读取。例如,人们可以使用的std :: cout.put()写入到输出流,而的std :: cin.get()从输入流中读取。

The nomenclature for boost::asio::streambuf is similar to that of which is defined in the C++ standard, and used across various classes in the standard template library, wherein data is written to an output stream and data is read from an input stream. For example, one could use std::cout.put() to write to the output stream, and std::cin.get() to read from the input stream.

在手动控制流缓冲输入和输出序列数据的生命周期一般是如下:

When manually controlling the streambuf input and output sequences, the general lifecycle of data is as follows:


  • 缓冲器获得分配与<一个href=\"http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_streambuf/$p$ppare.html\"相对=nofollow> prepare() 的输出序列。

  • 数据已经被写入输出序列的缓冲器后,数据将是<一个href=\"http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_streambuf/commit.html\"相对=nofollow> 提交() 版。这个已提交的数据从输出顺序取出并附加在从它可以被读取的输入序列。

  • 数据是从经由 数据()

  • 后的数据已被读出,就可以将其从输入序列由<一个除去href=\"http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_streambuf/consume.html\"相对=nofollow> 消耗()

在使用上流缓冲操作Boost.Asio的操作或使用流对象流缓冲,如的std :: ostream的,基本输入和输出序列将进行适当的管理。如果缓冲器被提供给一个操作代替,如通过传递 prepare()来读操作或数据()来写入操作,则必须明确地处理提交()消耗()

When using Boost.Asio operations that operate on streambuf or stream objects that use a streambuf, such as std::ostream, the underlying input and output sequences will be properly managed. If a buffer is provided to an operation instead, such as passing passing prepare() to a read operation or data() to a write operation, then one must explicitly handle the commit() and consume().

下面是例子code直接写从流缓冲来插座的注释版本:

Here is an annotated version of the example code which writes directly from an streambuf to a socket:

// The input and output sequence are empty.
boost::asio::streambuf b;
std::ostream os(&b);

// prepare() and write to the output sequence, then commit the written
// data to the input sequence.  The output sequence is empty and
// input sequence contains "Hello, World!\n".
os << "Hello, World!\n";

// Read from the input sequence, writing to the socket.  The input and
// output sequences remain unchanged.
size_t n = sock.send(b.data());

// Remove 'n' bytes from the input sequence. If the send operation sent
// the entire buffer, then the input sequence would be empty.
b.consume(n);

这是从套接字读取直接插入流缓冲注释的例子。注释假定你好已经接收到的话,但尚未读出,插座上

And here is the annotated example for reading from a socket directly into an streambuf. The annotations assume that the word "hello" has been received, but not yet read, on the socket:

boost::asio::streambuf b;

// prepare() 512 bytes for the output sequence.  The input sequence
// is empty.
auto bufs = b.prepare(512);

// Read from the socket, writing into the output sequence.  The
// input sequence is empty and the output sequence contains "hello".
size_t n = sock.receive(bufs);

// Remove 'n' (5) bytes from output sequence appending them to the
// input sequence.  The input sequence contains "hello" and the
// output sequence has 507 bytes.
b.commit(n);

// The input and output sequence remain unchanged.
std::istream is(&b);
std::string s;

// Read from the input sequence and consume the read data.  The string
// 's' contains "hello".  The input sequence is empty, the output
// sequence remains unchanged.
is >> s;

请注意如何在上述实施例中,蒸汽的对象处理提交和消耗流缓冲的输出和输入顺序。然而,当缓冲区本身被使用(即数据() prepare())的$ C $ç需要显式处理提交和消耗。

Note how in the above examples, the steam objects handled committed and consuming the streambuf's output and input sequences. However, when the buffers themselves were used (i.e. data() and prepare()), the code needed to explicitly handle commits and consumes.

这篇关于升压ASIO流缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆