Eri*_*rik 2 c++ boost boost-asio
我想用boost编程一个HTTP客户端。如果可以执行以下操作,则可以使用异步模型:
这是一个与我产生的类相似的类:
class HTTPClient {
public:
void sendTheRequest(...) {
// Send the HTTP request
}
std::string getTheResponse(...) {
// return the already (asynchronously) received response, or wait for it
// in this function
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以指出如何实现这一点吗?我担心我缺乏增强知识。
编辑以澄清问题:将在某个时间点调用sendTheRequest方法。也许直接在getTheResponse之后会被调用,但这也可能在几毫秒后发生。这就是为什么我要异步发送请求,但也需要同步等待它的原因。
Mabye有点晚,但是我认为应该这样做。std::future::get返回send_request_function的值,或者等待该值尚未返回,并在完成后返回它。
class HTTPClient {
public:
void sendTheRequest(...) {
// Send the HTTP request
f = std::async(std::launch::async, send_request_function, args ...);
}
std::string getTheResponse(...) {
// return the already (asynchronously) received response, or wait for it
// in this function
return f.get();
}
private:
std::future<std::string> f;
}
Run Code Online (Sandbox Code Playgroud)