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
//
// 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 "core/nng_impl.h"
#include "sockimpl.h"
 
#include <stdio.h>
 
// This file contains functions relating to pipes.
//
// Operations on pipes (to the transport) are generally blocking operations,
// performed in the context of the protocol.
 
static nni_id_map pipes;
static nni_mtx    pipes_lk;
 
static void pipe_destroy(void *);
 
static nni_reap_list pipe_reap_list = {
    .rl_offset = offsetof(nni_pipe, p_reap),
    .rl_func   = pipe_destroy,
};
 
int
nni_pipe_sys_init(void)
{
    nni_mtx_init(&pipes_lk);
 
    // Pipe IDs needs to have high order bit clear, and we want
    // them to start at a random value.
    nni_id_map_init(&pipes, 1, 0x7fffffff, true);
 
    return (0);
}
 
void
nni_pipe_sys_fini(void)
{
    nni_reap_drain();
    nni_mtx_fini(&pipes_lk);
    nni_id_map_fini(&pipes);
}
 
static void
pipe_destroy(void *arg)
{
    nni_pipe *p = arg;
    if (p == NULL) {
        return;
    }
 
    nni_pipe_run_cb(p, NNG_PIPE_EV_REM_POST);
 
    // Make sure any unlocked holders are done with this.
    // This happens during initialization for example.
    nni_mtx_lock(&pipes_lk);
    if (p->p_id != 0) {
        nni_id_remove(&pipes, p->p_id);
    }
    // This wait guarantees that all callers are done with us.
    while (p->p_ref != 0) {
        nni_cv_wait(&p->p_cv);
    }
    nni_mtx_unlock(&pipes_lk);
 
    if (p->p_proto_data != NULL) {
        p->p_proto_ops.pipe_stop(p->p_proto_data);
    }
    if ((p->p_tran_data != NULL) && (p->p_tran_ops.p_stop != NULL)) {
        p->p_tran_ops.p_stop(p->p_tran_data);
    }
 
#ifdef NNG_ENABLE_STATS
    nni_stat_unregister(&p->st_root);
#endif
    nni_pipe_remove(p);
 
    if (p->p_proto_data != NULL) {
        p->p_proto_ops.pipe_fini(p->p_proto_data);
    }
    if (p->p_tran_data != NULL) {
        p->p_tran_ops.p_fini(p->p_tran_data);
    }
    nni_cv_fini(&p->p_cv);
    nni_mtx_fini(&p->p_mtx);
    nni_free(p, p->p_size);
}
 
int
nni_pipe_find(nni_pipe **pp, uint32_t id)
{
    nni_pipe *p;
 
    // We don't care if the pipe is "closed".  End users only have
    // access to the pipe in order to obtain properties (which may
    // be retried during the post-close notification callback) or to
    // close the pipe.
    nni_mtx_lock(&pipes_lk);
    if ((p = nni_id_get(&pipes, id)) != NULL) {
        p->p_ref++;
        *pp = p;
    }
    nni_mtx_unlock(&pipes_lk);
    return (p == NULL ? NNG_ENOENT : 0);
}
 
void
nni_pipe_rele(nni_pipe *p)
{
    nni_mtx_lock(&pipes_lk);
    p->p_ref--;
    if (p->p_ref == 0) {
        nni_cv_wake(&p->p_cv);
    }
    nni_mtx_unlock(&pipes_lk);
}
 
// nni_pipe_id returns the 32-bit pipe id, which can be used in backtraces.
uint32_t
nni_pipe_id(nni_pipe *p)
{
    return (p->p_id);
}
 
void
nni_pipe_recv(nni_pipe *p, nni_aio *aio)
{
    p->p_tran_ops.p_recv(p->p_tran_data, aio);
}
 
void
nni_pipe_send(nni_pipe *p, nni_aio *aio)
{
    p->p_tran_ops.p_send(p->p_tran_data, aio);
}
 
// nni_pipe_close closes the underlying connection.  It is expected that
// subsequent attempts to receive or send (including any waiting receive) will
// simply return NNG_ECLOSED.
void
nni_pipe_close(nni_pipe *p)
{
    nni_mtx_lock(&p->p_mtx);
    if (p->p_closed) {
        // We already did a close.
        nni_mtx_unlock(&p->p_mtx);
        return;
    }
    p->p_closed = true;
    nni_mtx_unlock(&p->p_mtx);
 
    if (p->p_proto_data != NULL) {
        p->p_proto_ops.pipe_close(p->p_proto_data);
    }
 
    // Close the underlying transport.
    if (p->p_tran_data != NULL) {
        p->p_tran_ops.p_close(p->p_tran_data);
    }
 
    nni_reap(&pipe_reap_list, p);
}
 
uint16_t
nni_pipe_peer(nni_pipe *p)
{
    return (p->p_tran_ops.p_peer(p->p_tran_data));
}
 
#ifdef NNG_ENABLE_STATS
static void
pipe_stat_init(nni_pipe *p, nni_stat_item *item, const nni_stat_info *info)
{
    nni_stat_init(item, info);
    nni_stat_add(&p->st_root, item);
}
 
static void
pipe_stats_init(nni_pipe *p)
{
    static const nni_stat_info root_info = {
        .si_name = "pipe",
        .si_desc = "pipe statistics",
        .si_type = NNG_STAT_SCOPE,
    };
    static const nni_stat_info id_info = {
        .si_name = "id",
        .si_desc = "pipe id",
        .si_type = NNG_STAT_ID,
    };
    static const nni_stat_info socket_info = {
        .si_name = "socket",
        .si_desc = "socket for pipe",
        .si_type = NNG_STAT_ID,
    };
    static const nni_stat_info rx_msgs_info = {
        .si_name   = "rx_msgs",
        .si_desc   = "messages received",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_MESSAGES,
        .si_atomic = true,
    };
    static const nni_stat_info tx_msgs_info = {
        .si_name   = "tx_msgs",
        .si_desc   = "messages sent",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_MESSAGES,
        .si_atomic = true,
    };
    static const nni_stat_info rx_bytes_info = {
        .si_name   = "rx_bytes",
        .si_desc   = "bytes received",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_BYTES,
        .si_atomic = true,
    };
    static const nni_stat_info tx_bytes_info = {
        .si_name   = "tx_bytes",
        .si_desc   = "bytes sent",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_BYTES,
        .si_atomic = true,
    };
 
    nni_stat_init(&p->st_root, &root_info);
    pipe_stat_init(p, &p->st_id, &id_info);
    pipe_stat_init(p, &p->st_sock_id, &socket_info);
    pipe_stat_init(p, &p->st_rx_msgs, &rx_msgs_info);
    pipe_stat_init(p, &p->st_tx_msgs, &tx_msgs_info);
    pipe_stat_init(p, &p->st_rx_bytes, &rx_bytes_info);
    pipe_stat_init(p, &p->st_tx_bytes, &tx_bytes_info);
 
    nni_stat_set_id(&p->st_root, (int) p->p_id);
    nni_stat_set_id(&p->st_id, (int) p->p_id);
    nni_stat_set_id(&p->st_sock_id, (int) nni_sock_id(p->p_sock));
}
#endif // NNG_ENABLE_STATS
 
static int
pipe_create(nni_pipe **pp, nni_sock *sock, nni_sp_tran *tran, void *tdata)
{
    nni_pipe *          p;
    int                 rv;
    void *              sdata = nni_sock_proto_data(sock);
    nni_proto_pipe_ops *pops  = nni_sock_proto_pipe_ops(sock);
    size_t              sz;
 
    sz = NNI_ALIGN_UP(sizeof(*p)) + pops->pipe_size;
 
    if ((p = nni_zalloc(sz)) == NULL) {
        // In this case we just toss the pipe...
        tran->tran_pipe->p_fini(tdata);
        return (NNG_ENOMEM);
    }
 
    p->p_size       = sz;
    p->p_proto_data = p + 1;
    p->p_tran_ops   = *tran->tran_pipe;
    p->p_tran_data  = tdata;
    p->p_proto_ops  = *pops;
    p->p_sock       = sock;
    p->p_closed     = false;
    p->p_cbs        = false;
    p->p_ref        = 0;
 
    nni_atomic_flag_reset(&p->p_stop);
    NNI_LIST_NODE_INIT(&p->p_sock_node);
    NNI_LIST_NODE_INIT(&p->p_ep_node);
 
    nni_mtx_init(&p->p_mtx);
    nni_cv_init(&p->p_cv, &pipes_lk);
 
    nni_mtx_lock(&pipes_lk);
    if ((rv = nni_id_alloc(&pipes, &p->p_id, p)) == 0) {
        p->p_ref = 1;
    }
    nni_mtx_unlock(&pipes_lk);
 
#ifdef NNG_ENABLE_STATS
    pipe_stats_init(p);
#endif
 
    if ((rv != 0) || ((rv = p->p_tran_ops.p_init(tdata, p)) != 0) ||
        ((rv = pops->pipe_init(p->p_proto_data, p, sdata)) != 0)) {
        nni_pipe_close(p);
        nni_pipe_rele(p);
        return (rv);
    }
 
    *pp = p;
    return (0);
}
 
int
nni_pipe_create_dialer(nni_pipe **pp, nni_dialer *d, void *tdata)
{
    int          rv;
    nni_sp_tran *tran = d->d_tran;
    nni_pipe *   p;
 
    if ((rv = pipe_create(&p, d->d_sock, tran, tdata)) != 0) {
        return (rv);
    }
    p->p_dialer = d;
#ifdef NNG_ENABLE_STATS
    static const nni_stat_info dialer_info = {
        .si_name = "dialer",
        .si_desc = "dialer for pipe",
        .si_type = NNG_STAT_ID,
    };
    pipe_stat_init(p, &p->st_ep_id, &dialer_info);
    nni_stat_set_id(&p->st_ep_id, (int) nni_dialer_id(d));
#endif
    *pp = p;
    return (0);
}
 
int
nni_pipe_create_listener(nni_pipe **pp, nni_listener *l, void *tdata)
{
    int          rv;
    nni_sp_tran *tran = l->l_tran;
    nni_pipe *   p;
 
    if ((rv = pipe_create(&p, l->l_sock, tran, tdata)) != 0) {
        return (rv);
    }
    p->p_listener = l;
#if NNG_ENABLE_STATS
    static const nni_stat_info listener_info = {
        .si_name = "listener",
        .si_desc = "listener for pipe",
        .si_type = NNG_STAT_ID,
    };
    pipe_stat_init(p, &p->st_ep_id, &listener_info);
    nni_stat_set_id(&p->st_ep_id, (int) nni_listener_id(l));
#endif
    *pp = p;
    return (0);
}
 
int
nni_pipe_getopt(
    nni_pipe *p, const char *name, void *val, size_t *szp, nni_opt_type t)
{
    int rv;
 
    rv = p->p_tran_ops.p_getopt(p->p_tran_data, name, val, szp, t);
    if (rv != NNG_ENOTSUP) {
        return (rv);
    }
 
    // Maybe the endpoint knows? The guarantees on pipes ensure that the
    // pipe will not outlive its creating endpoint.
    if (p->p_dialer != NULL) {
        return (nni_dialer_getopt(p->p_dialer, name, val, szp, t));
    }
    if (p->p_listener != NULL) {
        return (nni_listener_getopt(p->p_listener, name, val, szp, t));
    }
    return (NNG_ENOTSUP);
}
 
uint32_t
nni_pipe_sock_id(nni_pipe *p)
{
    return (nni_sock_id(p->p_sock));
}
 
uint32_t
nni_pipe_listener_id(nni_pipe *p)
{
    return (p->p_listener ? nni_listener_id(p->p_listener) : 0);
}
 
uint32_t
nni_pipe_dialer_id(nni_pipe *p)
{
    return (p->p_dialer ? nni_dialer_id(p->p_dialer) : 0);
}
 
void
nni_pipe_add_stat(nni_pipe *p, nni_stat_item *item)
{
#ifdef NNG_ENABLE_STATS
    nni_stat_add(&p->st_root, item);
#else
    NNI_ARG_UNUSED(p);
    NNI_ARG_UNUSED(item);
#endif
}
 
void
nni_pipe_bump_rx(nni_pipe *p, size_t nbytes)
{
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&p->st_rx_bytes, nbytes);
    nni_stat_inc(&p->st_rx_msgs, 1);
#else
    NNI_ARG_UNUSED(p);
    NNI_ARG_UNUSED(nbytes);
#endif
}
 
void
nni_pipe_bump_tx(nni_pipe *p, size_t nbytes)
{
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&p->st_tx_bytes, nbytes);
    nni_stat_inc(&p->st_tx_msgs, 1);
#else
    NNI_ARG_UNUSED(p);
    NNI_ARG_UNUSED(nbytes);
#endif
}
 
void
nni_pipe_bump_error(nni_pipe *p, int err)
{
    if (p->p_dialer != NULL) {
        nni_dialer_bump_error(p->p_dialer, err);
    } else {
        nni_listener_bump_error(p->p_listener, err);
    }
}