boost::asio::streambuf

streambuf 介绍

boost::asio::streambuf和C++标准库中的流对象非常的相似,数据会写入到输出流,可以从输入流读取数据,比如可以用std::cout.put()从标准输出流输出数据,用std::cin.get()从标准输入流读取数据。当手动控制streambuf的时候,通常的过程是下面这样的:
1.streambuf通过prepare()为输出序列准备空间;
2.当数据被写入到streambuf的输出序列后,数据会被调拨走,调拨走的数据会从输出序列移动后并追加到到输入序列,
这样就可以被读取到;
3.数据通过data()函数从输入序列读取走;
4.一旦数据从输入序列读取走后,就可以用consume函数从输入序列读取走。


其中:
consume:从输入序列中移除字符
commit:将字符从输出序列移动到输入序列

当使用Boost.Asio操作streambuf或者使用streambuf的流对象的时候,比如说std::ostream,潜在的输入输出序列会被合理的管理,就是说不用在代码里调用commit和consume等函数,已经自动的调用了;但是当buffer是提供给一个操作的时候,比如说传递prepare()给一个读操作,或者data()给一个写操作,那么代码里必须显式的处理commit()函数和sonsume()函数。下面是几个例子:

下面的例子是从streambuf直接写数据到socket:
// The input and output sequence are empty.
boost::asio::streambuf b;
std::ostream os(&b);  // ostream 可以和cout做关联,接收数据的

// 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);
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
下面的例子是从socket接收数据到streambuf,这个粒子假设”hello”已经被接受了,但是没有读取:
// 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);
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
下面的例子表明,全部使用流对象操作streambuf的时候,不需要用consume函数和commit()函数:
 boost::asio::streambuf b;
  std::ostream os(&b);

  os << "H#e#l#l#o#W#o#r#l#d#!\n";
  std::string str;

  std::istream is(&b);
  while(b.size() !=0)
  {
    std::getline(is, str, '#');
    std::cout << str << std::endl;
  }

通过上面的例子可以看出,流对象可以自己调用commited和consuming操作来处理streambuf的输出和输入序列;但是当streambuf的缓冲自己被使用的的时候(比如通过data()或prepare()函数),那么必须用代码显式的调用commits和consumes。

streambuf 和 string 转换

  asio::streambuf b;
  asio::streambuf::mutable_buffers_type bufs = b.prepare(512);    // reserve 512 bytes in output sequence
  size_t n = sock.receive(bufs);
  b.commit(n);    // received data is "committed" from output sequence to input sequence

// 第一种将b转为istream 然后转为s
  std::istream is(&b);
  std::string s;
  is >> s;
// 第二种将b转为const char *
const char *data_ptr = boost::asio::buffer_cast< const char* >(m_buffer->data());
 std::string line(data_ptr ,data_ptr+n )

// 第三种调用buffers_begin
 asio::streambuf::const_buffers_type bufs = sb.data();
 std::string line(asio::buffers_begin(bufs), asio::buffers_begin(bufs) + n); 

引用文章:
async_read_until函数以及streambuf如何使用
boost.asio系列——buffer

你可能感兴趣的:(boost::asio::streambuf)