使用libcurl多接口来连续请求相同的“简单"请求.处理 [英] Using libcurl multi interface for consecutive requests for same "easy" handle

查看:164
本文介绍了使用libcurl多接口来连续请求相同的“简单"请求.处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的开发组织对线程和select()有自己的包装器实现.需要增强该应用程序以执行HTTPS请求,并且我决定使用libcurl.经过研究后,我发现curl_easy_perform是一个阻塞调用,因此我决定对非阻塞调用使用curl_multi_perform方法以允许线程中的其他工作.

My development organization has its own wrapper implementation for threads and select(). The application needs to be enhanced to perform HTTPS requests, and I've decided to use libcurl. After some research, I see that the curl_easy_perform is a blocking call, so I've decided to use the curl_multi_perform approach for non-blocking calls to allow other work in the threads.

HTTPS请求将需要定期对相同的URL执行.我知道我可以保持相同的curl_easy句柄并将其赋予curl_multi句柄.我将执行curl_multi_perform以获得结果,但稍后需要使用curl_multi_perform重新发送请求,例如在5分钟内. 因此,这将是使用相同简单句柄的连续请求.但是,我不确定在收到第一个请求的结果后curl_easy接口如何告知多接口何时重新发送请求.我该如何完成?

The HTTPS request will need to be performed periodically to the same URL. I know I can keep the same curl_easy handle and give that to the curl_multi handle. I would perform the curl_multi_perform to get the results, but I would later need to use curl_multi_perform to resend the request, say in 5 minutes. So, this would be a consecutive request using the same easy handle. However, I'm not sure how the curl_easy interface tells the multi interface as to when to resend the request after I have received results from the first request. How do I accomplish this?

(也许从多句柄中删除了easy句柄,然后在再次需要一个请求时将其重新添加到多句柄中?)

(Maybe drop the easy handle from the multi handle, and re-add it to the multi handle when a request is needed again?)

我认为无论使用什么技术,传出请求都将使用相同的传出端口.

I presume that whatever technique is used, that the outgoing request will be using the same outgoing port.

推荐答案

(也许从多句柄中删除了easy句柄,然后在再次需要一个请求时将其重新添加到多句柄中?)

(Maybe drop the easy handle from the multi handle, and re-add it to the multi handle when a request is needed again?)

正确.从libcurl 文档:

Correct. From the libcurl documentation:

单次传输完成后,仍将易手柄添加到多堆栈中.您需要先使用curl_multi_remove_handle删除easy句柄,然后使用curl_easy_cleanup将其关闭,或者可能为其设置新选项,然后使用curl_multi_add_handle再次添加它以开始另一个传输.

When a single transfer is completed, the easy handle is still left added to the multi stack. You need to first remove the easy handle with curl_multi_remove_handle and then close it with curl_easy_cleanup, or possibly set new options to it and add it again with curl_multi_add_handle to start another transfer.

.

我认为无论使用哪种技术,传出请求都将使用相同的传出端口

I presume that whatever technique is used, that the outgoing request will be using the same outgoing port

不能保证. libcurl将尝试重用与easy句柄关联的现有连接,但是如果先前的连接已终止,则将建立具有不可预测的本地端口的新连接.

This is not guaranteed. libcurl will attempt to re-use existing connections that are associated with the easy handle, but if the previous connection has already died then a new connection with an unpredictable local port will be established.

这篇关于使用libcurl多接口来连续请求相同的“简单"请求.处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆