1CURLMOPT_TIMERFUNCTION(3) curl_multi_setopt options CURLMOPT_TIMERFUNCTION(3)
2
3
4
6 CURLMOPT_TIMERFUNCTION - set callback to receive timeout values
7
9 #include <curl/curl.h>
10
11 int timer_callback(CURLM *multi, /* multi handle */
12 long timeout_ms, /* timeout in number of ms */
13 void *userp); /* private callback pointer */
14
15 CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_TIMERFUNCTION, timer_callback);
16
18 Pass a pointer to your callback function, which should match the proto‐
19 type shown above.
20
21 Certain features, such as timeouts and retries, require you to call
22 libcurl even when there is no activity on the file descriptors.
23
24 Your callback function timer_callback should install a non-repeating
25 timer with an interval of timeout_ms. When time that timer fires, call
26 either curl_multi_socket_action(3) or curl_multi_perform(3), depending
27 on which interface you use.
28
29 A timeout_ms value of -1 passed to this callback means you should
30 delete the timer. All other values are valid expire times in number of
31 milliseconds.
32
33 The timer_callback will only be called when the timeout expire time is
34 changed.
35
36 The userp pointer is set with CURLMOPT_TIMERDATA(3).
37
38 The timer callback should return 0 on success, and -1 on error. This
39 callback can be used instead of, or in addition to, curl_multi_time‐
40 out(3).
41
42 WARNING: even if it feels tempting, avoid calling libcurl directly from
43 within the callback itself when the timeout_ms value is zero, since it
44 risks triggering an unpleasant recursive behavior that immediately
45 calls another call to the callback with a zero timeout...
46
48 NULL
49
51 All
52
54 static gboolean timeout_cb(gpointer user_data)
55 {
56 int running;
57 if(user_data) {
58 g_free(user_data);
59 curl_multi_setopt(curl_handle, CURLMOPT_TIMERDATA, NULL);
60 }
61 curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running);
62 return G_SOURCE_REMOVE;
63 }
64
65 static int timerfunc(CURLM *multi, long timeout_ms, void *userp)
66 {
67 guint *id = userp;
68
69 if(id)
70 g_source_remove(*id);
71
72 /* -1 means we should just delete our timer. */
73 if(timeout_ms == -1) {
74 g_free(id);
75 id = NULL;
76 }
77 else {
78 if(!id)
79 id = g_new(guint, 1);
80 *id = g_timeout_add(timeout_ms, timeout_cb, id);
81 }
82 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, id);
83 return 0;
84 }
85
86 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timerfunc);
87
89 Added in 7.16.0
90
92 Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION
93 if not.
94
96 CURLMOPT_TIMERDATA(3), CURLMOPT_SOCKETFUNCTION(3),
97
98
99
100libcurl 7.69.1 May 03, 2019 CURLMOPT_TIMERFUNCTION(3)