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
|
/*
Copyright (c) 2007-2009 FastMQ Inc.
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the Lesser GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fd_signaler.hpp"
#include "platform.hpp"
#include "err.hpp"
#include "fd.hpp"
#include "ip.hpp"
#if defined ZMQ_HAVE_OPENVMS
#include <netinet/tcp.h>
#elif defined ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#else
#include <unistd.h>
#include <fcntl.h>
#endif
#if defined ZMQ_HAVE_EVENTFD
#include <sys/eventfd.h>
zmq::fd_signaler_t::fd_signaler_t ()
{
// Create eventfd object.
fd = eventfd (0, 0);
errno_assert (fd != -1);
// Set to non-blocking mode.
int flags = fcntl (fd, F_GETFL, 0);
if (flags == -1)
flags = 0;
int rc = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
errno_assert (rc != -1);
}
zmq::fd_signaler_t::~fd_signaler_t ()
{
int rc = close (fd);
errno_assert (rc != -1);
}
void zmq::fd_signaler_t::signal (int signal_)
{
zmq_assert (signal_ >= 0 && signal_ < 64);
signals_t inc = 1;
inc <<= signal_;
ssize_t sz = write (fd, &inc, sizeof (signals_t));
errno_assert (sz == sizeof (signals_t));
}
zmq::fd_signaler_t::signals_t zmq::fd_signaler_t::check ()
{
signals_t val;
ssize_t sz = read (fd, &val, sizeof (signals_t));
if (sz == -1 && (errno == EAGAIN || errno == EWOULDBLOCK ||
errno == EINTR))
return 0;
errno_assert (sz != -1);
return val;
}
zmq::fd_t zmq::fd_signaler_t::get_fd ()
{
return fd;
}
#elif defined ZMQ_HAVE_WINDOWS
zmq::fd_signaler_t::fd_signaler_t ()
{
struct sockaddr_in addr;
SOCKET listener;
int addrlen = sizeof (addr);
w = INVALID_SOCKET;
r = INVALID_SOCKET;
fd_t rcs = (listener = socket (AF_INET, SOCK_STREAM, 0));
wsa_assert (rcs != INVALID_SOCKET);
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
resolve_ip_hostname (&addr, "127.0.0.1:0");
int rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
wsa_assert (rc != SOCKET_ERROR);
rc = getsockname (listener, (struct sockaddr*) &addr, &addrlen);
wsa_assert (rc != SOCKET_ERROR);
// Listen for incomming connections.
rc = listen (listener, 1);
wsa_assert (rc != SOCKET_ERROR);
// Create the socket.
w = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
wsa_assert (w != INVALID_SOCKET);
// Connect to the remote peer.
rc = connect (w, (sockaddr *) &addr, sizeof (addr));
wsa_assert (rc != SOCKET_ERROR);
// Accept connection from w
r = accept (listener, NULL, NULL);
wsa_assert (r != INVALID_SOCKET);
rc = closesocket (listener);
wsa_assert (rc != SOCKET_ERROR);
}
zmq::fd_signaler_t::~fd_signaler_t ()
{
int rc = closesocket (w);
wsa_assert (rc != SOCKET_ERROR);
rc = closesocket (r);
wsa_assert (rc != SOCKET_ERROR);
}
void zmq::fd_signaler_t::signal (int signal_)
{
zmq_assert (signal_ >= 0 && signal_ < 64);
char c = (char) signal_;
int rc = send (w, &c, 1, 0);
win_assert (rc != SOCKET_ERROR);
}
zmq::fd_signaler_t::signals_t zmq::fd_signaler_t::check ()
{
char buffer [32];
int nbytes = recv (r, buffer, 32, 0);
win_assert (nbytes != -1);
signals_t signals = 0;
for (int pos = 0; pos != nbytes; pos++) {
zmq_assert (buffer [pos] < 64);
signals |= (signals_t (1) << (buffer [pos]));
}
return signals;
}
zmq::fd_t zmq::fd_signaler_t::get_fd ()
{
return r;
}
#else
#include <sys/types.h>
#include <sys/socket.h>
zmq::fd_signaler_t::fd_signaler_t ()
{
int sv [2];
int rc = socketpair (AF_UNIX, SOCK_STREAM, 0, sv);
errno_assert (rc == 0);
w = sv [0];
r = sv [1];
// Set to non-blocking mode.
int flags = fcntl (r, F_GETFL, 0);
if (flags == -1)
flags = 0;
rc = fcntl (r, F_SETFL, flags | O_NONBLOCK);
errno_assert (rc != -1);
}
zmq::fd_signaler_t::~fd_signaler_t ()
{
close (w);
close (r);
}
void zmq::fd_signaler_t::signal (int signal_)
{
zmq_assert (signal_ >= 0 && signal_ < 64);
unsigned char c = (unsigned char) signal_;
ssize_t nbytes = send (w, &c, 1, 0);
errno_assert (nbytes == 1);
}
zmq::fd_signaler_t::signals_t zmq::fd_signaler_t::check ()
{
unsigned char buffer [32];
ssize_t nbytes = recv (r, buffer, 32, 0);
errno_assert (nbytes != -1);
signals_t signals = 0;
for (int pos = 0; pos != nbytes; pos ++) {
zmq_assert (buffer [pos] < 64);
signals |= (1 << (buffer [pos]));
}
return signals;
}
zmq::fd_t zmq::fd_signaler_t::get_fd ()
{
return r;
}
#endif
#if defined ZMQ_HAVE_OPENVMS
int zmq::fd_signaler_t::socketpair (int domain_, int type_, int protocol_,
int sv_ [2])
{
int listener;
sockaddr_in lcladdr;
socklen_t lcladdr_len;
int rc;
int on = 1;
zmq_assert (type_ == SOCK_STREAM);
// Fill in the localhost address (127.0.0.1).
memset (&lcladdr, 0, sizeof (lcladdr));
lcladdr.sin_family = AF_INET;
lcladdr.sin_addr.s_addr = 0x0100007f;
lcladdr.sin_port = INADDR_ANY;
listener = socket (AF_INET, SOCK_STREAM, 0);
errno_assert (listener != -1);
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
errno_assert (rc != -1);
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
errno_assert (rc != -1);
rc = bind(listener, (struct sockaddr*) &lcladdr, sizeof (lcladdr));
errno_assert (rc != -1);
lcladdr_len = sizeof (lcladdr);
rc = getsockname (listener, (struct sockaddr*) &lcladdr, &lcladdr_len);
errno_assert (rc != -1);
rc = listen (listener, 1);
errno_assert (rc != -1);
sv_ [0] = socket (AF_INET, SOCK_STREAM, 0);
errno_assert (rc != -1);
rc = setsockopt (sv_ [0], IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
errno_assert (rc != -1);
rc = setsockopt (sv_ [0], IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
errno_assert (rc != -1);
rc = connect (sv_ [0], (struct sockaddr*) &lcladdr, sizeof (lcladdr));
errno_assert (rc != -1);
sv_ [1] = accept (listener, NULL, NULL);
errno_assert (sv_ [1] != -1);
close (listener);
return 0;
}
#endif
|