读取直到boost :: asio :: streambuf中的字符串定界符 [英] Read until a string delimiter in boost::asio::streambuf

查看:153
本文介绍了读取直到boost :: asio :: streambuf中的字符串定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用非常方便的Boost

I would like to use the very convenient Boost async_read_until to read a message until I get the \r\n\r\n delimiter.

我喜欢使用此定界符,因为使用telnet调试和编写多行命令很容易.我只用两行来表示命令结束.

I like using this delimiter because it's easy to debug with telnet and make multiline commands. I just signal end of command by two new lines.

我这样叫async_read_until:

void do_read()
{
    boost::asio::async_read_until(m_socket,
                                  m_input_buffer,
                                  "\r\n\r\n",
                                  std::bind(&player::handle_read, this, std::placeholders::_1, std::placeholders::_2));
}

此时我的处理程序如下:

And my handler looks like this at the moment:

void handle_read(boost::system::error_code ec, std::size_t nr)
{
    std::cout << "handle_read: ec=" << ec << ", nr=" << nr << std::endl;

    if (ec) {
        std::cout << "  -> emit on_disconnect\n";
    } else {
        std::istream iss(&m_input_buffer);
        std::string msg;
        std::getline(iss, msg);

        std::cout << "dump:\n";
        std::copy(msg.begin(), msg.end(), std::ostream_iterator<int>(std::cout, ", "));
        std::cout << std::endl;

        do_read();
    }
}

我想像示例一样使用std::getline,但是在我的系统上,它保留了\r字符.如您所见,如果我连接到服务器并写入hello加上两个CRLF,则会得到此转储服务器端:

I wanted to use std::getline just like the example, but on my system this keeps the \r character. As you can see, if I connect to the server and write hello plus two CRLF, I get this dump server side:

handle_read: ec=system:0, nr=9
dump:
104, 101, 108, 108, 111, 13, 
                         ^^^ \r here

顺便说一句,这还将下一个新行保留在缓冲区中.因此,我认为std::getline不会为我完成这项工作.

By the way, this will also keep the next new line in the buffer. So I think that std::getline will not do the job for me.

我搜索一种方便有效的方法来读取boost::asio::streambuf,直到获得此\r\n\r\n分隔符.因为我一次使用async_read_until,所以在调用处理程序时,缓冲区应该具有确切的完整数据,不是吗?在我得到\r\n\r\n之前,您建议阅读什么?

I search a convenient and efficient way to read from the boost::asio::streambuf until I get this \r\n\r\n delimiter. Since I use async_read_until once at a time, when the handler is called, the buffer is supposed to have the exact and full data isn't it? What do you recommend to read until I get \r\n\r\n?

推荐答案

async_read_until() 操作会将所有读入的数据提交到streambuf的输入序列中,并且bytes_transferred值将包含直至(包括)第一个定界符的字节数.尽管该操作可能会读取更多数据,但不能使用分隔符,但可以使用bytes_transferred和分隔符大小来仅提取所需的数据.例如,如果可以从套接字读取cmd1\r\n\r\ncmd2,并且使用\r\n\r\n的分隔符启动async_read_until()操作,则streambuf的输入序列可以包含cmd1\r\n\r\ncmd2:

The async_read_until() operation commits all data read into the streambuf's input sequence, and the bytes_transferred value will contain the number of bytes up to and including the first delimiter. While the operation may read more data beyond the delimiter, one can use the bytes_transferred and delimiter size to extract only the desired data. For example, if cmd1\r\n\r\ncmd2 is available to be read from a socket, and an async_read_until() operation is initiated with a delimiter of \r\n\r\n, then the streambuf's input sequence could contain cmd1\r\n\r\ncmd2:

    ,--------------- buffer_begin(streambuf.data())
   /   ,------------ buffer_begin(streambuf.data()) + bytes_transferred
  /   /                - delimiter.size()
 /   /       ,------ buffer_begin(streambuf.data()) + bytes_transferred
/   /       /   ,--  buffer_end(streambud.data())
cmd1\r\n\r\ncmd2

这样,可以通过以下方式从streambuf中将cmd1提取为字符串:

As such, one could extract cmd1 into a string from the streambuf via:

// Extract up to the first delimiter.
std::string command{
  boost::asio::buffers_begin(streambuf.data(), 
  boost::asio::buffers_begin(streambuf.data()) + bytes_transferred
    - delimiter.size()};
// Consume through the first delimiter.
m_input_buffer.consume(bytes_transferred);


这是一个完整的示例演示直接从streambuf的输入序列构造std::string :


Here is a complete example demonstrating constructing std::string directly from the streambuf's input sequence:

#include <functional> // std::bind
#include <iostream>
#include <boost/asio.hpp>

const auto noop = std::bind([]{});

int main()
{
  using boost::asio::ip::tcp;
  boost::asio::io_service io_service;

  // Create all I/O objects.
  tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 0));
  tcp::socket socket1(io_service);
  tcp::socket socket2(io_service);

  // Connect sockets.
  acceptor.async_accept(socket1, noop);
  socket2.async_connect(acceptor.local_endpoint(), noop);
  io_service.run();
  io_service.reset();

  const std::string delimiter = "\r\n\r\n";

  // Write two commands from socket1 to socket2.
  boost::asio::write(socket1, boost::asio::buffer("cmd1" + delimiter));
  boost::asio::write(socket1, boost::asio::buffer("cmd2" + delimiter));

  // Read a single command from socket2.
  boost::asio::streambuf streambuf;
  boost::asio::async_read_until(socket2, streambuf, delimiter,
    [delimiter, &streambuf](
      const boost::system::error_code& error_code,
      std::size_t bytes_transferred)
    {
      // Verify streambuf contains more data beyond the delimiter. (e.g.
      // async_read_until read beyond the delimiter)
      assert(streambuf.size() > bytes_transferred);

      // Extract up to the first delimiter.
      std::string command{
        buffers_begin(streambuf.data()),
        buffers_begin(streambuf.data()) + bytes_transferred
          - delimiter.size()};

      // Consume through the first delimiter so that subsequent async_read_until
      // will not reiterate over the same data.
      streambuf.consume(bytes_transferred);

      assert(command == "cmd1");
      std::cout << "received command: " << command << "\n"
                << "streambuf contains " << streambuf.size() << " bytes."
                << std::endl;
    }
  );
  io_service.run();
}

输出:

received command: cmd1
streambuf contains 8 bytes.

这篇关于读取直到boost :: asio :: streambuf中的字符串定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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