zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2018 Devolutions <info@devolutions.net>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
 
#include <string.h>
 
#include "core/nng_impl.h"
 
// Inproc transport.  This just transports messages from one
// peer to another.  The inproc transport is only valid within the same
// process.
 
typedef struct inproc_pair  inproc_pair;
typedef struct inproc_pipe  inproc_pipe;
typedef struct inproc_ep    inproc_ep;
typedef struct inproc_queue inproc_queue;
 
typedef struct {
    nni_mtx  mx;
    nni_list servers;
} inproc_global;
 
// inproc_pipe represents one half of a connection.
struct inproc_pipe {
    const char *  addr;
    inproc_pair * pair;
    inproc_queue *recv_queue;
    inproc_queue *send_queue;
    uint16_t      peer;
    uint16_t      proto;
};
 
struct inproc_queue {
    nni_list readers;
    nni_list writers;
    nni_mtx  lock;
    bool     closed;
};
 
// inproc_pair represents a pair of pipes.  Because we control both
// sides of the pipes, we can allocate and free this in one structure.
struct inproc_pair {
    nni_atomic_int ref;
    inproc_queue   queues[2];
};
 
struct inproc_ep {
    const char *  addr;
    bool          listener;
    nni_list_node node;
    uint16_t      proto;
    nni_cv        cv;
    nni_list      clients;
    nni_list      aios;
    size_t        rcvmax;
    nni_mtx       mtx;
};
 
// nni_inproc is our global state - this contains the list of active endpoints
// which we use for coordinating rendezvous.
static inproc_global nni_inproc;
 
static void
inproc_init(void)
{
    NNI_LIST_INIT(&nni_inproc.servers, inproc_ep, node);
 
    nni_mtx_init(&nni_inproc.mx);
}
 
static void
inproc_fini(void)
{
    nni_mtx_fini(&nni_inproc.mx);
}
 
// inproc_pair destroy is called when both pipe-ends of the pipe
// have been destroyed.
static void
inproc_pair_destroy(inproc_pair *pair)
{
    for (int i = 0; i < 2; i++) {
        nni_mtx_fini(&pair->queues[i].lock);
    }
    NNI_FREE_STRUCT(pair);
}
 
static int
inproc_pipe_alloc(inproc_pipe **pipep, inproc_ep *ep)
{
    inproc_pipe *pipe;
 
    if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) {
        return (NNG_ENOMEM);
    }
 
    pipe->proto = ep->proto;
    pipe->addr  = ep->addr;
    *pipep      = pipe;
    return (0);
}
 
static int
inproc_pipe_init(void *arg, nni_pipe *p)
{
    NNI_ARG_UNUSED(arg);
    NNI_ARG_UNUSED(p);
    return (0);
}
 
static void
inproc_pipe_fini(void *arg)
{
    inproc_pipe *pipe = arg;
    inproc_pair *pair;
 
    if ((pair = pipe->pair) != NULL) {
        // If we are the last peer, then toss the pair structure.
        if (nni_atomic_dec_nv(&pair->ref) == 0) {
            inproc_pair_destroy(pair);
        }
    }
 
    NNI_FREE_STRUCT(pipe);
}
 
static void
inproc_queue_run_closed(inproc_queue *queue)
{
    nni_aio *aio;
    while (((aio = nni_list_first(&queue->readers)) != NULL) ||
        ((aio = nni_list_first(&queue->writers)) != NULL)) {
        nni_aio_list_remove(aio);
        nni_aio_finish_error(aio, NNG_ECLOSED);
    }
}
 
static void
inproc_queue_run(inproc_queue *queue)
{
    if (queue->closed) {
        inproc_queue_run_closed(queue);
    }
    for (;;) {
        nni_aio *rd;
        nni_aio *wr;
        nni_msg *msg;
        nni_msg *pu;
 
        if (((rd = nni_list_first(&queue->readers)) == NULL) ||
            ((wr = nni_list_first(&queue->writers)) == NULL)) {
            return;
        }
 
        msg = nni_aio_get_msg(wr);
        NNI_ASSERT(msg != NULL);
 
        // At this point, we pass success back to the caller.  If
        // we drop the message for any reason, its accounted on the
        // receiver side.
        nni_aio_list_remove(wr);
        nni_aio_set_msg(wr, NULL);
        nni_aio_finish(
            wr, 0, nni_msg_len(msg) + nni_msg_header_len(msg));
 
        // TODO: We could check the max receive size here.
 
        // Now the receive side.  We need to ensure that we have
        // an exclusive copy of the message, and pull the header
        // up into the body to match protocol expectations.
        if ((pu = nni_msg_pull_up(msg)) == NULL) {
            nni_msg_free(msg);
            continue;
        }
        msg = pu;
 
        nni_aio_list_remove(rd);
        nni_aio_set_msg(rd, msg);
        nni_aio_finish(rd, 0, nni_msg_len(msg));
    }
}
 
static void
inproc_queue_cancel(nni_aio *aio, void *arg, int rv)
{
    inproc_queue *queue = arg;
 
    nni_mtx_lock(&queue->lock);
    if (nni_aio_list_active(aio)) {
        nni_aio_list_remove(aio);
        nni_aio_finish_error(aio, rv);
    }
    nni_mtx_unlock(&queue->lock);
}
 
static void
inproc_pipe_send(void *arg, nni_aio *aio)
{
    inproc_pipe * pipe  = arg;
    inproc_queue *queue = pipe->send_queue;
    int           rv;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
 
    nni_mtx_lock(&queue->lock);
    if ((rv = nni_aio_schedule(aio, inproc_queue_cancel, queue)) != 0) {
        nni_mtx_unlock(&queue->lock);
        nni_aio_finish_error(aio, rv);
        return;
    }
    nni_aio_list_append(&queue->writers, aio);
    inproc_queue_run(queue);
    nni_mtx_unlock(&queue->lock);
}
 
static void
inproc_pipe_recv(void *arg, nni_aio *aio)
{
    inproc_pipe * pipe  = arg;
    inproc_queue *queue = pipe->recv_queue;
    int           rv;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
 
    nni_mtx_lock(&queue->lock);
    if ((rv = nni_aio_schedule(aio, inproc_queue_cancel, queue)) != 0) {
        nni_mtx_unlock(&queue->lock);
        nni_aio_finish_error(aio, rv);
        return;
    }
    nni_aio_list_append(&queue->readers, aio);
    inproc_queue_run(queue);
    nni_mtx_unlock(&queue->lock);
}
 
static void
inproc_pipe_close(void *arg)
{
    inproc_pipe *pipe = arg;
    inproc_pair *pair = pipe->pair;
 
    for (int i = 0; i < 2; i++) {
        inproc_queue *queue = &pair->queues[i];
        nni_mtx_lock(&queue->lock);
        queue->closed = true;
        inproc_queue_run_closed(queue);
        nni_mtx_unlock(&queue->lock);
    }
}
 
static uint16_t
inproc_pipe_peer(void *arg)
{
    inproc_pipe *pipe = arg;
 
    return (pipe->peer);
}
 
static int
inproc_pipe_get_addr(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    inproc_pipe *p = arg;
    nni_sockaddr sa;
 
    memset(&sa, 0, sizeof(sa));
    sa.s_inproc.sa_family = NNG_AF_INPROC;
    nni_strlcpy(sa.s_inproc.sa_name, p->addr, sizeof(sa.s_inproc.sa_name));
    return (nni_copyout_sockaddr(&sa, buf, szp, t));
}
 
static int
inproc_dialer_init(void **epp, nni_url *url, nni_dialer *ndialer)
{
    inproc_ep *ep;
    nni_sock * sock = nni_dialer_sock(ndialer);
 
    if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) {
        return (NNG_ENOMEM);
    }
    nni_mtx_init(&ep->mtx);
 
    ep->listener = false;
    ep->proto    = nni_sock_proto_id(sock);
    ep->rcvmax   = 0;
    NNI_LIST_INIT(&ep->clients, inproc_ep, node);
    nni_aio_list_init(&ep->aios);
 
    ep->addr = url->u_rawurl; // we match on the full URL.
 
    *epp = ep;
    return (0);
}
 
static int
inproc_listener_init(void **epp, nni_url *url, nni_listener *nlistener)
{
    inproc_ep *ep;
    nni_sock * sock = nni_listener_sock(nlistener);
 
    if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) {
        return (NNG_ENOMEM);
    }
    nni_mtx_init(&ep->mtx);
 
    ep->listener = true;
    ep->proto    = nni_sock_proto_id(sock);
    ep->rcvmax   = 0;
    NNI_LIST_INIT(&ep->clients, inproc_ep, node);
    nni_aio_list_init(&ep->aios);
 
    ep->addr = url->u_rawurl; // we match on the full URL.
 
    *epp = ep;
    return (0);
}
 
static void
inproc_ep_fini(void *arg)
{
    inproc_ep *ep = arg;
    nni_mtx_fini(&ep->mtx);
    NNI_FREE_STRUCT(ep);
}
 
static void
inproc_conn_finish(nni_aio *aio, int rv, inproc_ep *ep, inproc_pipe *pipe)
{
    nni_aio_list_remove(aio);
 
    if ((!ep->listener) && nni_list_empty(&ep->aios)) {
        nni_list_node_remove(&ep->node);
    }
 
    if (rv == 0) {
        nni_aio_set_output(aio, 0, pipe);
        nni_aio_finish(aio, 0, 0);
    } else {
        NNI_ASSERT(pipe == NULL);
        nni_aio_finish_error(aio, rv);
    }
}
 
static void
inproc_ep_close(void *arg)
{
    inproc_ep *ep = arg;
    inproc_ep *client;
    nni_aio *  aio;
 
    nni_mtx_lock(&nni_inproc.mx);
    if (nni_list_active(&nni_inproc.servers, ep)) {
        nni_list_remove(&nni_inproc.servers, ep);
    }
    // Notify any waiting clients that we are closed.
    while ((client = nni_list_first(&ep->clients)) != NULL) {
        while ((aio = nni_list_first(&client->aios)) != NULL) {
            inproc_conn_finish(aio, NNG_ECONNREFUSED, ep, NULL);
        }
        nni_list_remove(&ep->clients, client);
    }
    while ((aio = nni_list_first(&ep->aios)) != NULL) {
        inproc_conn_finish(aio, NNG_ECLOSED, ep, NULL);
    }
    nni_mtx_unlock(&nni_inproc.mx);
}
 
static void
inproc_accept_clients(inproc_ep *srv)
{
    inproc_ep *cli, *nclient;
 
    nclient = nni_list_first(&srv->clients);
    while ((cli = nclient) != NULL) {
        nni_aio *caio;
        nclient = nni_list_next(&srv->clients, nclient);
        NNI_LIST_FOREACH (&cli->aios, caio) {
 
            inproc_pipe *cpipe;
            inproc_pipe *spipe;
            inproc_pair *pair;
            nni_aio *    saio;
            int          rv;
 
            if ((saio = nni_list_first(&srv->aios)) == NULL) {
                // No outstanding accept() calls.
                break;
            }
 
            if ((pair = NNI_ALLOC_STRUCT(pair)) == NULL) {
                inproc_conn_finish(
                    caio, NNG_ENOMEM, cli, NULL);
                inproc_conn_finish(
                    saio, NNG_ENOMEM, srv, NULL);
                continue;
            }
            for (int i = 0; i < 2; i++) {
                nni_aio_list_init(&pair->queues[i].readers);
                nni_aio_list_init(&pair->queues[i].writers);
                nni_mtx_init(&pair->queues[i].lock);
            }
            nni_atomic_init(&pair->ref);
            nni_atomic_set(&pair->ref, 2);
 
            spipe = cpipe = NULL;
            if (((rv = inproc_pipe_alloc(&cpipe, cli)) != 0) ||
                ((rv = inproc_pipe_alloc(&spipe, srv)) != 0)) {
 
                if (cpipe != NULL) {
                    inproc_pipe_fini(cpipe);
                }
                if (spipe != NULL) {
                    inproc_pipe_fini(spipe);
                }
                inproc_conn_finish(caio, rv, cli, NULL);
                inproc_conn_finish(saio, rv, srv, NULL);
                inproc_pair_destroy(pair);
                continue;
            }
 
            cpipe->peer       = spipe->proto;
            spipe->peer       = cpipe->proto;
            cpipe->pair       = pair;
            spipe->pair       = pair;
            cpipe->send_queue = &pair->queues[0];
            cpipe->recv_queue = &pair->queues[1];
            spipe->send_queue = &pair->queues[1];
            spipe->recv_queue = &pair->queues[0];
 
            inproc_conn_finish(caio, 0, cli, cpipe);
            inproc_conn_finish(saio, 0, srv, spipe);
        }
 
        if (nni_list_first(&cli->aios) == NULL) {
            // No more outstanding client connects.
            // Normally there should only be one.
            if (nni_list_active(&srv->clients, cli)) {
                nni_list_remove(&srv->clients, cli);
            }
        }
    }
}
 
static void
inproc_ep_cancel(nni_aio *aio, void *arg, int rv)
{
    inproc_ep *ep = arg;
 
    nni_mtx_lock(&nni_inproc.mx);
    if (nni_aio_list_active(aio)) {
        nni_aio_list_remove(aio);
        nni_list_node_remove(&ep->node);
        nni_aio_finish_error(aio, rv);
    }
    nni_mtx_unlock(&nni_inproc.mx);
}
 
static void
inproc_ep_connect(void *arg, nni_aio *aio)
{
    inproc_ep *ep = arg;
    inproc_ep *server;
    int        rv;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
 
    nni_mtx_lock(&nni_inproc.mx);
 
    // Find a server.
    NNI_LIST_FOREACH (&nni_inproc.servers, server) {
        if (strcmp(server->addr, ep->addr) == 0) {
            break;
        }
    }
    if (server == NULL) {
        nni_mtx_unlock(&nni_inproc.mx);
        nni_aio_finish_error(aio, NNG_ECONNREFUSED);
        return;
    }
 
    // We don't have to worry about the case where a zero timeout
    // on connect was specified, as there is no option to specify
    // that in the upper API.
    if ((rv = nni_aio_schedule(aio, inproc_ep_cancel, ep)) != 0) {
        nni_mtx_unlock(&nni_inproc.mx);
        nni_aio_finish_error(aio, rv);
        return;
    }
 
    nni_list_append(&server->clients, ep);
    nni_aio_list_append(&ep->aios, aio);
 
    inproc_accept_clients(server);
    nni_mtx_unlock(&nni_inproc.mx);
}
 
static int
inproc_ep_bind(void *arg)
{
    inproc_ep *ep = arg;
    inproc_ep *srch;
    nni_list * list = &nni_inproc.servers;
 
    nni_mtx_lock(&nni_inproc.mx);
    NNI_LIST_FOREACH (list, srch) {
        if (strcmp(srch->addr, ep->addr) == 0) {
            nni_mtx_unlock(&nni_inproc.mx);
            return (NNG_EADDRINUSE);
        }
    }
    nni_list_append(list, ep);
    nni_mtx_unlock(&nni_inproc.mx);
    return (0);
}
 
static void
inproc_ep_accept(void *arg, nni_aio *aio)
{
    inproc_ep *ep = arg;
    int        rv;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
 
    nni_mtx_lock(&nni_inproc.mx);
 
    // We need not worry about the case where a non-blocking
    // accept was tried -- there is no API to do such a thing.
    if ((rv = nni_aio_schedule(aio, inproc_ep_cancel, ep)) != 0) {
        nni_mtx_unlock(&nni_inproc.mx);
        nni_aio_finish_error(aio, rv);
        return;
    }
 
    // We are already on the master list of servers, thanks to bind.
    // Insert us into pending server aios, and then run accept list.
    nni_aio_list_append(&ep->aios, aio);
    inproc_accept_clients(ep);
    nni_mtx_unlock(&nni_inproc.mx);
}
 
static int
inproc_ep_get_recvmaxsz(void *arg, void *v, size_t *szp, nni_opt_type t)
{
    inproc_ep *ep = arg;
    int        rv;
    nni_mtx_lock(&ep->mtx);
    rv = nni_copyout_size(ep->rcvmax, v, szp, t);
    nni_mtx_unlock(&ep->mtx);
    return (rv);
}
 
static int
inproc_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t)
{
    inproc_ep *ep = arg;
    size_t     val;
    int        rv;
    if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) {
        nni_mtx_lock(&ep->mtx);
        ep->rcvmax = val;
        nni_mtx_unlock(&ep->mtx);
    }
    return (rv);
}
 
static int
inproc_ep_get_addr(void *arg, void *v, size_t *szp, nni_opt_type t)
{
    inproc_ep *  ep = arg;
    nng_sockaddr sa;
    sa.s_inproc.sa_family = NNG_AF_INPROC;
    nni_strlcpy(
        sa.s_inproc.sa_name, ep->addr, sizeof(sa.s_inproc.sa_name));
    return (nni_copyout_sockaddr(&sa, v, szp, t));
}
 
static const nni_option inproc_pipe_options[] = {
    {
        .o_name = NNG_OPT_LOCADDR,
        .o_get  = inproc_pipe_get_addr,
    },
    {
        .o_name = NNG_OPT_REMADDR,
        .o_get  = inproc_pipe_get_addr,
    },
    // terminate list
    {
        .o_name = NULL,
    },
};
 
static int
inproc_pipe_getopt(
    void *arg, const char *name, void *v, size_t *szp, nni_type t)
{
    return (nni_getopt(inproc_pipe_options, name, arg, v, szp, t));
}
 
static nni_sp_pipe_ops inproc_pipe_ops = {
    .p_init   = inproc_pipe_init,
    .p_fini   = inproc_pipe_fini,
    .p_send   = inproc_pipe_send,
    .p_recv   = inproc_pipe_recv,
    .p_close  = inproc_pipe_close,
    .p_peer   = inproc_pipe_peer,
    .p_getopt = inproc_pipe_getopt,
};
 
static const nni_option inproc_ep_options[] = {
    {
        .o_name = NNG_OPT_RECVMAXSZ,
        .o_get  = inproc_ep_get_recvmaxsz,
        .o_set  = inproc_ep_set_recvmaxsz,
    },
    {
        .o_name = NNG_OPT_LOCADDR,
        .o_get  = inproc_ep_get_addr,
    },
    {
        .o_name = NNG_OPT_REMADDR,
        .o_get  = inproc_ep_get_addr,
    },
    // terminate list
    {
        .o_name = NULL,
    },
};
 
static int
inproc_ep_getopt(void *arg, const char *name, void *v, size_t *szp, nni_type t)
{
    return (nni_getopt(inproc_ep_options, name, arg, v, szp, t));
}
 
static int
inproc_ep_setopt(
    void *arg, const char *name, const void *v, size_t sz, nni_type t)
{
    return (nni_setopt(inproc_ep_options, name, arg, v, sz, t));
}
 
static nni_sp_dialer_ops inproc_dialer_ops = {
    .d_init    = inproc_dialer_init,
    .d_fini    = inproc_ep_fini,
    .d_connect = inproc_ep_connect,
    .d_close   = inproc_ep_close,
    .d_getopt  = inproc_ep_getopt,
    .d_setopt  = inproc_ep_setopt,
};
 
static nni_sp_listener_ops inproc_listener_ops = {
    .l_init   = inproc_listener_init,
    .l_fini   = inproc_ep_fini,
    .l_bind   = inproc_ep_bind,
    .l_accept = inproc_ep_accept,
    .l_close  = inproc_ep_close,
    .l_getopt = inproc_ep_getopt,
    .l_setopt = inproc_ep_setopt,
};
 
// This is the inproc transport linkage, and should be the only global
// symbol in this entire file.
struct nni_sp_tran nni_inproc_tran = {
    .tran_scheme   = "inproc",
    .tran_dialer   = &inproc_dialer_ops,
    .tran_listener = &inproc_listener_ops,
    .tran_pipe     = &inproc_pipe_ops,
    .tran_init     = inproc_init,
    .tran_fini     = inproc_fini,
};
 
#ifndef NNG_ELIDE_DEPRECATED
int
nng_inproc_register(void)
{
    return (nni_init());
}
#endif
 
void
nni_sp_inproc_register(void)
{
    nni_sp_tran_register(&nni_inproc_tran);
}