CURLMOPT_SOCKETFUNCTION.3 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. .\" generated by cd2nroff 0.1 from CURLMOPT_SOCKETFUNCTION.md
  2. .TH CURLMOPT_SOCKETFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLMOPT_SOCKETFUNCTION \- callback informed about what to wait for
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. int socket_callback(CURL *easy, /* easy handle */
  9. curl_socket_t s, /* socket */
  10. int what, /* describes the socket */
  11. void *clientp, /* private callback pointer */
  12. void *socketp); /* private socket pointer */
  13. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_SOCKETFUNCTION, socket_callback);
  14. .fi
  15. .SH DESCRIPTION
  16. Pass a pointer to your callback function, which should match the prototype
  17. shown above.
  18. When the \fIcurl_multi_socket_action(3)\fP function is called, it uses this
  19. callback to inform the application about updates in the socket (file
  20. descriptor) status by doing none, one, or multiple calls to the
  21. \fBsocket_callback\fP. The callback function gets status updates with changes
  22. since the previous time the callback was called. If the given callback pointer
  23. is set to NULL, no callback is called.
  24. libcurl then expects the application to monitor the sockets for the specific
  25. activities and tell libcurl again when something happens on one of them. Tell
  26. libcurl by calling \fIcurl_multi_socket_action(3)\fP.
  27. .SH CALLBACK ARGUMENTS
  28. \fIeasy\fP identifies the specific transfer for which this update is related.
  29. \fIs\fP is the specific socket this function invocation concerns. If the
  30. \fBwhat\fP argument is not CURL_POLL_REMOVE then it holds information about
  31. what activity on this socket the application is supposed to
  32. monitor. Subsequent calls to this callback might update the \fBwhat\fP bits
  33. for a socket that is already monitored.
  34. The socket callback should return 0 on success, and \-1 on error. If this
  35. callback returns error, \fBall\fP transfers currently in progress in this
  36. multi handle are aborted and made to fail.
  37. \fBclientp\fP is set with \fICURLMOPT_SOCKETDATA(3)\fP.
  38. \fBsocketp\fP is set with \fIcurl_multi_assign(3)\fP or NULL.
  39. The \fBwhat\fP parameter informs the callback on the status of the given
  40. socket. It can hold one of these values:
  41. .IP CURL_POLL_IN
  42. Wait for incoming data. For the socket to become readable.
  43. .IP CURL_POLL_OUT
  44. Wait for outgoing data. For the socket to become writable.
  45. .IP CURL_POLL_INOUT
  46. Wait for incoming and outgoing data. For the socket to become readable or
  47. writable.
  48. .IP CURL_POLL_REMOVE
  49. The specified socket/file descriptor is no longer used by libcurl for any
  50. active transfer. It might soon be added again.
  51. .SH DEFAULT
  52. NULL (no callback)
  53. .SH PROTOCOLS
  54. This functionality affects all supported protocols
  55. .SH EXAMPLE
  56. .nf
  57. struct priv {
  58. void *ours;
  59. };
  60. static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
  61. {
  62. struct priv *p = sockp;
  63. printf("our ptr: %p\\n", p->ours);
  64. if(what == CURL_POLL_REMOVE) {
  65. /* remove the socket from our collection */
  66. }
  67. if(what & CURL_POLL_IN) {
  68. /* wait for read on this socket */
  69. }
  70. if(what & CURL_POLL_OUT) {
  71. /* wait for write on this socket */
  72. }
  73. return 0;
  74. }
  75. int main(void)
  76. {
  77. struct priv setup;
  78. CURLM *multi = curl_multi_init();
  79. /* ... use socket callback and custom pointer */
  80. curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
  81. curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, &setup);
  82. }
  83. .fi
  84. .SH AVAILABILITY
  85. Added in curl 7.15.4
  86. .SH RETURN VALUE
  87. Returns CURLM_OK.
  88. .SH SEE ALSO
  89. .BR CURLMOPT_SOCKETDATA (3),
  90. .BR CURLMOPT_TIMERFUNCTION (3),
  91. .BR curl_multi_socket_action (3)