2 #include "ace/SOCK_Acceptor.h"
3 #include "ace/SOCK_Connector.h"
4 #include "ace/Log_Category.h"
5 #include "ace/OS_NS_stdio.h"
6 #include "ace/OS_NS_sys_socket.h"
7 #include "ace/OS_Memory.h"
8 #include "ace/Truncate.h"
9 #include "ace/Malloc_Base.h"
11 #if defined (ACE_HAS_STREAM_PIPES) || defined (__QNX__)
12 # include "ace/OS_NS_unistd.h"
13 #endif // ACE_HAS_STREAM_PIPES || __QNX__
15 #if defined (ACE_LACKS_LISTEN) && defined (ACE_LACKS_SOCKETPAIR) \
16 && !defined (ACE_HAS_STREAM_PIPES)
17 # include "ace/OS_NS_time.h"
18 # include "ace/os_include/sys/os_un.h"
21 #include "ace/os_include/netinet/os_tcp.h"
23 #if !defined (__ACE_INLINE__)
24 #include "ace/Pipe.inl"
25 #endif /* __ACE_INLINE__ */
27 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
30 ACE_Pipe::dump () const
32 #if defined (ACE_HAS_DUMP)
33 ACE_TRACE ("ACE_Pipe::dump");
34 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
35 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("handles_[0] = %d"), this->handles_
[0]));
36 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nhandles_[1] = %d\n"), this->handles_
[1]));
37 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
38 #endif /* ACE_HAS_DUMP */
42 ACE_Pipe::open (int buffer_size
)
44 ACE_TRACE ("ACE_Pipe::open");
46 #if defined (ACE_LACKS_LISTEN) && defined (ACE_LACKS_SOCKETPAIR) \
47 && !defined (ACE_HAS_STREAM_PIPES)
48 ACE_UNUSED_ARG (buffer_size
);
50 if ((this->handles_
[0] = ACE_OS::socket (AF_LOCAL
, SOCK_DGRAM
, 0)) == -1)
56 #if defined(ACE_VXWORKS) || defined(__APPLE__)
60 unsigned seed
= static_cast<unsigned> (ACE_OS::time ());
61 ACE_OS::snprintf (addr
.sun_path
, sizeof addr
.sun_path
, "/tmp/ACE-Pipe-%d-%p",
62 ACE_OS::rand_r (&seed
), this);
64 if (ACE_OS::bind (this->handles_
[0], (sockaddr
*) &addr
, sizeof addr
) == -1)
70 if ((this->handles_
[1] = ACE_OS::socket (AF_LOCAL
, SOCK_DGRAM
, 0)) == -1 ||
71 ACE_OS::connect (this->handles_
[1], (sockaddr
*) &addr
, sizeof addr
) == -1)
73 ACE_OS::unlink (addr
.sun_path
);
78 ACE_OS::unlink (addr
.sun_path
);
80 #elif defined (ACE_LACKS_SOCKETPAIR)
81 ACE_INET_Addr my_addr
;
82 ACE_SOCK_Acceptor acceptor
;
83 ACE_SOCK_Connector connector
;
84 ACE_SOCK_Stream reader
;
85 ACE_SOCK_Stream writer
;
87 # if defined (ACE_WIN32)
88 ACE_INET_Addr
local_any (static_cast<u_short
> (0), ACE_LOCALHOST
);
90 ACE_Addr local_any
= ACE_Addr::sap_any
;
91 # endif /* ACE_WIN32 */
93 // Bind listener to any port and then find out what the port was.
94 if (acceptor
.open (local_any
) == -1 || acceptor
.get_local_addr (my_addr
) == -1)
100 ACE_INET_Addr sv_addr
;
101 if (sv_addr
.set (my_addr
.get_port_number (), ACE_LOCALHOST
) == -1)
105 // Establish a connection within the same process.
106 else if (connector
.connect (writer
, sv_addr
) == -1)
110 else if (acceptor
.accept (reader
) == -1)
117 // Close down the acceptor endpoint since we don't need it anymore.
122 this->handles_
[0] = reader
.get_handle ();
123 this->handles_
[1] = writer
.get_handle ();
125 # if !defined (ACE_LACKS_TCP_NODELAY)
128 // Make sure that the TCP stack doesn't try to buffer small writes.
129 // Since this communication is purely local to the host it doesn't
130 // affect network performance.
132 if (writer
.set_option (ACE_IPPROTO_TCP
,
140 # endif /* ! ACE_LACKS_TCP_NODELAY */
142 # if defined (ACE_LACKS_SO_RCVBUF) && defined (ACE_LACKS_SO_SNDBUF)
143 ACE_UNUSED_ARG (buffer_size
);
145 # if !defined (ACE_LACKS_SO_RCVBUF)
146 if (reader
.set_option (SOL_SOCKET
,
148 reinterpret_cast <void *> (&buffer_size
),
149 sizeof (buffer_size
)) == -1
155 # endif /* !ACE_LACKS_SO_RCVBUF */
156 # if !defined (ACE_LACKS_SO_SNDBUF)
157 if (writer
.set_option (SOL_SOCKET
,
159 reinterpret_cast <void *> (&buffer_size
),
160 sizeof (buffer_size
)) == -1
166 # endif /* !ACE_LACKS_SO_SNDBUF */
168 #elif defined (ACE_HAS_STREAM_PIPES) || defined (__QNX__)
169 ACE_UNUSED_ARG (buffer_size
);
170 if (ACE_OS::pipe (this->handles_
) == -1)
171 ACELIB_ERROR_RETURN ((LM_ERROR
,
176 # if !defined(__QNX__)
179 // Enable "msg no discard" mode, which ensures that record
180 // boundaries are maintained when messages are sent and received.
181 if (ACE_OS::ioctl (this->handles_
[0],
184 || ACE_OS::ioctl (this->handles_
[1],
189 ACELIB_ERROR_RETURN ((LM_ERROR
,
191 ACE_TEXT ("ioctl")), -1);
193 # endif /* __QNX__ */
195 #else /* ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */
196 if (ACE_OS::socketpair (AF_UNIX
,
199 this->handles_
) == -1)
200 ACELIB_ERROR_RETURN ((LM_ERROR
,
202 ACE_TEXT ("socketpair")),
204 # if defined (ACE_LACKS_SO_SNDBUF) && defined (ACE_LACKS_SO_RCVBUF)
205 ACE_UNUSED_ARG (buffer_size
);
207 # if !defined (ACE_LACKS_SO_RCVBUF)
208 if (ACE_OS::setsockopt (this->handles_
[0],
211 reinterpret_cast <const char *> (&buffer_size
),
212 sizeof (buffer_size
)) == -1
219 # if !defined (ACE_LACKS_SO_SNDBUF)
220 if (ACE_OS::setsockopt (this->handles_
[1],
223 reinterpret_cast <const char *> (&buffer_size
),
224 sizeof (buffer_size
)) == -1
230 # endif /* ! ACE_LACKS_SO_SNDBUF */
231 #endif /* ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */
232 // Point both the read and write HANDLES to the appropriate socket
239 ACE_Pipe::open (ACE_HANDLE handles
[2])
241 ACE_TRACE ("ACE_Pipe::open");
243 if (this->open () == -1)
247 handles
[0] = this->handles_
[0];
248 handles
[1] = this->handles_
[1];
255 ACE_Pipe::ACE_Pipe ()
257 ACE_TRACE ("ACE_Pipe::ACE_Pipe");
259 this->handles_
[0] = ACE_INVALID_HANDLE
;
260 this->handles_
[1] = ACE_INVALID_HANDLE
;
263 ACE_Pipe::ACE_Pipe (ACE_HANDLE handles
[2])
265 ACE_TRACE ("ACE_Pipe::ACE_Pipe");
267 if (this->open (handles
) == -1)
268 ACELIB_ERROR ((LM_ERROR
,
269 ACE_TEXT ("ACE_Pipe::ACE_Pipe")));
272 ACE_Pipe::ACE_Pipe (ACE_HANDLE read
,
275 ACE_TRACE ("ACE_Pipe::ACE_Pipe");
276 this->handles_
[0] = read
;
277 this->handles_
[1] = write
;
283 ACE_TRACE ("ACE_Pipe::close");
285 int result
= this->close_read ();
286 result
|= this->close_write ();
291 ACE_Pipe::close_read ()
293 return this->close_handle (0);
296 int ACE_Pipe::close_write ()
298 return this->close_handle (1);
301 // Send N char *ptrs and int lengths. Note that the char *'s precede
302 // the ints (basically, an varargs version of writev). The count N is
303 // the *total* number of trailing arguments, *not* a couple of the
304 // number of tuple pairs!
305 #if !defined (ACE_LACKS_VA_FUNCTIONS)
307 ACE_Pipe::send (size_t n
, ...) const
309 ACE_TRACE ("ACE_Pipe::send");
311 int total_tuples
= ACE_Utils::truncate_cast
<int> (n
/ 2);
313 #if defined (ACE_HAS_ALLOCA)
314 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
316 # ifdef ACE_HAS_ALLOC_HOOKS
317 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
318 ACE_Allocator::instance ()->malloc (total_tuples
*
322 ACE_NEW_RETURN (iovp
,
325 # endif /* ACE_HAS_ALLOC_HOOKS */
326 #endif /* !defined (ACE_HAS_ALLOCA) */
330 for (int i
= 0; i
< total_tuples
; ++i
)
332 iovp
[i
].iov_base
= va_arg (argp
, char *);
333 iovp
[i
].iov_len
= va_arg (argp
, int);
336 #if defined (ACE_WIN32)
337 ssize_t result
= ACE::sendv (this->write_handle (),
341 ssize_t result
= ACE_OS::writev (this->write_handle (),
344 #endif /* ACE_WIN32 */
346 #if !defined (ACE_HAS_ALLOCA)
347 # ifdef ACE_HAS_ALLOC_HOOKS
348 ACE_Allocator::instance ()->free (iovp
);
351 # endif /* ACE_HAS_ALLOC_HOOKS */
352 #endif /* !defined (ACE_HAS_ALLOCA) */
357 // This is basically an interface to ACE_OS::readv, that doesn't use
358 // the struct iovec explicitly. The ... can be passed as an arbitrary
359 // number of (char *ptr, int len) tuples. However, the count N is the
360 // *total* number of trailing arguments, *not* a couple of the number
364 ACE_Pipe::recv (size_t n
, ...) const
366 ACE_TRACE ("ACE_Pipe::recv");
368 int total_tuples
= ACE_Utils::truncate_cast
<int> (n
/ 2);
370 #if defined (ACE_HAS_ALLOCA)
371 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
373 # ifdef ACE_HAS_ALLOC_HOOKS
374 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
375 ACE_Allocator::instance ()->malloc (total_tuples
*
379 ACE_NEW_RETURN (iovp
,
382 # endif /* ACE_HAS_ALLOC_HOOKS */
383 #endif /* !defined (ACE_HAS_ALLOCA) */
387 for (int i
= 0; i
< total_tuples
; ++i
)
389 iovp
[i
].iov_base
= va_arg (argp
, char *);
390 iovp
[i
].iov_len
= va_arg (argp
, int);
393 #if defined (ACE_WIN32)
394 ssize_t
const result
= ACE::recvv (this->read_handle (),
398 ssize_t
const result
= ACE_OS::readv (this->read_handle (),
401 #endif /* ACE_WIN32 */
403 #if !defined (ACE_HAS_ALLOCA)
404 # ifdef ACE_HAS_ALLOC_HOOKS
405 ACE_Allocator::instance ()->free (iovp
);
408 # endif /* ACE_HAS_ALLOC_HOOKS */
409 #endif /* !defined (ACE_HAS_ALLOCA) */
413 #endif /* !ACE_LACKS_VA_FUNCTIONS */
415 ACE_END_VERSIONED_NAMESPACE_DECL