CURLMOPT_TIMERFUNCTION.3 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" **************************************************************************
  22. .\"
  23. .TH CURLMOPT_TIMERFUNCTION 3 "17 Jun 2014" "libcurl 7.37.0" "curl_multi_setopt options"
  24. .SH NAME
  25. CURLMOPT_TIMERFUNCTION \- callback to receive timeout values
  26. .SH SYNOPSIS
  27. .nf
  28. #include <curl/curl.h>
  29. int timer_callback(CURLM *multi, /* multi handle */
  30. long timeout_ms, /* timeout in number of ms */
  31. void *userp); /* private callback pointer */
  32. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_TIMERFUNCTION, timer_callback);
  33. .SH DESCRIPTION
  34. Pass a pointer to your callback function, which should match the prototype
  35. shown above.
  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. Your callback function \fBtimer_callback\fP should install a non-repeating
  39. timer with an expire time of \fBtimeout_ms\fP milliseconds. When that timer
  40. fires, call either \fIcurl_multi_socket_action(3)\fP or
  41. \fIcurl_multi_perform(3)\fP, depending on which interface you use.
  42. A \fBtimeout_ms\fP value of -1 passed to this callback means you should delete
  43. the timer. All other values are valid expire times in number of milliseconds.
  44. The \fBtimer_callback\fP will only be called when the timeout expire time is
  45. changed.
  46. The \fBuserp\fP pointer is set with \fICURLMOPT_TIMERDATA(3)\fP.
  47. The timer callback should return 0 on success, and -1 on error. If this
  48. callback returns error, \fBall\fP transfers currently in progress in this
  49. multi handle will be aborted and fail.
  50. This callback can be used instead of, or in addition to,
  51. \fIcurl_multi_timeout(3)\fP.
  52. \fBWARNING:\fP do not call libcurl directly from within the callback itself
  53. when the \fBtimeout_ms\fP value is zero, since it risks triggering an
  54. unpleasant recursive behavior that immediately calls another call to the
  55. callback with a zero timeout...
  56. .SH DEFAULT
  57. NULL
  58. .SH PROTOCOLS
  59. All
  60. .SH EXAMPLE
  61. .nf
  62. static gboolean timeout_cb(gpointer user_data)
  63. {
  64. int running;
  65. if(user_data) {
  66. g_free(user_data);
  67. curl_multi_setopt(curl_handle, CURLMOPT_TIMERDATA, NULL);
  68. }
  69. curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running);
  70. return G_SOURCE_REMOVE;
  71. }
  72. static int timerfunc(CURLM *multi, long timeout_ms, void *userp)
  73. {
  74. guint *id = userp;
  75. if(id)
  76. g_source_remove(*id);
  77. /* -1 means we should just delete our timer. */
  78. if(timeout_ms == -1) {
  79. g_free(id);
  80. id = NULL;
  81. }
  82. else {
  83. if(!id)
  84. id = g_new(guint, 1);
  85. *id = g_timeout_add(timeout_ms, timeout_cb, id);
  86. }
  87. current_timer = id;
  88. return 0;
  89. }
  90. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timerfunc);
  91. .fi
  92. .SH AVAILABILITY
  93. Added in 7.16.0
  94. .SH RETURN VALUE
  95. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  96. .SH "SEE ALSO"
  97. .BR CURLMOPT_TIMERDATA "(3), " CURLMOPT_SOCKETFUNCTION "(3), "