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
//
// 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/pubsub0/sub.h"
 
// Subscriber protocol.  The SUB protocol receives messages sent to
// it from publishers, and filters out those it is not interested in,
// only passing up ones that match known subscriptions.
 
#ifndef NNI_PROTO_SUB_V0
#define NNI_PROTO_SUB_V0 NNI_PROTO(2, 1)
#endif
 
#ifndef NNI_PROTO_PUB_V0
#define NNI_PROTO_PUB_V0 NNI_PROTO(2, 0)
#endif
 
typedef struct xsub0_pipe xsub0_pipe;
typedef struct xsub0_sock xsub0_sock;
 
static void xsub0_recv_cb(void *);
static void xsub0_pipe_fini(void *);
 
// xsub0_sock is our per-socket protocol private structure.
struct xsub0_sock {
    nni_msgq *urq;
    nni_mtx   lk;
};
 
// sub0_pipe is our per-pipe protocol private structure.
struct xsub0_pipe {
    nni_pipe *  pipe;
    xsub0_sock *sub;
    nni_aio    aio_recv;
};
 
static int
xsub0_sock_init(void *arg, nni_sock *sock)
{
    xsub0_sock *s = arg;
 
    s->urq = nni_sock_recvq(sock);
    return (0);
}
 
static void
xsub0_sock_fini(void *arg)
{
    xsub0_sock *s = arg;
    nni_mtx_fini(&s->lk);
}
 
static void
xsub0_sock_open(void *arg)
{
    NNI_ARG_UNUSED(arg);
}
 
static void
xsub0_sock_close(void *arg)
{
    NNI_ARG_UNUSED(arg);
}
 
static void
xsub0_pipe_stop(void *arg)
{
    xsub0_pipe *p = arg;
 
    nni_aio_stop(&p->aio_recv);
}
 
static void
xsub0_pipe_fini(void *arg)
{
    xsub0_pipe *p = arg;
 
    nni_aio_fini(&p->aio_recv);
}
 
static int
xsub0_pipe_init(void *arg, nni_pipe *pipe, void *s)
{
    xsub0_pipe *p = arg;
 
    nni_aio_init(&p->aio_recv, xsub0_recv_cb, p);
 
    p->pipe = pipe;
    p->sub  = s;
    return (0);
}
 
static int
xsub0_pipe_start(void *arg)
{
    xsub0_pipe *p = arg;
 
    if (nni_pipe_peer(p->pipe) != NNI_PROTO_PUB_V0) {
        // Peer protocol mismatch.
        return (NNG_EPROTO);
    }
 
    nni_pipe_recv(p->pipe, &p->aio_recv);
    return (0);
}
 
static void
xsub0_pipe_close(void *arg)
{
    xsub0_pipe *p = arg;
 
    nni_aio_close(&p->aio_recv);
}
 
static void
xsub0_recv_cb(void *arg)
{
    xsub0_pipe *p   = arg;
    xsub0_sock *s   = p->sub;
    nni_msgq *  urq = s->urq;
    nni_msg *   msg;
 
    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));
 
    if (nni_msgq_tryput(urq, msg) != 0) {
        // This only happens for two reasons.  For flow control,
        // in which case we just want to discard the message and
        // carry on, and for a close of the socket (which is very
        // hard to achieve, since we close the pipes.)  In either
        // case the easiest thing to do is just free the message
        // and try again.
        nni_msg_free(msg);
    }
    nni_pipe_recv(p->pipe, &p->aio_recv);
}
 
static void
xsub0_sock_send(void *arg, nni_aio *aio)
{
    NNI_ARG_UNUSED(arg);
    nni_aio_finish_error(aio, NNG_ENOTSUP);
}
 
static void
xsub0_sock_recv(void *arg, nni_aio *aio)
{
    xsub0_sock *s = arg;
 
    nni_msgq_aio_get(s->urq, aio);
}
 
// This is the global protocol structure -- our linkage to the core.
// This should be the only global non-static symbol in this file.
static nni_proto_pipe_ops xsub0_pipe_ops = {
    .pipe_size  = sizeof(xsub0_pipe),
    .pipe_init  = xsub0_pipe_init,
    .pipe_fini  = xsub0_pipe_fini,
    .pipe_start = xsub0_pipe_start,
    .pipe_close = xsub0_pipe_close,
    .pipe_stop  = xsub0_pipe_stop,
};
 
static nni_option xsub0_sock_options[] = {
    // terminate list
    {
        .o_name = NULL,
    },
};
 
static nni_proto_sock_ops xsub0_sock_ops = {
    .sock_size    = sizeof(xsub0_sock),
    .sock_init    = xsub0_sock_init,
    .sock_fini    = xsub0_sock_fini,
    .sock_open    = xsub0_sock_open,
    .sock_close   = xsub0_sock_close,
    .sock_send    = xsub0_sock_send,
    .sock_recv    = xsub0_sock_recv,
    .sock_options = xsub0_sock_options,
};
 
static nni_proto xsub0_proto = {
    .proto_version  = NNI_PROTOCOL_VERSION,
    .proto_self     = { NNI_PROTO_SUB_V0, "sub" },
    .proto_peer     = { NNI_PROTO_PUB_V0, "pub" },
    .proto_flags    = NNI_PROTO_FLAG_RCV | NNI_PROTO_FLAG_RAW,
    .proto_sock_ops = &xsub0_sock_ops,
    .proto_pipe_ops = &xsub0_pipe_ops,
};
 
int
nng_sub0_open_raw(nng_socket *sidp)
{
    return (nni_proto_open(sidp, &xsub0_proto));
}