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