zhangmeng
2021-12-10 5990bac28af438a914165441b3c33a370b320d16
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
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2019 Devolutions <info@devolutions.net>
//
// 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 "core/nng_impl.h"
 
#include "win_ipc.h"
 
#include <stdio.h>
 
typedef struct ipc_dialer {
    nng_stream_dialer sd;
    bool              closed; // dialers are locked by the worker lock
    nni_list          aios;
    nni_list_node     node; // node on worker list
    char *            path;
    nni_sockaddr      sa;
} ipc_dialer;
 
// Windows IPC is a bit different on the client side.  There is no
// support for asynchronous connection, but we can fake it with a
// single thread that runs to establish the connection.  That thread
// will run keep looping, sleeping for 10 ms between attempts.  It
// performs non-blocking attempts to connect.
typedef struct ipc_dial_work {
    nni_list waiters;
    nni_list workers;
    nni_mtx  mtx;
    nni_cv   cv;
    nni_thr  thr;
    int      exit;
} ipc_dial_work;
 
static ipc_dial_work ipc_connector;
 
static void
ipc_dial_thr(void *arg)
{
    ipc_dial_work *w = arg;
 
    nni_mtx_lock(&w->mtx);
    for (;;) {
        ipc_dialer *d;
 
        if (w->exit) {
            break;
        }
        while ((d = nni_list_first(&w->waiters)) != NULL) {
            nni_list_remove(&w->waiters, d);
            nni_list_append(&w->workers, d);
        }
 
        while ((d = nni_list_first(&w->workers)) != NULL) {
            nng_stream *c;
            nni_aio *   aio;
            HANDLE      f;
            int         rv;
 
            if ((aio = nni_list_first(&d->aios)) == NULL) {
                nni_list_remove(&w->workers, d);
                continue;
            }
 
            f = CreateFileA(d->path, GENERIC_READ | GENERIC_WRITE,
                0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED,
                NULL);
 
            if (f == INVALID_HANDLE_VALUE) {
                switch ((rv = GetLastError())) {
                case ERROR_PIPE_BUSY:
                    // Still in progress.  This
                    // shouldn't happen unless the
                    // other side aborts the
                    // connection.
                    // back at the head of the list
                    nni_list_remove(&w->workers, d);
                    nni_list_prepend(&w->waiters, d);
                    continue;
 
                case ERROR_FILE_NOT_FOUND:
                    rv = NNG_ECONNREFUSED;
                    break;
                default:
                    rv = nni_win_error(rv);
                    break;
                }
                nni_list_remove(&d->aios, aio);
                nni_aio_finish_error(aio, rv);
                continue;
            }
 
            nni_list_remove(&d->aios, aio);
 
            if (((rv = nni_win_io_register(f)) != 0) ||
                ((rv = nni_win_ipc_init(&c, f, &d->sa, true)) !=
                    0)) {
                DisconnectNamedPipe(f);
                CloseHandle(f);
                nni_aio_finish_error(aio, rv);
                continue;
            }
            nni_aio_set_output(aio, 0, c);
            nni_aio_finish(aio, 0, 0);
        }
 
        if (nni_list_empty(&w->waiters)) {
            // Wait until an endpoint is added.
            nni_cv_wait(&w->cv);
        } else {
            // Wait 10 ms, unless woken earlier.
            nni_cv_until(&w->cv, nni_clock() + 10);
        }
    }
    nni_mtx_unlock(&w->mtx);
}
 
static void
ipc_dial_cancel(nni_aio *aio, void *arg, int rv)
{
    ipc_dialer *   d = arg;
    ipc_dial_work *w = &ipc_connector;
 
    nni_mtx_lock(&w->mtx);
    if (nni_aio_list_active(aio)) {
        if (nni_list_active(&w->waiters, d)) {
            nni_list_remove(&w->waiters, d);
            nni_cv_wake(&w->cv);
        }
        nni_aio_list_remove(aio);
        nni_aio_finish_error(aio, rv);
    }
    nni_mtx_unlock(&w->mtx);
}
 
static void
ipc_dialer_dial(void *arg, nni_aio *aio)
{
    ipc_dialer *   d = arg;
    ipc_dial_work *w = &ipc_connector;
    int            rv;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
 
    nni_mtx_lock(&w->mtx);
    if ((rv = nni_aio_schedule(aio, ipc_dial_cancel, d)) != 0) {
        nni_mtx_unlock(&w->mtx);
        nni_aio_finish_error(aio, rv);
        return;
    }
 
    if (d->closed) {
        nni_mtx_unlock(&w->mtx);
        nni_aio_finish_error(aio, NNG_ECLOSED);
        return;
    }
 
    nni_list_append(&d->aios, aio);
    if (nni_list_first(&d->aios) == aio) {
        nni_list_append(&w->waiters, d);
        nni_cv_wake(&w->cv);
    }
    nni_mtx_unlock(&w->mtx);
}
 
static void
ipc_dialer_close(void *arg)
{
    ipc_dialer *   d = arg;
    ipc_dial_work *w = &ipc_connector;
    nni_aio *      aio;
 
    nni_mtx_lock(&w->mtx);
    d->closed = true;
    if (nni_list_active(&w->waiters, d)) {
        nni_list_remove(&w->waiters, d);
    }
    while ((aio = nni_list_first(&d->aios)) != NULL) {
        nni_list_remove(&d->aios, aio);
        nni_aio_finish_error(aio, NNG_ECLOSED);
    }
    nni_mtx_unlock(&w->mtx);
}
 
static void
ipc_dialer_free(void *arg)
{
    ipc_dialer *d = arg;
    ipc_dialer_close(d);
    if (d->path) {
        nni_strfree(d->path);
    }
    NNI_FREE_STRUCT(d);
}
 
static const nni_option ipc_dialer_options[] = {
    {
        .o_name = NULL,
    },
};
 
static int
ipc_dialer_set(
    void *arg, const char *nm, const void *buf, size_t sz, nni_type t)
{
    ipc_dialer *d = arg;
    return (nni_setopt(ipc_dialer_options, nm, d, buf, sz, t));
}
 
static int
ipc_dialer_get(void *arg, const char *nm, void *buf, size_t *szp, nni_type t)
{
    ipc_dialer *d = arg;
    return (nni_getopt(ipc_dialer_options, nm, d, buf, szp, t));
}
 
int
nni_ipc_dialer_alloc(nng_stream_dialer **dp, const nng_url *url)
{
    ipc_dialer *d;
    int         rv;
 
    if ((strcmp(url->u_scheme, "ipc") != 0) || (url->u_path == NULL) ||
        (strlen(url->u_path) == 0) ||
        (strlen(url->u_path) >= NNG_MAXADDRLEN)) {
        return (NNG_EADDRINVAL);
    }
    if ((d = NNI_ALLOC_STRUCT(d)) == NULL) {
        return (NNG_ENOMEM);
    }
 
    if ((rv = nni_asprintf(&d->path, IPC_PIPE_PREFIX "%s", url->u_path)) !=
        0) {
        NNI_FREE_STRUCT(d);
        return (rv);
    }
    snprintf(d->sa.s_ipc.sa_path, NNG_MAXADDRLEN, "%s", url->u_path);
    d->sa.s_ipc.sa_family = NNG_AF_IPC;
    d->closed             = false;
    d->sd.sd_free         = ipc_dialer_free;
    d->sd.sd_close        = ipc_dialer_close;
    d->sd.sd_dial         = ipc_dialer_dial;
    d->sd.sd_get          = ipc_dialer_get;
    d->sd.sd_set          = ipc_dialer_set;
    nni_aio_list_init(&d->aios);
    *dp = (void *) d;
    return (0);
}
 
int
nni_win_ipc_sysinit(void)
{
    int            rv;
    ipc_dial_work *worker = &ipc_connector;
 
    NNI_LIST_INIT(&worker->workers, ipc_dialer, node);
    NNI_LIST_INIT(&worker->waiters, ipc_dialer, node);
 
    nni_mtx_init(&worker->mtx);
    nni_cv_init(&worker->cv, &worker->mtx);
 
    rv = nni_thr_init(&worker->thr, ipc_dial_thr, worker);
    if (rv != 0) {
        return (rv);
    }
    nni_thr_set_name(&worker->thr, "nng:ipc:dial");
    nni_thr_run(&worker->thr);
 
    return (0);
}
 
void
nni_win_ipc_sysfini(void)
{
    ipc_dial_work *worker = &ipc_connector;
 
    nni_reap_drain(); // so that listeners get cleaned up.
 
    nni_mtx_lock(&worker->mtx);
    worker->exit = 1;
    nni_cv_wake(&worker->cv);
    nni_mtx_unlock(&worker->mtx);
    nni_thr_fini(&worker->thr);
    nni_cv_fini(&worker->cv);
    nni_mtx_fini(&worker->mtx);
}