Debian Codegraph @

Xpjproject/pjlib/src/pj/ioqueue_uwp.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pj/ioqueue.h>
19 #include <pj/errno.h>
20 #include <pj/lock.h>
21 #include <pj/log.h>
22 #include <pj/os.h>
23 #include <pj/pool.h>
24
25 /* Only build when the backend is using Windows UWP socket. */
26 #if PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_UWP
27
28
29 #include <ppltasks.h>
30 #include <string>
31
32 #define THIS_FILE "ioq_uwp"
33
34 #include "sock_uwp.h"
35 #include "ioqueue_common_abs.h"
36
37 /*
38 * This describes each key.
39 */
40 struct pj_ioqueue_key_t
41 {
42 DECLARE_COMMON_KEY
43 };
44
45 /*
46 * This describes the I/O queue itself.
47 */
48 struct pj_ioqueue_t
49 {
50 DECLARE_COMMON_IOQUEUE
51 pj_thread_desc thread_desc[16];
52 unsigned thread_cnt;
53 };
54
55
56 #include "ioqueue_common_abs.c"
57
58 static void ioqueue_remove_from_set2(pj_ioqueue_t *ioqueue,
59 pj_ioqueue_key_t *key,
60 unsigned event_types)
61 {
62 PJ_UNUSED_ARG(ioqueue);
63 PJ_UNUSED_ARG(key);
64 PJ_UNUSED_ARG(event_types);
65 }
66
67 static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue,
68 pj_ioqueue_key_t *key,
69 enum ioqueue_event_type event_type )
70 {
71 ioqueue_remove_from_set2(ioqueue, key, event_type);
72 }
73
74 static void start_next_read(pj_ioqueue_key_t *key)
75 {
76 if (key_has_pending_read(key)) {
77 PjUwpSocket *s = (PjUwpSocket*)key->fd;
78 struct read_operation *op;
79 op = (struct read_operation*)key->read_list.next;
80
81 if (op->op == PJ_IOQUEUE_OP_RECV)
82 s->Recv(NULL, (pj_ssize_t*)&op->size);
83 else
84 s->RecvFrom(NULL, (pj_ssize_t*)&op->size, NULL);
85 }
86 }
87
88
89 static void start_next_write(pj_ioqueue_key_t *key)
90 {
91 if (key_has_pending_write(key)) {
92 PjUwpSocket *s = (PjUwpSocket*)key->fd;
93 struct write_operation *op;
94 op = (struct write_operation*)key->write_list.next;
95
96 if (op->op == PJ_IOQUEUE_OP_SEND)
97 s->Send(op->buf, (pj_ssize_t*)&op->size);
98 else
99 s->SendTo(op->buf, (pj_ssize_t*)&op->size, &op->rmt_addr);
100 }
101 }
102
103
104 static void ioqueue_add_to_set2(pj_ioqueue_t *ioqueue,
105 pj_ioqueue_key_t *key,
106 unsigned event_types )
107 {
108 PJ_UNUSED_ARG(ioqueue);
109
110 if (event_types & READABLE_EVENT) {
111 /* This is either recv, recvfrom, or accept, do nothing on accept */
112 start_next_read(key);
113 }
114 if (event_types & WRITEABLE_EVENT) {
115 /* This is either send, sendto, or connect, do nothing on connect */
116 //start_next_write(key);
117 }
118 }
119
120 static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue,
121 pj_ioqueue_key_t *key,
122 enum ioqueue_event_type event_type )
123 {
124 ioqueue_add_to_set2(ioqueue, key, event_type);
125 }
126
127 static void check_thread(pj_ioqueue_t *ioq) {
128 if (pj_thread_is_registered())
129 return;
130
131 pj_thread_t *t;
132 char tmp[16];
133 pj_ansi_snprintf(tmp, sizeof(tmp), "UwpThread%02d", ioq->thread_cnt);
134 pj_thread_register(tmp, ioq->thread_desc[ioq->thread_cnt++], &t);
135 pj_assert(ioq->thread_cnt < PJ_ARRAY_SIZE(ioq->thread_desc));
136 ioq->thread_cnt %= PJ_ARRAY_SIZE(ioq->thread_desc);
137 }
138
139 static void on_read(PjUwpSocket *s, int bytes_read)
140 {
141 pj_ioqueue_key_t *key = (pj_ioqueue_key_t*)s->GetUserData();
142 pj_ioqueue_t *ioq = key->ioqueue;
143 check_thread(ioq);
144
145 ioqueue_dispatch_read_event(key->ioqueue, key);
146
147 if (bytes_read > 0)
148 start_next_read(key);
149 }
150
151 static void on_write(PjUwpSocket *s, int bytes_sent)
152 {
153 PJ_UNUSED_ARG(bytes_sent);
154 pj_ioqueue_key_t *key = (pj_ioqueue_key_t*)s->GetUserData();
155 pj_ioqueue_t *ioq = key->ioqueue;
156 check_thread(ioq);
157
158 ioqueue_dispatch_write_event(key->ioqueue, key);
159
160 //start_next_write(key);
161 }
162
163 static void on_accept(PjUwpSocket *s)
164 {
165 pj_ioqueue_key_t *key = (pj_ioqueue_key_t*)s->GetUserData();
166 pj_ioqueue_t *ioq = key->ioqueue;
167 check_thread(ioq);
168
169 ioqueue_dispatch_read_event(key->ioqueue, key);
170 }
171
172 static void on_connect(PjUwpSocket *s, pj_status_t status)
173 {
174 PJ_UNUSED_ARG(status);
175 pj_ioqueue_key_t *key = (pj_ioqueue_key_t*)s->GetUserData();
176 pj_ioqueue_t *ioq = key->ioqueue;
177 check_thread(ioq);
178
179 ioqueue_dispatch_write_event(key->ioqueue, key);
180 }
181
182
183 /*
184 * Return the name of the ioqueue implementation.
185 */
186 PJ_DEF(const char*) pj_ioqueue_name(void)
187 {
188 return "ioqueue-uwp";
189 }
190
191
192 /*
193 * Create a new I/O Queue framework.
194 */
195 PJ_DEF(pj_status_t) pj_ioqueue_create( pj_pool_t *pool,
196 pj_size_t max_fd,
197 pj_ioqueue_t **p_ioqueue)
198 {
199 return pj_ioqueue_create2(pool, max_fd, NULL, p_ioqueue);
200 }
201
202 /*
203 * Create a new I/O Queue framework.
204 */
205 PJ_DEF(pj_status_t) pj_ioqueue_create2(pj_pool_t *pool,
206 pj_size_t max_fd,
207 const pj_ioqueue_cfg *cfg,
208 pj_ioqueue_t **p_ioqueue)
209 {
210 pj_ioqueue_t *ioq;
211 pj_lock_t *lock;
212 pj_status_t rc;
213
214 PJ_UNUSED_ARG(max_fd);
215
216 ioq = PJ_POOL_ZALLOC_T(pool, pj_ioqueue_t);
217
218 if (cfg)
219 pj_memcpy(&ioq->cfg, cfg, sizeof(*cfg));
220 else
221 pj_ioqueue_cfg_default(&ioq->cfg);
222
223 /* Create and init ioqueue mutex */
224 rc = pj_lock_create_null_mutex(pool, "ioq%p", &lock);
225 if (rc != PJ_SUCCESS)
226 return rc;
227
228 rc = pj_ioqueue_set_lock(ioq, lock, PJ_TRUE);
229 if (rc != PJ_SUCCESS)
230 return rc;
231
232 PJ_LOG(4, ("pjlib", "uwp I/O Queue created (%p)", ioq));
233
234 *p_ioqueue = ioq;
235 return PJ_SUCCESS;
236 }
237
238
239 /*
240 * Destroy the I/O queue.
241 */
242 PJ_DEF(pj_status_t) pj_ioqueue_destroy( pj_ioqueue_t *ioq )
243 {
244 return ioqueue_destroy(ioq);
245 }
246
247
248 /*
249 * Register a socket to the I/O queue framework.
250 */
251 PJ_DEF(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool,
252 pj_ioqueue_t *ioqueue,
253 pj_sock_t sock,
254 void *user_data,
255 const pj_ioqueue_callback *cb,
256 pj_ioqueue_key_t **p_key )
257 {
258 return pj_ioqueue_register_sock2(pool, ioqueue, sock, NULL, user_data,
259 cb, p_key);
260 }
261
262 PJ_DEF(pj_status_t) pj_ioqueue_register_sock2(pj_pool_t *pool,
263 pj_ioqueue_t *ioqueue,
264 pj_sock_t sock,
265 pj_grp_lock_t *grp_lock,
266 void *user_data,
267 const pj_ioqueue_callback *cb,
268 pj_ioqueue_key_t **p_key)
269 {
270 PjUwpSocketCallback uwp_cb =
271 { &on_read, &on_write, &on_accept, &on_connect };
272 pj_ioqueue_key_t *key;
273 pj_status_t rc;
274
275 pj_lock_acquire(ioqueue->lock);
276
277 key = PJ_POOL_ZALLOC_T(pool, pj_ioqueue_key_t);
278 rc = ioqueue_init_key(pool, ioqueue, key, sock, grp_lock, user_data, cb);
279 if (rc != PJ_SUCCESS) {
280 key = NULL;
281 goto on_return;
282 }
283
284 /* Create ioqueue key lock, if not yet */
285 if (!key->lock) {
286 rc = pj_lock_create_simple_mutex(pool, NULL, &key->lock);
287 if (rc != PJ_SUCCESS) {
288 key = NULL;
289 goto on_return;
290 }
291 }
292
293 PjUwpSocket *s = (PjUwpSocket*)sock;
294 s->SetNonBlocking(&uwp_cb, key);
295
296 on_return:
297 if (rc != PJ_SUCCESS) {
298 if (key && key->grp_lock)
299 pj_grp_lock_dec_ref_dbg(key->grp_lock, "ioqueue", 0);
300 }
301 *p_key = key;
302 pj_lock_release(ioqueue->lock);
303
304 return rc;
305
306 }
307
308 /*
309 * Unregister from the I/O Queue framework.
310 */
311 PJ_DEF(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key )
312 {
313 pj_ioqueue_t *ioqueue;
314
315 PJ_ASSERT_RETURN(key, PJ_EINVAL);
316
317 ioqueue = key->ioqueue;
318
319 /* Lock the key to make sure no callback is simultaneously modifying
320 * the key. We need to lock the key before ioqueue here to prevent
321 * deadlock.
322 */
323 pj_ioqueue_lock_key(key);
324
325 /* Also lock ioqueue */
326 pj_lock_acquire(ioqueue->lock);
327
328 /* Close socket. */
329 pj_sock_close(key->fd);
330
331 /* Clear callback */
332 key->cb.on_accept_complete = NULL;
333 key->cb.on_connect_complete = NULL;
334 key->cb.on_read_complete = NULL;
335 key->cb.on_write_complete = NULL;
336
337 pj_lock_release(ioqueue->lock);
338
339 if (key->grp_lock) {
340 pj_grp_lock_t *grp_lock = key->grp_lock;
341 pj_grp_lock_dec_ref_dbg(grp_lock, "ioqueue", 0);
342 pj_grp_lock_release(grp_lock);
343 } else {
344 pj_ioqueue_unlock_key(key);
345 }
346
347 pj_lock_destroy(key->lock);
348
349 return PJ_SUCCESS;
350 }
351
352
353 /*
354 * Poll the I/O Queue for completed events.
355 */
356 PJ_DEF(int) pj_ioqueue_poll( pj_ioqueue_t *ioq,
357 const pj_time_val *timeout)
358 {
359 /* Polling is not necessary on UWP, since each socket handles
360 * its events already.
361 */
362 PJ_UNUSED_ARG(ioq);
363
364 pj_thread_sleep(PJ_TIME_VAL_MSEC(*timeout));
365
366 return 0;
367 }
368
369 PJ_DEF(pj_oshandle_t) pj_ioqueue_get_os_handle( pj_ioqueue_t *ioqueue )
370 {
371 PJ_UNUSED_ARG(ioqueue);
372 return NULL;
373 }
374
375 #endif /* PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_UWP */