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