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
//
// 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.
//
 
#define TEST_NO_MAIN
 
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
 
typedef struct {
    uint8_t *   base;
    size_t      rem;
    nng_iov     iov;
    nng_aio *   upper_aio;
    nng_aio *   lower_aio;
    nng_stream *s;
    void (*submit)(nng_stream *, nng_aio *);
} stream_xfr_t;
 
static void
stream_xfr_free(stream_xfr_t *x)
{
    if (x == NULL) {
        return;
    }
    if (x->upper_aio != NULL) {
        nng_aio_free(x->upper_aio);
    }
    if (x->lower_aio != NULL) {
        nng_aio_free(x->lower_aio);
    }
    nng_free(x, sizeof(*x));
}
 
static void
stream_xfr_start(stream_xfr_t *x)
{
    nng_iov iov;
    iov.iov_buf = x->base;
    iov.iov_len = x->rem;
 
    nng_aio_set_iov(x->lower_aio, 1, &iov);
    x->submit(x->s, x->lower_aio);
}
 
static void
stream_xfr_cb(void *arg)
{
    stream_xfr_t *x = arg;
    int           rv;
    size_t        n;
 
    rv = nng_aio_result(x->lower_aio);
    if (rv != 0) {
        nng_aio_finish(x->upper_aio, rv);
        return;
    }
    n = nng_aio_count(x->lower_aio);
 
    x->rem -= n;
    x->base += n;
 
    if (x->rem == 0) {
        nng_aio_finish(x->upper_aio, 0);
        return;
    }
 
    stream_xfr_start(x);
}
 
static stream_xfr_t *
stream_xfr_alloc(nng_stream *s, void (*submit)(nng_stream *, nng_aio *),
    void *buf, size_t size)
{
    stream_xfr_t *x;
 
    if ((x = nng_alloc(size)) == NULL) {
        return (NULL);
    }
    if (nng_aio_alloc(&x->upper_aio, NULL, NULL) != 0) {
        stream_xfr_free(x);
        return (NULL);
    }
    if (nng_aio_alloc(&x->lower_aio, stream_xfr_cb, x) != 0) {
        stream_xfr_free(x);
        return (NULL);
    }
 
    // Upper should not take more than 30 seconds, lower not more than 5.
    nng_aio_set_timeout(x->upper_aio, 30000);
    nng_aio_set_timeout(x->lower_aio, 5000);
 
    nng_aio_begin(x->upper_aio);
 
    x->s      = s;
    x->rem    = size;
    x->base   = buf;
    x->submit = submit;
 
    return (x);
}
 
int
nuts_stream_wait(stream_xfr_t *x)
{
    int rv;
    if (x == NULL) {
        return (NNG_ENOMEM);
    }
    nng_aio_wait(x->upper_aio);
    rv = nng_aio_result(x->upper_aio);
    stream_xfr_free(x);
    return (rv);
}
 
void *
nuts_stream_recv_start(nng_stream *s, void *buf, size_t size)
{
    stream_xfr_t *x;
 
    x = stream_xfr_alloc(s, nng_stream_recv, buf, size);
    if (x == NULL) {
        return (x);
    }
    stream_xfr_start(x);
    return (x);
}
 
void *
nuts_stream_send_start(nng_stream *s, void *buf, size_t size)
{
    stream_xfr_t *x;
 
    x = stream_xfr_alloc(s, nng_stream_send, buf, size);
    if (x == NULL) {
        return (x);
    }
    stream_xfr_start(x);
    return (x);
}