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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//
// 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 req0_pipe req0_pipe;
typedef struct req0_sock req0_sock;
typedef struct req0_ctx  req0_ctx;
 
static void req0_run_send_queue(req0_sock *, nni_list *);
static void req0_ctx_reset(req0_ctx *);
static void req0_ctx_timeout(void *);
static void req0_pipe_fini(void *);
static void req0_ctx_fini(void *);
static int  req0_ctx_init(void *, void *);
 
// A req0_ctx is a "context" for the request.  It uses most of the
// socket, but keeps track of its own outstanding replays, the request ID,
// and so forth.
struct req0_ctx {
    req0_sock *    sock;
    nni_list_node  sock_node;  // node on the socket context list
    nni_list_node  send_node;  // node on the send_queue
    nni_list_node  pipe_node;  // node on the pipe list
    uint32_t       request_id; // request ID, without high bit set
    nni_aio *      recv_aio;   // user aio waiting to recv - only one!
    nni_aio *      send_aio;   // user aio waiting to send
    nng_msg *      req_msg;    // request message (owned by protocol)
    size_t         req_len;    // length of request message (for stats)
    nng_msg *      rep_msg;    // reply message
    nni_timer_node timer;
    nni_duration   retry;
    bool           conn_reset; // sent message w/o retry, peer disconnect
};
 
// A req0_sock is our per-socket protocol private structure.
struct req0_sock {
    nni_duration   retry;
    bool           closed;
    nni_atomic_int ttl;
    req0_ctx       master; // base socket master
    nni_list       ready_pipes;
    nni_list       busy_pipes;
    nni_list       stop_pipes;
    nni_list       contexts;
    nni_list       send_queue; // contexts waiting to send.
    nni_id_map     requests;   // contexts by request ID
    nni_pollable   readable;
    nni_pollable   writable;
    nni_mtx        mtx;
};
 
// A req0_pipe is our per-pipe protocol private structure.
struct req0_pipe {
    nni_pipe *    pipe;
    req0_sock *   req;
    nni_list_node node;
    nni_list      contexts; // contexts with pending traffic
    bool          closed;
    nni_aio       aio_send;
    nni_aio       aio_recv;
};
 
static void req0_sock_fini(void *);
static void req0_send_cb(void *);
static void req0_recv_cb(void *);
 
static int
req0_sock_init(void *arg, nni_sock *sock)
{
    req0_sock *s = arg;
 
    NNI_ARG_UNUSED(sock);
 
    // Request IDs are 32 bits, with the high order bit set.
    // We start at a random point, to minimize likelihood of
    // accidental collision across restarts.
    nni_id_map_init(&s->requests, 0x80000000u, 0xffffffffu, true);
 
    nni_mtx_init(&s->mtx);
 
    NNI_LIST_INIT(&s->ready_pipes, req0_pipe, node);
    NNI_LIST_INIT(&s->busy_pipes, req0_pipe, node);
    NNI_LIST_INIT(&s->stop_pipes, req0_pipe, node);
    NNI_LIST_INIT(&s->send_queue, req0_ctx, send_node);
    NNI_LIST_INIT(&s->contexts, req0_ctx, sock_node);
 
    // this is "semi random" start for request IDs.
    s->retry = NNI_SECOND * 60;
 
    (void) req0_ctx_init(&s->master, s);
 
    nni_pollable_init(&s->writable);
    nni_pollable_init(&s->readable);
 
    nni_atomic_init(&s->ttl);
    nni_atomic_set(&s->ttl, 8);
    return (0);
}
 
static void
req0_sock_open(void *arg)
{
    NNI_ARG_UNUSED(arg);
}
 
static void
req0_sock_close(void *arg)
{
    req0_sock *s = arg;
 
    nni_mtx_lock(&s->mtx);
    s->closed = true;
    nni_mtx_unlock(&s->mtx);
}
 
static void
req0_sock_fini(void *arg)
{
    req0_sock *s = arg;
 
    nni_mtx_lock(&s->mtx);
    NNI_ASSERT(nni_list_empty(&s->busy_pipes));
    NNI_ASSERT(nni_list_empty(&s->stop_pipes));
    NNI_ASSERT(nni_list_empty(&s->ready_pipes));
    nni_mtx_unlock(&s->mtx);
 
    req0_ctx_fini(&s->master);
    nni_pollable_fini(&s->readable);
    nni_pollable_fini(&s->writable);
    nni_id_map_fini(&s->requests);
    nni_mtx_fini(&s->mtx);
}
 
static void
req0_pipe_stop(void *arg)
{
    req0_pipe *p = arg;
    req0_sock *s = p->req;
 
    nni_aio_stop(&p->aio_recv);
    nni_aio_stop(&p->aio_send);
    nni_mtx_lock(&s->mtx);
    nni_list_node_remove(&p->node);
    nni_mtx_unlock(&s->mtx);
}
 
static void
req0_pipe_fini(void *arg)
{
    req0_pipe *p = arg;
 
    nni_aio_fini(&p->aio_recv);
    nni_aio_fini(&p->aio_send);
}
 
static int
req0_pipe_init(void *arg, nni_pipe *pipe, void *s)
{
    req0_pipe *p = arg;
 
    nni_aio_init(&p->aio_recv, req0_recv_cb, p);
    nni_aio_init(&p->aio_send, req0_send_cb, p);
    NNI_LIST_NODE_INIT(&p->node);
    NNI_LIST_INIT(&p->contexts, req0_ctx, pipe_node);
    p->pipe = pipe;
    p->req  = s;
    return (0);
}
 
static int
req0_pipe_start(void *arg)
{
    req0_pipe *p = arg;
    req0_sock *s = p->req;
 
    if (nni_pipe_peer(p->pipe) != NNG_REQ0_PEER) {
        return (NNG_EPROTO);
    }
 
    nni_mtx_lock(&s->mtx);
    nni_list_append(&s->ready_pipes, p);
    nni_pollable_raise(&s->writable);
    req0_run_send_queue(s, NULL);
    nni_mtx_unlock(&s->mtx);
 
    nni_pipe_recv(p->pipe, &p->aio_recv);
    return (0);
}
 
static void
req0_pipe_close(void *arg)
{
    req0_pipe *p = arg;
    req0_sock *s = p->req;
    req0_ctx * ctx;
 
    nni_aio_close(&p->aio_recv);
    nni_aio_close(&p->aio_send);
 
    nni_mtx_lock(&s->mtx);
    // This removes the node from either busy_pipes or ready_pipes.
    // It doesn't much matter which.  We stick the pipe on the stop
    // list, so that we can wait for that to close down safely.
    p->closed = true;
    nni_list_node_remove(&p->node);
    nni_list_append(&s->stop_pipes, p);
    if (nni_list_empty(&s->ready_pipes)) {
        nni_pollable_clear(&s->writable);
    }
 
    while ((ctx = nni_list_first(&p->contexts)) != NULL) {
        nni_list_remove(&p->contexts, ctx);
        nng_aio *aio;
        if (ctx->retry <= 0) {
            // If we can't retry, then just cancel the operation
            // altogether.  We should only be waiting for recv,
            // because we will already have sent if we are here.
            if ((aio = ctx->recv_aio) != NULL) {
                ctx->recv_aio = NULL;
                nni_aio_finish_error(aio, NNG_ECONNRESET);
                                req0_ctx_reset(ctx);
            } else {
                req0_ctx_reset(ctx);
                ctx->conn_reset = true;
            }
        } else {
            // Reset the timer on this so it expires immediately.
            // This is actually easier than canceling the timer and
            // running the send_queue separately.  (In particular,
            // it avoids a potential deadlock on cancelling the
            // timer.)
            nni_timer_schedule(&ctx->timer, NNI_TIME_ZERO);
        }
    }
    nni_mtx_unlock(&s->mtx);
}
 
// For cooked mode, we use a context, and send out that way.  This
// completely bypasses the upper write queue.  Each context keeps one
// message pending; these are "scheduled" via the send_queue.  The send_queue
// is ordered, so FIFO ordering between contexts is provided for.
 
static void
req0_send_cb(void *arg)
{
    req0_pipe *p = arg;
    req0_sock *s = p->req;
    nni_aio *  aio;
    nni_list   sent_list;
 
    nni_aio_list_init(&sent_list);
    if (nni_aio_result(&p->aio_send) != 0) {
        // We failed to send... clean up and deal with it.
        nni_msg_free(nni_aio_get_msg(&p->aio_send));
        nni_aio_set_msg(&p->aio_send, NULL);
        nni_pipe_close(p->pipe);
        return;
    }
 
    // We completed a cooked send, so we need to reinsert ourselves
    // in the ready list, and re-run the send_queue.
 
    nni_mtx_lock(&s->mtx);
    if (p->closed || s->closed) {
        // This occurs if the req0_pipe_close has been called.
        // In that case we don't want any more processing.
        nni_mtx_unlock(&s->mtx);
        return;
    }
    nni_list_remove(&s->busy_pipes, p);
    nni_list_append(&s->ready_pipes, p);
    if (nni_list_empty(&s->send_queue)) {
        nni_pollable_raise(&s->writable);
    }
    req0_run_send_queue(s, &sent_list);
    nni_mtx_unlock(&s->mtx);
 
    while ((aio = nni_list_first(&sent_list)) != NULL) {
        nni_list_remove(&sent_list, aio);
        nni_aio_finish_sync(aio, 0, 0);
    }
}
 
static void
req0_recv_cb(void *arg)
{
    req0_pipe *p = arg;
    req0_sock *s = p->req;
    req0_ctx * ctx;
    nni_msg *  msg;
    nni_aio *  aio;
    uint32_t   id;
 
    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));
 
    // We yank 4 bytes from front of body, and move them to the header.
    if (nni_msg_len(msg) < 4) {
        // Malformed message.
        goto malformed;
    }
    id = nni_msg_trim_u32(msg);
 
    // Schedule another receive while we are processing this.
    nni_mtx_lock(&s->mtx);
 
    // NB: If close was called, then this will just abort.
    nni_pipe_recv(p->pipe, &p->aio_recv);
 
    // Look for a context to receive it.
    if (((ctx = nni_id_get(&s->requests, id)) == NULL) ||
        (ctx->send_aio != NULL) || (ctx->rep_msg != NULL)) {
        nni_mtx_unlock(&s->mtx);
        // No waiting context, we have not sent the request out to
        // the wire yet, or context already has a reply ready.
        // Discard the message.
        nni_msg_free(msg);
        return;
    }
 
    // We have our match, so we can remove this.
    nni_list_node_remove(&ctx->send_node);
    nni_id_remove(&s->requests, id);
    ctx->request_id = 0;
    if (ctx->req_msg != NULL) {
        nni_msg_free(ctx->req_msg);
        ctx->req_msg = NULL;
    }
 
    // Is there an aio waiting for us?
    if ((aio = ctx->recv_aio) != NULL) {
        ctx->recv_aio = NULL;
        nni_mtx_unlock(&s->mtx);
        nni_aio_set_msg(aio, msg);
        nni_aio_finish_sync(aio, 0, nni_msg_len(msg));
    } else {
        // No AIO, so stash msg.  Receive will pick it up later.
        ctx->rep_msg = msg;
        if (ctx == &s->master) {
            nni_pollable_raise(&s->readable);
        }
        nni_mtx_unlock(&s->mtx);
    }
    return;
 
malformed:
    nni_msg_free(msg);
    nni_pipe_close(p->pipe);
}
 
static void
req0_ctx_timeout(void *arg)
{
    req0_ctx * ctx = arg;
    req0_sock *s   = ctx->sock;
 
    nni_mtx_lock(&s->mtx);
    if ((ctx->req_msg != NULL) && (!s->closed)) {
        if (!nni_list_node_active(&ctx->send_node)) {
            nni_list_append(&s->send_queue, ctx);
        }
        req0_run_send_queue(s, NULL);
    }
    nni_mtx_unlock(&s->mtx);
}
 
static int
req0_ctx_init(void *arg, void *sock)
{
    req0_sock *s   = sock;
    req0_ctx * ctx = arg;
 
    nni_timer_init(&ctx->timer, req0_ctx_timeout, ctx);
 
    nni_mtx_lock(&s->mtx);
    ctx->sock     = s;
    ctx->recv_aio = NULL;
    ctx->retry    = s->retry;
    nni_list_append(&s->contexts, ctx);
    nni_mtx_unlock(&s->mtx);
 
    return (0);
}
 
static void
req0_ctx_fini(void *arg)
{
    req0_ctx * ctx = arg;
    req0_sock *s   = ctx->sock;
    nni_aio *  aio;
 
    nni_mtx_lock(&s->mtx);
    if ((aio = ctx->recv_aio) != NULL) {
        ctx->recv_aio = NULL;
        nni_aio_finish_error(aio, NNG_ECLOSED);
    }
    if ((aio = ctx->send_aio) != NULL) {
        ctx->send_aio = NULL;
        nni_aio_set_msg(aio, ctx->req_msg);
        ctx->req_msg = NULL;
        nni_aio_finish_error(aio, NNG_ECLOSED);
    }
    req0_ctx_reset(ctx);
    nni_list_remove(&s->contexts, ctx);
    nni_mtx_unlock(&s->mtx);
 
    nni_timer_cancel(&ctx->timer);
    nni_timer_fini(&ctx->timer);
}
 
static int
req0_ctx_set_resend_time(void *arg, const void *buf, size_t sz, nni_opt_type t)
{
    req0_ctx *ctx = arg;
    return (nni_copyin_ms(&ctx->retry, buf, sz, t));
}
 
static int
req0_ctx_get_resend_time(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    req0_ctx *ctx = arg;
    return (nni_copyout_ms(ctx->retry, buf, szp, t));
}
 
static void
req0_run_send_queue(req0_sock *s, nni_list *sent_list)
{
    req0_ctx *ctx;
    nni_aio * aio;
 
    // Note: This routine should be called with the socket lock held.
    while ((ctx = nni_list_first(&s->send_queue)) != NULL) {
        req0_pipe *p;
 
        if ((p = nni_list_first(&s->ready_pipes)) == NULL) {
            return;
        }
 
        // We have a place to send it, so do the send.
        // If a sending error occurs that causes the message to
        // be dropped, we rely on the resend timer to pick it up.
        // We also notify the completion callback if this is the
        // first send attempt.
        nni_list_remove(&s->send_queue, ctx);
 
        // Schedule a resubmit timer.  We only do this if we got
        // a pipe to send to.  Otherwise, we should get handled
        // the next time that the send_queue is run.  We don't do this
        // if the retry is "disabled" with NNG_DURATION_INFINITE.
        if (ctx->retry > 0) {
            nni_timer_schedule(
                &ctx->timer, nni_clock() + ctx->retry);
        }
 
        // Put us on the pipe list of active contexts.
        // This gives the pipe a chance to kick a resubmit
        // if the pipe is removed.
        nni_list_node_remove(&ctx->pipe_node);
        nni_list_append(&p->contexts, ctx);
 
        nni_list_remove(&s->ready_pipes, p);
        nni_list_append(&s->busy_pipes, p);
        if (nni_list_empty(&s->ready_pipes)) {
            nni_pollable_clear(&s->writable);
        }
 
        if ((aio = ctx->send_aio) != NULL) {
            ctx->send_aio = NULL;
            nni_aio_bump_count(aio, ctx->req_len);
            // If the list was passed in, we want to do a
            // synchronous completion later.
            if (sent_list != NULL) {
                nni_list_append(sent_list, aio);
            } else {
                nni_aio_finish(aio, 0, 0);
            }
        }
 
        // At this point, we will never give this message back to
        // to the user, so we don't have to worry about making it
        // unique.  We can freely clone it.
        nni_msg_clone(ctx->req_msg);
        nni_aio_set_msg(&p->aio_send, ctx->req_msg);
        nni_pipe_send(p->pipe, &p->aio_send);
    }
}
 
void
req0_ctx_reset(req0_ctx *ctx)
{
    req0_sock *s = ctx->sock;
    // Call with sock lock held!
 
    // We cannot safely "wait" using nni_timer_cancel, but this removes
    // any scheduled timer activation.  If the timeout is already running
    // concurrently, it will still run.  It should do nothing, because
    // we toss the request.  There is still a very narrow race if the
    // timeout fires, but doesn't actually start running before we
    // both finish this function, *and* manage to reschedule another
    // request.  The consequence of that occurring is that the request
    // will be emitted on the wire twice.  This is not actually tragic.
    nni_timer_schedule(&ctx->timer, NNI_TIME_NEVER);
 
    nni_list_node_remove(&ctx->pipe_node);
    nni_list_node_remove(&ctx->send_node);
    if (ctx->request_id != 0) {
        nni_id_remove(&s->requests, ctx->request_id);
        ctx->request_id = 0;
    }
    if (ctx->req_msg != NULL) {
        nni_msg_free(ctx->req_msg);
        ctx->req_msg = NULL;
    }
    if (ctx->rep_msg != NULL) {
        nni_msg_free(ctx->rep_msg);
        ctx->rep_msg = NULL;
    }
    ctx->conn_reset = false;
}
 
static void
req0_ctx_cancel_recv(nni_aio *aio, void *arg, int rv)
{
    req0_ctx * ctx = arg;
    req0_sock *s   = ctx->sock;
 
    nni_mtx_lock(&s->mtx);
    if (ctx->recv_aio == aio) {
        ctx->recv_aio = NULL;
 
        // Cancellation of a pending receive is treated as aborting the
        // entire state machine.  This allows us to preserve the
        // semantic of exactly one receive operation per send
        // operation, and should be the least surprising for users. The
        // main consequence is that if a receive operation is completed
        // (in error or otherwise), the user must submit a new send
        // operation to restart the state machine.
        req0_ctx_reset(ctx);
 
        nni_aio_finish_error(aio, rv);
    }
    nni_mtx_unlock(&s->mtx);
}
 
static void
req0_ctx_recv(void *arg, nni_aio *aio)
{
    req0_ctx * ctx = arg;
    req0_sock *s   = ctx->sock;
    nni_msg *  msg;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    nni_mtx_lock(&s->mtx);
    if ((ctx->recv_aio != NULL) ||
        ((ctx->req_msg == NULL) && (ctx->rep_msg == NULL))) {
        // We have already got a pending receive or have not
        // tried to send a request yet.
        // Either of these violate our basic state assumptions.
        int rv;
        if (ctx->conn_reset) {
            ctx->conn_reset = false;
            rv              = NNG_ECONNRESET;
        } else {
            rv = NNG_ESTATE;
        }
        nni_mtx_unlock(&s->mtx);
        nni_aio_finish_error(aio, rv);
        return;
    }
 
    if ((msg = ctx->rep_msg) == NULL) {
        int rv;
        rv = nni_aio_schedule(aio, req0_ctx_cancel_recv, ctx);
        if (rv != 0) {
            nni_mtx_unlock(&s->mtx);
            nni_aio_finish_error(aio, rv);
            return;
        }
        ctx->recv_aio = aio;
        nni_mtx_unlock(&s->mtx);
        return;
    }
 
    ctx->rep_msg = NULL;
 
    // We have got a message to pass up, yay!
    nni_aio_set_msg(aio, msg);
    if (ctx == &s->master) {
        nni_pollable_clear(&s->readable);
    }
    nni_mtx_unlock(&s->mtx);
    nni_aio_finish(aio, 0, nni_msg_len(msg));
}
 
static void
req0_ctx_cancel_send(nni_aio *aio, void *arg, int rv)
{
    req0_ctx * ctx = arg;
    req0_sock *s   = ctx->sock;
 
    nni_mtx_lock(&s->mtx);
    if (ctx->send_aio == aio) {
        // There should not be a pending reply, because we canceled
        // it while we were waiting.
        NNI_ASSERT(ctx->recv_aio == NULL);
        ctx->send_aio = NULL;
        // Restore the message back to the aio.
        nni_aio_set_msg(aio, ctx->req_msg);
        nni_msg_header_clear(ctx->req_msg);
        ctx->req_msg = NULL;
 
        // Cancellation of a pending receive is treated as aborting the
        // entire state machine.  This allows us to preserve the
        // semantic of exactly one receive operation per send
        // operation, and should be the least surprising for users. The
        // main consequence is that if a receive operation is completed
        // (in error or otherwise), the user must submit a new send
        // operation to restart the state machine.
        req0_ctx_reset(ctx);
 
        nni_aio_finish_error(aio, rv);
    }
    nni_mtx_unlock(&s->mtx);
}
 
static void
req0_ctx_send(void *arg, nni_aio *aio)
{
    req0_ctx * ctx = arg;
    req0_sock *s   = ctx->sock;
    nng_msg *  msg = nni_aio_get_msg(aio);
    int        rv;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    nni_mtx_lock(&s->mtx);
    if (s->closed) {
        nni_mtx_unlock(&s->mtx);
        nni_aio_finish_error(aio, NNG_ECLOSED);
        return;
    }
    // Sending a new request cancels the old one, including any
    // outstanding reply.
    if (ctx->recv_aio != NULL) {
        nni_aio_finish_error(ctx->recv_aio, NNG_ECANCELED);
        ctx->recv_aio = NULL;
    }
    if (ctx->send_aio != NULL) {
        nni_aio_set_msg(ctx->send_aio, ctx->req_msg);
        nni_msg_header_clear(ctx->req_msg);
        ctx->req_msg = NULL;
        nni_aio_finish_error(ctx->send_aio, NNG_ECANCELED);
        ctx->send_aio = NULL;
        nni_list_remove(&s->send_queue, ctx);
    }
 
    // This resets the entire state machine.
    req0_ctx_reset(ctx);
 
    // Insert us on the per ID hash list, so that receives can find us.
    if ((rv = nni_id_alloc(&s->requests, &ctx->request_id, ctx)) != 0) {
        nni_mtx_unlock(&s->mtx);
        nni_aio_finish_error(aio, rv);
        return;
    }
    nni_msg_header_clear(msg);
    nni_msg_header_append_u32(msg, ctx->request_id);
 
    // If no pipes are ready, and the request was a poll (no background
    // schedule), then fail it.  Should be NNG_ETIMEDOUT.
    rv = nni_aio_schedule(aio, req0_ctx_cancel_send, ctx);
    if ((rv != 0) && (nni_list_empty(&s->ready_pipes))) {
        nni_id_remove(&s->requests, ctx->request_id);
        nni_mtx_unlock(&s->mtx);
        nni_aio_finish_error(aio, rv);
        return;
    }
    ctx->req_len  = nni_msg_len(msg);
    ctx->req_msg  = msg;
    ctx->send_aio = aio;
    nni_aio_set_msg(aio, NULL);
 
    // Stick us on the send_queue list.
    nni_list_append(&s->send_queue, ctx);
 
    req0_run_send_queue(s, NULL);
    nni_mtx_unlock(&s->mtx);
}
 
static void
req0_sock_send(void *arg, nni_aio *aio)
{
    req0_sock *s = arg;
    req0_ctx_send(&s->master, aio);
}
 
static void
req0_sock_recv(void *arg, nni_aio *aio)
{
    req0_sock *s = arg;
    req0_ctx_recv(&s->master, aio);
}
 
static int
req0_sock_set_max_ttl(void *arg, const void *buf, size_t sz, nni_opt_type t)
{
    req0_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
req0_sock_get_max_ttl(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    req0_sock *s = arg;
    return (nni_copyout_int(nni_atomic_get(&s->ttl), buf, szp, t));
}
 
static int
req0_sock_set_resend_time(
    void *arg, const void *buf, size_t sz, nni_opt_type t)
{
    req0_sock *s = arg;
    int        rv;
    rv       = req0_ctx_set_resend_time(&s->master, buf, sz, t);
    s->retry = s->master.retry;
    return (rv);
}
 
static int
req0_sock_get_resend_time(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    req0_sock *s = arg;
    return (req0_ctx_get_resend_time(&s->master, buf, szp, t));
}
 
static int
req0_sock_get_send_fd(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    req0_sock *s = arg;
    int        rv;
    int        fd;
 
    if ((rv = nni_pollable_getfd(&s->writable, &fd)) != 0) {
        return (rv);
    }
    return (nni_copyout_int(fd, buf, szp, t));
}
 
static int
req0_sock_get_recv_fd(void *arg, void *buf, size_t *szp, nni_opt_type t)
{
    req0_sock *s = arg;
    int        rv;
    int        fd;
 
    if ((rv = nni_pollable_getfd(&s->readable, &fd)) != 0) {
        return (rv);
    }
 
    return (nni_copyout_int(fd, buf, szp, t));
}
 
static nni_proto_pipe_ops req0_pipe_ops = {
    .pipe_size  = sizeof(req0_pipe),
    .pipe_init  = req0_pipe_init,
    .pipe_fini  = req0_pipe_fini,
    .pipe_start = req0_pipe_start,
    .pipe_close = req0_pipe_close,
    .pipe_stop  = req0_pipe_stop,
};
 
static nni_option req0_ctx_options[] = {
    {
        .o_name = NNG_OPT_REQ_RESENDTIME,
        .o_get  = req0_ctx_get_resend_time,
        .o_set  = req0_ctx_set_resend_time,
    },
    {
        .o_name = NULL,
    },
};
 
static nni_proto_ctx_ops req0_ctx_ops = {
    .ctx_size    = sizeof(req0_ctx),
    .ctx_init    = req0_ctx_init,
    .ctx_fini    = req0_ctx_fini,
    .ctx_recv    = req0_ctx_recv,
    .ctx_send    = req0_ctx_send,
    .ctx_options = req0_ctx_options,
};
 
static nni_option req0_sock_options[] = {
    {
        .o_name = NNG_OPT_MAXTTL,
        .o_get  = req0_sock_get_max_ttl,
        .o_set  = req0_sock_set_max_ttl,
    },
    {
        .o_name = NNG_OPT_REQ_RESENDTIME,
        .o_get  = req0_sock_get_resend_time,
        .o_set  = req0_sock_set_resend_time,
    },
    {
        .o_name = NNG_OPT_RECVFD,
        .o_get  = req0_sock_get_recv_fd,
    },
    {
        .o_name = NNG_OPT_SENDFD,
        .o_get  = req0_sock_get_send_fd,
    },
    // terminate list
    {
        .o_name = NULL,
    },
};
 
static nni_proto_sock_ops req0_sock_ops = {
    .sock_size    = sizeof(req0_sock),
    .sock_init    = req0_sock_init,
    .sock_fini    = req0_sock_fini,
    .sock_open    = req0_sock_open,
    .sock_close   = req0_sock_close,
    .sock_options = req0_sock_options,
    .sock_send    = req0_sock_send,
    .sock_recv    = req0_sock_recv,
};
 
static nni_proto req0_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,
    .proto_sock_ops = &req0_sock_ops,
    .proto_pipe_ops = &req0_pipe_ops,
    .proto_ctx_ops  = &req0_ctx_ops,
};
 
int
nng_req0_open(nng_socket *sock)
{
    return (nni_proto_open(sock, &req0_proto));
}