1 #include "ace/SPIPE_Connector.h"
2 #include "ace/Handle_Ops.h"
3 #include "ace/Log_Category.h"
4 #include "ace/OS_NS_sys_time.h"
5 #include "ace/OS_NS_fcntl.h"
6 #include "ace/OS_NS_unistd.h"
7 #if defined (ACE_HAS_ALLOC_HOOKS)
8 # include "ace/Malloc_Base.h"
9 #endif /* ACE_HAS_ALLOC_HOOKS */
11 #if !defined (__ACE_INLINE__)
12 #include "ace/SPIPE_Connector.inl"
13 #endif /* __ACE_INLINE__ */
15 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 ACE_ALLOC_HOOK_DEFINE(ACE_SPIPE_Connector
)
19 // Creates a Local ACE_SPIPE.
21 ACE_SPIPE_Connector::ACE_SPIPE_Connector (ACE_SPIPE_Stream
&new_io
,
22 const ACE_SPIPE_Addr
&remote_sap
,
23 ACE_Time_Value
*timeout
,
24 const ACE_Addr
& local_sap
,
28 LPSECURITY_ATTRIBUTES sa
,
31 ACE_TRACE ("ACE_SPIPE_Connector::ACE_SPIPE_Connector");
32 if (this->connect (new_io
, remote_sap
, timeout
, local_sap
,
33 reuse_addr
, flags
, perms
, sa
, pipe_mode
) == -1
34 && timeout
!= 0 && !(errno
== EWOULDBLOCK
|| errno
== ETIME
))
35 ACELIB_ERROR ((LM_ERROR
, ACE_TEXT ("address %s, %p\n"),
36 remote_sap
.get_path_name (), ACE_TEXT ("ACE_SPIPE_Connector")));
40 ACE_SPIPE_Connector::dump () const
42 #if defined (ACE_HAS_DUMP)
43 ACE_TRACE ("ACE_SPIPE_Connector::dump");
44 #endif /* ACE_HAS_DUMP */
47 ACE_SPIPE_Connector::ACE_SPIPE_Connector ()
49 ACE_TRACE ("ACE_SPIPE_Connector::ACE_SPIPE_Connector");
53 ACE_SPIPE_Connector::connect (ACE_SPIPE_Stream
&new_io
,
54 const ACE_SPIPE_Addr
&remote_sap
,
55 ACE_Time_Value
*timeout
,
56 const ACE_Addr
& /* local_sap */,
60 LPSECURITY_ATTRIBUTES sa
,
63 ACE_TRACE ("ACE_SPIPE_Connector::connect");
64 // Make darn sure that the O_CREAT flag is not set!
65 ACE_CLR_BITS (flags
, O_CREAT
);
69 ACE_UNUSED_ARG (pipe_mode
);
70 #if defined (ACE_WIN32)
71 // We need to allow for more than one attempt to connect,
72 // calculate the absolute time at which we give up.
73 ACE_Time_Value absolute_time
;
75 absolute_time
= ACE_OS::gettimeofday () + *timeout
;
77 // Loop until success or failure.
80 handle
= ACE_OS::open (remote_sap
.get_path_name(), flags
, perms
, sa
);
81 if (handle
!= ACE_INVALID_HANDLE
)
85 // Check if we have a busy pipe condition.
86 if (::GetLastError() != ERROR_PIPE_BUSY
)
87 // Nope, this is a failure condition.
90 // This will hold the time out value used in the ::WaitNamedPipe
94 // Check if we are to block until we connect.
96 // Wait for as long as it takes.
97 time_out_value
= NMPWAIT_WAIT_FOREVER
;
100 // Calculate the amount of time left to wait.
101 ACE_Time_Value
relative_time (absolute_time
- ACE_OS::gettimeofday ());
102 // Check if we have run out of time.
103 if (relative_time
<= ACE_Time_Value::zero
)
105 // Mimick the errno value returned by
106 // ACE::handle_timed_open.
107 if (*timeout
== ACE_Time_Value::zero
)
111 // Exit the connect loop with the failure.
114 // Get the amount of time remaining for ::WaitNamedPipe.
115 time_out_value
= relative_time
.msec ();
118 // Wait for the named pipe to become available.
119 ACE_TEXT_WaitNamedPipe (remote_sap
.get_path_name (),
122 // Regardless of the return value, we'll do one more attempt to
123 // connect to see if it is now available and to return
124 // consistent error values.
127 // Set named pipe mode if we have a valid handle.
128 if (handle
!= ACE_INVALID_HANDLE
)
130 // Check if we are changing the pipe mode from the default.
131 if (pipe_mode
!= (PIPE_READMODE_BYTE
| PIPE_WAIT
))
133 DWORD dword_pipe_mode
= pipe_mode
;
134 if (!::SetNamedPipeHandleState (handle
,
139 // We were not able to put the pipe into the requested
141 ACE_OS::close (handle
);
142 handle
= ACE_INVALID_HANDLE
;
146 #else /* ACE_WIN32 */
147 handle
= ACE::handle_timed_open (timeout
,
148 remote_sap
.get_path_name (),
150 #endif /* !ACE_WIN32 */
152 new_io
.set_handle (handle
);
153 new_io
.remote_addr_
= remote_sap
; // class copy.
155 return handle
== ACE_INVALID_HANDLE
? -1 : 0;
158 ACE_END_VERSIONED_NAMESPACE_DECL