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
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// 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 <stdlib.h>
 
#include "core/nng_impl.h"
#include "nng/protocol/pipeline0/pull.h"
 
// Pull protocol.  The PULL protocol is the "read" side of a pipeline.
 
#ifndef NNI_PROTO_PULL_V0
#define NNI_PROTO_PULL_V0 NNI_PROTO(5, 1)
#endif
 
#ifndef NNI_PROTO_PUSH_V0
#define NNI_PROTO_PUSH_V0 NNI_PROTO(5, 0)
#endif
 
typedef struct pull0_pipe pull0_pipe;
typedef struct pull0_sock pull0_sock;
 
static void pull0_recv_cb(void *);
 
// pull0_sock is our per-socket protocol private structure.
struct pull0_sock {
    bool         raw;
    nni_list     pl; // pipe list (pipes with data ready)
    nni_list     rq; // recv queue (aio list)
    nni_mtx      m;
    nni_pollable readable;
};
 
// pull0_pipe is our per-pipe protocol private structure.
struct pull0_pipe {
    nni_pipe *    p;
    pull0_sock *  s;
    nni_msg *     m;
    nni_aio       aio;
    bool          closed;
    nni_list_node node;
};
 
static int
pull0_sock_init(void *arg, nni_sock *sock)
{
    pull0_sock *s = arg;
    NNI_ARG_UNUSED(sock);
 
    nni_aio_list_init(&s->rq);
    NNI_LIST_INIT(&s->pl, pull0_pipe, node);
    nni_mtx_init(&s->m);
    nni_pollable_init(&s->readable);
    return (0);
}
 
static void
pull0_sock_fini(void *arg)
{
    pull0_sock *s = arg;
    nni_mtx_fini(&s->m);
    nni_pollable_fini(&s->readable);
}
 
static void
pull0_pipe_stop(void *arg)
{
    pull0_pipe *p = arg;
 
    nni_aio_stop(&p->aio);
}
 
static void
pull0_pipe_fini(void *arg)
{
    pull0_pipe *p = arg;
 
    nni_aio_fini(&p->aio);
    if (p->m) {
        nni_msg_free(p->m);
    }
}
 
static int
pull0_pipe_init(void *arg, nni_pipe *pipe, void *s)
{
    pull0_pipe *p = arg;
 
    nni_aio_init(&p->aio, pull0_recv_cb, p);
    p->p = pipe;
    p->s = s;
    return (0);
}
 
static int
pull0_pipe_start(void *arg)
{
    pull0_pipe *p = arg;
 
    if (nni_pipe_peer(p->p) != NNI_PROTO_PUSH_V0) {
        // Peer protocol mismatch.
        return (NNG_EPROTO);
    }
 
    // Start the pending receive...
    nni_pipe_recv(p->p, &p->aio);
 
    return (0);
}
 
static void
pull0_pipe_close(void *arg)
{
    pull0_pipe *p = arg;
    pull0_sock *s = p->s;
 
    nni_mtx_lock(&s->m);
    p->closed = true;
    if (nni_list_node_active(&p->node)) {
        nni_list_node_remove(&p->node);
        if (nni_list_empty(&s->pl)) {
            nni_pollable_clear(&s->readable);
        }
    }
    nni_mtx_unlock(&s->m);
 
    nni_aio_close(&p->aio);
}
 
static void
pull0_recv_cb(void *arg)
{
    pull0_pipe *p  = arg;
    pull0_sock *s  = p->s;
    nni_aio *   ap = &p->aio;
    nni_aio *   as;
    nni_msg *   m;
 
    if (nni_aio_result(ap) != 0) {
        // Failed to get a message, probably the pipe is closed.
        nni_pipe_close(p->p);
        return;
    }
 
    // Got a message... start the put to send it up to the application.
    m = nni_aio_get_msg(ap);
    nni_aio_set_msg(ap, NULL);
    nni_msg_set_pipe(m, nni_pipe_id(p->p));
 
    nni_mtx_lock(&s->m);
    if (p->closed) {
        nni_mtx_unlock(&s->m);
        nni_msg_free(m);
        return;
    }
    if (nni_list_empty(&s->rq)) {
        nni_list_append(&s->pl, p);
        if (nni_list_first(&s->pl) == p) {
            nni_pollable_raise(&s->readable);
        }
        p->m = m;
        nni_mtx_unlock(&s->m);
        return;
    }
    nni_pipe_recv(p->p, ap);
    as = nni_list_first(&s->rq);
    nni_aio_list_remove(as);
    nni_mtx_unlock(&s->m);
    nni_aio_set_msg(as, m);
    nni_aio_finish_sync(as, 0, nni_msg_len(m));
}
 
static void
pull0_sock_open(void *arg)
{
    NNI_ARG_UNUSED(arg);
}
 
static void
pull0_sock_close(void *arg)
{
    pull0_sock *s = arg;
    nni_aio *   a;
    nni_mtx_lock(&s->m);
    while ((a = nni_list_first(&s->rq)) != NULL) {
        nni_aio_list_remove(a);
        nni_aio_finish_error(a, NNG_ECLOSED);
    }
    // NB: The common socket framework closes pipes before this.
    nni_mtx_unlock(&s->m);
}
 
static void
pull0_sock_send(void *arg, nni_aio *aio)
{
    NNI_ARG_UNUSED(arg);
    nni_aio_finish_error(aio, NNG_ENOTSUP);
}
 
static void
pull0_cancel(nni_aio *aio, void *arg, int rv)
{
    pull0_sock *s = arg;
    nni_mtx_lock(&s->m);
    if (nni_aio_list_active(aio)) {
        nni_aio_list_remove(aio);
        nni_aio_finish_error(aio, rv);
    }
    nni_mtx_unlock(&s->m);
}
 
static void
pull0_sock_recv(void *arg, nni_aio *aio)
{
    pull0_sock *s = arg;
    pull0_pipe *p;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
 
    nni_mtx_lock(&s->m);
    if ((p = nni_list_first(&s->pl)) == NULL) {
 
        int rv;
        if ((rv = nni_aio_schedule(aio, pull0_cancel, s)) != 0) {
            nni_mtx_unlock(&s->m);
            nni_aio_finish_error(aio, rv);
            return;
        }
 
        nni_aio_list_append(&s->rq, aio);
        nni_mtx_unlock(&s->m);
        return;
    }
 
    nni_list_remove(&s->pl, p);
    if (nni_list_empty(&s->pl)) {
        nni_pollable_clear(&s->readable);
    }
    nni_aio_finish_msg(aio, p->m);
    p->m = NULL;
    nni_pipe_recv(p->p, &p->aio);
    nni_mtx_unlock(&s->m);
}
 
static int
pull0_sock_get_recv_fd(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    pull0_sock *s = arg;
    int         rv;
    int         fd;
 
    if ((rv = nni_pollable_getfd(&s->readable, &fd)) != 0) {
        return (rv);
    }
    return (nni_copyout_int(fd, buf, szp, t));
}
 
static nni_option pull0_sock_options[] = {
    {
        .o_name = NNG_OPT_RECVFD,
        .o_get  = pull0_sock_get_recv_fd,
    },
    // terminate list
    {
        .o_name = NULL,
    },
};
 
static nni_proto_pipe_ops pull0_pipe_ops = {
    .pipe_size  = sizeof(pull0_pipe),
    .pipe_init  = pull0_pipe_init,
    .pipe_fini  = pull0_pipe_fini,
    .pipe_start = pull0_pipe_start,
    .pipe_close = pull0_pipe_close,
    .pipe_stop  = pull0_pipe_stop,
};
 
static nni_proto_sock_ops pull0_sock_ops = {
    .sock_size    = sizeof(pull0_sock),
    .sock_init    = pull0_sock_init,
    .sock_fini    = pull0_sock_fini,
    .sock_open    = pull0_sock_open,
    .sock_close   = pull0_sock_close,
    .sock_send    = pull0_sock_send,
    .sock_recv    = pull0_sock_recv,
    .sock_options = pull0_sock_options,
};
 
static nni_proto pull0_proto = {
    .proto_version  = NNI_PROTOCOL_VERSION,
    .proto_self     = { NNI_PROTO_PULL_V0, "pull" },
    .proto_peer     = { NNI_PROTO_PUSH_V0, "push" },
    .proto_flags    = NNI_PROTO_FLAG_RCV,
    .proto_pipe_ops = &pull0_pipe_ops,
    .proto_sock_ops = &pull0_sock_ops,
};
 
static nni_proto pull0_proto_raw = {
    .proto_version  = NNI_PROTOCOL_VERSION,
    .proto_self     = { NNI_PROTO_PULL_V0, "pull" },
    .proto_peer     = { NNI_PROTO_PUSH_V0, "push" },
    .proto_flags    = NNI_PROTO_FLAG_RCV | NNI_PROTO_FLAG_RAW,
    .proto_pipe_ops = &pull0_pipe_ops,
    .proto_sock_ops = &pull0_sock_ops,
};
 
int
nng_pull0_open(nng_socket *s)
{
    return (nni_proto_open(s, &pull0_proto));
}
 
int
nng_pull0_open_raw(nng_socket *s)
{
    return (nni_proto_open(s, &pull0_proto_raw));
}