<i id='iwrRJ'><tr id='iwrRJ'><dt id='iwrRJ'><q id='iwrRJ'><span id='iwrRJ'><b id='iwrRJ'><form id='iwrRJ'><ins id='iwrRJ'></ins><ul id='iwrRJ'></ul><sub id='iwrRJ'></sub></form><legend id='iwrRJ'></legend><bdo id='iwrRJ'><pre id='iwrRJ'><center id='iwrRJ'></center></pre></bdo></b><th id='iwrRJ'></th></span></q></dt></tr></i><div id='iwrRJ'><tfoot id='iwrRJ'></tfoot><dl id='iwrRJ'><fieldset id='iwrRJ'></fieldset></dl></div>
    <legend id='iwrRJ'><style id='iwrRJ'><dir id='iwrRJ'><q id='iwrRJ'></q></dir></style></legend>

    <tfoot id='iwrRJ'></tfoot>

      <small id='iwrRJ'></small><noframes id='iwrRJ'>

      • <bdo id='iwrRJ'></bdo><ul id='iwrRJ'></ul>

      1. 提升 ASIO 流缓冲

        Boost ASIO streambuf(提升 ASIO 流缓冲)
          <bdo id='JCXvH'></bdo><ul id='JCXvH'></ul>
            <legend id='JCXvH'><style id='JCXvH'><dir id='JCXvH'><q id='JCXvH'></q></dir></style></legend>

              1. <tfoot id='JCXvH'></tfoot>

                <small id='JCXvH'></small><noframes id='JCXvH'>

                    <tbody id='JCXvH'></tbody>

                  <i id='JCXvH'><tr id='JCXvH'><dt id='JCXvH'><q id='JCXvH'><span id='JCXvH'><b id='JCXvH'><form id='JCXvH'><ins id='JCXvH'></ins><ul id='JCXvH'></ul><sub id='JCXvH'></sub></form><legend id='JCXvH'></legend><bdo id='JCXvH'><pre id='JCXvH'><center id='JCXvH'></center></pre></bdo></b><th id='JCXvH'></th></span></q></dt></tr></i><div id='JCXvH'><tfoot id='JCXvH'></tfoot><dl id='JCXvH'><fieldset id='JCXvH'></fieldset></dl></div>
                  本文介绍了提升 ASIO 流缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 boost asio::streambuf 类中的输入序列和输出序列感到困惑.

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

                  根据文档中的代码示例(用于发送数据),表示输入序列的缓冲区用于写入套接字,而表示输出序列的缓冲区用于读取.

                  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!
                  ";
                  // try sending some data in input sequence
                  size_t n = sock.send(b.data());
                  b.consume(n); // sent data is removed from input sequence
                  

                  现在有命名问题吗?

                  推荐答案

                  boost::asio::streambuf 与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.

                  手动控制streambuf输入输出序列时,数据的大致生命周期如下:

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

                  • 缓冲区使用 prepare() 用于输出序列.
                  • 将数据写入输出序列的缓冲区后,数据将为 commit().提交的数据将从输出序列中删除,并附加到可以从中读取数据的输入序列中.
                  • 从通过 data().
                  • 读取数据后,可以通过consume().

                  当使用 Boost.Asio 操作对 streambuf 或使用 streambuf 的流对象进行操作时,例如 std::ostream,底层的输入和输出序列将得到妥善管理.如果为某个操作提供缓冲区,例如将 prepare() 传递给读操作或将 data() 传递给写操作,则必须显式处理commit()consume().

                  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().

                  这是示例代码的注释版本,它直接从 streambuf 写入套接字:

                  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!
                  ".
                  os << "Hello, World!
                  ";
                  
                  // 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);
                  

                  这里是从套接字直接读取到 streambuf 的带注释的示例.注释假定套接字上已收到单词hello",但尚未读取:

                  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;
                  

                  注意在上面的例子中,steam 对象如何处理提交和消耗流缓冲的输出和输入序列.但是,当使用缓冲区本身时(即 data()prepare()),需要显式处理提交和消耗的代码.

                  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 流缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                  • <i id='aAKin'><tr id='aAKin'><dt id='aAKin'><q id='aAKin'><span id='aAKin'><b id='aAKin'><form id='aAKin'><ins id='aAKin'></ins><ul id='aAKin'></ul><sub id='aAKin'></sub></form><legend id='aAKin'></legend><bdo id='aAKin'><pre id='aAKin'><center id='aAKin'></center></pre></bdo></b><th id='aAKin'></th></span></q></dt></tr></i><div id='aAKin'><tfoot id='aAKin'></tfoot><dl id='aAKin'><fieldset id='aAKin'></fieldset></dl></div>

                    <small id='aAKin'></small><noframes id='aAKin'>

                    <legend id='aAKin'><style id='aAKin'><dir id='aAKin'><q id='aAKin'></q></dir></style></legend>

                          <tbody id='aAKin'></tbody>

                          <bdo id='aAKin'></bdo><ul id='aAKin'></ul>

                          1. <tfoot id='aAKin'></tfoot>