xref: /curl/docs/libcurl/curl_multi_poll.md (revision 6429ce8e)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_multi_poll
5Section: 3
6Source: libcurl
7See-also:
8  - curl_multi_fdset (3)
9  - curl_multi_perform (3)
10  - curl_multi_wait (3)
11  - curl_multi_wakeup (3)
12Protocol:
13  - All
14Added-in: 7.66.0
15---
16
17# NAME
18
19curl_multi_poll - poll on all easy handles in a multi handle
20
21# SYNOPSIS
22
23~~~c
24#include <curl/curl.h>
25
26CURLMcode curl_multi_poll(CURLM *multi_handle,
27                          struct curl_waitfd extra_fds[],
28                          unsigned int extra_nfds,
29                          int timeout_ms,
30                          int *numfds);
31~~~
32
33# DESCRIPTION
34
35curl_multi_poll(3) polls all file descriptors used by the curl easy
36handles contained in the given multi handle set. It blocks until activity is
37detected on at least one of the handles or *timeout_ms* has passed.
38Alternatively, if the multi handle has a pending internal timeout that has a
39shorter expiry time than *timeout_ms*, that shorter time is used instead
40to make sure timeout accuracy is reasonably kept.
41
42The calling application may pass additional curl_waitfd structures which are
43similar to *poll(2)*'s *pollfd* structure to be waited on in the same
44call.
45
46On completion, if *numfds* is non-NULL, it gets populated with the total
47number of file descriptors on which interesting events occurred. This number
48can include both libcurl internal descriptors as well as descriptors provided
49in *extra_fds*.
50
51The curl_multi_wakeup(3) function can be used from another thread to
52wake up this function and return faster. This is one of the details
53that makes this function different than curl_multi_wait(3) which cannot
54be woken up this way.
55
56If no extra file descriptors are provided and libcurl has no file descriptor
57to offer to wait for, this function instead waits during *timeout_ms*
58milliseconds (or shorter if an internal timer indicates so). This is the other
59detail that makes this function different than curl_multi_wait(3).
60
61This function is encouraged to be used instead of select(3) when using the
62multi interface to allow applications to easier circumvent the common problem
63with 1024 maximum file descriptors.
64
65# curl_waitfd
66
67~~~c
68struct curl_waitfd {
69  curl_socket_t fd;
70  short events;
71  short revents;
72};
73~~~
74
75## CURL_WAIT_POLLIN
76
77Bit flag to curl_waitfd.events indicating the socket should poll on read
78events such as new data received.
79
80## CURL_WAIT_POLLPRI
81
82Bit flag to curl_waitfd.events indicating the socket should poll on high
83priority read events such as out of band data.
84
85## CURL_WAIT_POLLOUT
86
87Bit flag to curl_waitfd.events indicating the socket should poll on write
88events such as the socket being clear to write without blocking.
89
90# %PROTOCOLS%
91
92# EXAMPLE
93
94~~~c
95extern void handle_fd(int);
96
97int main(void)
98{
99  CURL *easy_handle;
100  CURLM *multi_handle;
101  int still_running = 0;
102  int myfd; /* this is our own file descriptor */
103
104  /* add the individual easy handle */
105  curl_multi_add_handle(multi_handle, easy_handle);
106
107  do {
108    CURLMcode mc;
109    int numfds;
110
111    mc = curl_multi_perform(multi_handle, &still_running);
112
113    if(mc == CURLM_OK) {
114      struct curl_waitfd myown;
115      myown.fd = myfd;
116      myown.events = CURL_WAIT_POLLIN; /* wait for input */
117      myown.revents = 0; /* clear it */
118
119      /* wait for activity on curl's descriptors or on our own,
120         or timeout */
121      mc = curl_multi_poll(multi_handle, &myown, 1, 1000, &numfds);
122
123      if(myown.revents) {
124        /* did our descriptor receive an event? */
125        handle_fd(myfd);
126      }
127    }
128
129    if(mc != CURLM_OK) {
130      fprintf(stderr, "curl_multi failed, code %d.\n", mc);
131      break;
132    }
133
134  } while(still_running);
135
136  curl_multi_remove_handle(multi_handle, easy_handle);
137}
138~~~
139
140# %AVAILABILITY%
141
142# RETURN VALUE
143
144CURLMcode type, general libcurl multi interface error code. See
145libcurl-errors(3)
146