使用流缓冲缓冲升压ASIO读写 [英] Use streambuf as buffer for boost asio read and write

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

问题描述

我使用这个code阅读

I'm using this code for reading

  socket_.async_read_some(boost::asio::buffer(data_, max_length),
        boost::bind(&session::handle_read, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));

和本写

boost::asio::async_write(socket_,
    boost::asio::buffer(data_, bytes_transferred),
    boost::bind(&session::handle_write, this,
    boost::asio::placeholders::error));

其中socket_是插座,MAX_LENGTH是枚举值为1024和data_中的数据是字符数组MAX_LENGTH的长度。

where socket_ is socket, max_length is enum with value 1024 and data_ is char array with length of max_length.

不过,我想用流缓冲来代替字符数组缓冲区。我试过

But I want to replace char array buffer with streambuf. I've tried

  boost::asio::streambuf streamBuffer;
  socket_.async_read_some(boost::asio::buffer(streamBuffer),
        boost::bind(&session::handle_read, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));

但不工作。我该怎么办呢?

But isn't working. How can I do it ?

推荐答案

您需要获得一个<一个href=\"http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_streambuf/mutable_buffers_type.html\"><$c$c>mutable_buffers_type流缓冲作为你的第一个参数使用 async_read_some

You need to get a mutable_buffers_type from the streambuf to use as your first parameter to async_read_some.

  boost::asio::streambuf streamBuffer;
  boost::asio::streambuf::mutable_buffers_type mutableBuffer =
      streamBuffer.prepare(max_length);
  socket_.async_read_some(boost::asio::buffer(mutableBuffer),
        boost::bind(&session::handle_read, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));

查看 ASIO 文件<一个href=\"http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_streambuf.html\">here和这里获取更多信息。

See the asio documentation here and here for more info.

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

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