Analysis of Libcurl (2)-The use of Libcurl to poll

Source: Internet
Author: User

The Libcurl also encapsulates both the I/O mechanisms of select and poll. The code uses a macro have_poll_fine to separate the two. If this macro is defined, use poll, otherwise select is used.
The use code for both is defined in the function Curl_poll (), and the function is defined in the file lib/select.c. To facilitate analysis, reading will separate select and poll-related code from each other and analyze independently.
This article mainly analyzes the encapsulation use of poll in Curl_poll ().
Let's look at the definitions of some of the data structures used:
typedef int CURL_SOCKET_T; #define CURL_SOCKET_BAD-1STRUCT pollfd{curl_socket_t FD;        Short events; Short revents;};/ * Refer to the Linux Man manual for the three members described below: The field FD contains a file descriptor for an open file. If This field was negative, then the corresponding events field is ignored and the Revents field returns zero. (This provides an easy to ignoring a file descriptor for a single poll () call:simply negate the FD field.) The field events is an input parameter, a bit mask specifying the events the application are interested in for the file des Criptorfd. If This field was specified as zero, then all events was ignored for FD and Revents returns zero. The field revents is a output parameter, filled by the kernel and the events that actually occurred. The bits returned inrevents can include any of those specified in events, or one of the values Pollerr, Pollhup, or POLLNV AL. (These three bits is meaningless in the events field, and would be set in the Revents field WHenever the corresponding condition is true.) */

The following are the specific implementations of Curl_poll:

/* This function is the encapsulation of poll (). If poll () does not exist, a select () substitution is used. If you are using select () and the file descriptor FD is too large to exceed fd_setsize, the error is returned. If the passed-in timeout value is a negative number, it waits indefinitely until no valid FD is provided. When this occurs (without a valid FD), the negative timeout value is ignored and the function immediately times out. return value:-1 = system call error or fd>=fd_setsize.0 = timeout. N = number of POLLFD structures returned, with revents members not 0.*/int curl_poll (struct POLLFD UFDs [], unsigned int nfds, int timeout_ms) {s        Truct Timeval Initial_tv = {0, 0};   bool Fds_none = TRUE;        Used to verify that the passed-in UFDs array is valid unsigned int i;        int Pending_ms = 0;   int error;        Save error code int r;        Detects if there is a valid FD in all FD.                       If there is at least one valid FD, the Fds_none is set to false, stop detection if (UFDS) {for (i = 0; i < Nfds; i++) { if (ufds[i].fd! = Curl_socket_bad) {Fds_none = FALSE                            ;                     Break        }}}//If all of the FD are invalid (i.e. bad socket,-1), then wait for a period of time before returning directly. if (fds_none) {r = Curl_wait_ms (tiMeout_ms);       This function will then parse the return R;        }//When the incoming timeout value is a negative number (blocking case) or 0 o'clock, you do not need to measure elapsed time.        Otherwise, gets the current time.              if (Timeout_ms > 0) {Pending_ms = Timeout_ms;   INITIAL_TV = Curlx_tvnow ();                     Call Gettimeofday () or time () to get the current times} do {if (Timeout_ms < 0)//negative, then poll infinite wait               Pending_ms =-1;                     else if (! Timeout_ms)//is 0, then poll returns immediately, even if no events are available.              Pending_ms = 0;   R = Poll (UFDs, Nfds, Pending_ms);                      True Call Poll () if (r! =-1) the//poll () call succeeds, then jumps out of the loop.               Break Poll call failed, return-1.              The error code can be obtained by errno.    Error = Sockerrno; Macro definition. #define Sockerrno (errno)//The following error_not_eintr is a macro definition. #define ERROR_NOT_EINTR (0 | | Error! = EINTR) if (Error && error_not_eintr)//Detect if error is present and not eintr               Error break; Elapsed_ms is a macro definition. #define ELAPsed_ms (int) Curlx_tvdiff (Curlx_tvnow (), INITIAL_TV) if (Timeout_ms > 0) {                      Pending_ms = Timeout_ms-elapsed_ms;     if (Pending_ms <= 0) {r = 0;                     Simulate the poll timeout situation break;       }}} while (r = =-1);       /* You can now summarize this while loop above: 1. If the poll call succeeds (that is, the return value >=0), the Loop 2 is ended. If the poll call fails (that is, the return value ==-1), the errno is detected as a eintr error.       If it is not eintr, the loop is ended. If it is eintr, the detection has timed out.       The timeout then ends the loop, and the polling continues without a timeout.        */if (R < 0)//poll () call failed return-1;        if (r = = 0)//poll () timeout return 0;               for (i = 0; i < Nfds; i++) {if (ufds[i].fd = = Curl_socket_bad) continue;              if (Ufds[i].revents & pollhup) ufds[i].revents |= Pollin;         FD can still read if (Ufds[i].revents & Pollerr)             Ufds[i].revents |= (Pollin |  Pollout); FD may still read and write}/* Pollerr,pollhup and pollnval meaning the following: Pollhup means the socket connection has been interrupted.       In TCP, it means that fin has been received and has been sent out. Pollerr means that the socket has an asynchronous error. In TCP, it usually means that an RST has been received, or that a RST has been sent out.       If the FD is not a socket, Pollerr may indicate that the device does not support polling.       For both of these identities, FD may be in the open state and has not been closed (but the shutdown () function may have been called).       Pollnval means that the FD is invalid and does not represent any open files.     */return R; The return value. This must be >0}
when this function is completed, the members of the first input parameter UFDs may be modified. Finally, the function returns the actual return value of the poll.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of Libcurl (2)-The use of Libcurl to poll

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.