0

我正在使用boost::beast以建立基于客户端的 http 会话并发送/接收消息。

我的连接被定义为一个类成员,只要它没有被对等方断开,我就可以重新使用它。

class myClass {
      boost::beast::ssl_stream<boost::beast::tcp_stream> stream_;
}

这是调用 myClass c'tor 时我连接到服务器的方式。

get_lowest_layer(stream_).async_connect(results, yield[ec_]);

stream_.async_handshake(boost::asio::ssl::stream_base::client, yield[ec_]);

boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));

这就是我发送按需消息的方式:

  http::request<http::string_body> req(http::verb::post, myPostUri, 12);
  req.set(boost::beast::http::field::content_length,
          std::to_string(someJsonText.length()));
  req.set(boost::beast::http::field::body, someJsonText);
  req.set(boost::beast::http::field::content_type, "application/json");
  req.prepare_payload();
  http::write(stream_, req);

  boost::beast::flat_buffer buffer;
  http::response<boost::beast::http::dynamic_body> res;
  http::read(stream_, buffer, res);

我想添加一些异常处理程序以解决以下情况:

  1. 客户端尝试连接时服务器处于脱机状态。所以它应该重试重新连接,直到服务器重新联机。

  2. 服务器重置连接(对等方重置连接)。发生这种情况是由于一段时间未使用连接。所以我想重试并恢复连接。

目前,当其中一种情况发生时,我得到以下异常

libc++abi: terminating with uncaught exception of type 
boost::wrapexcept<boost::system::system_error>: end of stream

terminating with uncaught exception of type 
boost::wrapexcept<boost::system::system_error>: end of stream

所以我知道一般该怎么做,但我不知道怎么做。我应该尝试重新连接还是执行初始化流程中的其他步骤(ssl 握手)

也许有人可以告诉我如何做对吗?

4

0 回答 0