CURLMOPT_TIMERFUNCTION.3 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. .\" generated by cd2nroff 0.1 from CURLMOPT_TIMERFUNCTION.md
  2. .TH CURLMOPT_TIMERFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLMOPT_TIMERFUNCTION \- callback to receive timeout values
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. int timer_callback(CURLM *multi, /* multi handle */
  9. long timeout_ms, /* timeout in number of ms */
  10. void *clientp); /* private callback pointer */
  11. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_TIMERFUNCTION, timer_callback);
  12. .fi
  13. .SH DESCRIPTION
  14. Pass a pointer to your callback function, which should match the prototype
  15. shown above.
  16. Certain features, such as timeouts and retries, require you to call libcurl
  17. even when there is no activity on the file descriptors.
  18. Your callback function \fBtimer_callback\fP should install a non\-repeating
  19. timer with an expire time of \fBtimeout_ms\fP milliseconds. When that timer
  20. fires, call either \fIcurl_multi_socket_action(3)\fP or
  21. \fIcurl_multi_perform(3)\fP, depending on which interface you use.
  22. A \fBtimeout_ms\fP value of \-1 passed to this callback means you should delete
  23. the timer. All other values are valid expire times in number of milliseconds.
  24. The \fBtimer_callback\fP is called when the timeout expire time is changed.
  25. The \fBclientp\fP pointer is set with \fICURLMOPT_TIMERDATA(3)\fP.
  26. The timer callback should return 0 on success, and \-1 on error. If this
  27. callback returns error, \fBall\fP transfers currently in progress in this
  28. multi handle are aborted and made to fail.
  29. This callback can be used instead of, or in addition to,
  30. \fIcurl_multi_timeout(3)\fP.
  31. \fBWARNING:\fP do not call libcurl directly from within the callback itself
  32. when the \fBtimeout_ms\fP value is zero, since it risks triggering an
  33. unpleasant recursive behavior that immediately calls another call to the
  34. callback with a zero timeout...
  35. .SH DEFAULT
  36. NULL
  37. .SH PROTOCOLS
  38. This functionality affects all supported protocols
  39. .SH EXAMPLE
  40. .nf
  41. struct priv {
  42. void *custom;
  43. };
  44. static int timerfunc(CURLM *multi, long timeout_ms, void *clientp)
  45. {
  46. struct priv *mydata = clientp;
  47. printf("our ptr: %p\\n", mydata->custom);
  48. if(timeout_ms) {
  49. /* this is the new single timeout to wait for */
  50. }
  51. else {
  52. /* delete the timeout, nothing to wait for now */
  53. }
  54. }
  55. int main(void)
  56. {
  57. struct priv mydata;
  58. CURLM *multi = curl_multi_init();
  59. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timerfunc);
  60. curl_multi_setopt(multi, CURLMOPT_TIMERDATA, &mydata);
  61. }
  62. .fi
  63. .SH AVAILABILITY
  64. Added in curl 7.16.0
  65. .SH RETURN VALUE
  66. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  67. .SH SEE ALSO
  68. .BR CURLMOPT_SOCKETFUNCTION (3),
  69. .BR CURLMOPT_TIMERDATA (3)