对同一 "easy" 句柄的连续请求使用 libcurl 多接口

Using libcurl multi interface for consecutive requests for same "easy" handle

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

HTTPS 请求将需要定期执行到相同的 URL。我知道我可以保留相同的 curl_easy 句柄并将其提供给 curl_multi 句柄。我会执行 curl_multi_perform 以获得结果,但我稍后需要使用 curl_multi_perform 重新发送请求,比如在 5 分钟内。 因此,这将是使用相同易处理句柄的连续请求。但是,我不确定 curl_easy 接口如何在收到第一个请求的结果后告诉多接口何时重新发送请求。我该如何做到这一点?

(或许可以把easy handle从multi handle中去掉,等有需要的时候重新加入multi handle?)

我假设无论使用何种技术,传出请求都将使用相同的传出端口。

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

正确。来自 libcurl documentation:

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 handle 关联的现有连接,但如果先前的连接已经终止,则将建立一个具有不可预测的本地端口的新连接。