CURLMOPT_TIMERFUNCTION.3 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 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. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .\"
  25. .TH CURLMOPT_TIMERFUNCTION 3 "17 Jun 2014" libcurl libcurl
  26. .SH NAME
  27. CURLMOPT_TIMERFUNCTION \- callback to receive timeout values
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/curl.h>
  31. int timer_callback(CURLM *multi, /* multi handle */
  32. long timeout_ms, /* timeout in number of ms */
  33. void *clientp); /* private callback pointer */
  34. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_TIMERFUNCTION, timer_callback);
  35. .SH DESCRIPTION
  36. Pass a pointer to your callback function, which should match the prototype
  37. shown above.
  38. Certain features, such as timeouts and retries, require you to call libcurl
  39. even when there is no activity on the file descriptors.
  40. Your callback function \fBtimer_callback\fP should install a non-repeating
  41. timer with an expire time of \fBtimeout_ms\fP milliseconds. When that timer
  42. fires, call either \fIcurl_multi_socket_action(3)\fP or
  43. \fIcurl_multi_perform(3)\fP, depending on which interface you use.
  44. A \fBtimeout_ms\fP value of -1 passed to this callback means you should delete
  45. the timer. All other values are valid expire times in number of milliseconds.
  46. The \fBtimer_callback\fP will only be called when the timeout expire time is
  47. changed.
  48. The \fBclientp\fP pointer is set with \fICURLMOPT_TIMERDATA(3)\fP.
  49. The timer callback should return 0 on success, and -1 on error. If this
  50. callback returns error, \fBall\fP transfers currently in progress in this
  51. multi handle will be aborted and fail.
  52. This callback can be used instead of, or in addition to,
  53. \fIcurl_multi_timeout(3)\fP.
  54. \fBWARNING:\fP do not call libcurl directly from within the callback itself
  55. when the \fBtimeout_ms\fP value is zero, since it risks triggering an
  56. unpleasant recursive behavior that immediately calls another call to the
  57. callback with a zero timeout...
  58. .SH DEFAULT
  59. NULL
  60. .SH PROTOCOLS
  61. All
  62. .SH EXAMPLE
  63. .nf
  64. static gboolean timeout_cb(gpointer user_data)
  65. {
  66. int running;
  67. if(user_data) {
  68. g_free(user_data);
  69. curl_multi_setopt(curl_handle, CURLMOPT_TIMERDATA, NULL);
  70. }
  71. curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running);
  72. return G_SOURCE_REMOVE;
  73. }
  74. static int timerfunc(CURLM *multi, long timeout_ms, void *clientp)
  75. {
  76. guint *id = clientp;
  77. if(id)
  78. g_source_remove(*id);
  79. /* -1 means we should just delete our timer. */
  80. if(timeout_ms == -1) {
  81. g_free(id);
  82. id = NULL;
  83. }
  84. else {
  85. if(!id)
  86. id = g_new(guint, 1);
  87. *id = g_timeout_add(timeout_ms, timeout_cb, id);
  88. }
  89. current_timer = id;
  90. return 0;
  91. }
  92. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timerfunc);
  93. .fi
  94. .SH AVAILABILITY
  95. Added in 7.16.0
  96. .SH RETURN VALUE
  97. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  98. .SH "SEE ALSO"
  99. .BR CURLMOPT_TIMERDATA "(3), " CURLMOPT_SOCKETFUNCTION "(3), "