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