等待异步请求完成
我想用 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
}
}
有人能指出如何实现这一点吗?我担心我缺乏boost的知识。
编辑澄清:方法 sendTheRequest 将在某个时刻被调用。也许紧随其后 getTheResponse 将被调用,但这也可能在几(毫秒)秒后发生。这就是为什么我想异步发送请求,但也需要同步等待它。
I want to program a HTTP client with boost. I could use the asynchronous model if I manage to do the following:
- In a first step, send the request to the server.
- In a second step, either read the the response that has already arrived or wait synchronously until it arrives.
This is a class that looks similar to what I have produce:
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
}
}
Can someone point out how to realize this? I fear I am lacking the knowledge in boost.
Edit for clarification: The method sendTheRequest will be called at a point. Maybe directly after it getTheResponse will be called, but this can also happen some (milli)seconds later. Thats why I want to send the request asynchronously, but need to wait for it synchronously too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许有点晚了,但我认为这应该可以了。
std::future::get
返回 send_request_function 的值,如果尚未返回则等待,并在完成后返回。Mabye a bit late, but I think this should do it.
std::future::get
returns the value of the send_request_function or waits if that didn't return yet and returns it after it has completed.为什么不使用 boost 附带的这个示例异步 http 客户端?
Why don't you use this example async http client that came with boost?