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
|
//
// 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 "core/nng_impl.h"
#include <string.h>
struct nni_device_pair {
nni_thr thrs[2];
nni_sock *socks[2];
int err[2];
};
typedef struct nni_device_pair nni_device_pair;
static int
nni_device_loop(nni_sock *from, nni_sock *to)
{
nni_msg *msg;
int rv = 0;
for (;;) {
// Take messages sock[0], and send to sock[1].
// If an error occurs, we close both sockets.
if ((rv = nni_sock_recvmsg(from, &msg, NNI_TIME_NEVER)) != 0) {
break;
}
if ((rv = nni_sock_sendmsg(to, msg, NNI_TIME_NEVER)) != 0) {
nni_msg_free(msg);
break;
}
}
return (rv);
}
static void
nni_device_fwd(void *p)
{
nni_device_pair *pair = p;
pair->err[0] = nni_device_loop(pair->socks[0], pair->socks[1]);
nni_sock_shutdown(pair->socks[0]);
nni_sock_shutdown(pair->socks[1]);
}
static void
nni_device_rev(void *p)
{
nni_device_pair *pair = p;
pair->err[1] = nni_device_loop(pair->socks[1], pair->socks[0]);
nni_sock_shutdown(pair->socks[0]);
nni_sock_shutdown(pair->socks[1]);
}
int
nni_device(nni_sock *sock1, nni_sock *sock2)
{
nni_device_pair pair;
int rv;
memset(&pair, 0, sizeof(pair));
pair.socks[0] = sock1;
pair.socks[1] = sock2;
if (sock1 == NULL) {
sock1 = sock2;
}
if (sock2 == NULL) {
sock2 = sock1;
}
if ((sock1 == NULL) || (sock2 == NULL)) {
rv = NNG_EINVAL;
goto out;
}
if ((sock1->s_peer_id.p_id != sock2->s_self_id.p_id) ||
(sock2->s_peer_id.p_id != sock1->s_self_id.p_id)) {
rv = NNG_EINVAL;
goto out;
}
pair.socks[0] = sock1;
pair.socks[1] = sock2;
if ((rv = nni_thr_init(&pair.thrs[0], nni_device_fwd, &pair)) != 0) {
goto out;
}
if ((rv = nni_thr_init(&pair.thrs[1], nni_device_rev, &pair)) != 0) {
nni_thr_fini(&pair.thrs[0]);
goto out;
}
if (((sock1->s_flags & NNI_PROTO_FLAG_RCV) != 0) &&
((sock2->s_flags & NNI_PROTO_FLAG_SND) != 0)) {
nni_thr_run(&pair.thrs[0]);
}
// If the sockets are the same, then its a simple one way forwarder,
// and we don't need two workers (but would be harmless if we did it).
if ((sock1 != sock2) && ((sock2->s_flags & NNI_PROTO_FLAG_RCV) != 0) &&
((sock1->s_flags & NNI_PROTO_FLAG_SND) != 0)) {
nni_thr_run(&pair.thrs[1]);
}
// This blocks on both threads (though if we didn't start one, that
// will return immediately.)
nni_thr_fini(&pair.thrs[0]);
nni_thr_fini(&pair.thrs[1]);
nni_sock_rele(sock1);
if (sock1 != sock2) {
nni_sock_rele(sock2);
}
rv = pair.err[0];
if (rv == 0) {
rv = pair.err[1];
}
if (rv == 0) {
// This can happen if neither thread ran. Shouldn't happen
// really.
rv = NNG_EINVAL;
}
out:
return (rv);
}
|