curl_multi_socket_action.3 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2018, 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. .TH curl_multi_socket_action 3 "June 30, 2018" "libcurl 7.61.1" "libcurl Manual"
  23. .SH NAME
  24. curl_multi_socket_action \- reads/writes available data given an action
  25. .SH SYNOPSIS
  26. .nf
  27. #include <curl/curl.h>
  28. CURLMcode curl_multi_socket_action(CURLM * multi_handle,
  29. curl_socket_t sockfd,
  30. int ev_bitmask,
  31. int *running_handles);
  32. .fi
  33. .SH DESCRIPTION
  34. When the application has detected action on a socket handled by libcurl, it
  35. should call \fIcurl_multi_socket_action(3)\fP with the \fBsockfd\fP argument
  36. set to the socket with the action. When the events on a socket are known, they
  37. can be passed as an events bitmask \fBev_bitmask\fP by first setting
  38. \fBev_bitmask\fP to 0, and then adding using bitwise OR (|) any combination of
  39. events to be chosen from CURL_CSELECT_IN, CURL_CSELECT_OUT or
  40. CURL_CSELECT_ERR. When the events on a socket are unknown, pass 0 instead, and
  41. libcurl will test the descriptor internally. It is also permissible to pass
  42. CURL_SOCKET_TIMEOUT to the \fBsockfd\fP parameter in order to initiate the
  43. whole process or when a timeout occurs.
  44. At return, \fBrunning_handles\fP points to the number
  45. of running easy handles within the multi handle. When this number reaches
  46. zero, all transfers are complete/done. When you call
  47. \fIcurl_multi_socket_action(3)\fP on a specific socket and the counter
  48. decreases by one, it DOES NOT necessarily mean that this exact socket/transfer
  49. is the one that completed. Use \fIcurl_multi_info_read(3)\fP to figure out
  50. which easy handle that completed.
  51. The \fIcurl_multi_socket_action(3)\fP functions inform the application about
  52. updates in the socket (file descriptor) status by doing none, one, or multiple
  53. calls to the socket callback function set with the
  54. \fICURLMOPT_SOCKETFUNCTION(3)\fP option to \fIcurl_multi_setopt(3)\fP. They
  55. update the status with changes since the previous time the callback was
  56. called.
  57. Get the timeout time by setting the \fICURLMOPT_TIMERFUNCTION(3)\fP option
  58. with \fIcurl_multi_setopt(3)\fP. Your application will then get called with
  59. information on how long to wait for socket actions at most before doing the
  60. timeout action: call the \fIcurl_multi_socket_action(3)\fP function with the
  61. \fBsockfd\fP argument set to CURL_SOCKET_TIMEOUT. You can also use the
  62. \fIcurl_multi_timeout(3)\fP function to poll the value at any given time, but
  63. for an event-based system using the callback is far better than relying on
  64. polling the timeout value.
  65. .SH "CALLBACK DETAILS"
  66. The socket \fBcallback\fP function uses a prototype like this
  67. .nf
  68. int curl_socket_callback(CURL *easy, /* easy handle */
  69. curl_socket_t s, /* socket */
  70. int action, /* see values below */
  71. void *userp, /* private callback pointer */
  72. void *socketp); /* private socket pointer,
  73. \fBNULL\fP if not
  74. previously assigned with
  75. \fIcurl_multi_assign(3)\fP */
  76. .fi
  77. The callback MUST return 0.
  78. The \fIeasy\fP argument is a pointer to the easy handle that deals with this
  79. particular socket. Note that a single handle may work with several sockets
  80. simultaneously.
  81. The \fIs\fP argument is the actual socket value as you use it within your
  82. system.
  83. The \fIaction\fP argument to the callback has one of five values:
  84. .RS
  85. .IP "CURL_POLL_NONE (0)"
  86. register, not interested in readiness (yet)
  87. .IP "CURL_POLL_IN (1)"
  88. register, interested in read readiness
  89. .IP "CURL_POLL_OUT (2)"
  90. register, interested in write readiness
  91. .IP "CURL_POLL_INOUT (3)"
  92. register, interested in both read and write readiness
  93. .IP "CURL_POLL_REMOVE (4)"
  94. unregister
  95. .RE
  96. The \fIsocketp\fP argument is a private pointer you have previously set with
  97. \fIcurl_multi_assign(3)\fP to be associated with the \fIs\fP socket. If no
  98. pointer has been set, socketp will be NULL. This argument is of course a
  99. service to applications that want to keep certain data or structs that are
  100. strictly associated to the given socket.
  101. The \fIuserp\fP argument is a private pointer you have previously set with
  102. \fIcurl_multi_setopt(3)\fP and the \fICURLMOPT_SOCKETDATA(3)\fP option.
  103. .SH "RETURN VALUE"
  104. CURLMcode type, general libcurl multi interface error code.
  105. Before version 7.20.0: If you receive \fICURLM_CALL_MULTI_PERFORM\fP, this
  106. basically means that you should call \fIcurl_multi_socket_action(3)\fP again
  107. before you wait for more actions on libcurl's sockets. You don't have to do it
  108. immediately, but the return code means that libcurl may have more data
  109. available to return or that there may be more data to send off before it is
  110. "satisfied".
  111. The return code from this function is for the whole multi stack. Problems
  112. still might have occurred on individual transfers even when one of these
  113. functions return OK.
  114. .SH "TYPICAL USAGE"
  115. 1. Create a multi handle
  116. 2. Set the socket callback with \fICURLMOPT_SOCKETFUNCTION(3)\fP
  117. 3. Set the timeout callback with \fICURLMOPT_TIMERFUNCTION(3)\fP, to get to
  118. know what timeout value to use when waiting for socket activities.
  119. 4. Add easy handles with curl_multi_add_handle()
  120. 5. Provide some means to manage the sockets libcurl is using, so you can check
  121. them for activity. This can be done through your application code, or by way
  122. of an external library such as libevent or glib.
  123. 6. Call curl_multi_socket_action(..., CURL_SOCKET_TIMEOUT, 0, ...)
  124. to kickstart everything. To get one or more callbacks called.
  125. 7. Wait for activity on any of libcurl's sockets, use the timeout value your
  126. callback has been told.
  127. 8, When activity is detected, call curl_multi_socket_action() for the
  128. socket(s) that got action. If no activity is detected and the timeout expires,
  129. call \fIcurl_multi_socket_action(3)\fP with \fICURL_SOCKET_TIMEOUT\fP.
  130. .SH AVAILABILITY
  131. This function was added in libcurl 7.15.4, and is deemed stable since 7.16.0.
  132. .SH "SEE ALSO"
  133. .BR curl_multi_cleanup "(3), " curl_multi_init "(3), "
  134. .BR curl_multi_fdset "(3), " curl_multi_info_read "(3), "
  135. .BR "the hiperfifo.c example"