xref: /aosp_15_r20/external/curl/docs/libcurl/opts/CURLMOPT_SOCKETFUNCTION.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLMOPT_SOCKETFUNCTION
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - CURLMOPT_SOCKETDATA (3)
9*6236dae4SAndroid Build Coastguard Worker  - CURLMOPT_TIMERFUNCTION (3)
10*6236dae4SAndroid Build Coastguard Worker  - curl_multi_socket_action (3)
11*6236dae4SAndroid Build Coastguard WorkerProtocol:
12*6236dae4SAndroid Build Coastguard Worker  - All
13*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.15.4
14*6236dae4SAndroid Build Coastguard Worker---
15*6236dae4SAndroid Build Coastguard Worker
16*6236dae4SAndroid Build Coastguard Worker# NAME
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard WorkerCURLMOPT_SOCKETFUNCTION - callback informed about what to wait for
19*6236dae4SAndroid Build Coastguard Worker
20*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
21*6236dae4SAndroid Build Coastguard Worker
22*6236dae4SAndroid Build Coastguard Worker~~~c
23*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
24*6236dae4SAndroid Build Coastguard Worker
25*6236dae4SAndroid Build Coastguard Workerint socket_callback(CURL *easy,      /* easy handle */
26*6236dae4SAndroid Build Coastguard Worker                    curl_socket_t s, /* socket */
27*6236dae4SAndroid Build Coastguard Worker                    int what,        /* describes the socket */
28*6236dae4SAndroid Build Coastguard Worker                    void *clientp,   /* private callback pointer */
29*6236dae4SAndroid Build Coastguard Worker                    void *socketp);  /* private socket pointer */
30*6236dae4SAndroid Build Coastguard Worker
31*6236dae4SAndroid Build Coastguard WorkerCURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_SOCKETFUNCTION, socket_callback);
32*6236dae4SAndroid Build Coastguard Worker~~~
33*6236dae4SAndroid Build Coastguard Worker
34*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
35*6236dae4SAndroid Build Coastguard Worker
36*6236dae4SAndroid Build Coastguard WorkerPass a pointer to your callback function, which should match the prototype
37*6236dae4SAndroid Build Coastguard Workershown above.
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard WorkerWhen the curl_multi_socket_action(3) function is called, it uses this
40*6236dae4SAndroid Build Coastguard Workercallback to inform the application about updates in the socket (file
41*6236dae4SAndroid Build Coastguard Workerdescriptor) status by doing none, one, or multiple calls to the
42*6236dae4SAndroid Build Coastguard Worker**socket_callback**. The callback function gets status updates with changes
43*6236dae4SAndroid Build Coastguard Workersince the previous time the callback was called. If the given callback pointer
44*6236dae4SAndroid Build Coastguard Workeris set to NULL, no callback is called.
45*6236dae4SAndroid Build Coastguard Worker
46*6236dae4SAndroid Build Coastguard Workerlibcurl then expects the application to monitor the sockets for the specific
47*6236dae4SAndroid Build Coastguard Workeractivities and tell libcurl again when something happens on one of them. Tell
48*6236dae4SAndroid Build Coastguard Workerlibcurl by calling curl_multi_socket_action(3).
49*6236dae4SAndroid Build Coastguard Worker
50*6236dae4SAndroid Build Coastguard Worker# CALLBACK ARGUMENTS
51*6236dae4SAndroid Build Coastguard Worker
52*6236dae4SAndroid Build Coastguard Worker*easy* identifies the specific transfer for which this update is related.
53*6236dae4SAndroid Build Coastguard WorkerSince this callback manages a whole multi handle, an application should not
54*6236dae4SAndroid Build Coastguard Workermake assumptions about which particular handle that is passed here. It might
55*6236dae4SAndroid Build Coastguard Workereven be an internal easy handle that the application did not add itself.
56*6236dae4SAndroid Build Coastguard Worker
57*6236dae4SAndroid Build Coastguard Worker*s* is the specific socket this function invocation concerns. If the
58*6236dae4SAndroid Build Coastguard Worker**what** argument is not CURL_POLL_REMOVE then it holds information about
59*6236dae4SAndroid Build Coastguard Workerwhat activity on this socket the application is supposed to
60*6236dae4SAndroid Build Coastguard Workermonitor. Subsequent calls to this callback might update the **what** bits
61*6236dae4SAndroid Build Coastguard Workerfor a socket that is already monitored.
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard WorkerThe socket callback should return 0 on success, and -1 on error. If this
64*6236dae4SAndroid Build Coastguard Workercallback returns error, **all** transfers currently in progress in this
65*6236dae4SAndroid Build Coastguard Workermulti handle are aborted and made to fail.
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard Worker**clientp** is set with CURLMOPT_SOCKETDATA(3).
68*6236dae4SAndroid Build Coastguard Worker
69*6236dae4SAndroid Build Coastguard Worker**socketp** is set with curl_multi_assign(3) or NULL.
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard WorkerThe **what** parameter informs the callback on the status of the given
72*6236dae4SAndroid Build Coastguard Workersocket. It can hold one of these values:
73*6236dae4SAndroid Build Coastguard Worker
74*6236dae4SAndroid Build Coastguard Worker## CURL_POLL_IN
75*6236dae4SAndroid Build Coastguard Worker
76*6236dae4SAndroid Build Coastguard WorkerWait for incoming data. For the socket to become readable.
77*6236dae4SAndroid Build Coastguard Worker
78*6236dae4SAndroid Build Coastguard Worker## CURL_POLL_OUT
79*6236dae4SAndroid Build Coastguard Worker
80*6236dae4SAndroid Build Coastguard WorkerWait for outgoing data. For the socket to become writable.
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard Worker## CURL_POLL_INOUT
83*6236dae4SAndroid Build Coastguard Worker
84*6236dae4SAndroid Build Coastguard WorkerWait for incoming and outgoing data. For the socket to become readable or
85*6236dae4SAndroid Build Coastguard Workerwritable.
86*6236dae4SAndroid Build Coastguard Worker
87*6236dae4SAndroid Build Coastguard Worker## CURL_POLL_REMOVE
88*6236dae4SAndroid Build Coastguard Worker
89*6236dae4SAndroid Build Coastguard WorkerThe specified socket/file descriptor is no longer used by libcurl for any
90*6236dae4SAndroid Build Coastguard Workeractive transfer. It might soon be added again.
91*6236dae4SAndroid Build Coastguard Worker
92*6236dae4SAndroid Build Coastguard Worker# DEFAULT
93*6236dae4SAndroid Build Coastguard Worker
94*6236dae4SAndroid Build Coastguard WorkerNULL (no callback)
95*6236dae4SAndroid Build Coastguard Worker
96*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
97*6236dae4SAndroid Build Coastguard Worker
98*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
99*6236dae4SAndroid Build Coastguard Worker
100*6236dae4SAndroid Build Coastguard Worker~~~c
101*6236dae4SAndroid Build Coastguard Workerstruct priv {
102*6236dae4SAndroid Build Coastguard Worker  void *ours;
103*6236dae4SAndroid Build Coastguard Worker};
104*6236dae4SAndroid Build Coastguard Worker
105*6236dae4SAndroid Build Coastguard Workerstatic int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
106*6236dae4SAndroid Build Coastguard Worker{
107*6236dae4SAndroid Build Coastguard Worker  struct priv *p = sockp;
108*6236dae4SAndroid Build Coastguard Worker  printf("our ptr: %p\n", p->ours);
109*6236dae4SAndroid Build Coastguard Worker
110*6236dae4SAndroid Build Coastguard Worker  if(what == CURL_POLL_REMOVE) {
111*6236dae4SAndroid Build Coastguard Worker    /* remove the socket from our collection */
112*6236dae4SAndroid Build Coastguard Worker  }
113*6236dae4SAndroid Build Coastguard Worker  if(what & CURL_POLL_IN) {
114*6236dae4SAndroid Build Coastguard Worker    /* wait for read on this socket */
115*6236dae4SAndroid Build Coastguard Worker  }
116*6236dae4SAndroid Build Coastguard Worker  if(what & CURL_POLL_OUT) {
117*6236dae4SAndroid Build Coastguard Worker    /* wait for write on this socket */
118*6236dae4SAndroid Build Coastguard Worker  }
119*6236dae4SAndroid Build Coastguard Worker
120*6236dae4SAndroid Build Coastguard Worker  return 0;
121*6236dae4SAndroid Build Coastguard Worker}
122*6236dae4SAndroid Build Coastguard Worker
123*6236dae4SAndroid Build Coastguard Workerint main(void)
124*6236dae4SAndroid Build Coastguard Worker{
125*6236dae4SAndroid Build Coastguard Worker  struct priv setup;
126*6236dae4SAndroid Build Coastguard Worker  CURLM *multi = curl_multi_init();
127*6236dae4SAndroid Build Coastguard Worker  /* ... use socket callback and custom pointer */
128*6236dae4SAndroid Build Coastguard Worker  curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
129*6236dae4SAndroid Build Coastguard Worker  curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, &setup);
130*6236dae4SAndroid Build Coastguard Worker}
131*6236dae4SAndroid Build Coastguard Worker~~~
132*6236dae4SAndroid Build Coastguard Worker
133*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
134*6236dae4SAndroid Build Coastguard Worker
135*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
136*6236dae4SAndroid Build Coastguard Worker
137*6236dae4SAndroid Build Coastguard WorkerReturns CURLM_OK.
138