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