CURLMOPT_SOCKETFUNCTION.3 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_SOCKETFUNCTION 3 "3 Nov 2016" libcurl libcurl
  26. .SH NAME
  27. CURLMOPT_SOCKETFUNCTION \- callback informed about what to wait for
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/curl.h>
  31. int socket_callback(CURL *easy, /* easy handle */
  32. curl_socket_t s, /* socket */
  33. int what, /* describes the socket */
  34. void *clientp, /* private callback pointer */
  35. void *socketp); /* private socket pointer */
  36. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_SOCKETFUNCTION, socket_callback);
  37. .SH DESCRIPTION
  38. Pass a pointer to your callback function, which should match the prototype
  39. shown above.
  40. When the \fIcurl_multi_socket_action(3)\fP function is called, it uses this
  41. callback to inform the application about updates in the socket (file
  42. descriptor) status by doing none, one, or multiple calls to the
  43. \fBsocket_callback\fP. The callback function gets status updates with changes
  44. since the previous time the callback was called. If the given callback pointer
  45. is set to NULL, no callback will be called.
  46. libcurl then expects the application to monitor the sockets for the specific
  47. activities and tell libcurl again when something happens on one of them. Tell
  48. libcurl by calling \fIcurl_multi_socket_action(3)\fP.
  49. .SH "CALLBACK ARGUMENTS"
  50. \fIeasy\fP identifies the specific transfer for which this update is related.
  51. \fIs\fP is the specific socket this function invocation concerns. If the
  52. \fBwhat\fP argument is not CURL_POLL_REMOVE then it holds information about
  53. what activity on this socket the application is supposed to
  54. monitor. Subsequent calls to this callback might update the \fBwhat\fP bits
  55. for a socket that is already monitored.
  56. The socket callback should return 0 on success, and -1 on error. If this
  57. callback returns error, \fBall\fP transfers currently in progress in this
  58. multi handle will be aborted and fail.
  59. \fBclientp\fP is set with \fICURLMOPT_SOCKETDATA(3)\fP.
  60. \fBsocketp\fP is set with \fIcurl_multi_assign(3)\fP or will be NULL.
  61. The \fBwhat\fP parameter informs the callback on the status of the given
  62. socket. It can hold one of these values:
  63. .IP CURL_POLL_IN
  64. Wait for incoming data. For the socket to become readable.
  65. .IP CURL_POLL_OUT
  66. Wait for outgoing data. For the socket to become writable.
  67. .IP CURL_POLL_INOUT
  68. Wait for incoming and outgoing data. For the socket to become readable or
  69. writable.
  70. .IP CURL_POLL_REMOVE
  71. The specified socket/file descriptor is no longer used by libcurl for any
  72. active transfer. It might soon be added again.
  73. .SH DEFAULT
  74. NULL (no callback)
  75. .SH PROTOCOLS
  76. All
  77. .SH EXAMPLE
  78. .nf
  79. static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
  80. {
  81. GlobalInfo *g = cbp;
  82. SockInfo *fdp = sockp;
  83. if(what == CURL_POLL_REMOVE) {
  84. remsock(fdp);
  85. }
  86. else {
  87. if(!fdp) {
  88. addsock(s, e, what, g);
  89. }
  90. else {
  91. setsock(fdp, s, e, what, g);
  92. }
  93. }
  94. return 0;
  95. }
  96. main()
  97. {
  98. GlobalInfo setup;
  99. /* ... use socket callback and custom pointer */
  100. curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
  101. curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, &setup);
  102. }
  103. .fi
  104. .SH AVAILABILITY
  105. Added in 7.15.4
  106. .SH RETURN VALUE
  107. Returns CURLM_OK.
  108. .SH "SEE ALSO"
  109. .BR CURLMOPT_SOCKETDATA "(3), " curl_multi_socket_action "(3), "
  110. .BR CURLMOPT_TIMERFUNCTION "(3) "