aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/reqrep/rep.c
blob: 3cf215b54ca9176f360d607e4cbdce519169b173 (plain)
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
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// 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 <string.h>

#include "core/nng_impl.h"

// Response protocol.  The REP protocol is the "reply" side of a
// request-reply pair.  This is useful for building RPC servers, for
// example.

typedef struct nni_rep_pipe	nni_rep_pipe;
typedef struct nni_rep_sock	nni_rep_sock;

// An nni_rep_sock is our per-socket protocol private structure.
struct nni_rep_sock {
	nni_sock *	sock;
	nni_msgq *	uwq;
	nni_msgq *	urq;
	int		raw;
	int		ttl;
	nni_thr		sender;
	nni_idhash *	pipes;
	char *		btrace;
	size_t		btrace_len;
};

// An nni_rep_pipe is our per-pipe protocol private structure.
struct nni_rep_pipe {
	nni_pipe *	pipe;
	nni_rep_sock *	rep;
	nni_msgq *	sendq;
	int		sigclose;
};

static int
nni_rep_sock_init(void **repp, nni_sock *sock)
{
	nni_rep_sock *rep;
	int rv;

	if ((rep = NNI_ALLOC_STRUCT(rep)) == NULL) {
		return (NNG_ENOMEM);
	}
	rep->ttl = 8;   // Per RFC
	rep->sock = sock;
	rep->raw = 0;
	rep->btrace = NULL;
	rep->btrace_len = 0;
	if ((rv = nni_idhash_create(&rep->pipes)) != 0) {
		NNI_FREE_STRUCT(rep);
		return (rv);
	}

	rep->uwq = nni_sock_sendq(sock);
	rep->urq = nni_sock_recvq(sock);

	*repp = rep;
	nni_sock_senderr(sock, NNG_ESTATE);
	return (0);
}


static void
nni_rep_sock_fini(void *arg)
{
	nni_rep_sock *rep = arg;

	nni_idhash_destroy(rep->pipes);
	if (rep->btrace != NULL) {
		nni_free(rep->btrace, rep->btrace_len);
	}
	NNI_FREE_STRUCT(rep);
}


static int
nni_rep_pipe_init(void **rpp, nni_pipe *pipe, void *rsock)
{
	nni_rep_pipe *rp;
	int rv;

	if ((rp = NNI_ALLOC_STRUCT(rp)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_msgq_init(&rp->sendq, 2)) != 0) {
		NNI_FREE_STRUCT(rp);
		return (rv);
	}
	rp->pipe = pipe;
	rp->rep = rsock;
	rp->sigclose = 0;
	*rpp = rp;
	return (0);
}


static void
nni_rep_pipe_fini(void *arg)
{
	nni_rep_pipe *rp = arg;

	nni_msgq_fini(rp->sendq);
	NNI_FREE_STRUCT(rp);
}


static int
nni_rep_pipe_add(void *arg)
{
	nni_rep_pipe *rp = arg;
	nni_rep_sock *rep = rp->rep;

	return (nni_idhash_insert(rep->pipes, nni_pipe_id(rp->pipe), rp));
}


static void
nni_rep_pipe_rem(void *arg)
{
	nni_rep_pipe *rp = arg;
	nni_rep_sock *rep = rp->rep;

	nni_idhash_remove(rep->pipes, nni_pipe_id(rp->pipe));
}


// nni_rep_sock_send watches for messages from the upper write queue,
// extracts the destination pipe, and forwards it to the appropriate
// destination pipe via a separate queue.  This prevents a single bad
// or slow pipe from gumming up the works for the entire socket.
static void
nni_rep_sock_send(void *arg)
{
	nni_rep_sock *rep = arg;
	nni_msgq *uwq = rep->uwq;
	nni_mtx *mx = nni_sock_mtx(rep->sock);
	nni_msg *msg;

	for (;;) {
		uint8_t *header;
		uint32_t id;
		nni_rep_pipe *rp;
		int rv;

		if ((rv = nni_msgq_get(uwq, &msg)) != 0) {
			break;
		}
		// We yank the outgoing pipe id from the header
		if (nni_msg_header_len(msg) < 4) {
			nni_msg_free(msg);
			continue;
		}
		header = nni_msg_header(msg);
		NNI_GET32(header, id);
		nni_msg_trim_header(msg, 4);

		nni_mtx_lock(mx);
		if (nni_idhash_find(rep->pipes, id, (void **) &rp) != 0) {
			nni_mtx_unlock(mx);
			nni_msg_free(msg);
			continue;
		}
		// Try a non-blocking put to the lower writer.
		rv = nni_msgq_put_until(rp->sendq, msg, NNI_TIME_ZERO);
		if (rv != 0) {
			// message queue is full, we have no choice but
			// to drop it.  This should not happen under normal
			// circumstances.
			nni_msg_free(msg);
		}
		nni_mtx_unlock(mx);
	}
}


static void
nni_rep_pipe_send(void *arg)
{
	nni_rep_pipe *rp = arg;
	nni_rep_sock *rep = rp->rep;
	nni_msgq *urq = rep->urq;
	nni_msgq *wq = rp->sendq;
	nni_pipe *pipe = rp->pipe;
	nni_msg *msg;
	int rv;

	for (;;) {
		rv = nni_msgq_get_sig(wq, &msg, &rp->sigclose);
		if (rv != 0) {
			break;
		}

		rv = nni_pipe_send(pipe, msg);
		if (rv != 0) {
			nni_msg_free(msg);
			break;
		}
	}
	nni_msgq_signal(urq, &rp->sigclose);
	nni_pipe_close(pipe);
}


static void
nni_rep_pipe_recv(void *arg)
{
	nni_rep_pipe *rp = arg;
	nni_rep_sock *rep = rp->rep;
	nni_msgq *urq = rep->urq;
	nni_msgq *uwq = rep->uwq;
	nni_pipe *pipe = rp->pipe;
	nni_msg *msg;
	int rv;
	uint8_t idbuf[4];
	uint32_t id = nni_pipe_id(pipe);

	NNI_PUT32(idbuf, id);

	for (;;) {
		size_t len;
		uint8_t *body;
		int hops;

again:
		rv = nni_pipe_recv(pipe, &msg);
		if (rv != 0) {
			break;
		}

		// Store the pipe id in the header, first thing.
		rv = nni_msg_append_header(msg, idbuf, 4);
		if (rv != 0) {
			nni_msg_free(msg);
			continue;
		}

		// Move backtrace from body to header
		hops = 0;
		for (;;) {
			int end = 0;
			if (hops >= rep->ttl) {
				nni_msg_free(msg);
				goto again;
			}
			if (nni_msg_len(msg) < 4) {
				nni_msg_free(msg);
				goto again;
			}
			body = nni_msg_body(msg);
			end = (body[0] & 0x80) ? 1 : 0;
			rv = nni_msg_append_header(msg, body, 4);
			if (rv != 0) {
				nni_msg_free(msg);
				goto again;
			}
			nni_msg_trim(msg, 4);
			if (end) {
				break;
			}
		}

		// Now send it up.
		rv = nni_msgq_put_sig(urq, msg, &rp->sigclose);
		if (rv != 0) {
			nni_msg_free(msg);
			break;
		}
	}
	nni_msgq_signal(uwq, &rp->sigclose);
	nni_msgq_signal(rp->sendq, &rp->sigclose);
	nni_pipe_close(pipe);
}


static int
nni_rep_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
{
	nni_rep_sock *rep = arg;
	int rv;

	switch (opt) {
	case NNG_OPT_MAXTTL:
		rv = nni_setopt_int(&rep->ttl, buf, sz, 1, 255);
		break;
	case NNG_OPT_RAW:
		rv = nni_setopt_int(&rep->raw, buf, sz, 0, 1);
		break;
	default:
		rv = NNG_ENOTSUP;
	}
	return (rv);
}


static int
nni_rep_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
{
	nni_rep_sock *rep = arg;
	int rv;

	switch (opt) {
	case NNG_OPT_MAXTTL:
		rv = nni_getopt_int(&rep->ttl, buf, szp);
		break;
	case NNG_OPT_RAW:
		rv = nni_getopt_int(&rep->raw, buf, szp);
		break;
	default:
		rv = NNG_ENOTSUP;
	}
	return (rv);
}


static nni_msg *
nni_rep_sock_sfilter(void *arg, nni_msg *msg)
{
	nni_rep_sock *rep = arg;
	size_t len;

	if (rep->raw) {
		return (msg);
	}

	// Cannot send again until a receive is done...
	nni_sock_senderr(rep->sock, NNG_ESTATE);

	// If we have a stored backtrace, append it to the header...
	// if we don't have a backtrace, discard the message.
	if (rep->btrace == NULL) {
		nni_msg_free(msg);
		return (NULL);
	}

	// drop anything else in the header...
	nni_msg_trunc_header(msg, nni_msg_header_len(msg));

	if (nni_msg_append_header(msg, rep->btrace, rep->btrace_len) != 0) {
		nni_free(rep->btrace, rep->btrace_len);
		rep->btrace = NULL;
		rep->btrace_len = 0;
		nni_msg_free(msg);
		return (NULL);
	}

	nni_free(rep->btrace, rep->btrace_len);
	rep->btrace = NULL;
	rep->btrace_len = 0;
	return (msg);
}


static nni_msg *
nni_rep_sock_rfilter(void *arg, nni_msg *msg)
{
	nni_rep_sock *rep = arg;
	char *header;
	size_t len;

	if (rep->raw) {
		return (msg);
	}

	nni_sock_senderr(rep->sock, 0);
	len = nni_msg_header_len(msg);
	header = nni_msg_header(msg);
	if (rep->btrace != NULL) {
		nni_free(rep->btrace, rep->btrace_len);
		rep->btrace = NULL;
		rep->btrace_len = 0;
	}
	if ((rep->btrace = nni_alloc(len)) == NULL) {
		nni_msg_free(msg);
		return (NULL);
	}
	rep->btrace_len = len;
	memcpy(rep->btrace, header, len);
	nni_msg_trunc_header(msg, len);
	return (msg);
}


// 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 nni_rep_pipe_ops = {
	.pipe_init	= nni_rep_pipe_init,
	.pipe_fini	= nni_rep_pipe_fini,
	.pipe_add	= nni_rep_pipe_add,
	.pipe_rem	= nni_rep_pipe_rem,
	.pipe_send	= nni_rep_pipe_send,
	.pipe_recv	= nni_rep_pipe_recv,
};

static nni_proto_sock_ops nni_rep_sock_ops = {
	.sock_init	= nni_rep_sock_init,
	.sock_fini	= nni_rep_sock_fini,
	.sock_close	= NULL,
	.sock_setopt	= nni_rep_sock_setopt,
	.sock_getopt	= nni_rep_sock_getopt,
	.sock_rfilter	= nni_rep_sock_rfilter,
	.sock_sfilter	= nni_rep_sock_sfilter,
	.sock_send	= nni_rep_sock_send,
	.sock_recv	= NULL,
};

nni_proto nni_rep_proto = {
	.proto_self	= NNG_PROTO_REP,
	.proto_peer	= NNG_PROTO_REQ,
	.proto_name	= "rep",
	.proto_sock_ops = &nni_rep_sock_ops,
	.proto_pipe_ops = &nni_rep_pipe_ops,
};