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