简体   繁体   English

如何使用libcurl实现http流

[英]Howto implement http streaming using libcurl

I am trying to use CURL to implement Microsoft's EWS Streaming Notifications ie HTTP Streaming where the request is sent once and the server responds with a header with "Transfer Encoding: chunked". 我正在尝试使用CURL来实现Microsoft的EWS Streaming Notifications,即HTTP Streaming,其中请求被发送一次,服务器响应带有“Transfer Encoding:chunked”的标头。 The server will send multiple keepalive or notification chunks before the final packet. 服务器将在最终数据包之前发送多个keepalive或通知块。 The chunks are terminated with cr lf. 块以cr lf终止。

If I create a standard CURL client then curl_easy_perform will not return until the final chunk is received whereas I need curl_easy_perform to return upon receipt of each chunk whereupon the application will process the received chunk and call curl_easy_perform to wait for the next chunk. 如果我创建一个标准的CURL客户端,那么curl_easy_perform将不会返回,直到收到最后一个块,而我需要curl_easy_perform在收到每个块时返回,然后应用程序将处理收到的块并调用curl_easy_perform等待下一个块。

I realize that I could process the chunk in the CURLOPT_WRITEFUNCTION callback but the architecture of the application doesn't allow for that (this is a GSOAP plugin) 我意识到我可以处理CURLOPT_WRITEFUNCTION回调中的块,但是应用程序的体系结构不允许(这是一个GSOAP插件)

Any suggestions other than switching to CURLOPT_CONNECT_ONLY and handling the write all subsequent reads with curl_easy_send and curl_easy_recv? 除了切换到CURLOPT_CONNECT_ONLY以及处理使用curl_easy_send和curl_easy_recv写入所有后续读取之外的任何建议? Which seems a shame as I will have to duplicate CURL's formatting and parsing. 这似乎很遗憾,因为我将不得不复制CURL的格式和解析。

Alan 艾伦

curl_easy_perform is completely synchronous and will return only once the entire transfer is done. curl_easy_perform是完全同步的,只有在整个传输完成后才会返回。 There's really no way around that (you already mentioned CURLOPT_CONNECT_ONLY and I wouldn't recommend that either) with this API. 这个API真的没办法(你已经提到了CURLOPT_CONNECT_ONLY ,我也不建议这样做)。

If you want control back in the same thread before the entire transfer is done, which your question suggests, you probably rather want to use the multi interface. 如果你想在整个传输完成之前控制回同一个线程,你的问题建议,你可能更想使用多接口。

Using that interface, curl_multi_perform will only do as much as it can right now without blocking and return control back to your function. 使用该接口, curl_multi_perform将只执行尽可能多的操作而不会阻塞并将控制权返回给您的函数。 It does however put the responsibility over to your code to wait for socket activity and call libcurl again when there is. 但它确实将责任交给你的代码来等待套接字活动并在有时再次调用libcurl。

(Sorry, but I don't know what restrictions a "GSOAP plugin" has and you didn't state them here, so maybe this is all crap) (对不起,但我不知道“GSOAP插件”有什么限制,你没有在这里说明,所以也许这都是废话)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM