c++ - 如何使用 boost 将流放入缓冲区

标签 c++ boost buffer

我是 Streams 和 Buffers 实现的新手,我真的不知道如何解决这个问题:

#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <thread>
#include <boost/property_tree/json_parser.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>

using tcp = boost::asio::ip::tcp;
namespace websocket = boost::beast::websocket;
namespace pt = boost::property_tree;

struct DefaultMessage {
    std::string command;
    int value;
};

void defaultMessage(DefaultMessage *dm ,pt::ptree *root) {
    root->put("command", dm->command);
    root->put("value", dm->value);
}

// Echoes back all received WebSocket messages
void do_session(tcp::socket &socket) {
    try {
        // Construct the stream by moving in the socket
        websocket::stream<tcp::socket> ws{std::move(socket)};

        // Accept the websocket handshake
        ws.accept();

        for (;;) {
            // Read a message into the buffer
            boost::beast::multi_buffer buffer;
            ws.read(buffer);

            // Make string from buffer
            auto s = boost::beast::buffers_to_string(buffer.data());

            // Create ptree root
            pt::ptree root;

            // Create array source from s
            boost::iostreams::array_source array_source(&s[0], s.size());

            // Create input stream from array source
            boost::iostreams::stream<boost::iostreams::array_source> input_stream(array_source);

            // Read the json an populate ptree root
            pt::read_json(input_stream, root);

            // Discard all in buffer
            buffer.consume(buffer.size());

            // Construct a default message
            auto message = DefaultMessage{
                root.get<std::string>("command"),
                root.get<int>("value")
            };
            defaultMessage(&message, &root);

             // **This won't compile.**
            pt::write_json(buffer, root);

            // Echo the message back
            ws.text(ws.got_text());
            ws.write(buffer.data());
        }
    }
    catch (boost::system::system_error const &se) {
        // This indicates that the session was closed
        if (se.code() != websocket::error::closed) {
            std::cerr << "Error: " << se.code().message() << std::endl;
        }
    }
    catch (std::exception const &e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
}

我想用 message 结构进行响应。所以我想我需要把它放回缓冲区。这部分是不知道怎么做。

最佳答案

如果效率不是您最关心的问题,我建议

std::ostringstream oss;
pt::write_json(oss, root);

写入一个字符串,并且

// Echo the message back
ws.text(ws.got_text());
ws.write(boost::asio::buffer(oss.str()));

将该缓冲区写出。

注意事项:

  • this does more allocations than strictly required, probably
  • Boost PropertyTree is NOT a JSON library

关于c++ - 如何使用 boost 将流放入缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580364/

相关文章:

node.js - 缓冲区通常比流处理更快吗?

c++ - 对 <std::string>std::string& 的 undefined reference

c++ - fscanf : is error or EOF possible without EOF returned?

c++ - BGL depth_first_search 颜色图错误

c++ - 如何禁用有关某些库的编译器警告?

android - ArcGIS 在 android 中使用 GeometryServer 创建缓冲区

c++ - 当我输入密码时,第一个字母与我输入的不同

c++ - 有条件地将映射键复制到 vector

c++ - Boost 1.57.0 程序选项 MSVS 2013 链接器错误

c++ - 如何在行尾添加问号?