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
|
//
// Copyright 2025 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 <string.h>
#include "core/nng_impl.h"
// Publish protocol. The PUB protocol simply sends messages out, as
// a broadcast. It has nothing more sophisticated because it does not
// perform sender-side filtering. Its best effort delivery, so anything
// that can't receive the message won't get one.
#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 pub0_pipe pub0_pipe;
typedef struct pub0_sock pub0_sock;
static void pub0_pipe_recv_cb(void *);
static void pub0_pipe_send_cb(void *);
static void pub0_sock_fini(void *);
static void pub0_pipe_fini(void *);
// pub0_sock is our per-socket protocol private structure.
struct pub0_sock {
nni_list pipes;
nni_mtx mtx;
bool closed;
size_t sendbuf;
nni_pollable sendable;
#ifdef NNG_ENABLE_STATS
nni_stat_item stat_tx_direct;
nni_stat_item stat_tx_discard;
nni_stat_item stat_tx_queued;
nni_stat_item stat_tx_bufsz;
#endif
};
// pub0_pipe is our per-pipe protocol private structure.
struct pub0_pipe {
nni_pipe *pipe;
pub0_sock *pub;
nni_lmq sendq;
bool closed;
bool busy;
nni_aio aio_send;
nni_aio aio_recv;
nni_list_node node;
};
static void
pub0_sock_fini(void *arg)
{
pub0_sock *s = arg;
nni_pollable_fini(&s->sendable);
nni_mtx_fini(&s->mtx);
}
static void
pub0_sock_init(void *arg, nni_sock *ns)
{
pub0_sock *sock = arg;
NNI_ARG_UNUSED(ns);
nni_pollable_init(&sock->sendable);
nni_mtx_init(&sock->mtx);
NNI_LIST_INIT(&sock->pipes, pub0_pipe, node);
sock->sendbuf = 16; // fairly arbitrary
#if NNG_ENABLE_STATS
static const nni_stat_info tx_direct_info = {
.si_name = "tx_direct",
.si_desc = "messages sent without queueing (per pipe)",
.si_type = NNG_STAT_COUNTER,
.si_unit = NNG_UNIT_MESSAGES,
};
static const nni_stat_info tx_discard_info = {
.si_name = "tx_discard",
.si_desc = "messages dropped (once per pipe)",
.si_type = NNG_STAT_COUNTER,
.si_unit = NNG_UNIT_MESSAGES,
};
static const nni_stat_info tx_queued_info = {
.si_name = "tx_queued",
.si_desc = "messages queued (once per pipe)",
.si_type = NNG_STAT_COUNTER,
.si_unit = NNG_UNIT_MESSAGES,
};
static const nni_stat_info tx_bufsz_info = {
.si_name = "tx_buf_size",
.si_desc = "pipe buffer size for queued messages",
.si_type = NNG_STAT_LEVEL,
.si_unit = NNG_UNIT_MESSAGES,
};
nni_stat_init(&sock->stat_tx_direct, &tx_direct_info);
nni_stat_init(&sock->stat_tx_discard, &tx_discard_info);
nni_stat_init(&sock->stat_tx_queued, &tx_queued_info);
nni_stat_init(&sock->stat_tx_bufsz, &tx_bufsz_info);
nni_sock_add_stat(ns, &sock->stat_tx_direct);
nni_sock_add_stat(ns, &sock->stat_tx_discard);
nni_sock_add_stat(ns, &sock->stat_tx_queued);
nni_sock_add_stat(ns, &sock->stat_tx_bufsz);
nni_stat_set_value(&sock->stat_tx_bufsz, sock->sendbuf);
#endif
}
static void
pub0_sock_open(void *arg)
{
NNI_ARG_UNUSED(arg);
}
static void
pub0_sock_close(void *arg)
{
NNI_ARG_UNUSED(arg);
}
static void
pub0_pipe_stop(void *arg)
{
pub0_pipe *p = arg;
nni_aio_stop(&p->aio_send);
nni_aio_stop(&p->aio_recv);
}
static void
pub0_pipe_fini(void *arg)
{
pub0_pipe *p = arg;
nni_aio_fini(&p->aio_send);
nni_aio_fini(&p->aio_recv);
nni_lmq_fini(&p->sendq);
}
static int
pub0_pipe_init(void *arg, nni_pipe *pipe, void *s)
{
pub0_pipe *p = arg;
pub0_sock *sock = s;
size_t len;
nni_mtx_lock(&sock->mtx);
len = sock->sendbuf;
nni_mtx_unlock(&sock->mtx);
nni_lmq_init(&p->sendq, len);
nni_aio_init(&p->aio_send, pub0_pipe_send_cb, p);
nni_aio_init(&p->aio_recv, pub0_pipe_recv_cb, p);
p->busy = false;
p->pipe = pipe;
p->pub = s;
return (0);
}
static int
pub0_pipe_start(void *arg)
{
pub0_pipe *p = arg;
pub0_sock *sock = p->pub;
if (nni_pipe_peer(p->pipe) != NNI_PROTO_SUB_V0) {
nng_log_warn("NNG-PEER-MISMATCH",
"Peer protocol mismatch: %d != %d, rejected.",
nni_pipe_peer(p->pipe), NNI_PROTO_SUB_V0);
return (NNG_EPROTO);
}
nni_mtx_lock(&sock->mtx);
nni_list_append(&sock->pipes, p);
nni_mtx_unlock(&sock->mtx);
// Start the receiver.
nni_pipe_recv(p->pipe, &p->aio_recv);
return (0);
}
static void
pub0_pipe_close(void *arg)
{
pub0_pipe *p = arg;
pub0_sock *sock = p->pub;
nni_aio_close(&p->aio_send);
nni_aio_close(&p->aio_recv);
nni_mtx_lock(&sock->mtx);
p->closed = true;
nni_lmq_flush(&p->sendq);
if (nni_list_active(&sock->pipes, p)) {
nni_list_remove(&sock->pipes, p);
}
nni_mtx_unlock(&sock->mtx);
}
static void
pub0_pipe_recv_cb(void *arg)
{
pub0_pipe *p = arg;
// We should never receive a message -- the only valid reason for us to
// be here is on pipe close.
if (nni_aio_result(&p->aio_recv) == 0) {
nni_msg_free(nni_aio_get_msg(&p->aio_recv));
}
nni_pipe_close(p->pipe);
}
static void
pub0_pipe_send_cb(void *arg)
{
pub0_pipe *p = arg;
pub0_sock *sock = p->pub;
nni_msg *msg;
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;
}
nni_mtx_lock(&sock->mtx);
if (p->closed) {
nni_mtx_unlock(&sock->mtx);
return;
}
if (nni_lmq_get(&p->sendq, &msg) == 0) {
nni_aio_set_msg(&p->aio_send, msg);
nni_pipe_send(p->pipe, &p->aio_send);
} else {
p->busy = false;
}
nni_mtx_unlock(&sock->mtx);
}
static void
pub0_sock_recv(void *arg, nni_aio *aio)
{
NNI_ARG_UNUSED(arg);
nni_aio_finish_error(aio, NNG_ENOTSUP);
}
static void
pub0_sock_send(void *arg, nni_aio *aio)
{
pub0_sock *sock = arg;
pub0_pipe *p;
nng_msg *msg;
size_t len;
msg = nni_aio_get_msg(aio);
len = nni_msg_len(msg);
nni_mtx_lock(&sock->mtx);
#ifdef NNG_ENABLE_STATS
int dropped = 0;
int direct = 0;
int queued = 0;
#endif
NNI_LIST_FOREACH (&sock->pipes, p) {
nni_msg_clone(msg);
if (p->busy) {
if (nni_lmq_full(&p->sendq)) {
// Make space for the new message.
nni_msg *old;
(void) nni_lmq_get(&p->sendq, &old);
nni_msg_free(old);
#ifdef NNG_ENABLE_STATS
dropped++;
#endif
}
#ifdef NNG_ENABLE_STATS
queued++;
#endif
nni_lmq_put(&p->sendq, msg);
} else {
p->busy = true;
nni_aio_set_msg(&p->aio_send, msg);
nni_pipe_send(p->pipe, &p->aio_send);
#ifdef NNG_ENABLE_STATS
direct++;
#endif
}
}
#ifdef NNG_ENABLE_STATS
if (direct == 0 && queued == 0) {
dropped++; // we didn't find a pipe to send it to!
}
nni_stat_inc(&sock->stat_tx_discard, dropped);
nni_stat_inc(&sock->stat_tx_queued, queued);
nni_stat_inc(&sock->stat_tx_direct, direct);
#endif
nni_mtx_unlock(&sock->mtx);
nng_msg_free(msg);
nni_aio_finish(aio, 0, len);
}
static nng_err
pub0_sock_get_sendfd(void *arg, int *fdp)
{
pub0_sock *sock = arg;
// PUB sockets are *always* writable.
nni_pollable_raise(&sock->sendable);
return (nni_pollable_getfd(&sock->sendable, fdp));
}
static nng_err
pub0_sock_set_sendbuf(void *arg, const void *buf, size_t sz, nni_type t)
{
pub0_sock *sock = arg;
pub0_pipe *p;
int val;
nng_err rv;
if ((rv = nni_copyin_int(&val, buf, sz, 1, 8192, t)) != NNG_OK) {
return (rv);
}
nni_mtx_lock(&sock->mtx);
sock->sendbuf = (size_t) val;
#ifdef NNG_ENABLE_STATS
nni_stat_set_value(&sock->stat_tx_bufsz, sock->sendbuf);
#endif
NNI_LIST_FOREACH (&sock->pipes, p) {
// If we fail part way through (should only be ENOMEM), we
// stop short. The others would likely fail for ENOMEM as
// well anyway. There is a weird effect here where the
// buffers may have been set for *some* of the pipes, but
// we have no way to correct partial failure.
if ((rv = nni_lmq_resize(&p->sendq, (size_t) val)) != 0) {
break;
}
}
nni_mtx_unlock(&sock->mtx);
return (rv);
}
static nng_err
pub0_sock_get_sendbuf(void *arg, void *buf, size_t *szp, nni_type t)
{
pub0_sock *sock = arg;
int val;
nni_mtx_lock(&sock->mtx);
val = (int) sock->sendbuf;
nni_mtx_unlock(&sock->mtx);
return (nni_copyout_int(val, buf, szp, t));
}
static nni_proto_pipe_ops pub0_pipe_ops = {
.pipe_size = sizeof(pub0_pipe),
.pipe_init = pub0_pipe_init,
.pipe_fini = pub0_pipe_fini,
.pipe_start = pub0_pipe_start,
.pipe_close = pub0_pipe_close,
.pipe_stop = pub0_pipe_stop,
};
static nni_option pub0_sock_options[] = {
// terminate list
{
.o_name = NNG_OPT_SENDBUF,
.o_get = pub0_sock_get_sendbuf,
.o_set = pub0_sock_set_sendbuf,
},
{
.o_name = NULL,
},
};
static nni_proto_sock_ops pub0_sock_ops = {
.sock_size = sizeof(pub0_sock),
.sock_init = pub0_sock_init,
.sock_fini = pub0_sock_fini,
.sock_open = pub0_sock_open,
.sock_close = pub0_sock_close,
.sock_send = pub0_sock_send,
.sock_recv = pub0_sock_recv,
.sock_send_poll_fd = pub0_sock_get_sendfd,
.sock_options = pub0_sock_options,
};
static nni_proto pub0_proto = {
.proto_version = NNI_PROTOCOL_VERSION,
.proto_self = { NNI_PROTO_PUB_V0, "pub" },
.proto_peer = { NNI_PROTO_SUB_V0, "sub" },
.proto_flags = NNI_PROTO_FLAG_SND,
.proto_sock_ops = &pub0_sock_ops,
.proto_pipe_ops = &pub0_pipe_ops,
};
static nni_proto pub0_proto_raw = {
.proto_version = NNI_PROTOCOL_VERSION,
.proto_self = { NNI_PROTO_PUB_V0, "pub" },
.proto_peer = { NNI_PROTO_SUB_V0, "sub" },
.proto_flags = NNI_PROTO_FLAG_SND | NNI_PROTO_FLAG_RAW,
.proto_sock_ops = &pub0_sock_ops,
.proto_pipe_ops = &pub0_pipe_ops,
};
int
nng_pub0_open(nng_socket *id)
{
return (nni_proto_open(id, &pub0_proto));
}
int
nng_pub0_open_raw(nng_socket *id)
{
return (nni_proto_open(id, &pub0_proto_raw));
}
|