使用wininet进行异步请求
我已经使用 wininet 发送一些同步 HTTP 请求。现在,我想更进一步,想要异步请求一些内容。
目标是获得类似“反向代理”的东西。我发送一个 HTTP 请求,一旦有人想要联系我,该请求就会延迟得到答复。我的线程应该继续下去,就好像同时没有任何事情一样,并且一旦响应到达,就应该在该线程中调用回调。请注意,我不想要第二个线程来处理回复(如果有必要,它应该只提供某种机制来中断主线程以在那里调用回调)!
更新:也许,描述我想要的最好方法是像 JavaScript 中那样的行为,其中只有一个线程,但可以发送 AJAX 请求,然后导致在此主线程中调用回调。
因为我想了解它是如何工作的,所以我不需要库解决方案。有人知道一些好的教程可以解释我如何实现我想要的行为吗?
I have already used wininet to send some synchronous HTTP requests. Now, I want to go one step further and want to request some content asynchronously.
The goal is to get something "reverse proxy"-like. I send an HTTP request which gets answered delayed - as soon as someone wants to contact me. My thread should continue as if there was nothing in the meanwhile, and a callback should be called in this thread as soon as the response arrives. Note that I don't want a second thread which handles the reply (if it is necessary, it should only provide some mechanism which interrupts the main thread to invoke the callback there)!
Update: Maybe, the best way to describe what I want is a behaviour like in JavaScript where you have only one thread but can send AJAX requests which then result in a callback being invoked in this main thread.
Since I want to understand how it works, I don't want library solutions. Does anybody know some good tutorial which explains me how to achieve my wanted behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这里要求的基本上是来自(而不是转到)。这是一条神话般的指令,实际上并不存在。调用代码的唯一方法是在发出线程中进行轮询,或者使用一个单独的线程来执行同步 IO,然后使用结果执行回调(在该线程中或在另一个生成的线程中) 。
当我在 C++ 中使用套接字时,我设置了一个专用线程来迭代所有打开的套接字,轮询可用而不会阻塞的数据,获取数据并将其填充到缓冲区中,将缓冲区发送到回调上的回调给定的情况(EOL、EOF,诸如此类)。
What you're asking for here is basically COME FROM (as opposed to GO TO). This is a mythical instruction which doesn't really exist. The only way you can get your code called is to either poll in the issuing thread, or to have a separate thread which is performing the synchronous IO and then executing the callback (in that thread, or in yet another spawned thread) with the results.
When I was working in C++ with sockets I set up a dedicated thread to iterate over all the open sockets, poll for data which would be available without blocking, take the data and stuff it in a buffer, sending the buffer to a callback on a given circumstance (EOL, EOF, that sort of thing).
除非您的主线程正在侦听消息队列之类的内容,否则实际上没有办法劫持它并启动它执行当前正在执行的代码之外的代码。
看看 boost::asio 是如何工作的,它基本上可以让你异步地进行连接、读取、写入等...例如,你使用主(或任何)线程启动异步读取,asio 然后使用重叠 IO 来询问操作系统通知它 IO 完成。当异步读取完成时,您的回调将由工作线程之一执行。
您需要做的就是确保使用主线程或工作线程调用 io_service::run() 来处理 IO 完成队列。您调用 run 的任何线程都将是执行回调的线程。
如果您遵循规则,Asio 有一些保证可以使这种多线程方法相当健壮。
查看 asio 文档,即使你不打算使用它,如果这是你想自己解决的问题,那么很多模式和想法都非常有趣。
如果您不想看它,请记住,在 Windows 上执行异步 IO 的方法称为“重叠 IO”。
Unless your main thread is listening to something like a message queue there isn't really a way to just hijack it and start it executing code other than what it is currently doing.
Take a look at how boost::asio works, it basically lets you asyncronously do connects, reads, writes, etc... For example you start an async read with the primary (or any) thread, asio then uses overlapped IO to ask the OS to notify it of IO completion. When the async read completes your callback will be executed by one of the worker threads.
All you need to do is to be sure to call io_service::run() with either your main thread or a worker thread to handle the IO completion queue. Any threads that you call run with will be the ones that execute the callback.
Asio has some guarantees that make this method of multithreading fairly robust if you follow the rules.
Take a look at the documentation for asio even if you don't plan to use it, a lot of the patterns and ideas are quite interesting if this is something you want to tackle yourself.
If you don't want to look at it, remember, on Windows the method of doing async IO is called "Overlapped IO".