summaryrefslogtreecommitdiffstats
path: root/src/windows/identity/kmq/consumer.c
blob: 87d24130f13e3940303787ba4e07d07387cd970d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * Copyright (c) 2005 Massachusetts Institute of Technology
 *
 * Copyright (c) 2007 Secure Endpoints Inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* $Id$ */

#include<kmqinternal.h>
#include<assert.h>

DWORD kmq_tls_queue;

CRITICAL_SECTION cs_kmq_msg_ref;

kmq_message_ref * kmq_msg_ref_free = NULL;

/* ad-hoc subscriptions */
kmq_msg_subscription * kmq_adhoc_subs = NULL;

#ifdef DEBUG

#include<stdio.h>

void
kmqint_dump_consumer(FILE * f) {
    kmq_message_ref * r;
    kmq_msg_subscription * s;

    int n_free = 0;
    int n_adhoc = 0;

    EnterCriticalSection(&cs_kmq_msg_ref);

    fprintf(f, "qc0\t*** Free Message References ***\n");

    fprintf(f, "qc1\tAddress\n");

    r = kmq_msg_ref_free;
    while(r) {
        n_free ++;

        fprintf(f, "qc2\t0x%p\n", r);

        r = LNEXT(r);
    }

    fprintf(f, "qc3\tTotal free message references : %d\n", n_free);

    fprintf(f, "qc4\t--- End ---\n");

    LeaveCriticalSection(&cs_kmq_msg_ref);

    EnterCriticalSection(&cs_kmq_global);

    fprintf(f, "qc5\t*** Adhoc Message Subscriptions ***\n");

    fprintf(f, "qc6\tAddress\tMsg Type\tRcpt Type\tRcpt\tQueue\n");

    s = kmq_adhoc_subs;

    while(s) {
        n_adhoc ++;

        fprintf(f, "qc7\t0x%p\t%d\t%s\t0x%p\t0x%p\n",
                s,
                s->type,
                (s->rcpt_type == KMQ_RCPTTYPE_CB)?"CALLBACK":"HWND",
                (s->rcpt_type == KMQ_RCPTTYPE_CB)? s->recipient.cb: (void *) s->recipient.hwnd,
                s->queue);

        s = LNEXT(s);
    }

    fprintf(f, "qc8\tTotal ad-hoc subscriptions : %d\n", n_adhoc);

    fprintf(f, "qc9\t--- End ---\n");

    LeaveCriticalSection(&cs_kmq_global);

}

#endif

/*! \internal
    \brief Get a message ref object
    \note called with cs_kmq_msg_ref held */
kmq_message_ref * kmqint_get_message_ref(void) {
    kmq_message_ref * r;

    LPOP(&kmq_msg_ref_free, &r);
    if(!r) {
        r = PMALLOC(sizeof(kmq_message_ref));
    }
    ZeroMemory(r, sizeof(kmq_message_ref));

    r->msg = NULL;
    r->recipient = NULL;

    return r;
}

/*! \internal
    \brief Free a message ref object
    \note called with cs_kmq_msg_ref and cs_kmq_msg held */
void kmqint_put_message_ref(kmq_message_ref * r) {
    if(!r)
        return;
    if(r->msg) {
        r->msg->refcount--;
        r->msg = NULL;
    }
    LPUSH(&kmq_msg_ref_free, r);
}

/*! \internal
    \brief Get the queue associated with the current thread
    \note Obtains ::cs_kmq_global
    */
kmq_queue * kmqint_get_thread_queue(void) {
    kmq_queue * q;

    q = (kmq_queue *) TlsGetValue(kmq_tls_queue);
    if(!q) {
        kmqint_attach_this_thread();
        q = (kmq_queue *) TlsGetValue(kmq_tls_queue);
    }

    return q;
}

/*! \internal
    \brief Get the topmost message ref for a queue
    \note Obtains kmq_queue::cs
    */
void kmqint_get_queue_message_ref(kmq_queue * q, kmq_message_ref ** r) {
    EnterCriticalSection(&q->cs);

    if (q->flags & KMQ_QUEUE_FLAG_DELETED) {
        *r = NULL;
    } else {
        QGET(q,r);
        if(QTOP(q))
            SetEvent(q->wait_o);
    }

    LeaveCriticalSection(&q->cs);
}

/*! \internal
    \brief Post a message to a queue
    \note Obtains ::cs_kmq_msg_ref, ::cs_kmq_msg, kmq_queue::cs
    */
void kmqint_post_queue(kmq_queue * q, kmq_message *m) {
    kmq_message_ref *r;

    EnterCriticalSection(&q->cs);
    if (q->flags & KMQ_QUEUE_FLAG_DELETED) {
        LeaveCriticalSection(&q->cs);
        return;
    }
    LeaveCriticalSection(&q->cs);

    EnterCriticalSection(&cs_kmq_msg_ref);
    r = kmqint_get_message_ref();
    LeaveCriticalSection(&cs_kmq_msg_ref);

    r->msg = m;
    r->recipient = NULL;

    EnterCriticalSection(&cs_kmq_msg);
    m->refcount++;
    m->nSent++;
    LeaveCriticalSection(&cs_kmq_msg);

    EnterCriticalSection(&q->cs);
    QPUT(q,r);
    SetEvent(q->wait_o);
    LeaveCriticalSection(&q->cs);
}

/*! \internal
    \brief Post a message to a subscriber
    \note Obtains ::cs_kmq_msg_ref, ::cs_kmq_msg, kmq_queue::cs
    \note Should be called with ::cs_kmq_msg held
    \note Should be called with ::cs_kmq_types held if try_send is true
    */
void kmqint_post(kmq_msg_subscription * s, kmq_message * m, khm_boolean try_send) {
    if(s->rcpt_type == KMQ_RCPTTYPE_CB) {
        kmq_queue *q;
        kmq_message_ref *r;

        q = s->queue;

        if(try_send && q->thread == GetCurrentThreadId()) {
            khm_int32 rv;
            /* we are sending a message from this thread to this
               thread.  just call the recipient directly, bypassing
               the message queue. */
            m->refcount++;
            m->nSent++;
            rv = s->recipient.cb(m->type, m->subtype,
                                 m->uparam, m->vparam);
            m->refcount--;
            if(KHM_SUCCEEDED(rv))
                m->nCompleted++;
            else
                m->nFailed++;
        } else {

            EnterCriticalSection(&q->cs);
            if (q->flags & KMQ_QUEUE_FLAG_DELETED) {
                LeaveCriticalSection(&q->cs);
                return;
            }
            LeaveCriticalSection(&q->cs);

            EnterCriticalSection(&cs_kmq_msg_ref);
            r = kmqint_get_message_ref();
            LeaveCriticalSection(&cs_kmq_msg_ref);

            r->msg = m;
            r->recipient = s->recipient.cb;

            m->refcount++;
            m->nSent++;

            EnterCriticalSection(&q->cs);
            QPUT(q,r);
            SetEvent(q->wait_o);
            LeaveCriticalSection(&q->cs);
        }
    }

#ifdef _WIN32
    else if(s->rcpt_type == KMQ_RCPTTYPE_HWND) {
        if(try_send &&
           GetCurrentThreadId() == GetWindowThreadProcessId(s->recipient.hwnd,
                                                            NULL)) {
            /* kmqint_post does not know whether there are any other
               messages waiting to be posted at this point.  Hence,
               simply sending the message is not the right thing to do
               as the recipient may incorrectly assume that the
               message has completed when (m->nCompleted + m->nFailed
               == m->nSent).  Therefore, we only increment nSent after
               the message is sent. */

            m->refcount++;

            /* the kmq_wm_begin()/kmq_wm_end() and kmq_wm_dispatch()
               handlers decrement the reference count on the message
               when they are done. */
            SendMessage(s->recipient.hwnd, KMQ_WM_DISPATCH,
                        m->type, (LPARAM) m);

            m->nSent++;

        } else {
            m->nSent++;
            m->refcount++;

            /* the kmq_wm_begin()/kmq_wm_end() and kmq_wm_dispatch()
               handlers decrement the reference count on the message
               when they are done. */
            PostMessage(s->recipient.hwnd, KMQ_WM_DISPATCH,
                        m->type, (LPARAM) m);
        }
    }
#endif

    else {
        /* This could either be because we were passed in an invalid
           subscription or because we lost a race to a thread that
           deleted an ad-hoc subscription. */
#ifdef DEBUG
        assert(FALSE);
#endif
    }
}

/*! \internal
    \brief Subscribes a window to a message type
    \note Obtains ::cs_kmq_types
    */
KHMEXP khm_int32 KHMAPI kmq_subscribe_hwnd(khm_int32 type, HWND hwnd) {
    kmq_msg_subscription * s;

    s = PMALLOC(sizeof(kmq_msg_subscription));
    ZeroMemory(s, sizeof(*s));
    s->magic = KMQ_MSG_SUB_MAGIC;
    LINIT(s);
    s->queue = NULL;
    s->rcpt_type = KMQ_RCPTTYPE_HWND;
    s->recipient.hwnd = hwnd;
    kmqint_msg_type_add_sub(type, s);

    return KHM_ERROR_SUCCESS;
}

/*! \internal
    \note Obtains ::cs_kmq_types, ::cs_kmq_global
    */
KHMEXP khm_int32 KHMAPI kmq_subscribe(khm_int32 type, kmq_callback_t cb) {
    kmq_msg_subscription * s;

    s = PMALLOC(sizeof(kmq_msg_subscription));
    ZeroMemory(s, sizeof(*s));
    s->magic = KMQ_MSG_SUB_MAGIC;
    LINIT(s);
    s->queue = kmqint_get_thread_queue();
    s->rcpt_type = KMQ_RCPTTYPE_CB;
    s->recipient.cb = cb;
    kmqint_msg_type_add_sub(type, s);

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI kmq_create_hwnd_subscription(HWND hw,
                                                     khm_handle * result)
{
    kmq_msg_subscription * s;

    s = PMALLOC(sizeof(kmq_msg_subscription));
    ZeroMemory(s, sizeof(*s));
    s->magic = KMQ_MSG_SUB_MAGIC;
    LINIT(s);
    s->queue = NULL;
    s->rcpt_type = KMQ_RCPTTYPE_HWND;
    s->recipient.hwnd = hw;

    EnterCriticalSection(&cs_kmq_global);
    LPUSH(&kmq_adhoc_subs, s);
    LeaveCriticalSection(&cs_kmq_global);

    *result = (khm_handle) s;

    return KHM_ERROR_SUCCESS;
}

/*! \internal
    \note Obtains ::cs_kmq_global
*/
KHMEXP khm_int32 KHMAPI kmq_create_subscription(kmq_callback_t cb,
                                                khm_handle * result)
{
    kmq_msg_subscription * s;

    s = PMALLOC(sizeof(kmq_msg_subscription));
    ZeroMemory(s, sizeof(*s));
    s->magic = KMQ_MSG_SUB_MAGIC;
    LINIT(s);
    s->queue = kmqint_get_thread_queue();
    s->rcpt_type = KMQ_RCPTTYPE_CB;
    s->recipient.cb = cb;

    EnterCriticalSection(&cs_kmq_global);
    LPUSH(&kmq_adhoc_subs, s);
    LeaveCriticalSection(&cs_kmq_global);

    *result = (khm_handle) s;

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI kmq_delete_subscription(khm_handle sub)
{
    kmq_msg_subscription * s;

    s = (kmq_msg_subscription *) sub;

    assert(s->magic == KMQ_MSG_SUB_MAGIC);

    s->type = 0;

    EnterCriticalSection(&cs_kmq_global);
    LDELETE(&kmq_adhoc_subs, s);
    LeaveCriticalSection(&cs_kmq_global);

    PFREE(s);

    return KHM_ERROR_SUCCESS;
}

/*! \internal
    \brief Unsubscribes a window from a message type
    \note Obtains ::cs_kmq_types
    */
KHMEXP khm_int32 KHMAPI kmq_unsubscribe_hwnd(khm_int32 type, HWND hwnd) {
    kmq_msg_subscription * s;

    s = kmqint_msg_type_del_sub_hwnd(type, hwnd);
    if(s)
        PFREE(s);
    return (s)?KHM_ERROR_SUCCESS:KHM_ERROR_NOT_FOUND;
}

/*! \internal
    \brief Unsubscribe a callback from a message type
    \note Obtains ::cs_kmq_types, ::cs_kmq_global
    */
KHMEXP khm_int32 KHMAPI kmq_unsubscribe(khm_int32 type, kmq_callback_t cb) {
    kmq_msg_subscription * s;

    s = kmqint_msg_type_del_sub_cb(type,cb);
    if(s)
        PFREE(s);

    return (s)?KHM_ERROR_SUCCESS:KHM_ERROR_NOT_FOUND;
}

KHMEXP LRESULT KHMAPI kmq_wm_begin(LPARAM lparm, kmq_message ** m) {
    *m = (kmq_message *) lparm;
    if ((*m)->err_ctx) {
        kherr_push_context((*m)->err_ctx);
    }
    return TRUE;
}

/*! \internal
    \note Obtains ::cs_kmq_msg
    */
KHMEXP LRESULT KHMAPI kmq_wm_end(kmq_message *m, khm_int32 rv) {
    if (m->err_ctx)
        kherr_pop_context();

    EnterCriticalSection(&cs_kmq_msg);
    m->refcount--;
    if(KHM_SUCCEEDED(rv))
        m->nCompleted++;
    else
        m->nFailed++;

    if(m->nCompleted + m->nFailed == m->nSent) {
        kmqint_put_message(m);
    }
    LeaveCriticalSection(&cs_kmq_msg);

    return TRUE;
}

/*! \internal
    \note Obtains ::cs_kmq_msg
    */
KHMEXP LRESULT KHMAPI kmq_wm_dispatch(LPARAM lparm, kmq_callback_t cb) {
    kmq_message *m;
    khm_int32 rv;

    m = (kmq_message *) lparm;

    if (m->err_ctx)
        kherr_push_context(m->err_ctx);

    rv = cb(m->type, m->subtype, m->uparam, m->vparam);

    if (m->err_ctx)
        kherr_pop_context();

    EnterCriticalSection(&cs_kmq_msg);

    m->refcount--;
    if(KHM_SUCCEEDED(rv))
        m->nCompleted++;
    else
        m->nFailed++;

    if(m->nCompleted + m->nFailed == m->nSent) {
        kmqint_put_message(m);
    }
    LeaveCriticalSection(&cs_kmq_msg);

    return TRUE;
}

KHMEXP khm_boolean KHMAPI kmq_is_call_aborted(void) {
    /* TODO: Implement this */
    return FALSE;
}

/*! \internal

    \note Obtains ::cs_kmq_global, kmq_queue::cs, ::cs_kmq_msg_ref, ::cs_kmq_msg,
*/
KHMEXP khm_int32 KHMAPI kmq_dispatch(kmq_timer timeout) {
    kmq_queue * q;
    kmq_message_ref * r;
    kmq_message *m;
    DWORD hr;

    q = kmqint_get_thread_queue();

    assert(q->wait_o);

    hr = WaitForSingleObject(q->wait_o, timeout);
    if(hr == WAIT_OBJECT_0) {
        /* signalled */
        kmqint_get_queue_message_ref(q, &r);

        m = r->msg;

        if(m->type != KMSG_SYSTEM || m->subtype != KMSG_SYSTEM_EXIT) {
            khm_boolean rv;

            if (m->err_ctx)
                kherr_push_context(m->err_ctx);

            /* TODO: before dispatching the message, the message being
               dispatched for this thread needs to be stored so that
               it can be looked up in kmq_is_call_aborted(). This
               needs to happen in kmq_wm_dispatch() and
               kmq_wm_begin() as well. */

            /* dispatch */
            rv = r->recipient(m->type, m->subtype, m->uparam, m->vparam);

            if (m->err_ctx)
                kherr_pop_context();

            EnterCriticalSection(&cs_kmq_msg);
            EnterCriticalSection(&cs_kmq_msg_ref);
            kmqint_put_message_ref(r);
            LeaveCriticalSection(&cs_kmq_msg_ref);

            if(KHM_SUCCEEDED(rv))
                m->nCompleted++;
            else
                m->nFailed++;

            if(m->nCompleted + m->nFailed == m->nSent) {
                kmqint_put_message(m);
            }
            LeaveCriticalSection(&cs_kmq_msg);

            return KHM_ERROR_SUCCESS;
        } else {
            EnterCriticalSection(&cs_kmq_msg);
            EnterCriticalSection(&cs_kmq_msg_ref);
            kmqint_put_message_ref(r);
            LeaveCriticalSection(&cs_kmq_msg_ref);
            m->nCompleted++;
            if(m->nCompleted + m->nFailed == m->nSent) {
                kmqint_put_message(m);
            }
            LeaveCriticalSection(&cs_kmq_msg);

            return KHM_ERROR_EXIT;
        }
    } else {
        return KHM_ERROR_TIMEOUT;
    }
}

/* TODO: rename this file to subscriber.c */