[译]The multi Interface

The multi Interface
multi接口

The easy interface as described in detail in this document is a synchronous interface that transfers one file at a time and doesn‘t return until it is done.
easy接口是同步的,调用同步接口传输文件,需要等到传输完毕函数才会返回。

The multi interface, on the other hand, allows your program to transfer multiple files in both directions at the same time, without forcing you to use multiple threads. The name might make it seem that the multi interface is for multi-threaded programs, but the truth is almost the reverse. The multi interface allows a single-threaded application to perform the same kinds of multiple, simultaneous transfers that multi-threaded programs can perform. It allows many of the benefits of multi-threaded transfers without the complexity of managing and synchronizing many threads.
multi接口,允许你的程序同时向不同目标传输文件,而不需要使用多线程。
multi这个名字让他看起来像是多线程的程序,但是实际上不是。
multi可以在单线程、多线程程序中执行。

To complicate matters somewhat more, there are even two versions of the multi interface. The event based one, also called multi_socket and the "normal one" designed for using with select(). See the libcurl-multi.3 man page for details on the multi_socket event based API, this description here is for the select() oriented one.
比较麻烦的是,有2个版本的multi接口。
第一个被称为multi_socket,使用的是select()机制。

To use this interface, you are better off if you first understand the basics of how to use the easy interface. The multi interface is simply a way to make multiple transfers at the same time by adding up multiple easy handles into a "multi stack".
要使用这个接口,最好先了解如何使用easy接口。
Multi接口同时传输文件是通过把多个easy_curl的句柄添加到“multi stack”中。

You create the easy handles you want, one for each concurrent transfer, and you set all the options just like you learned above, and then you create a multi handle with curl_multi_init and add all those easy handles to that multi handle with curl_multi_add_handle.
创建easy句柄后,作为并发传输的句柄之一,并设置你所需要的属性,
然后使用curl_multi_init函数创建一个multi句柄,使用curl_multi_add_handle函数添加所有easy句柄到multi句柄中。

When you‘ve added the handles you have for the moment (you can still add new ones at any time), you start the transfers by calling curl_multi_perform.
你可以随时添加easy句柄到multi句柄中,然后使用curl_multi_perfrorm函数执行传输。

curl_multi_perform is asynchronous. It will only perform what can be done now and then return back control to your program. It is designed to never block. You need to keep calling the function until all transfers are completed.
curl_multi_perform异步的,这个函数只执行当前能执行的操作,等到全部执行完毕再回调通知程序。
你需要保持这个函数的调用,直到所有传输完成。

The best usage of this interface is when you do a select() on all possible file descriptors or sockets to know when to call libcurl again. This also makes it easy for you to wait and respond to actions on your own application‘s sockets/handles. You figure out what to select() for by using curl_multi_fdset, that fills in a set of fd_set variables for you with the particular file descriptors libcurl uses for the moment.
这个接口最好的用法是:你select()所有文件描述符或者sokect,来查看libcurl是否通知了。
这也使得你的程序在等待、回复时更简单。
使用curl_multi_fdset函数来找出应该select()什么,填入参数为你当前使用的libcurl文件描述符的fd_set对。

When you then call select(), it‘ll return when one of the file handles signal action and you then call curl_multi_perform to allow libcurl to do what it wants to do. Take note that libcurl does also feature some time-out code so we advise you to never use very long timeouts on select() before you call curl_multi_perform again. curl_multi_timeout is provided to help you get a suitable timeout period.
当调用select()时候,当检测到文件句柄有信号会返回,
你可以调用curl_multi_perform来允许libcurl执行。
需要注意的是libcurl会超时,所以建议绝对不要在调用curl_multi_perform之前,设置长时间的select()设置长时间的超时。
curl_multi_timeout函数用于帮你获取一个合适的超时时间。

Another precaution you should use: always call curl_multi_fdset immediately before the select() call since the current set of file descriptors may change in any curl function invoke.
其他注意点:在调用select()前,使用curl_multi_fdset,防止当前文件描述符对被其他libcurl函数改变。

If you want to stop the transfer of one of the easy handles in the stack, you can use curl_multi_remove_handle to remove individual easy handles. Remember that easy handles should be curl_easy_cleanuped.
如果你想停止multi stack中的easy handle,你可以使用curl_mult_remove_handle函数来移除一个easy handle。
之后记得要使用curl_easy_cleanuped。

When a transfer within the multi stack has finished, the counter of running transfers (as filled in by curl_multi_perform) will decrease. When the number reaches zero, all transfers are done.
当multi stack中的一个传输完成,运行中的传输计数(curl_multi_perform函数的参数)会减一。
当计数为0,表示所有传输完成。

curl_multi_info_read can be used to get information about completed transfers. It then returns the CURLcode for each easy transfer, to allow you to figure out success on each individual transfer.
curl_multi_info_read用于获取已完成的传输信息,这个函数会返回每个easy handle传输的CURLcode,
CURLcode用于查看传输结果

时间: 2024-10-07 16:53:01

[译]The multi Interface的相关文章

[译]curl_multi_add_handle

NAMEcurl_multi_add_handle - add an easy handle to a multi session添加easy handle到multi session中 SYNOPSIS#include <curl/curl.h>CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *easy_handle); DESCRIPTIONAdds a standard easy handle to the multi

[译]curl_multi_perform

http://curl.haxx.se/libcurl/c/curl_multi_perform.html curl_multi_perform.3 -- man page NAMEcurl_multi_perform - reads/writes available data from each easy handle在每个easy handle上读写数据 SYNOPSIS#include <curl/curl.h>CURLMcode curl_multi_perform(CURLM *mu

[译]libcurl错误码

CURLcode Almost all "easy" interface functions return a CURLcode error code. No matter what, using the curl_easy_setopt option CURLOPT_ERRORBUFFER is a good idea as it will give you a human readable error string that may offer more details about

实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现

之前我们已经详细介绍了WinHttp接口如何实现Http的相关功能.本文我将主要讲解如何使用libcurl库去实现相关功能.(转载请指明出于breaksoftware的csdn博客) libcurl在http://curl.haxx.se/libcurl/有详细的介绍,有兴趣的朋友可以去读下.本文我只是从实际使用的角度讲解其中的一些功能. libcurl中主要有两个接口类型:CURL和CURLM.CURL又称easy interface,它接口简单.使用方便,但是它是一个同步接口,我们不能使用它

libcurl教程

名称 libcurl 的编程教程 目标 本文档介绍使用libcurl编程的一般原则和一些基本方法.本文主要是介绍 c 语言的调用接口,同时也可能很好的适用于其他类 c 语言的接口. 跨平台的可移植代码 libcurl库背后的开发人员投入了相当大的努力确保libcurl可以在很多不同的系统和环境里工作. 全局的准备 程序必须初始化一些libcurl的全局函数.这意味着不管你准备使用libcurl多少次,你都应该,且只初始化一次.当你的程序开始的时候,使用 curl_global_init() 这个

(转) c/c++调用libcurl库发送http请求的两种基本用法

libcurl主要提供了两种发送http请求的方式,分别是Easy interface方式和multi interface方式,前者是采用阻塞的方式发送单条数据,后者采用组合的方式可以一次性发送多条数据 一.Easy interface libcurl的easy interface是最基本的用法,简要流程为: 1.在主线程中调用curl_global_init(CURL_GLOBAL_ALL)初始化 2.调用curl_easy_init获取一个句柄: 3.调用curl_easy_setopt函数

linux c libcurl的简单使用(转)

curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: 1 #include <curl/curl.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 CURL *curl; 6 CURLcode res; 7 8 size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) 9 { 10 i

Libcurl笔记二

一: multi与easy接口的不同处The multi interface offers several abilities that the easy interface doesn't. They are mainly:1. Enable a "pull" interface. The application that uses libcurl decides where and when to ask libcurl to get/send data.2. Enable mul

curl 基本使用简介

curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <stdio.h> #include <string.h> CURL *curl; CURLcode res; size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) { if (strlen((char *)str