/src/FreeRDP/winpr/libwinpr/utils/collections/PubSub.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Publisher/Subscriber Pattern |
4 | | * |
5 | | * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/config.h> |
21 | | |
22 | | #include <winpr/crt.h> |
23 | | |
24 | | #include <winpr/collections.h> |
25 | | |
26 | | /** |
27 | | * Events (C# Programming Guide) |
28 | | * http://msdn.microsoft.com/en-us/library/awbftdfh.aspx |
29 | | */ |
30 | | |
31 | | struct s_wPubSub |
32 | | { |
33 | | CRITICAL_SECTION lock; |
34 | | BOOL synchronized; |
35 | | |
36 | | size_t size; |
37 | | size_t count; |
38 | | wEventType* events; |
39 | | }; |
40 | | |
41 | | /** |
42 | | * Properties |
43 | | */ |
44 | | |
45 | | wEventType* PubSub_GetEventTypes(wPubSub* pubSub, size_t* count) |
46 | 0 | { |
47 | 0 | WINPR_ASSERT(pubSub); |
48 | 0 | if (count) |
49 | 0 | *count = pubSub->count; |
50 | |
|
51 | 0 | return pubSub->events; |
52 | 0 | } |
53 | | |
54 | | /** |
55 | | * Methods |
56 | | */ |
57 | | |
58 | | void PubSub_Lock(wPubSub* pubSub) |
59 | 0 | { |
60 | 0 | WINPR_ASSERT(pubSub); |
61 | 0 | if (pubSub->synchronized) |
62 | 0 | EnterCriticalSection(&pubSub->lock); |
63 | 0 | } |
64 | | |
65 | | void PubSub_Unlock(wPubSub* pubSub) |
66 | 0 | { |
67 | 0 | WINPR_ASSERT(pubSub); |
68 | 0 | if (pubSub->synchronized) |
69 | 0 | LeaveCriticalSection(&pubSub->lock); |
70 | 0 | } |
71 | | |
72 | | wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName) |
73 | 0 | { |
74 | 0 | wEventType* event = NULL; |
75 | |
|
76 | 0 | WINPR_ASSERT(pubSub); |
77 | 0 | WINPR_ASSERT(EventName); |
78 | 0 | for (size_t index = 0; index < pubSub->count; index++) |
79 | 0 | { |
80 | 0 | if (strcmp(pubSub->events[index].EventName, EventName) == 0) |
81 | 0 | { |
82 | 0 | event = &(pubSub->events[index]); |
83 | 0 | break; |
84 | 0 | } |
85 | 0 | } |
86 | |
|
87 | 0 | return event; |
88 | 0 | } |
89 | | |
90 | | void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, size_t count) |
91 | 0 | { |
92 | 0 | WINPR_ASSERT(pubSub); |
93 | 0 | WINPR_ASSERT(events || (count == 0)); |
94 | 0 | if (pubSub->synchronized) |
95 | 0 | PubSub_Lock(pubSub); |
96 | |
|
97 | 0 | const size_t required = pubSub->count + count; |
98 | 0 | WINPR_ASSERT((required >= pubSub->count) && (required >= count)); |
99 | |
|
100 | 0 | if (required >= pubSub->size) |
101 | 0 | { |
102 | 0 | size_t new_size = pubSub->size; |
103 | 0 | do |
104 | 0 | { |
105 | 0 | WINPR_ASSERT(new_size <= SIZE_MAX - 128ull); |
106 | 0 | new_size += 128ull; |
107 | 0 | } while (new_size <= required); |
108 | |
|
109 | 0 | wEventType* new_event = (wEventType*)realloc(pubSub->events, new_size * sizeof(wEventType)); |
110 | 0 | if (!new_event) |
111 | 0 | goto fail; |
112 | 0 | pubSub->size = new_size; |
113 | 0 | pubSub->events = new_event; |
114 | 0 | } |
115 | | |
116 | 0 | CopyMemory(&pubSub->events[pubSub->count], events, count * sizeof(wEventType)); |
117 | 0 | pubSub->count += count; |
118 | |
|
119 | 0 | fail: |
120 | 0 | if (pubSub->synchronized) |
121 | 0 | PubSub_Unlock(pubSub); |
122 | 0 | } |
123 | | |
124 | | int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, ...) |
125 | 0 | { |
126 | 0 | wEventType* event = NULL; |
127 | 0 | int status = -1; |
128 | 0 | WINPR_ASSERT(pubSub); |
129 | |
|
130 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
131 | 0 | va_start(ap, EventName); |
132 | 0 | pEventHandler EventHandler = va_arg(ap, pEventHandler); |
133 | |
|
134 | 0 | if (pubSub->synchronized) |
135 | 0 | PubSub_Lock(pubSub); |
136 | |
|
137 | 0 | event = PubSub_FindEventType(pubSub, EventName); |
138 | |
|
139 | 0 | if (event) |
140 | 0 | { |
141 | 0 | status = 0; |
142 | |
|
143 | 0 | if (event->EventHandlerCount < MAX_EVENT_HANDLERS) |
144 | 0 | event->EventHandlers[event->EventHandlerCount++] = EventHandler; |
145 | 0 | else |
146 | 0 | status = -1; |
147 | 0 | } |
148 | |
|
149 | 0 | if (pubSub->synchronized) |
150 | 0 | PubSub_Unlock(pubSub); |
151 | |
|
152 | 0 | va_end(ap); |
153 | 0 | return status; |
154 | 0 | } |
155 | | |
156 | | int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, ...) |
157 | 0 | { |
158 | 0 | wEventType* event = NULL; |
159 | 0 | int status = -1; |
160 | 0 | WINPR_ASSERT(pubSub); |
161 | 0 | WINPR_ASSERT(EventName); |
162 | |
|
163 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
164 | 0 | va_start(ap, EventName); |
165 | 0 | pEventHandler EventHandler = va_arg(ap, pEventHandler); |
166 | |
|
167 | 0 | if (pubSub->synchronized) |
168 | 0 | PubSub_Lock(pubSub); |
169 | |
|
170 | 0 | event = PubSub_FindEventType(pubSub, EventName); |
171 | |
|
172 | 0 | if (event) |
173 | 0 | { |
174 | 0 | status = 0; |
175 | |
|
176 | 0 | for (size_t index = 0; index < event->EventHandlerCount; index++) |
177 | 0 | { |
178 | 0 | if (event->EventHandlers[index] == EventHandler) |
179 | 0 | { |
180 | 0 | event->EventHandlers[index] = NULL; |
181 | 0 | event->EventHandlerCount--; |
182 | 0 | MoveMemory((void*)&event->EventHandlers[index], |
183 | 0 | (void*)&event->EventHandlers[index + 1], |
184 | 0 | (MAX_EVENT_HANDLERS - index - 1) * sizeof(pEventHandler)); |
185 | 0 | status = 1; |
186 | 0 | } |
187 | 0 | } |
188 | 0 | } |
189 | |
|
190 | 0 | if (pubSub->synchronized) |
191 | 0 | PubSub_Unlock(pubSub); |
192 | |
|
193 | 0 | va_end(ap); |
194 | 0 | return status; |
195 | 0 | } |
196 | | |
197 | | int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context, const wEventArgs* e) |
198 | 0 | { |
199 | 0 | wEventType* event = NULL; |
200 | 0 | int status = -1; |
201 | |
|
202 | 0 | if (!pubSub) |
203 | 0 | return -1; |
204 | 0 | WINPR_ASSERT(e); |
205 | |
|
206 | 0 | if (pubSub->synchronized) |
207 | 0 | PubSub_Lock(pubSub); |
208 | |
|
209 | 0 | event = PubSub_FindEventType(pubSub, EventName); |
210 | |
|
211 | 0 | if (pubSub->synchronized) |
212 | 0 | PubSub_Unlock(pubSub); |
213 | |
|
214 | 0 | if (event) |
215 | 0 | { |
216 | 0 | status = 0; |
217 | |
|
218 | 0 | for (size_t index = 0; index < event->EventHandlerCount; index++) |
219 | 0 | { |
220 | 0 | if (event->EventHandlers[index]) |
221 | 0 | { |
222 | 0 | event->EventHandlers[index](context, e); |
223 | 0 | status++; |
224 | 0 | } |
225 | 0 | } |
226 | 0 | } |
227 | |
|
228 | 0 | return status; |
229 | 0 | } |
230 | | |
231 | | /** |
232 | | * Construction, Destruction |
233 | | */ |
234 | | |
235 | | wPubSub* PubSub_New(BOOL synchronized) |
236 | 0 | { |
237 | 0 | wPubSub* pubSub = (wPubSub*)calloc(1, sizeof(wPubSub)); |
238 | |
|
239 | 0 | if (!pubSub) |
240 | 0 | return NULL; |
241 | | |
242 | 0 | pubSub->synchronized = synchronized; |
243 | |
|
244 | 0 | if (pubSub->synchronized && !InitializeCriticalSectionAndSpinCount(&pubSub->lock, 4000)) |
245 | 0 | goto fail; |
246 | | |
247 | 0 | pubSub->count = 0; |
248 | 0 | pubSub->size = 64; |
249 | |
|
250 | 0 | pubSub->events = (wEventType*)calloc(pubSub->size, sizeof(wEventType)); |
251 | 0 | if (!pubSub->events) |
252 | 0 | goto fail; |
253 | | |
254 | 0 | return pubSub; |
255 | 0 | fail: |
256 | 0 | WINPR_PRAGMA_DIAG_PUSH |
257 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
258 | 0 | PubSub_Free(pubSub); |
259 | 0 | WINPR_PRAGMA_DIAG_POP |
260 | 0 | return NULL; |
261 | 0 | } |
262 | | |
263 | | void PubSub_Free(wPubSub* pubSub) |
264 | 0 | { |
265 | 0 | if (pubSub) |
266 | 0 | { |
267 | 0 | if (pubSub->synchronized) |
268 | 0 | DeleteCriticalSection(&pubSub->lock); |
269 | |
|
270 | 0 | free(pubSub->events); |
271 | 0 | free(pubSub); |
272 | 0 | } |
273 | 0 | } |