简体   繁体   English

如何在异步读取Boost Asio C ++上获取已传输的字节数

[英]How can I get the amount of transferred bytes on asynchronous reading boost asio c++

As I've seen in Boost::asio the async read functions does not return the amount of bytes transferred but normal read functions does. 正如我在Boost :: asio中看到的那样,异步读取函数不会返回传输的字节数,但普通读取函数会返回。 How can I get the amount of bytes transferred when I use async_read_some? 使用async_read_some时如何获取传输的字节数? (Params: buffer, handler) (参数:缓冲区,处理程序)

All forms of async_read expect a " ReadHandler " callback of the form 所有形式的async_read期望该形式的ReadHandler ”回调

void handler(
  const boost::system::error_code& error, // Result of operation.

  std::size_t bytes_transferred           // Number of bytes copied into the
                                          // buffers. If an error occurred,
                                          // this will be the  number of
                                          // bytes successfully transferred
                                          // prior to the error.
); 

The second parameter of your callback will be the number of bytes read. 回调的第二个参数是读取的字节数。

The asynchronous read functions call a "handler" function (or function object) once the read is complete. 读取完成后,异步读取函数将调用“处理程序”函数(或函数对象)。 The number of bytes transferred is passed to that function; 传输的字节数传递给该函数; the signature of the function must be: 函数的签名必须是:

void handler(
    const boost::system::error_code& error, // Result of operation.
    std::size_t bytes_transferred           // Number of bytes read.
);

The requirements for read handlers are documented here 读取处理程序的要求在此处记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM