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