/src/PROJ/curl/lib/multi_ev.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
26 | | |
27 | | #include <curl/curl.h> |
28 | | |
29 | | #include "urldata.h" |
30 | | #include "url.h" |
31 | | #include "cfilters.h" |
32 | | #include "curl_trc.h" |
33 | | #include "multiif.h" |
34 | | #include "curlx/timeval.h" |
35 | | #include "multi_ev.h" |
36 | | #include "select.h" |
37 | | #include "uint-bset.h" |
38 | | #include "uint-spbset.h" |
39 | | #include "uint-table.h" |
40 | | #include "curlx/warnless.h" |
41 | | #include "multihandle.h" |
42 | | #include "socks.h" |
43 | | |
44 | | /* The last 2 #include files should be in this order */ |
45 | | #include "curl_memory.h" |
46 | | #include "memdebug.h" |
47 | | |
48 | | |
49 | | static void mev_in_callback(struct Curl_multi *multi, bool value) |
50 | 0 | { |
51 | 0 | multi->in_callback = value; |
52 | 0 | } |
53 | | |
54 | | /* Information about a socket for which we inform the libcurl application |
55 | | * what to supervise (CURL_POLL_IN/CURL_POLL_OUT/CURL_POLL_REMOVE) |
56 | | */ |
57 | | struct mev_sh_entry { |
58 | | struct uint_spbset xfers; /* bitset of transfers `mid`s on this socket */ |
59 | | struct connectdata *conn; /* connection using this socket or NULL */ |
60 | | void *user_data; /* libcurl app data via curl_multi_assign() */ |
61 | | unsigned int action; /* CURL_POLL_IN/CURL_POLL_OUT we last told the |
62 | | * libcurl application to watch out for */ |
63 | | unsigned int readers; /* this many transfers want to read */ |
64 | | unsigned int writers; /* this many transfers want to write */ |
65 | | BIT(announced); /* this socket has been passed to the socket |
66 | | callback at least once */ |
67 | | }; |
68 | | |
69 | | static size_t mev_sh_entry_hash(void *key, size_t key_length, size_t slots_num) |
70 | 0 | { |
71 | 0 | curl_socket_t fd = *((curl_socket_t *) key); |
72 | 0 | (void)key_length; |
73 | 0 | return (fd % (curl_socket_t)slots_num); |
74 | 0 | } |
75 | | |
76 | | static size_t mev_sh_entry_compare(void *k1, size_t k1_len, |
77 | | void *k2, size_t k2_len) |
78 | 0 | { |
79 | 0 | (void)k1_len; (void)k2_len; |
80 | 0 | return (*((curl_socket_t *) k1)) == (*((curl_socket_t *) k2)); |
81 | 0 | } |
82 | | |
83 | | /* sockhash entry destructor callback */ |
84 | | static void mev_sh_entry_dtor(void *freethis) |
85 | 0 | { |
86 | 0 | struct mev_sh_entry *entry = (struct mev_sh_entry *)freethis; |
87 | 0 | Curl_uint_spbset_destroy(&entry->xfers); |
88 | 0 | free(entry); |
89 | 0 | } |
90 | | |
91 | | /* look up a given socket in the socket hash, skip invalid sockets */ |
92 | | static struct mev_sh_entry * |
93 | | mev_sh_entry_get(struct Curl_hash *sh, curl_socket_t s) |
94 | 0 | { |
95 | 0 | if(s != CURL_SOCKET_BAD) { |
96 | | /* only look for proper sockets */ |
97 | 0 | return Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t)); |
98 | 0 | } |
99 | 0 | return NULL; |
100 | 0 | } |
101 | | |
102 | | /* make sure this socket is present in the hash for this handle */ |
103 | | static struct mev_sh_entry * |
104 | | mev_sh_entry_add(struct Curl_hash *sh, curl_socket_t s) |
105 | 0 | { |
106 | 0 | struct mev_sh_entry *there = mev_sh_entry_get(sh, s); |
107 | 0 | struct mev_sh_entry *check; |
108 | |
|
109 | 0 | if(there) { |
110 | | /* it is present, return fine */ |
111 | 0 | return there; |
112 | 0 | } |
113 | | |
114 | | /* not present, add it */ |
115 | 0 | check = calloc(1, sizeof(struct mev_sh_entry)); |
116 | 0 | if(!check) |
117 | 0 | return NULL; /* major failure */ |
118 | | |
119 | 0 | Curl_uint_spbset_init(&check->xfers); |
120 | | |
121 | | /* make/add new hash entry */ |
122 | 0 | if(!Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) { |
123 | 0 | mev_sh_entry_dtor(check); |
124 | 0 | return NULL; /* major failure */ |
125 | 0 | } |
126 | | |
127 | 0 | return check; /* things are good in sockhash land */ |
128 | 0 | } |
129 | | |
130 | | /* delete the given socket entry from the hash */ |
131 | | static void mev_sh_entry_kill(struct Curl_multi *multi, curl_socket_t s) |
132 | 0 | { |
133 | 0 | Curl_hash_delete(&multi->ev.sh_entries, (char *)&s, sizeof(curl_socket_t)); |
134 | 0 | } |
135 | | |
136 | | static size_t mev_sh_entry_user_count(struct mev_sh_entry *e) |
137 | 0 | { |
138 | 0 | return Curl_uint_spbset_count(&e->xfers) + (e->conn ? 1 : 0); |
139 | 0 | } |
140 | | |
141 | | static bool mev_sh_entry_xfer_known(struct mev_sh_entry *e, |
142 | | struct Curl_easy *data) |
143 | 0 | { |
144 | 0 | return Curl_uint_spbset_contains(&e->xfers, data->mid); |
145 | 0 | } |
146 | | |
147 | | static bool mev_sh_entry_conn_known(struct mev_sh_entry *e, |
148 | | struct connectdata *conn) |
149 | 0 | { |
150 | 0 | return (e->conn == conn); |
151 | 0 | } |
152 | | |
153 | | static bool mev_sh_entry_xfer_add(struct mev_sh_entry *e, |
154 | | struct Curl_easy *data) |
155 | 0 | { |
156 | | /* detect weird values */ |
157 | 0 | DEBUGASSERT(mev_sh_entry_user_count(e) < 100000); |
158 | 0 | return Curl_uint_spbset_add(&e->xfers, data->mid); |
159 | 0 | } |
160 | | |
161 | | static bool mev_sh_entry_conn_add(struct mev_sh_entry *e, |
162 | | struct connectdata *conn) |
163 | 0 | { |
164 | | /* detect weird values */ |
165 | 0 | DEBUGASSERT(mev_sh_entry_user_count(e) < 100000); |
166 | 0 | DEBUGASSERT(!e->conn); |
167 | 0 | if(e->conn) |
168 | 0 | return FALSE; |
169 | 0 | e->conn = conn; |
170 | 0 | return TRUE; |
171 | 0 | } |
172 | | |
173 | | |
174 | | static bool mev_sh_entry_xfer_remove(struct mev_sh_entry *e, |
175 | | struct Curl_easy *data) |
176 | 0 | { |
177 | 0 | bool present = Curl_uint_spbset_contains(&e->xfers, data->mid); |
178 | 0 | if(present) |
179 | 0 | Curl_uint_spbset_remove(&e->xfers, data->mid); |
180 | 0 | return present; |
181 | 0 | } |
182 | | |
183 | | static bool mev_sh_entry_conn_remove(struct mev_sh_entry *e, |
184 | | struct connectdata *conn) |
185 | 0 | { |
186 | 0 | DEBUGASSERT(e->conn == conn); |
187 | 0 | if(e->conn == conn) { |
188 | 0 | e->conn = NULL; |
189 | 0 | return TRUE; |
190 | 0 | } |
191 | 0 | return FALSE; |
192 | 0 | } |
193 | | |
194 | | /* Purge any information about socket `s`. |
195 | | * Let the socket callback know as well when necessary */ |
196 | | static CURLMcode mev_forget_socket(struct Curl_multi *multi, |
197 | | struct Curl_easy *data, |
198 | | curl_socket_t s, |
199 | | const char *cause) |
200 | 0 | { |
201 | 0 | struct mev_sh_entry *entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
202 | 0 | int rc = 0; |
203 | |
|
204 | 0 | if(!entry) /* we never knew or already forgot about this socket */ |
205 | 0 | return CURLM_OK; |
206 | | |
207 | | /* We managed this socket before, tell the socket callback to forget it. */ |
208 | 0 | if(entry->announced && multi->socket_cb) { |
209 | 0 | CURL_TRC_M(data, "ev %s, call(fd=%" FMT_SOCKET_T ", ev=REMOVE)", |
210 | 0 | cause, s); |
211 | 0 | mev_in_callback(multi, TRUE); |
212 | 0 | rc = multi->socket_cb(data, s, CURL_POLL_REMOVE, |
213 | 0 | multi->socket_userp, entry->user_data); |
214 | 0 | mev_in_callback(multi, FALSE); |
215 | 0 | entry->announced = FALSE; |
216 | 0 | } |
217 | |
|
218 | 0 | mev_sh_entry_kill(multi, s); |
219 | 0 | if(rc == -1) { |
220 | 0 | multi->dead = TRUE; |
221 | 0 | return CURLM_ABORTED_BY_CALLBACK; |
222 | 0 | } |
223 | 0 | return CURLM_OK; |
224 | 0 | } |
225 | | |
226 | | static CURLMcode mev_sh_entry_update(struct Curl_multi *multi, |
227 | | struct Curl_easy *data, |
228 | | struct mev_sh_entry *entry, |
229 | | curl_socket_t s, |
230 | | unsigned char last_action, |
231 | | unsigned char cur_action) |
232 | 0 | { |
233 | 0 | int rc, comboaction; |
234 | | |
235 | | /* we should only be called when the callback exists */ |
236 | 0 | DEBUGASSERT(multi->socket_cb); |
237 | 0 | if(!multi->socket_cb) |
238 | 0 | return CURLM_OK; |
239 | | |
240 | | /* Transfer `data` goes from `last_action` to `cur_action` on socket `s` |
241 | | * with `multi->ev.sh_entries` entry `entry`. Update `entry` and trigger |
242 | | * `multi->socket_cb` on change, if the callback is set. */ |
243 | 0 | if(last_action == cur_action) /* nothing from `data` changed */ |
244 | 0 | return CURLM_OK; |
245 | | |
246 | 0 | if(last_action & CURL_POLL_IN) { |
247 | 0 | DEBUGASSERT(entry->readers); |
248 | 0 | if(!(cur_action & CURL_POLL_IN)) |
249 | 0 | entry->readers--; |
250 | 0 | } |
251 | 0 | else if(cur_action & CURL_POLL_IN) |
252 | 0 | entry->readers++; |
253 | |
|
254 | 0 | if(last_action & CURL_POLL_OUT) { |
255 | 0 | DEBUGASSERT(entry->writers); |
256 | 0 | if(!(cur_action & CURL_POLL_OUT)) |
257 | 0 | entry->writers--; |
258 | 0 | } |
259 | 0 | else if(cur_action & CURL_POLL_OUT) |
260 | 0 | entry->writers++; |
261 | |
|
262 | 0 | DEBUGASSERT(entry->readers <= mev_sh_entry_user_count(entry)); |
263 | 0 | DEBUGASSERT(entry->writers <= mev_sh_entry_user_count(entry)); |
264 | 0 | DEBUGASSERT(entry->writers + entry->readers); |
265 | |
|
266 | 0 | CURL_TRC_M(data, "ev update fd=%" FMT_SOCKET_T ", action '%s%s' -> '%s%s'" |
267 | 0 | " (%d/%d r/w)", s, |
268 | 0 | (last_action & CURL_POLL_IN) ? "IN" : "", |
269 | 0 | (last_action & CURL_POLL_OUT) ? "OUT" : "", |
270 | 0 | (cur_action & CURL_POLL_IN) ? "IN" : "", |
271 | 0 | (cur_action & CURL_POLL_OUT) ? "OUT" : "", |
272 | 0 | entry->readers, entry->writers); |
273 | |
|
274 | 0 | comboaction = (entry->writers ? CURL_POLL_OUT : 0) | |
275 | 0 | (entry->readers ? CURL_POLL_IN : 0); |
276 | 0 | if(((int)entry->action == comboaction)) /* nothing for socket changed */ |
277 | 0 | return CURLM_OK; |
278 | | |
279 | 0 | CURL_TRC_M(data, "ev update call(fd=%" FMT_SOCKET_T ", ev=%s%s)", |
280 | 0 | s, (comboaction & CURL_POLL_IN) ? "IN" : "", |
281 | 0 | (comboaction & CURL_POLL_OUT) ? "OUT" : ""); |
282 | 0 | mev_in_callback(multi, TRUE); |
283 | 0 | rc = multi->socket_cb(data, s, comboaction, multi->socket_userp, |
284 | 0 | entry->user_data); |
285 | 0 | mev_in_callback(multi, FALSE); |
286 | 0 | entry->announced = TRUE; |
287 | 0 | if(rc == -1) { |
288 | 0 | multi->dead = TRUE; |
289 | 0 | return CURLM_ABORTED_BY_CALLBACK; |
290 | 0 | } |
291 | 0 | entry->action = (unsigned int)comboaction; |
292 | 0 | return CURLM_OK; |
293 | 0 | } |
294 | | |
295 | | static CURLMcode mev_pollset_diff(struct Curl_multi *multi, |
296 | | struct Curl_easy *data, |
297 | | struct connectdata *conn, |
298 | | struct easy_pollset *ps, |
299 | | struct easy_pollset *prev_ps) |
300 | 0 | { |
301 | 0 | struct mev_sh_entry *entry; |
302 | 0 | curl_socket_t s; |
303 | 0 | unsigned int i, j; |
304 | 0 | CURLMcode mresult; |
305 | | |
306 | | /* The transfer `data` reports in `ps` the sockets it is interested |
307 | | * in and which combination of CURL_POLL_IN/CURL_POLL_OUT it wants |
308 | | * to have monitored for events. |
309 | | * There can be more than 1 transfer interested in the same socket |
310 | | * and 1 transfer might be interested in more than 1 socket. |
311 | | * `prev_ps` is the pollset copy from the previous call here. On |
312 | | * the 1st call it will be empty. |
313 | | */ |
314 | 0 | DEBUGASSERT(ps); |
315 | 0 | DEBUGASSERT(prev_ps); |
316 | | |
317 | | /* Handle changes to sockets the transfer is interested in. */ |
318 | 0 | for(i = 0; i < ps->n; i++) { |
319 | 0 | unsigned char last_action; |
320 | 0 | bool first_time = FALSE; /* data/conn appears first time on socket */ |
321 | |
|
322 | 0 | s = ps->sockets[i]; |
323 | | /* Have we handled this socket before? */ |
324 | 0 | entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
325 | 0 | if(!entry) { |
326 | | /* new socket, add new entry */ |
327 | 0 | first_time = TRUE; |
328 | 0 | entry = mev_sh_entry_add(&multi->ev.sh_entries, s); |
329 | 0 | if(!entry) /* fatal */ |
330 | 0 | return CURLM_OUT_OF_MEMORY; |
331 | 0 | CURL_TRC_M(data, "ev new entry fd=%" FMT_SOCKET_T, s); |
332 | 0 | } |
333 | 0 | else if(conn) { |
334 | 0 | first_time = !mev_sh_entry_conn_known(entry, conn); |
335 | 0 | } |
336 | 0 | else { |
337 | 0 | first_time = !mev_sh_entry_xfer_known(entry, data); |
338 | 0 | } |
339 | | |
340 | | /* What was the previous action the transfer had regarding this socket? |
341 | | * If the transfer is new to the socket, disregard the information |
342 | | * in `last_poll`, because the socket might have been destroyed and |
343 | | * reopened. We'd have cleared the sh_entry for that, but the socket |
344 | | * might still be mentioned in the hashed pollsets. */ |
345 | 0 | last_action = 0; |
346 | 0 | if(first_time) { |
347 | 0 | if(conn) { |
348 | 0 | if(!mev_sh_entry_conn_add(entry, conn)) |
349 | 0 | return CURLM_OUT_OF_MEMORY; |
350 | 0 | } |
351 | 0 | else { |
352 | 0 | if(!mev_sh_entry_xfer_add(entry, data)) |
353 | 0 | return CURLM_OUT_OF_MEMORY; |
354 | 0 | } |
355 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", added %s #%" FMT_OFF_T |
356 | 0 | ", total=%u/%d (xfer/conn)", s, |
357 | 0 | conn ? "connection" : "transfer", |
358 | 0 | conn ? conn->connection_id : data->mid, |
359 | 0 | Curl_uint_spbset_count(&entry->xfers), |
360 | 0 | entry->conn ? 1 : 0); |
361 | 0 | } |
362 | 0 | else { |
363 | 0 | for(j = 0; j < prev_ps->n; j++) { |
364 | 0 | if(s == prev_ps->sockets[j]) { |
365 | 0 | last_action = prev_ps->actions[j]; |
366 | 0 | break; |
367 | 0 | } |
368 | 0 | } |
369 | 0 | } |
370 | | /* track readers/writers changes and report to socket callback */ |
371 | 0 | mresult = mev_sh_entry_update(multi, data, entry, s, |
372 | 0 | last_action, ps->actions[i]); |
373 | 0 | if(mresult) |
374 | 0 | return mresult; |
375 | 0 | } |
376 | | |
377 | | /* Handle changes to sockets the transfer is NO LONGER interested in. */ |
378 | 0 | for(i = 0; i < prev_ps->n; i++) { |
379 | 0 | bool stillused = FALSE; |
380 | |
|
381 | 0 | s = prev_ps->sockets[i]; |
382 | 0 | for(j = 0; j < ps->n; j++) { |
383 | 0 | if(s == ps->sockets[j]) { |
384 | | /* socket is still supervised */ |
385 | 0 | stillused = TRUE; |
386 | 0 | break; |
387 | 0 | } |
388 | 0 | } |
389 | 0 | if(stillused) |
390 | 0 | continue; |
391 | | |
392 | 0 | entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
393 | | /* if entry does not exist, we were either never told about it or |
394 | | * have already cleaned up this socket via Curl_multi_ev_socket_done(). |
395 | | * In other words: this is perfectly normal */ |
396 | 0 | if(!entry) |
397 | 0 | continue; |
398 | | |
399 | 0 | if(conn && !mev_sh_entry_conn_remove(entry, conn)) { |
400 | | /* `conn` says in `prev_ps` that it had been using a socket, |
401 | | * but `conn` has not been registered for it. |
402 | | * This should not happen if our book-keeping is correct? */ |
403 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", conn lost " |
404 | 0 | "interest but is not registered", s); |
405 | 0 | DEBUGASSERT(NULL); |
406 | 0 | continue; |
407 | 0 | } |
408 | | |
409 | 0 | if(!conn && !mev_sh_entry_xfer_remove(entry, data)) { |
410 | | /* `data` says in `prev_ps` that it had been using a socket, |
411 | | * but `data` has not been registered for it. |
412 | | * This should not happen if our book-keeping is correct? */ |
413 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", transfer lost " |
414 | 0 | "interest but is not registered", s); |
415 | 0 | DEBUGASSERT(NULL); |
416 | 0 | continue; |
417 | 0 | } |
418 | | |
419 | 0 | if(mev_sh_entry_user_count(entry)) { |
420 | | /* track readers/writers changes and report to socket callback */ |
421 | 0 | mresult = mev_sh_entry_update(multi, data, entry, s, |
422 | 0 | prev_ps->actions[i], 0); |
423 | 0 | if(mresult) |
424 | 0 | return mresult; |
425 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", removed transfer, " |
426 | 0 | "total=%u/%d (xfer/conn)", s, |
427 | 0 | Curl_uint_spbset_count(&entry->xfers), |
428 | 0 | entry->conn ? 1 : 0); |
429 | 0 | } |
430 | 0 | else { |
431 | 0 | mresult = mev_forget_socket(multi, data, s, "last user gone"); |
432 | 0 | if(mresult) |
433 | 0 | return mresult; |
434 | 0 | } |
435 | 0 | } /* for loop over num */ |
436 | | |
437 | | /* Remember for next time */ |
438 | 0 | Curl_pollset_move(prev_ps, ps); |
439 | 0 | return CURLM_OK; |
440 | 0 | } |
441 | | |
442 | | static void mev_pollset_dtor(void *key, size_t klen, void *entry) |
443 | 0 | { |
444 | 0 | struct easy_pollset *ps = entry; |
445 | 0 | (void)key; |
446 | 0 | (void)klen; |
447 | 0 | if(ps) { |
448 | 0 | Curl_pollset_cleanup(ps); |
449 | 0 | free(ps); |
450 | 0 | } |
451 | 0 | } |
452 | | |
453 | | static struct easy_pollset* |
454 | | mev_add_new_conn_pollset(struct connectdata *conn) |
455 | 0 | { |
456 | 0 | struct easy_pollset *ps; |
457 | |
|
458 | 0 | ps = Curl_pollset_create(); |
459 | 0 | if(!ps) |
460 | 0 | return NULL; |
461 | 0 | if(Curl_conn_meta_set(conn, CURL_META_MEV_POLLSET, ps, mev_pollset_dtor)) |
462 | 0 | return NULL; |
463 | 0 | return ps; |
464 | 0 | } |
465 | | |
466 | | static struct easy_pollset* |
467 | | mev_add_new_xfer_pollset(struct Curl_easy *data) |
468 | 0 | { |
469 | 0 | struct easy_pollset *ps; |
470 | |
|
471 | 0 | ps = Curl_pollset_create(); |
472 | 0 | if(!ps) |
473 | 0 | return NULL; |
474 | 0 | if(Curl_meta_set(data, CURL_META_MEV_POLLSET, ps, mev_pollset_dtor)) |
475 | 0 | return NULL; |
476 | 0 | return ps; |
477 | 0 | } |
478 | | |
479 | | static struct easy_pollset * |
480 | | mev_get_last_pollset(struct Curl_easy *data, |
481 | | struct connectdata *conn) |
482 | 0 | { |
483 | 0 | if(data) { |
484 | 0 | if(conn) |
485 | 0 | return Curl_conn_meta_get(conn, CURL_META_MEV_POLLSET); |
486 | 0 | return Curl_meta_get(data, CURL_META_MEV_POLLSET); |
487 | 0 | } |
488 | 0 | return NULL; |
489 | 0 | } |
490 | | |
491 | | static CURLMcode mev_assess(struct Curl_multi *multi, |
492 | | struct Curl_easy *data, |
493 | | struct connectdata *conn) |
494 | 0 | { |
495 | 0 | struct easy_pollset ps, *last_ps; |
496 | 0 | CURLMcode res = CURLM_OK; |
497 | |
|
498 | 0 | if(!multi || !multi->socket_cb) |
499 | 0 | return CURLM_OK; |
500 | | |
501 | 0 | Curl_pollset_init(&ps); |
502 | 0 | if(conn) { |
503 | 0 | CURLcode r = Curl_conn_adjust_pollset(data, conn, &ps); |
504 | 0 | if(r) { |
505 | 0 | res = (r == CURLE_OUT_OF_MEMORY) ? |
506 | 0 | CURLM_OUT_OF_MEMORY : CURLM_INTERNAL_ERROR; |
507 | 0 | goto out; |
508 | 0 | } |
509 | 0 | } |
510 | 0 | else |
511 | 0 | Curl_multi_pollset(data, &ps, "ev assess"); |
512 | 0 | last_ps = mev_get_last_pollset(data, conn); |
513 | |
|
514 | 0 | if(!last_ps && ps.n) { |
515 | 0 | if(conn) |
516 | 0 | last_ps = mev_add_new_conn_pollset(conn); |
517 | 0 | else |
518 | 0 | last_ps = mev_add_new_xfer_pollset(data); |
519 | 0 | if(!last_ps) { |
520 | 0 | res = CURLM_OUT_OF_MEMORY; |
521 | 0 | goto out; |
522 | 0 | } |
523 | 0 | } |
524 | | |
525 | 0 | if(last_ps) |
526 | 0 | res = mev_pollset_diff(multi, data, conn, &ps, last_ps); |
527 | 0 | else |
528 | 0 | DEBUGASSERT(!ps.n); |
529 | 0 | out: |
530 | 0 | Curl_pollset_cleanup(&ps); |
531 | 0 | return res; |
532 | 0 | } |
533 | | |
534 | | CURLMcode Curl_multi_ev_assess_xfer(struct Curl_multi *multi, |
535 | | struct Curl_easy *data) |
536 | 0 | { |
537 | 0 | return mev_assess(multi, data, NULL); |
538 | 0 | } |
539 | | |
540 | | CURLMcode Curl_multi_ev_assess_conn(struct Curl_multi *multi, |
541 | | struct Curl_easy *data, |
542 | | struct connectdata *conn) |
543 | 0 | { |
544 | 0 | return mev_assess(multi, data, conn); |
545 | 0 | } |
546 | | |
547 | | CURLMcode Curl_multi_ev_assess_xfer_bset(struct Curl_multi *multi, |
548 | | struct uint_bset *set) |
549 | 0 | { |
550 | 0 | unsigned int mid; |
551 | 0 | CURLMcode result = CURLM_OK; |
552 | |
|
553 | 0 | if(multi && multi->socket_cb && Curl_uint_bset_first(set, &mid)) { |
554 | 0 | do { |
555 | 0 | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
556 | 0 | if(data) |
557 | 0 | result = Curl_multi_ev_assess_xfer(multi, data); |
558 | 0 | } |
559 | 0 | while(!result && Curl_uint_bset_next(set, mid, &mid)); |
560 | 0 | } |
561 | 0 | return result; |
562 | 0 | } |
563 | | |
564 | | |
565 | | CURLMcode Curl_multi_ev_assign(struct Curl_multi *multi, |
566 | | curl_socket_t s, |
567 | | void *user_data) |
568 | 0 | { |
569 | 0 | struct mev_sh_entry *e = mev_sh_entry_get(&multi->ev.sh_entries, s); |
570 | 0 | if(!e) |
571 | 0 | return CURLM_BAD_SOCKET; |
572 | 0 | e->user_data = user_data; |
573 | 0 | return CURLM_OK; |
574 | 0 | } |
575 | | |
576 | | void Curl_multi_ev_dirty_xfers(struct Curl_multi *multi, |
577 | | curl_socket_t s, |
578 | | bool *run_cpool) |
579 | 0 | { |
580 | 0 | struct mev_sh_entry *entry; |
581 | |
|
582 | 0 | DEBUGASSERT(s != CURL_SOCKET_TIMEOUT); |
583 | 0 | entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
584 | | |
585 | | /* Unmatched socket, we cannot act on it but we ignore this fact. In |
586 | | real-world tests it has been proved that libevent can in fact give |
587 | | the application actions even though the socket was just previously |
588 | | asked to get removed, so thus we better survive stray socket actions |
589 | | and just move on. */ |
590 | 0 | if(entry) { |
591 | 0 | struct Curl_easy *data; |
592 | 0 | unsigned int mid; |
593 | |
|
594 | 0 | if(Curl_uint_spbset_first(&entry->xfers, &mid)) { |
595 | 0 | do { |
596 | 0 | data = Curl_multi_get_easy(multi, mid); |
597 | 0 | if(data) { |
598 | 0 | Curl_multi_mark_dirty(data); |
599 | 0 | } |
600 | 0 | else { |
601 | 0 | CURL_TRC_M(multi->admin, "socket transfer %u no longer found", mid); |
602 | 0 | Curl_uint_spbset_remove(&entry->xfers, mid); |
603 | 0 | } |
604 | 0 | } |
605 | 0 | while(Curl_uint_spbset_next(&entry->xfers, mid, &mid)); |
606 | 0 | } |
607 | |
|
608 | 0 | if(entry->conn) |
609 | 0 | *run_cpool = TRUE; |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | void Curl_multi_ev_socket_done(struct Curl_multi *multi, |
614 | | struct Curl_easy *data, curl_socket_t s) |
615 | 0 | { |
616 | 0 | mev_forget_socket(multi, data, s, "socket done"); |
617 | 0 | } |
618 | | |
619 | | void Curl_multi_ev_xfer_done(struct Curl_multi *multi, |
620 | | struct Curl_easy *data) |
621 | 0 | { |
622 | 0 | DEBUGASSERT(!data->conn); /* transfer should have been detached */ |
623 | 0 | if(data != multi->admin) { |
624 | 0 | (void)mev_assess(multi, data, NULL); |
625 | 0 | Curl_meta_remove(data, CURL_META_MEV_POLLSET); |
626 | 0 | } |
627 | 0 | } |
628 | | |
629 | | void Curl_multi_ev_conn_done(struct Curl_multi *multi, |
630 | | struct Curl_easy *data, |
631 | | struct connectdata *conn) |
632 | 0 | { |
633 | 0 | (void)mev_assess(multi, data, conn); |
634 | 0 | Curl_conn_meta_remove(conn, CURL_META_MEV_POLLSET); |
635 | 0 | } |
636 | | |
637 | | void Curl_multi_ev_init(struct Curl_multi *multi, size_t hashsize) |
638 | 0 | { |
639 | 0 | Curl_hash_init(&multi->ev.sh_entries, hashsize, mev_sh_entry_hash, |
640 | 0 | mev_sh_entry_compare, mev_sh_entry_dtor); |
641 | 0 | } |
642 | | |
643 | | void Curl_multi_ev_cleanup(struct Curl_multi *multi) |
644 | 0 | { |
645 | 0 | Curl_hash_destroy(&multi->ev.sh_entries); |
646 | 0 | } |