如何将boost :: beast中的序列化数据转换为字符串,以便可以以FIFO方式进行处理? [英] How can I convert serialized data in boost::beast to a string so that I could process it in a FIFO manner?

查看:454
本文介绍了如何将boost :: beast中的序列化数据转换为字符串,以便可以以FIFO方式进行处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端应用程序,需要从服务器接收http长时间运行的请求".我发送一条命令,获得响应的标头之后,我只需要接收用\r\n分隔的json数据,直到连接终止.

I have an application of a client where I need to receive http "long running requests" from a server. I send a command, and after getting the header of the response, I have to just receive json data separated by \r\n until the connection is terminated.

我设法适应了增强野兽客户端示例,以发送消息并接收标头,然后解析标头并从服务器接收响应.但是,我未能找到一种方法来序列化数据以便处理json消息.

I managed to adapt boost beast client example to send the message and receive the header and parse it and receive responses from the server. However, I failed at finding a way to serialize the data so that I could process the json messages.

可以在此中继示例.在该示例中(p是解析器,sr是序列化器,input是套接字输入流,而output是套接字输出流),在读取http标头之后,我们有了一个循环,可以连续读取从服务器上:

The closest demonstration of the problem can be found in this relay example. In that example (p is a parser, sr is a serializer, input is a socket input stream and output is an socket output stream), after reading the http header, we have a loop that reads continuously from the server:

do
{
    if(! p.is_done())
    {
        // Set up the body for writing into our small buffer
        p.get().body().data = buf;
        p.get().body().size = sizeof(buf);

        // Read as much as we can
        read(input, buffer, p, ec);

        // This error is returned when buffer_body uses up the buffer
        if(ec == error::need_buffer)
            ec = {};
        if(ec)
            return;

        // Set up the body for reading.
        // This is how much was parsed:
        p.get().body().size = sizeof(buf) - p.get().body().size;
        p.get().body().data = buf;
        p.get().body().more = ! p.is_done();
    }
    else
    {
        p.get().body().data = nullptr;
        p.get().body().size = 0;
    }

    // Write everything in the buffer (which might be empty)
    write(output, sr, ec);

    // This error is returned when buffer_body uses up the buffer
    if(ec == error::need_buffer)
        ec = {};
    if(ec)
        return;
}
while(! p.is_done() && ! sr.is_done());

我在这里不明白的几件事:

A few things I don't understand here:

  1. 我们已经阅读了标题.为什么我们需要增强野兽而不需要增强asio来读取原始tcp消息?当我尝试执行此操作时(同时使用async_read/async_read_some),我得到了零大小的无限读取.
  2. 解析器(在页面末尾)说,每条消息都需要一个新实例,但是在示例中我看不到.
  3. 由于无法读取tcp消息,是否可以将解析器/序列化器数据转换为某种字符串?甚至以FIFO方式将其写入文本文件,以便可以使用某些json库对其进行处理?我不想像示例一样使用另一个套接字.
  1. We're done reading the header. Why do we need boost beast and not boost asio to read a raw tcp message? When I tried to do that (with both async_read/async_read_some) I got an infinite reads of zero size.
  2. The documentation of parser says (at the end of the page) that a new instance is needed for every message, but I don't see that in the example.
  3. Since tcp message reading is not working, is there a way to convert the parser/serializer data to some kind of string? Even write it to a text file in a FIFO manner, so that I could process it with some json library? I don't want to use another socket like the example.

函数boost::beast::buffers()无法为解析器和序列化器编译,并且对于解析器,没有使用功能,并且序列化器的使用似乎是针对消息的特定http部分的,如果我这样做,它将触发一个断言用于body().

The function boost::beast::buffers() failed to compile for the parser and the serializer, and for the parser there's no consume function, and the serializer's consume seems to be for particular http parts of the message, which fires an assert if I do it for body().

除此之外,我还无法通过老式的std::copy从解析器和缓冲区中获取一致的数据块.我似乎不明白如何将数据组合在一起以获取数据流.接收数据时随时在.consume()处使用缓冲区会导致need buffer错误.

Besides that, I also failed at getting consistent chunks of data from the parser and the buffer with old-school std::copy. I don't seem to understand how to combine the data together to get the stream of data. Consuming the buffer with .consume() at any point while receiving data leads to need buffer error.

我真的很感谢有人解释所有这些如何协同工作的逻辑.

I would really appreciate someone explaining the logic of how all this should work together.

推荐答案

buf在哪里?您可以直接阅读std::string.调用string.resize(N),然后将buffer_body::value_type中的指针和大小设置为string.data()string.size().

Where is buf? You could read directly into the std::string instead. Call string.resize(N), and set the pointer and size in the buffer_body::value_type to string.data() and string.size().

这篇关于如何将boost :: beast中的序列化数据转换为字符串,以便可以以FIFO方式进行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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