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