aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/reqrep/req.c
blob: 9cfc6f54d9b82e64ed38c45cc31d131e705b9c6b (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
421
422
423
424
425
426
427
428
//
// Copyright 2016 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"

// Request protocol.  The REQ protocol is the "request" side of a
// request-reply pair.  This is useful for bulding RPC clients, for
// example.

typedef struct nni_req_pipe	nni_req_pipe;
typedef struct nni_req_sock	nni_req_sock;

// An nni_req_sock is our per-socket protocol private structure.
struct nni_req_sock {
	nni_sock *	sock;
	nni_mtx		mx;
	nni_cv		cv;
	nni_msgq *	uwq;
	nni_msgq *	urq;
	nni_duration	retry;
	nni_time	resend;
	nni_thr		resender;
	int		raw;
	int		closing;
	nni_msg *	reqmsg;
	uint32_t	nextid;         // next id
	uint8_t		reqid[4];       // outstanding request ID (big endian)
};

// An nni_req_pipe is our per-pipe protocol private structure.
struct nni_req_pipe {
	nni_pipe *	pipe;
	nni_req_sock *	req;
	int		sigclose;
	nni_list_node	node;
};

static void nni_req_resender(void *);

static int
nni_req_init(void **reqp, nni_sock *sock)
{
	nni_req_sock *req;
	int rv;

	if ((req = NNI_ALLOC_STRUCT(req)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_mtx_init(&req->mx)) != 0) {
		NNI_FREE_STRUCT(req);
		return (rv);
	}
	if ((rv = nni_cv_init(&req->cv, &req->mx)) != 0) {
		nni_mtx_fini(&req->mx);
		NNI_FREE_STRUCT(req);
		return (rv);
	}
	// this is "semi random" start for request IDs.
	req->nextid = (nni_clock() >> 32) ^ (nni_clock() & 0xffffffff);
	req->retry = NNI_SECOND * 60;
	req->sock = sock;
	req->reqmsg = NULL;
	req->raw = 0;
	req->resend = NNI_TIME_ZERO;

	req->uwq = nni_sock_sendq(sock);
	req->urq = nni_sock_recvq(sock);
	*reqp = req;
	nni_sock_recverr(sock, NNG_ESTATE);
	rv = nni_thr_init(&req->resender, nni_req_resender, req);
	if (rv != 0) {
		nni_cv_fini(&req->cv);
		nni_mtx_fini(&req->mx);
		NNI_FREE_STRUCT(req);
		return (rv);
	}
	nni_thr_run(&req->resender);
	return (0);
}


static void
nni_req_fini(void *arg)
{
	nni_req_sock *req = arg;

	// Shut down the resender.  We request it to exit by clearing
	// its old value, then kick it.
	nni_mtx_lock(&req->mx);
	req->closing = 1;
	nni_cv_wake(&req->cv);
	nni_mtx_unlock(&req->mx);

	nni_thr_fini(&req->resender);
	nni_cv_fini(&req->cv);
	nni_mtx_fini(&req->mx);
	NNI_FREE_STRUCT(req);
}


static int
nni_req_pipe_init(void **rpp, nni_pipe *pipe, void *rsock)
{
	nni_req_pipe *rp;

	if ((rp = NNI_ALLOC_STRUCT(rp)) == NULL) {
		return (NNG_ENOMEM);
	}
	rp->pipe = pipe;
	rp->sigclose = 0;
	rp->req = rsock;
	*rpp = rp;
	return (0);
}


static void
nni_req_pipe_fini(void *arg)
{
	nni_req_pipe *rp = arg;

	NNI_FREE_STRUCT(rp);
}


static int
nni_req_pipe_add(void *arg)
{
	// We have nothing to do, since we don't need to maintain a global
	// list of related pipes.
	NNI_ARG_UNUSED(arg);
	return (0);
}


static void
nni_req_pipe_rem(void *arg)
{
	// As with add, nothing to do here.
	NNI_ARG_UNUSED(arg);
}


static void
nni_req_pipe_send(void *arg)
{
	nni_req_pipe *rp = arg;
	nni_req_sock *req = rp->req;
	nni_msgq *uwq = req->uwq;
	nni_msgq *urq = req->urq;
	nni_pipe *pipe = rp->pipe;
	nni_msg *msg;
	int rv;

	for (;;) {
		rv = nni_msgq_get_sig(uwq, &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_req_pipe_recv(void *arg)
{
	nni_req_pipe *rp = arg;
	nni_req_sock *req = rp->req;
	nni_msgq *urq = req->urq;
	nni_msgq *uwq = req->uwq;
	nni_pipe *pipe = rp->pipe;
	nni_msg *msg;
	int rv;

	for (;;) {
		size_t len;
		char *body;
		rv = nni_pipe_recv(pipe, &msg);
		if (rv != 0) {
			break;
		}
		// We yank 4 bytes of body, and move them to the header.
		body = nni_msg_body(msg, &len);
		if (len < 4) {
			// Not enough data, just toss it.
			nni_msg_free(msg);
			continue;
		}
		if (nni_msg_append_header(msg, body, 4) != 0) {
			// Should be NNG_ENOMEM
			nni_msg_free(msg);
			continue;
		}
		if (nni_msg_trim(msg, 4) != 0) {
			// This should never happen - could be an assert.
			nni_panic("Failed to trim REQ header from body");
		}
		rv = nni_msgq_put_sig(urq, msg, &rp->sigclose);
		if (rv != 0) {
			nni_msg_free(msg);
			break;
		}
	}
	nni_msgq_signal(uwq, &rp->sigclose);
	nni_pipe_close(pipe);
}


static int
nni_req_setopt(void *arg, int opt, const void *buf, size_t sz)
{
	nni_req_sock *req = arg;
	int rv;

	switch (opt) {
	case NNG_OPT_RESENDTIME:
		nni_mtx_lock(&req->mx);
		rv = nni_setopt_duration(&req->retry, buf, sz);
		nni_mtx_unlock(&req->mx);
		break;
	case NNG_OPT_RAW:
		nni_mtx_lock(&req->mx);
		rv = nni_setopt_int(&req->raw, buf, sz, 0, 1);
		nni_mtx_unlock(&req->mx);
		break;
	default:
		rv = NNG_ENOTSUP;
	}
	return (rv);
}


static int
nni_req_getopt(void *arg, int opt, void *buf, size_t *szp)
{
	nni_req_sock *req = arg;
	int rv;

	switch (opt) {
	case NNG_OPT_RESENDTIME:
		nni_mtx_lock(&req->mx);
		rv = nni_getopt_duration(&req->retry, buf, szp);
		nni_mtx_unlock(&req->mx);
		break;
	case NNG_OPT_RAW:
		nni_mtx_lock(&req->mx);
		rv = nni_getopt_int(&req->raw, buf, szp);
		nni_mtx_unlock(&req->mx);
		break;
	default:
		rv = NNG_ENOTSUP;
	}
	return (rv);
}


static void
nni_req_resender(void *arg)
{
	nni_req_sock *req = arg;
	int rv;

	for (;;) {
		nni_mtx_lock(&req->mx);
		if (req->closing) {
			nni_mtx_unlock(&req->mx);
			return;
		}
		if (req->reqmsg == NULL) {
			nni_cv_wait(&req->cv);
			nni_mtx_unlock(&req->mx);
			continue;
		}
		rv = nni_cv_until(&req->cv, req->resend);
		if ((rv == NNG_ETIMEDOUT) && (req->reqmsg != NULL)) {
			nni_msg *dup;
			// XXX: check for final timeout on this?
			if (nni_msg_dup(&dup, req->reqmsg) != 0) {
				if (nni_msgq_putback(req->uwq, dup) != 0) {
					nni_msg_free(dup);
				}
			}
			req->resend = nni_clock() + req->retry;
		}
		nni_mtx_unlock(&req->mx);
	}
}


static nni_msg *
nni_req_sendfilter(void *arg, nni_msg *msg)
{
	nni_req_sock *req = arg;
	uint32_t id;

	nni_mtx_lock(&req->mx);
	if (req->raw) {
		// No automatic retry, and the request ID must
		// be in the header coming down.
		nni_mtx_unlock(&req->mx);
		return (msg);
	}

	// Generate a new request ID.  We always set the high
	// order bit so that the peer can locate the end of the
	// backtrace.  (Pipe IDs have the high order bit clear.)
	id = (req->nextid++) | 0x80000000u;

	// Request ID is in big endian format.
	req->reqid[0] = (uint8_t) (id >> 24);
	req->reqid[1] = (uint8_t) (id >> 16);
	req->reqid[2] = (uint8_t) (id >> 8);
	req->reqid[3] = (uint8_t) (id);

	if (nni_msg_append_header(msg, req->reqid, 4) != 0) {
		// Should be ENOMEM.
		nni_mtx_unlock(&req->mx);
		nni_msg_free(msg);
		return (NULL);
	}

	// If another message is there, this cancels it.
	if (req->reqmsg != NULL) {
		nni_msg_free(req->reqmsg);
		req->reqmsg = NULL;
	}

	// Make a duplicate message... for retries.
	if (nni_msg_dup(&req->reqmsg, msg) != 0) {
		nni_mtx_unlock(&req->mx);
		nni_msg_free(msg);
		return (NULL);
	}

	// Schedule the next retry
	req->resend = nni_clock() + req->retry;
	nni_cv_wake(&req->cv);

	// Clear the error condition.
	nni_sock_recverr(req->sock, 0);
	nni_mtx_unlock(&req->mx);

	return (msg);
}


static nni_msg *
nni_req_recvfilter(void *arg, nni_msg *msg)
{
	nni_req_sock *req = arg;
	char *header;
	size_t len;

	nni_mtx_lock(&req->mx);
	if (req->raw) {
		// Pass it unmolested
		nni_mtx_unlock(&req->mx);
		return (msg);
	}

	header = nni_msg_header(msg, &len);
	if (len < 4) {
		nni_mtx_unlock(&req->mx);
		nni_msg_free(msg);
		return (NULL);
	}

	if (req->reqmsg == NULL) {
		// We had no outstanding request.
		nni_mtx_unlock(&req->mx);
		nni_msg_free(msg);
		return (NULL);
	}
	if (memcmp(header, req->reqid, 4) != 0) {
		// Wrong request id
		nni_mtx_unlock(&req->mx);
		nni_msg_free(msg);
		return (NULL);
	}

	nni_sock_recverr(req->sock, NNG_ESTATE);
	nni_msg_free(req->reqmsg);
	req->reqmsg = NULL;
	nni_cv_wake(&req->cv);
	nni_mtx_unlock(&req->mx);
	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 nni_req_proto_pipe = {
	.pipe_init	= nni_req_pipe_init,
	.pipe_fini	= nni_req_pipe_fini,
	.pipe_add	= nni_req_pipe_add,
	.pipe_rem	= nni_req_pipe_rem,
	.pipe_send	= nni_req_pipe_send,
	.pipe_recv	= nni_req_pipe_recv,
};

nni_proto nni_req_proto = {
	.proto_self		= NNG_PROTO_REQ,
	.proto_peer		= NNG_PROTO_REP,
	.proto_name		= "req",
	.proto_pipe		= &nni_req_proto_pipe,
	.proto_init		= nni_req_init,
	.proto_fini		= nni_req_fini,
	.proto_setopt		= nni_req_setopt,
	.proto_getopt		= nni_req_getopt,
	.proto_recv_filter	= nni_req_recvfilter,
	.proto_send_filter	= nni_req_sendfilter,
};