/src/PROJ/curl/lib/multi_ntfy.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 "curl_trc.h" |
31 | | #include "multihandle.h" |
32 | | #include "multiif.h" |
33 | | #include "multi_ntfy.h" |
34 | | |
35 | | /* The last 3 #include files should be in this order */ |
36 | | #include "curl_printf.h" |
37 | | #include "curl_memory.h" |
38 | | #include "memdebug.h" |
39 | | |
40 | | |
41 | | struct mntfy_entry { |
42 | | unsigned int mid; |
43 | | unsigned int type; |
44 | | }; |
45 | | |
46 | 0 | #define CURL_MNTFY_CHUNK_SIZE 128 |
47 | | |
48 | | struct mntfy_chunk { |
49 | | struct mntfy_chunk *next; |
50 | | size_t r_offset; |
51 | | size_t w_offset; |
52 | | struct mntfy_entry entries[CURL_MNTFY_CHUNK_SIZE]; |
53 | | }; |
54 | | |
55 | | static struct mntfy_chunk *mnfty_chunk_create(void) |
56 | 0 | { |
57 | 0 | return calloc(1, sizeof(struct mntfy_chunk)); |
58 | 0 | } |
59 | | |
60 | | static void mnfty_chunk_destroy(struct mntfy_chunk *chunk) |
61 | 0 | { |
62 | 0 | free(chunk); |
63 | 0 | } |
64 | | |
65 | | static void mnfty_chunk_reset(struct mntfy_chunk *chunk) |
66 | 0 | { |
67 | 0 | memset(chunk, 0, sizeof(*chunk)); |
68 | 0 | } |
69 | | |
70 | | static bool mntfy_chunk_append(struct mntfy_chunk *chunk, |
71 | | struct Curl_easy *data, |
72 | | unsigned int type) |
73 | 0 | { |
74 | 0 | struct mntfy_entry *e; |
75 | |
|
76 | 0 | if(chunk->w_offset >= CURL_MNTFY_CHUNK_SIZE) |
77 | 0 | return FALSE; |
78 | 0 | e = &chunk->entries[chunk->w_offset++]; |
79 | 0 | e->mid = data->mid; |
80 | 0 | e->type = type; |
81 | 0 | return TRUE; |
82 | 0 | } |
83 | | |
84 | | static struct mntfy_chunk *mntfy_non_full_tail(struct curl_multi_ntfy *mntfy) |
85 | 0 | { |
86 | 0 | struct mntfy_chunk *chunk; |
87 | 0 | if(!mntfy->tail) { |
88 | 0 | chunk = mnfty_chunk_create(); |
89 | 0 | if(!chunk) |
90 | 0 | return NULL; |
91 | 0 | DEBUGASSERT(!mntfy->head); |
92 | 0 | mntfy->head = mntfy->tail = chunk; |
93 | 0 | return chunk; |
94 | 0 | } |
95 | 0 | else if(mntfy->tail->w_offset < CURL_MNTFY_CHUNK_SIZE) |
96 | 0 | return mntfy->tail; |
97 | 0 | else { /* tail is full. */ |
98 | 0 | chunk = mnfty_chunk_create(); |
99 | 0 | if(!chunk) |
100 | 0 | return NULL; |
101 | 0 | DEBUGASSERT(mntfy->head); |
102 | 0 | mntfy->tail->next = chunk; |
103 | 0 | mntfy->tail = chunk; |
104 | 0 | return chunk; |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | static void mntfy_chunk_dispatch_all(struct Curl_multi *multi, |
109 | | struct mntfy_chunk *chunk) |
110 | 0 | { |
111 | 0 | struct mntfy_entry *e; |
112 | 0 | struct Curl_easy *data; |
113 | |
|
114 | 0 | if(multi->ntfy.ntfy_cb) { |
115 | 0 | while((chunk->r_offset < chunk->w_offset) && !multi->ntfy.failure) { |
116 | 0 | e = &chunk->entries[chunk->r_offset]; |
117 | 0 | data = e->mid ? Curl_multi_get_easy(multi, e->mid) : multi->admin; |
118 | | /* only when notification has not been disabled in the meantime */ |
119 | 0 | if(data && Curl_uint_bset_contains(&multi->ntfy.enabled, e->type)) { |
120 | | /* this may cause new notifications to be added! */ |
121 | 0 | CURL_TRC_M(multi->admin, "[NTFY] dispatch %d to xfer %u", |
122 | 0 | e->type, e->mid); |
123 | 0 | multi->ntfy.ntfy_cb(multi, e->type, data, multi->ntfy.ntfy_cb_data); |
124 | 0 | } |
125 | | /* once dispatched, safe to increment */ |
126 | 0 | chunk->r_offset++; |
127 | 0 | } |
128 | 0 | } |
129 | 0 | mnfty_chunk_reset(chunk); |
130 | 0 | } |
131 | | |
132 | | void Curl_mntfy_init(struct Curl_multi *multi) |
133 | 0 | { |
134 | 0 | memset(&multi->ntfy, 0, sizeof(multi->ntfy)); |
135 | 0 | Curl_uint_bset_init(&multi->ntfy.enabled); |
136 | 0 | } |
137 | | |
138 | | CURLMcode Curl_mntfy_resize(struct Curl_multi *multi) |
139 | 0 | { |
140 | 0 | if(Curl_uint_bset_resize(&multi->ntfy.enabled, CURLMNOTIFY_EASY_DONE + 1)) |
141 | 0 | return CURLM_OUT_OF_MEMORY; |
142 | 0 | return CURLM_OK; |
143 | 0 | } |
144 | | |
145 | | void Curl_mntfy_cleanup(struct Curl_multi *multi) |
146 | 0 | { |
147 | 0 | while(multi->ntfy.head) { |
148 | 0 | struct mntfy_chunk *chunk = multi->ntfy.head; |
149 | 0 | multi->ntfy.head = chunk->next; |
150 | 0 | mnfty_chunk_destroy(chunk); |
151 | 0 | } |
152 | 0 | multi->ntfy.tail = NULL; |
153 | 0 | Curl_uint_bset_destroy(&multi->ntfy.enabled); |
154 | 0 | } |
155 | | |
156 | | CURLMcode Curl_mntfy_enable(struct Curl_multi *multi, unsigned int type) |
157 | 0 | { |
158 | 0 | if(type > CURLMNOTIFY_EASY_DONE) |
159 | 0 | return CURLM_UNKNOWN_OPTION; |
160 | 0 | Curl_uint_bset_add(&multi->ntfy.enabled, type); |
161 | 0 | return CURLM_OK; |
162 | 0 | } |
163 | | |
164 | | CURLMcode Curl_mntfy_disable(struct Curl_multi *multi, unsigned int type) |
165 | 0 | { |
166 | 0 | if(type > CURLMNOTIFY_EASY_DONE) |
167 | 0 | return CURLM_UNKNOWN_OPTION; |
168 | 0 | Curl_uint_bset_remove(&multi->ntfy.enabled, type); |
169 | 0 | return CURLM_OK; |
170 | 0 | } |
171 | | |
172 | | void Curl_mntfy_add(struct Curl_easy *data, unsigned int type) |
173 | 0 | { |
174 | 0 | struct Curl_multi *multi = data ? data->multi : NULL; |
175 | 0 | if(multi && multi->ntfy.ntfy_cb && !multi->ntfy.failure && |
176 | 0 | Curl_uint_bset_contains(&multi->ntfy.enabled, type)) { |
177 | | /* append to list of outstanding notifications */ |
178 | 0 | struct mntfy_chunk *tail = mntfy_non_full_tail(&multi->ntfy); |
179 | 0 | CURL_TRC_M(data, "[NTFY] add %d for xfer %u", type, data->mid); |
180 | 0 | if(tail) |
181 | 0 | mntfy_chunk_append(tail, data, type); |
182 | 0 | else |
183 | 0 | multi->ntfy.failure = CURLM_OUT_OF_MEMORY; |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | CURLMcode Curl_mntfy_dispatch_all(struct Curl_multi *multi) |
188 | 0 | { |
189 | 0 | DEBUGASSERT(!multi->in_ntfy_callback); |
190 | 0 | multi->in_ntfy_callback = TRUE; |
191 | 0 | while(multi->ntfy.head && !multi->ntfy.failure) { |
192 | 0 | struct mntfy_chunk *chunk = multi->ntfy.head; |
193 | | /* this may cause new notifications to be added! */ |
194 | 0 | mntfy_chunk_dispatch_all(multi, chunk); |
195 | 0 | DEBUGASSERT(chunk->r_offset == chunk->w_offset); |
196 | |
|
197 | 0 | if(chunk == multi->ntfy.tail) /* last one, keep */ |
198 | 0 | break; |
199 | 0 | DEBUGASSERT(chunk->next); |
200 | 0 | DEBUGASSERT(multi->ntfy.head != multi->ntfy.tail); |
201 | 0 | multi->ntfy.head = chunk->next; |
202 | 0 | mnfty_chunk_destroy(chunk); |
203 | 0 | } |
204 | 0 | multi->in_ntfy_callback = FALSE; |
205 | |
|
206 | 0 | if(multi->ntfy.failure) { |
207 | 0 | CURLMcode result = multi->ntfy.failure; |
208 | 0 | multi->ntfy.failure = CURLM_OK; /* reset, once delivered */ |
209 | 0 | return result; |
210 | 0 | } |
211 | 0 | return CURLM_OK; |
212 | 0 | } |