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
//
// 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 <stdio.h>
 
#include "core/nng_impl.h"
#include "nng/protocol/reqrep0/req.h"
 
// Request protocol.  The REQ protocol is the "request" side of a
// request-reply pair.  This is useful for building RPC clients, for example.
 
typedef struct xreq0_pipe xreq0_pipe;
typedef struct xreq0_sock xreq0_sock;
 
// An xreq0_sock is our per-socket protocol private structure.
struct xreq0_sock {
    nni_msgq *     uwq;
    nni_msgq *     urq;
    nni_atomic_int ttl;
};
 
// A req0_pipe is our per-pipe protocol private structure.
struct xreq0_pipe {
    nni_pipe *  pipe;
    xreq0_sock *req;
    nni_aio     aio_getq;
    nni_aio     aio_send;
    nni_aio     aio_recv;
    nni_aio     aio_putq;
};
 
static void xreq0_sock_fini(void *);
static void xreq0_getq_cb(void *);
static void xreq0_send_cb(void *);
static void xreq0_recv_cb(void *);
static void xreq0_putq_cb(void *);
 
static int
xreq0_sock_init(void *arg, nni_sock *sock)
{
    xreq0_sock *s = arg;
 
    nni_atomic_init(&s->ttl);
    nni_atomic_set(&s->ttl, 8);
    s->uwq = nni_sock_sendq(sock);
    s->urq = nni_sock_recvq(sock);
 
    return (0);
}
 
static void
xreq0_sock_open(void *arg)
{
    NNI_ARG_UNUSED(arg);
}
 
static void
xreq0_sock_close(void *arg)
{
    NNI_ARG_UNUSED(arg);
}
 
static void
xreq0_sock_fini(void *arg)
{
    NNI_ARG_UNUSED(arg);
}
 
static void
xreq0_pipe_stop(void *arg)
{
    xreq0_pipe *p = arg;
 
    nni_aio_stop(&p->aio_getq);
    nni_aio_stop(&p->aio_putq);
    nni_aio_stop(&p->aio_recv);
    nni_aio_stop(&p->aio_send);
}
 
static void
xreq0_pipe_fini(void *arg)
{
    xreq0_pipe *p = arg;
 
    nni_aio_fini(&p->aio_getq);
    nni_aio_fini(&p->aio_putq);
    nni_aio_fini(&p->aio_recv);
    nni_aio_fini(&p->aio_send);
}
 
static int
xreq0_pipe_init(void *arg, nni_pipe *pipe, void *s)
{
    xreq0_pipe *p = arg;
 
    nni_aio_init(&p->aio_getq, xreq0_getq_cb, p);
    nni_aio_init(&p->aio_putq, xreq0_putq_cb, p);
    nni_aio_init(&p->aio_recv, xreq0_recv_cb, p);
    nni_aio_init(&p->aio_send, xreq0_send_cb, p);
 
    p->pipe = pipe;
    p->req  = s;
    return (0);
}
 
static int
xreq0_pipe_start(void *arg)
{
    xreq0_pipe *p = arg;
    xreq0_sock *s = p->req;
 
    if (nni_pipe_peer(p->pipe) != NNG_REQ0_PEER) {
        return (NNG_EPROTO);
    }
 
    nni_msgq_aio_get(s->uwq, &p->aio_getq);
    nni_pipe_recv(p->pipe, &p->aio_recv);
    return (0);
}
 
static void
xreq0_pipe_close(void *arg)
{
    xreq0_pipe *p = arg;
 
    nni_aio_close(&p->aio_getq);
    nni_aio_close(&p->aio_putq);
    nni_aio_close(&p->aio_recv);
    nni_aio_close(&p->aio_send);
}
 
// For raw mode we can just let the pipes "contend" via get queue to get a
// message from the upper write queue.  The msg queue implementation
// actually provides ordering, so load will be spread automatically.
// (NB: We may have to revise this in the future if we want to provide some
// kind of priority.)
 
static void
xreq0_getq_cb(void *arg)
{
    xreq0_pipe *p = arg;
 
    if (nni_aio_result(&p->aio_getq) != 0) {
        nni_pipe_close(p->pipe);
        return;
    }
 
    nni_aio_set_msg(&p->aio_send, nni_aio_get_msg(&p->aio_getq));
    nni_aio_set_msg(&p->aio_getq, NULL);
 
    nni_pipe_send(p->pipe, &p->aio_send);
}
 
static void
xreq0_send_cb(void *arg)
{
    xreq0_pipe *p = arg;
 
    if (nni_aio_result(&p->aio_send) != 0) {
        nni_msg_free(nni_aio_get_msg(&p->aio_send));
        nni_aio_set_msg(&p->aio_send, NULL);
        nni_pipe_close(p->pipe);
        return;
    }
 
    // Sent a message so we just need to look for another one.
    nni_msgq_aio_get(p->req->uwq, &p->aio_getq);
}
 
static void
xreq0_putq_cb(void *arg)
{
    xreq0_pipe *p = arg;
 
    if (nni_aio_result(&p->aio_putq) != 0) {
        nni_msg_free(nni_aio_get_msg(&p->aio_putq));
        nni_aio_set_msg(&p->aio_putq, NULL);
        nni_pipe_close(p->pipe);
        return;
    }
    nni_aio_set_msg(&p->aio_putq, NULL);
 
    nni_pipe_recv(p->pipe, &p->aio_recv);
}
 
static void
xreq0_recv_cb(void *arg)
{
    xreq0_pipe *p    = arg;
    xreq0_sock *sock = p->req;
    nni_msg *   msg;
    bool        end;
 
    if (nni_aio_result(&p->aio_recv) != 0) {
        nni_pipe_close(p->pipe);
        return;
    }
 
    msg = nni_aio_get_msg(&p->aio_recv);
    nni_aio_set_msg(&p->aio_recv, NULL);
    nni_msg_set_pipe(msg, nni_pipe_id(p->pipe));
    end = false;
 
    while (!end) {
        uint8_t *body;
 
        if (nni_msg_len(msg) < 4) {
            // Peer gave us garbage, so kick it.
            nni_msg_free(msg);
            nni_pipe_close(p->pipe);
            return;
        }
        body = nni_msg_body(msg);
        end  = ((body[0] & 0x80u) != 0);
 
        if (nng_msg_header_append(msg, body, sizeof (uint32_t)) != 0) {
            // TODO: bump a no-memory stat
            nni_msg_free(msg);
            // Closing the pipe may release some memory.
            // It at least gives an indication to the peer
            // that we've lost the message.
            nni_pipe_close(p->pipe);
            return;
        }
        nni_msg_trim(msg, sizeof (uint32_t));
    }
    nni_aio_set_msg(&p->aio_putq, msg);
    nni_msgq_aio_put(sock->urq, &p->aio_putq);
}
 
static void
xreq0_sock_send(void *arg, nni_aio *aio)
{
    xreq0_sock *s = arg;
 
    nni_msgq_aio_put(s->uwq, aio);
}
 
static void
xreq0_sock_recv(void *arg, nni_aio *aio)
{
    xreq0_sock *s = arg;
 
    nni_msgq_aio_get(s->urq, aio);
}
 
static int
xreq0_sock_set_max_ttl(void *arg, const void *buf, size_t sz, nni_opt_type t)
{
    xreq0_sock *s = arg;
    int         ttl;
    int         rv;
    if ((rv = nni_copyin_int(&ttl, buf, sz, 1, NNI_MAX_MAX_TTL, t)) == 0) {
        nni_atomic_set(&s->ttl, ttl);
    }
    return (rv);
}
 
static int
xreq0_sock_get_max_ttl(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    xreq0_sock *s = arg;
    return (nni_copyout_int(nni_atomic_get(&s->ttl), buf, szp, t));
}
 
static nni_proto_pipe_ops xreq0_pipe_ops = {
    .pipe_size  = sizeof(xreq0_pipe),
    .pipe_init  = xreq0_pipe_init,
    .pipe_fini  = xreq0_pipe_fini,
    .pipe_start = xreq0_pipe_start,
    .pipe_close = xreq0_pipe_close,
    .pipe_stop  = xreq0_pipe_stop,
};
 
static nni_option xreq0_sock_options[] = {
    {
        .o_name = NNG_OPT_MAXTTL,
        .o_get  = xreq0_sock_get_max_ttl,
        .o_set  = xreq0_sock_set_max_ttl,
    },
    // terminate list
    {
        .o_name = NULL,
    },
};
 
static nni_proto_sock_ops xreq0_sock_ops = {
    .sock_size    = sizeof(xreq0_sock),
    .sock_init    = xreq0_sock_init,
    .sock_fini    = xreq0_sock_fini,
    .sock_open    = xreq0_sock_open,
    .sock_close   = xreq0_sock_close,
    .sock_options = xreq0_sock_options,
    .sock_send    = xreq0_sock_send,
    .sock_recv    = xreq0_sock_recv,
};
 
static nni_proto xreq0_proto = {
    .proto_version  = NNI_PROTOCOL_VERSION,
    .proto_self     = { NNG_REQ0_SELF, NNG_REQ0_SELF_NAME },
    .proto_peer     = { NNG_REQ0_PEER, NNG_REQ0_PEER_NAME },
    .proto_flags    = NNI_PROTO_FLAG_SNDRCV | NNI_PROTO_FLAG_RAW,
    .proto_sock_ops = &xreq0_sock_ops,
    .proto_pipe_ops = &xreq0_pipe_ops,
    .proto_ctx_ops  = NULL, // raw mode does not support contexts
};
 
int
nng_req0_open_raw(nng_socket *sock)
{
    return (nni_proto_open(sock, &xreq0_proto));
}