[译]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用于查看传输结果