我正在尝试将 boost::asio::http::async_write 调用转换为将来运行。 查看有关 boost 的 use_future 文档和一些示例,我更改了代码,如下所示:
之前的代码运行良好
auto bytes_transferred =
boost::beast::http::async_write(*d_tcpStream, req, yield[ec]);
std::cout << "Number of Bytes transmitted: " << bytes_transferred;
将其转换为:
try{
auto write_fut = boost::beast::http::async_write(*d_tcpStream, req, boost::asio::use_future);
if (write_fut.wait_for(std::chrono::seconds(10)) == std::future_status::timeout){
std::cout << "TIMEOUTTT" << std::endl;
return 1;
}else{
auto bytes_tx = write_fut.get();
BALL_LOG_DEBUG << "Number of Bytes transmitted: " << bytes_tx
<< BALL_LOG_END;
}
}
catch (std::system_error& ex)
{
std::cerr << ex.what() << std::endl;
}
在检查另一端的服务器时,我发现上面的客户端没有传入呼叫。 看来客户端改用use_future后并没有完全触发写操作