CURLMOPT_TIMERFUNCTION.3 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2019, 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 \- set 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 interval of \fBtimeout_ms\fP. When time that timer fires, call
  40. either \fIcurl_multi_socket_action(3)\fP or \fIcurl_multi_perform(3)\fP,
  41. 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. This callback
  48. can be used instead of, or in addition to, \fIcurl_multi_timeout(3)\fP.
  49. \fBWARNING:\fP even if it feels tempting, avoid calling libcurl directly from
  50. within the callback itself when the \fBtimeout_ms\fP value is zero, since it
  51. risks triggering an unpleasant recursive behavior that immediately calls
  52. another call to the callback with a zero timeout...
  53. .SH DEFAULT
  54. NULL
  55. .SH PROTOCOLS
  56. All
  57. .SH EXAMPLE
  58. .nf
  59. static gboolean timeout_cb(gpointer user_data)
  60. {
  61. int running;
  62. if(user_data) {
  63. g_free(user_data);
  64. curl_multi_setopt(curl_handle, CURLMOPT_TIMERDATA, NULL);
  65. }
  66. curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running);
  67. return G_SOURCE_REMOVE;
  68. }
  69. static int timerfunc(CURLM *multi, long timeout_ms, void *userp)
  70. {
  71. guint *id = userp;
  72. if(id)
  73. g_source_remove(*id);
  74. /* -1 means we should just delete our timer. */
  75. if(timeout_ms == -1) {
  76. g_free(id);
  77. id = NULL;
  78. }
  79. else {
  80. if(!id)
  81. id = g_new(guint, 1);
  82. *id = g_timeout_add(timeout_ms, timeout_cb, id);
  83. }
  84. curl_multi_setopt(multi, CURLMOPT_TIMERDATA, id);
  85. return 0;
  86. }
  87. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timerfunc);
  88. .fi
  89. .SH AVAILABILITY
  90. Added in 7.16.0
  91. .SH RETURN VALUE
  92. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  93. .SH "SEE ALSO"
  94. .BR CURLMOPT_TIMERDATA "(3), " CURLMOPT_SOCKETFUNCTION "(3), "