1--- 2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3SPDX-License-Identifier: curl 4Title: CURLMOPT_TIMERFUNCTION 5Section: 3 6Source: libcurl 7See-also: 8 - CURLMOPT_SOCKETFUNCTION (3) 9 - CURLMOPT_TIMERDATA (3) 10Protocol: 11 - All 12Added-in: 7.16.0 13--- 14 15# NAME 16 17CURLMOPT_TIMERFUNCTION - callback to receive timeout values 18 19# SYNOPSIS 20 21~~~c 22#include <curl/curl.h> 23 24int timer_callback(CURLM *multi, /* multi handle */ 25 long timeout_ms, /* timeout in number of ms */ 26 void *clientp); /* private callback pointer */ 27 28CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_TIMERFUNCTION, timer_callback); 29~~~ 30 31# DESCRIPTION 32 33Pass a pointer to your callback function, which should match the prototype 34shown above. 35 36Certain features, such as timeouts and retries, require you to call libcurl 37even when there is no activity on the file descriptors. 38 39Your callback function **timer_callback** should install a non-repeating 40timer with an expire time of **timeout_ms** milliseconds. When that timer 41fires, call either curl_multi_socket_action(3) or 42curl_multi_perform(3), depending on which interface you use. 43 44A **timeout_ms** value of -1 passed to this callback means you should delete 45the timer. All other values are valid expire times in number of milliseconds. 46 47The **timer_callback** is called when the timeout expire time is changed. 48 49The **clientp** pointer is set with CURLMOPT_TIMERDATA(3). 50 51The timer callback should return 0 on success, and -1 on error. If this 52callback returns error, **all** transfers currently in progress in this 53multi handle are aborted and made to fail. 54 55This callback can be used instead of, or in addition to, 56curl_multi_timeout(3). 57 58**WARNING:** do not call libcurl directly from within the callback itself 59when the **timeout_ms** value is zero, since it risks triggering an 60unpleasant recursive behavior that immediately calls another call to the 61callback with a zero timeout... 62 63# DEFAULT 64 65NULL 66 67# %PROTOCOLS% 68 69# EXAMPLE 70 71~~~c 72struct priv { 73 void *custom; 74}; 75 76static int timerfunc(CURLM *multi, long timeout_ms, void *clientp) 77{ 78 struct priv *mydata = clientp; 79 printf("our ptr: %p\n", mydata->custom); 80 81 if(timeout_ms) { 82 /* this is the new single timeout to wait for */ 83 } 84 else { 85 /* delete the timeout, nothing to wait for now */ 86 } 87} 88 89int main(void) 90{ 91 struct priv mydata; 92 CURLM *multi = curl_multi_init(); 93 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timerfunc); 94 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, &mydata); 95} 96~~~ 97 98# %AVAILABILITY% 99 100# RETURN VALUE 101 102Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not. 103