Line | Count | Source |
1 | | #ifndef HEADER_CURL_CSHUTDN_H |
2 | | #define HEADER_CURL_CSHUTDN_H |
3 | | /*************************************************************************** |
4 | | * _ _ ____ _ |
5 | | * Project ___| | | | _ \| | |
6 | | * / __| | | | |_) | | |
7 | | * | (__| |_| | _ <| |___ |
8 | | * \___|\___/|_| \_\_____| |
9 | | * |
10 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
11 | | * Copyright (C) Linus Nielsen Feltzing, <linus@haxx.se> |
12 | | * |
13 | | * This software is licensed as described in the file COPYING, which |
14 | | * you should have received as part of this distribution. The terms |
15 | | * are also available at https://curl.se/docs/copyright.html. |
16 | | * |
17 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
18 | | * copies of the Software, and permit persons to whom the Software is |
19 | | * furnished to do so, under the terms of the COPYING file. |
20 | | * |
21 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
22 | | * KIND, either express or implied. |
23 | | * |
24 | | * SPDX-License-Identifier: curl |
25 | | * |
26 | | ***************************************************************************/ |
27 | | #include "ptrarray.h" |
28 | | |
29 | | struct connectdata; |
30 | | struct Curl_easy; |
31 | | struct Curl_cfchain; |
32 | | struct curl_pollfds; |
33 | | struct Curl_waitfds; |
34 | | struct Curl_multi; |
35 | | struct Curl_share; |
36 | | struct Curl_sigpipe_ctx; |
37 | | |
38 | | |
39 | | /* Terminates the connection, e.g. closes and destroys it. |
40 | | * If `do_shutdown` is TRUE, the shutdown will be run once. |
41 | | * Takes ownership of `conn`. `conn` MUST no longer be in |
42 | | * a pool or on a shutdown list. */ |
43 | | void Curl_cshutdn_terminate(struct Curl_easy *admin, |
44 | | struct connectdata *conn, |
45 | | bool do_shutdown); |
46 | | |
47 | | /* Start the shutdown timer, |
48 | | * marks the connection as being shut down. */ |
49 | | void Curl_cshutdn_start_timer(struct Curl_easy *data, |
50 | | struct Curl_cfchain *chain, |
51 | | int timeout_ms); |
52 | | /* Clear the shutdown timer at the chain again. */ |
53 | | void Curl_cshutdn_clear_timer(struct Curl_cfchain *chain); |
54 | | |
55 | | /* Return how much time there is left to shutdown the connection. |
56 | | * Returns 0 if there is no limit or shutdown has not started. */ |
57 | | timediff_t Curl_cshutdn_timeleft_ms(struct Curl_easy *data, |
58 | | struct Curl_cfchain *chain); |
59 | | |
60 | 0 | #define CURL_CFCHAIN_IN_SHUTDOWN(c) ((c)->shutdown_start_ms >= 0) |
61 | | |
62 | | /* Shutdown the connection filter chain non-blocking. |
63 | | * Will start the shutdown timer if not already set. |
64 | | * Return CURLE_OK and *done == FALSE if not finished. */ |
65 | | CURLcode Curl_cshutdn_cfchain_try_once(struct Curl_easy *data, |
66 | | struct Curl_cfchain *chain, |
67 | | bool *done); |
68 | | |
69 | | /* Run the shutdown of the connection once non-blocking for both |
70 | | * socket indices. Will start the shutdown timer if not already set. |
71 | | * Shortly attach/detach the admin handle to `conn` while doing so. |
72 | | * `done` will be set TRUE if any error was encountered or if |
73 | | * the connection was shut down completely. */ |
74 | | void Curl_cshutdn_try_once(struct Curl_easy *admin, |
75 | | struct connectdata *conn, bool *done); |
76 | | |
77 | | /* A `cshutdown` is always owned by a multi handle to maintain |
78 | | * the connections to be shut down. It registers timers and |
79 | | * sockets to monitor via the multi handle. */ |
80 | | struct cshutdn { |
81 | | struct ptrarray conns; /* connections being shut down */ |
82 | | }; |
83 | | |
84 | | /* Init as part of the given multi handle. */ |
85 | | void Curl_cshutdn_init(struct cshutdn *cshutdn); |
86 | | |
87 | | /* Terminate all remaining connections and free resources. */ |
88 | | void Curl_cshutdn_destroy(struct cshutdn *cshutdn, |
89 | | struct Curl_easy *admin); |
90 | | |
91 | | /* Number of connections being shut down. */ |
92 | | size_t Curl_cshutdn_count(struct cshutdn *cshutdn); |
93 | | |
94 | | /* Number of connections to the destination being shut down. */ |
95 | | size_t Curl_cshutdn_dest_count(struct cshutdn *cshutdn, |
96 | | const char *destination); |
97 | | |
98 | | /* Close the oldest `count` connections in shutdown to destination or, |
99 | | * when destination is NULL for any destination. |
100 | | * Return the number of connections closed. */ |
101 | | size_t Curl_cshutdn_close_oldest(struct cshutdn *cshutdn, |
102 | | struct Curl_easy *admin, |
103 | | const char *destination, |
104 | | size_t count); |
105 | | |
106 | | /* Add a connection to have it shut down. Terminate the oldest |
107 | | * connection when shutdowns exceed max_shutdowns. */ |
108 | | void Curl_cshutdn_add(struct cshutdn *cshutdn, |
109 | | struct Curl_multi *multi, |
110 | | struct connectdata *conn, |
111 | | size_t max_shutdowns); |
112 | | |
113 | | /* Add sockets and POLLIN/OUT flags for connections being shut down. */ |
114 | | CURLcode Curl_cshutdn_add_pollfds(struct cshutdn *cshutdn, |
115 | | struct Curl_easy *admin, |
116 | | struct curl_pollfds *cpfds); |
117 | | |
118 | | unsigned int Curl_cshutdn_add_waitfds(struct cshutdn *cshutdn, |
119 | | struct Curl_easy *admin, |
120 | | struct Curl_waitfds *cwfds); |
121 | | |
122 | | void Curl_cshutdn_setfds(struct cshutdn *cshutdn, |
123 | | struct Curl_easy *admin, |
124 | | fd_set *read_fd_set, fd_set *write_fd_set, |
125 | | int *maxfd); |
126 | | |
127 | | /* Run maintenance on all connections. */ |
128 | | void Curl_cshutdn_perform(struct cshutdn *cshutdn, |
129 | | struct Curl_easy *admin, |
130 | | struct Curl_sigpipe_ctx *sigpipe_ctx); |
131 | | |
132 | | #endif /* HEADER_CURL_CSHUTDN_H */ |