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
|
// $Id$
#include "ace/Pipe.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/OS_Memory.h"
#include "ace/Truncate.h"
#if defined (ACE_HAS_STREAM_PIPES) || defined (__QNX__)
# include "ace/OS_NS_unistd.h"
#endif // ACE_HAS_STREAM_PIPES || __QNX__
#include "ace/os_include/netinet/os_tcp.h"
#if !defined (__ACE_INLINE__)
#include "ace/Pipe.inl"
#endif /* __ACE_INLINE__ */
ACE_RCSID(ace, Pipe, "$Id$")
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
void
ACE_Pipe::dump (void) const
{
#if defined (ACE_HAS_DUMP)
ACE_TRACE ("ACE_Pipe::dump");
ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("handles_[0] = %d"), this->handles_[0]));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhandles_[1] = %d\n"), this->handles_[1]));
ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}
int
ACE_Pipe::open (int buffer_size)
{
ACE_TRACE ("ACE_Pipe::open");
#if defined (ACE_LACKS_SOCKETPAIR)
ACE_INET_Addr my_addr;
ACE_SOCK_Acceptor acceptor;
ACE_SOCK_Connector connector;
ACE_SOCK_Stream reader;
ACE_SOCK_Stream writer;
int result = 0;
# if defined (ACE_WIN32)
ACE_INET_Addr local_any (static_cast<u_short> (0), ACE_LOCALHOST);
# else
ACE_Addr local_any = ACE_Addr::sap_any;
# endif /* ACE_WIN32 */
// Bind listener to any port and then find out what the port was.
if (acceptor.open (local_any) == -1
|| acceptor.get_local_addr (my_addr) == -1)
result = -1;
else
{
ACE_INET_Addr sv_addr (my_addr.get_port_number (),
ACE_LOCALHOST);
// Establish a connection within the same process.
if (connector.connect (writer, sv_addr) == -1)
result = -1;
else if (acceptor.accept (reader) == -1)
{
writer.close ();
result = -1;
}
}
// Close down the acceptor endpoint since we don't need it anymore.
acceptor.close ();
if (result == -1)
return -1;
this->handles_[0] = reader.get_handle ();
this->handles_[1] = writer.get_handle ();
# if !defined (ACE_LACKS_TCP_NODELAY)
int one = 1;
// Make sure that the TCP stack doesn't try to buffer small writes.
// Since this communication is purely local to the host it doesn't
// affect network performance.
if (writer.set_option (ACE_IPPROTO_TCP,
TCP_NODELAY,
&one,
sizeof one) == -1)
{
this->close ();
return -1;
}
# endif /* ! ACE_LACKS_TCP_NODELAY */
# if defined (ACE_LACKS_SO_RCVBUF) && defined (ACE_LACKS_SO_SNDBUF)
ACE_UNUSED_ARG (buffer_size);
# endif
# if !defined (ACE_LACKS_SO_RCVBUF)
if (reader.set_option (SOL_SOCKET,
SO_RCVBUF,
reinterpret_cast <void *> (&buffer_size),
sizeof (buffer_size)) == -1
&& errno != ENOTSUP)
{
this->close ();
return -1;
}
# endif /* !ACE_LACKS_SO_RCVBUF */
# if !defined (ACE_LACKS_SO_SNDBUF)
if (writer.set_option (SOL_SOCKET,
SO_SNDBUF,
reinterpret_cast <void *> (&buffer_size),
sizeof (buffer_size)) == -1
&& errno != ENOTSUP)
{
this->close ();
return -1;
}
# endif /* !ACE_LACKS_SO_SNDBUF */
#elif defined (ACE_HAS_STREAM_PIPES) || defined (__QNX__)
ACE_UNUSED_ARG (buffer_size);
if (ACE_OS::pipe (this->handles_) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("pipe")),
-1);
#if !defined(__QNX__)
int arg = RMSGN;
// Enable "msg no discard" mode, which ensures that record
// boundaries are maintained when messages are sent and received.
if (ACE_OS::ioctl (this->handles_[0],
I_SRDOPT,
(void *) arg) == -1
|| ACE_OS::ioctl (this->handles_[1],
I_SRDOPT,
(void *) arg) == -1)
{
this->close ();
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ioctl")), -1);
}
#endif /* __QNX__ */
#else /* ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */
if (ACE_OS::socketpair (AF_UNIX,
SOCK_STREAM,
0,
this->handles_) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("socketpair")),
-1);
# if defined (ACE_LACKS_SO_SNDBUF) && defined (ACE_LACKS_SO_RCVBUF)
ACE_UNUSED_ARG (buffer_size);
# endif
# if !defined (ACE_LACKS_SO_RCVBUF)
if (ACE_OS::setsockopt (this->handles_[0],
SOL_SOCKET,
SO_RCVBUF,
reinterpret_cast <const char *> (&buffer_size),
sizeof (buffer_size)) == -1
&& errno != ENOTSUP)
{
this->close ();
return -1;
}
# endif
# if !defined (ACE_LACKS_SO_SNDBUF)
if (ACE_OS::setsockopt (this->handles_[1],
SOL_SOCKET,
SO_SNDBUF,
reinterpret_cast <const char *> (&buffer_size),
sizeof (buffer_size)) == -1
&& errno != ENOTSUP)
{
this->close ();
return -1;
}
# endif /* ! ACE_LACKS_SO_SNDBUF */
# if defined (ACE_OPENVMS) && !defined (ACE_LACKS_TCP_NODELAY)
int one = 1;
// OpenVMS implements socketpair(AF_UNIX...) by returning AF_INET sockets.
// Since these are plagued by Nagle as any other INET socket we need to set
// TCP_NODELAY on the write handle.
if (ACE_OS::setsockopt (this->handles_[1],
ACE_IPPROTO_TCP,
TCP_NODELAY,
reinterpret_cast <const char *> (&one),
sizeof (one)) == -1)
{
this->close ();
return -1;
}
# endif /* ACE_OPENVMS && !ACE_LACKS_TCP_NODELAY */
#endif /* ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */
// Point both the read and write HANDLES to the appropriate socket
// HANDLEs.
return 0;
}
int
ACE_Pipe::open (ACE_HANDLE handles[2])
{
ACE_TRACE ("ACE_Pipe::open");
if (this->open () == -1)
return -1;
else
{
handles[0] = this->handles_[0];
handles[1] = this->handles_[1];
return 0;
}
}
// Do nothing...
ACE_Pipe::ACE_Pipe (void)
{
ACE_TRACE ("ACE_Pipe::ACE_Pipe");
this->handles_[0] = ACE_INVALID_HANDLE;
this->handles_[1] = ACE_INVALID_HANDLE;
}
ACE_Pipe::ACE_Pipe (ACE_HANDLE handles[2])
{
ACE_TRACE ("ACE_Pipe::ACE_Pipe");
if (this->open (handles) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Pipe::ACE_Pipe")));
}
ACE_Pipe::ACE_Pipe (ACE_HANDLE read,
ACE_HANDLE write)
{
ACE_TRACE ("ACE_Pipe::ACE_Pipe");
this->handles_[0] = read;
this->handles_[1] = write;
}
int
ACE_Pipe::close (void)
{
ACE_TRACE ("ACE_Pipe::close");
int result = 0;
// Note that the following will work even if we aren't closing down
// sockets because <ACE_OS::closesocket> will just call <::close> in
// that case!
if (this->handles_[0] != ACE_INVALID_HANDLE)
result = ACE_OS::closesocket (this->handles_[0]);
this->handles_[0] = ACE_INVALID_HANDLE;
if (this->handles_[1] != ACE_INVALID_HANDLE)
result |= ACE_OS::closesocket (this->handles_[1]);
this->handles_[1] = ACE_INVALID_HANDLE;
return result;
}
// Send N char *ptrs and int lengths. Note that the char *'s precede
// the ints (basically, an varargs version of writev). The count N is
// the *total* number of trailing arguments, *not* a couple of the
// number of tuple pairs!
ssize_t
ACE_Pipe::send (size_t n, ...) const
{
ACE_TRACE ("ACE_Pipe::send");
va_list argp;
int total_tuples = ACE_Utils::truncate_cast<int> (n / 2);
iovec *iovp;
#if defined (ACE_HAS_ALLOCA)
iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
#else
ACE_NEW_RETURN (iovp,
iovec[total_tuples],
-1);
#endif /* !defined (ACE_HAS_ALLOCA) */
va_start (argp, n);
for (int i = 0; i < total_tuples; ++i)
{
iovp[i].iov_base = va_arg (argp, char *);
iovp[i].iov_len = va_arg (argp, int);
}
#if defined (ACE_WIN32)
ssize_t result = ACE::sendv (this->write_handle (),
iovp,
total_tuples);
#else
ssize_t result = ACE_OS::writev (this->write_handle (),
iovp,
total_tuples);
#endif /* ACE_WIN32 */
#if !defined (ACE_HAS_ALLOCA)
delete [] iovp;
#endif /* !defined (ACE_HAS_ALLOCA) */
va_end (argp);
return result;
}
// This is basically an interface to ACE_OS::readv, that doesn't use
// the struct iovec explicitly. The ... can be passed as an arbitrary
// number of (char *ptr, int len) tuples. However, the count N is the
// *total* number of trailing arguments, *not* a couple of the number
// of tuple pairs!
ssize_t
ACE_Pipe::recv (size_t n, ...) const
{
ACE_TRACE ("ACE_Pipe::recv");
va_list argp;
int total_tuples = ACE_Utils::truncate_cast<int> (n / 2);
iovec *iovp;
#if defined (ACE_HAS_ALLOCA)
iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
#else
ACE_NEW_RETURN (iovp,
iovec[total_tuples],
-1);
#endif /* !defined (ACE_HAS_ALLOCA) */
va_start (argp, n);
for (int i = 0; i < total_tuples; ++i)
{
iovp[i].iov_base = va_arg (argp, char *);
iovp[i].iov_len = va_arg (argp, int);
}
#if defined (ACE_WIN32)
ssize_t const result = ACE::recvv (this->read_handle (),
iovp,
total_tuples);
#else
ssize_t const result = ACE_OS::readv (this->read_handle (),
iovp,
total_tuples);
#endif /* ACE_WIN32 */
#if !defined (ACE_HAS_ALLOCA)
delete [] iovp;
#endif /* !defined (ACE_HAS_ALLOCA) */
va_end (argp);
return result;
}
ACE_END_VERSIONED_NAMESPACE_DECL
|