CURLMOPT_TIMERFUNCTION.3 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2017, 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.haxx.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 "May 27, 2017" "libcurl 7.61.1" "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, /* see above */
  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. Each 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 means you should delete your timer.
  43. A \fBtimeout_ms\fP value of 0 means you should call
  44. \fIcurl_multi_socket_action(3)\fP or \fIcurl_multi_perform(3)\fP (once) as soon
  45. as possible.
  46. \fBtimer_callback\fP will only be called when the \fBtimeout_ms\fP changes.
  47. The \fBuserp\fP pointer is set with \fICURLMOPT_TIMERDATA(3)\fP.
  48. The timer callback should return 0 on success, and -1 on error. This callback
  49. can be used instead of, or in addition to, \fIcurl_multi_timeout(3)\fP.
  50. .SH DEFAULT
  51. NULL
  52. .SH PROTOCOLS
  53. All
  54. .SH EXAMPLE
  55. .nf
  56. static gboolean timeout_cb(gpointer user_data)
  57. {
  58. int running;
  59. if(user_data) {
  60. g_free(user_data);
  61. curl_multi_setopt(curl_handle, CURLMOPT_TIMERDATA, NULL);
  62. }
  63. curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running);
  64. return G_SOURCE_REMOVE;
  65. }
  66. static int timerfunc(CURLM *multi, long timeout_ms, void *userp)
  67. {
  68. guint *id = userp;
  69. if(id)
  70. g_source_remove(*id);
  71. /* -1 means we should just delete our timer. */
  72. if(timeout_ms == -1) {
  73. g_free(id);
  74. id = NULL;
  75. }
  76. else {
  77. if(!id)
  78. id = g_new(guint, 1);
  79. *id = g_timeout_add(timeout_ms, timeout_cb, id);
  80. }
  81. curl_multi_setopt(multi, CURLMOPT_TIMERDATA, id);
  82. return 0;
  83. }
  84. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timerfunc);
  85. .fi
  86. .SH AVAILABILITY
  87. Added in 7.16.0
  88. .SH RETURN VALUE
  89. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  90. .SH "SEE ALSO"
  91. .BR CURLMOPT_TIMERDATA "(3), " CURLMOPT_SOCKETFUNCTION "(3), "