2

我想使用 boost::beast 来读写 etcd。首先,我希望能够用 boost beast来做这些例子。它们很容易用 curl 完成。Etcd 可以看作是一个键/值存储。设置/获取(示例页面中的放置/范围)的功能很容易使用boost::beast 客户端示例。那里没有问题。

但是“看”,我不明白。根据docs, watch 是一个连续的流,与其他会话不同,后者是在检索结果后立即终止的会话。curl 示例显示了在手表仍处于活动状态时更改值和现场响应。我应该使用相同的流来执行与该手表相关的所有事情,包括停止它。

我的问题大致是:如何在 boost::beast 中实现这一点

假设从客户端示例中我通过一个线程提交ioc.run

std::thread t(&std::iocontext::run, &ioc);
t.detach();

现在我可以完全控制主线程中的客户端。我应该创建新的 http 请求并async_write通过套接字对象提交它们吗?但是如果我这样做,我会失去 boost::beast 用 nice 包装 http 标头的功能http::request<http::string_body>。我应该手动创建标题吗?还是我应该只发送json某种行终止符来指示消息已结束?通信协议是什么样的?

boost::beast 的一个例子会很棒。

4

1 回答 1

1

看起来 etcd 使用“长时间运行的请求”。为此,您希望使用http::read_header[1] 或http::async_read_header[2] 来获取响应标头,然后在循环中使用http::read_some[3] 或http::async_read_some[4] 来读取响应正文的部分。为此,您需要使用http::buffer_body专为此类事情设计的 [5]。文档中的 HTTP Relay 示例 [6] 演示了 的使用buffer_body,并且可以适应处理长时间运行的请求。

[1] https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/ref/boost__beast__http__read_header/overload2.html

[2] https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/ref/boost__beast__http__async_read_header.html

[3] https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/ref/boost__beast__http__read_some/overload2.html

{4] https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/ref/boost__beast__http__async_read_some.html

[5] https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/ref/boost__beast__http__buffer_body.html

[6] https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/more_examples/http_relay.html

于 2019-04-09T13:43:56.723 回答