curl_multi_socket_action.3 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. .\" generated by cd2nroff 0.1 from curl_multi_socket_action.md
  2. .TH curl_multi_socket_action 3 "2025-01-17" libcurl
  3. .SH NAME
  4. curl_multi_socket_action \- read/write available data given an action
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLMcode curl_multi_socket_action(CURLM *multi_handle,
  9. curl_socket_t sockfd,
  10. int ev_bitmask,
  11. int *running_handles);
  12. .fi
  13. .SH DESCRIPTION
  14. When the application has detected action on a socket handled by libcurl, it
  15. should call \fIcurl_multi_socket_action(3)\fP with the \fBsockfd\fP argument
  16. set to the socket with the action. When the events on a socket are known, they
  17. can be passed as an events bitmask \fBev_bitmask\fP by first setting
  18. \fBev_bitmask\fP to 0, and then adding using bitwise OR (|) any combination of
  19. events to be chosen from CURL_CSELECT_IN, CURL_CSELECT_OUT or
  20. CURL_CSELECT_ERR. When the events on a socket are unknown, pass 0 instead, and
  21. libcurl tests the descriptor internally. It is also permissible to pass
  22. CURL_SOCKET_TIMEOUT to the \fBsockfd\fP parameter in order to initiate the
  23. whole process or when a timeout occurs.
  24. At return, \fBrunning_handles\fP points to the number of running easy handles
  25. within the multi handle. When this number reaches zero, all transfers are
  26. complete/done. When you call \fIcurl_multi_socket_action(3)\fP on a specific
  27. socket and the counter decreases by one, it DOES NOT necessarily mean that
  28. this exact socket/transfer is the one that completed. Use
  29. \fIcurl_multi_info_read(3)\fP to figure out which easy handle that completed.
  30. The \fIcurl_multi_socket_action(3)\fP function informs the application about
  31. updates in the socket (file descriptor) status by doing none, one, or multiple
  32. calls to the socket callback function set with the
  33. \fICURLMOPT_SOCKETFUNCTION(3)\fP option to \fIcurl_multi_setopt(3)\fP. They
  34. update the status with changes since the previous time the callback was
  35. called.
  36. Get the timeout time by setting the \fICURLMOPT_TIMERFUNCTION(3)\fP option
  37. with \fIcurl_multi_setopt(3)\fP. Your application then gets called with
  38. information on how long to wait for socket actions at most before doing the
  39. timeout action: call the \fIcurl_multi_socket_action(3)\fP function with the
  40. \fBsockfd\fP argument set to CURL_SOCKET_TIMEOUT. You can also use the
  41. \fIcurl_multi_timeout(3)\fP function to poll the value at any given time, but
  42. for an event\-based system using the callback is far better than relying on
  43. polling the timeout value.
  44. When this function returns error, the state of all transfers are uncertain and
  45. they cannot be continued. \fIcurl_multi_socket_action(3)\fP should not be
  46. called again on the same multi handle after an error has been returned, unless
  47. first removing all the handles and adding new ones.
  48. .SH TYPICAL USAGE
  49. 1. Create a multi handle
  50. 2. Set the socket callback with \fICURLMOPT_SOCKETFUNCTION(3)\fP
  51. 3. Set the timeout callback with \fICURLMOPT_TIMERFUNCTION(3)\fP, to get to
  52. know what timeout value to use when waiting for socket activities.
  53. 4. Add easy handles with curl_multi_add_handle()
  54. 5. Provide some means to manage the sockets libcurl is using, so you can check
  55. them for activity. This can be done through your application code, or by way
  56. of an external library such as libevent or glib.
  57. 6. Call curl_multi_socket_action(..., CURL_SOCKET_TIMEOUT, 0, ...)
  58. to kickstart everything. To get one or more callbacks called.
  59. 7. Wait for activity on any of libcurl\(aqs sockets, use the timeout value your
  60. callback has been told.
  61. 8, When activity is detected, call curl_multi_socket_action() for the
  62. socket(s) that got action. If no activity is detected and the timeout expires,
  63. call \fIcurl_multi_socket_action(3)\fP with \fICURL_SOCKET_TIMEOUT\fP.
  64. .SH PROTOCOLS
  65. This functionality affects all supported protocols
  66. .SH EXAMPLE
  67. .nf
  68. int main(void)
  69. {
  70. /* the event-library gets told when there activity on the socket 'fd',
  71. which we translate to a call to curl_multi_socket_action() */
  72. int running;
  73. CURLM *multi; /* the stack we work with */
  74. int fd; /* the descriptor that had action */
  75. int bitmask; /* what activity that happened */
  76. CURLMcode mc = curl_multi_socket_action(multi, fd, bitmask, &running);
  77. if(mc)
  78. printf("error: %s\\n", curl_multi_strerror(mc));
  79. }
  80. .fi
  81. .SH AVAILABILITY
  82. Added in curl 7.15.4
  83. .SH RETURN VALUE
  84. CURLMcode type, general libcurl multi interface error code. See
  85. \fIlibcurl\-errors(3)\fP
  86. .SH SEE ALSO
  87. .BR curl_multi_cleanup (3),
  88. .BR curl_multi_fdset (3),
  89. .BR curl_multi_info_read (3),
  90. .BR curl_multi_init (3),
  91. .BR the hiperfifo.c example