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__ */
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 ACE_ALLOC_HOOK_DEFINE(ACE_SPIPE_Connector
)
21 // Creates a Local ACE_SPIPE.
23 ACE_SPIPE_Connector::ACE_SPIPE_Connector (ACE_SPIPE_Stream
&new_io
,
24 const ACE_SPIPE_Addr
&remote_sap
,
25 ACE_Time_Value
*timeout
,
26 const ACE_Addr
& local_sap
,
30 LPSECURITY_ATTRIBUTES sa
,
33 ACE_TRACE ("ACE_SPIPE_Connector::ACE_SPIPE_Connector");
34 if (this->connect (new_io
, remote_sap
, timeout
, local_sap
,
35 reuse_addr
, flags
, perms
, sa
, pipe_mode
) == -1
36 && timeout
!= 0 && !(errno
== EWOULDBLOCK
|| errno
== ETIME
))
37 ACELIB_ERROR ((LM_ERROR
, ACE_TEXT ("address %s, %p\n"),
38 remote_sap
.get_path_name (), ACE_TEXT ("ACE_SPIPE_Connector")));
42 ACE_SPIPE_Connector::dump (void) const
44 #if defined (ACE_HAS_DUMP)
45 ACE_TRACE ("ACE_SPIPE_Connector::dump");
46 #endif /* ACE_HAS_DUMP */
49 ACE_SPIPE_Connector::ACE_SPIPE_Connector (void)
51 ACE_TRACE ("ACE_SPIPE_Connector::ACE_SPIPE_Connector");
55 ACE_SPIPE_Connector::connect (ACE_SPIPE_Stream
&new_io
,
56 const ACE_SPIPE_Addr
&remote_sap
,
57 ACE_Time_Value
*timeout
,
58 const ACE_Addr
& /* local_sap */,
62 LPSECURITY_ATTRIBUTES sa
,
65 ACE_TRACE ("ACE_SPIPE_Connector::connect");
66 // Make darn sure that the O_CREAT flag is not set!
67 ACE_CLR_BITS (flags
, O_CREAT
);
71 ACE_UNUSED_ARG (pipe_mode
);
72 #if defined (ACE_WIN32) && \
73 !defined (ACE_HAS_PHARLAP) && !defined (ACE_HAS_WINCE)
74 // We need to allow for more than one attempt to connect,
75 // calculate the absolute time at which we give up.
76 ACE_Time_Value absolute_time
;
78 absolute_time
= ACE_OS::gettimeofday () + *timeout
;
80 // Loop until success or failure.
83 handle
= ACE_OS::open (remote_sap
.get_path_name(), flags
, perms
, sa
);
84 if (handle
!= ACE_INVALID_HANDLE
)
88 // Check if we have a busy pipe condition.
89 if (::GetLastError() != ERROR_PIPE_BUSY
)
90 // Nope, this is a failure condition.
93 // This will hold the time out value used in the ::WaitNamedPipe
97 // Check if we are to block until we connect.
99 // Wait for as long as it takes.
100 time_out_value
= NMPWAIT_WAIT_FOREVER
;
103 // Calculate the amount of time left to wait.
104 ACE_Time_Value
relative_time (absolute_time
- ACE_OS::gettimeofday ());
105 // Check if we have run out of time.
106 if (relative_time
<= ACE_Time_Value::zero
)
108 // Mimick the errno value returned by
109 // ACE::handle_timed_open.
110 if (*timeout
== ACE_Time_Value::zero
)
114 // Exit the connect loop with the failure.
117 // Get the amount of time remaining for ::WaitNamedPipe.
118 time_out_value
= relative_time
.msec ();
122 // Wait for the named pipe to become available.
123 ACE_TEXT_WaitNamedPipe (remote_sap
.get_path_name (),
126 // Regardless of the return value, we'll do one more attempt to
127 // connect to see if it is now available and to return
128 // consistent error values.
131 // Set named pipe mode if we have a valid handle.
132 if (handle
!= ACE_INVALID_HANDLE
)
134 // Check if we are changing the pipe mode from the default.
135 if (pipe_mode
!= (PIPE_READMODE_BYTE
| PIPE_WAIT
))
137 DWORD dword_pipe_mode
= pipe_mode
;
138 if (!::SetNamedPipeHandleState (handle
,
143 // We were not able to put the pipe into the requested
145 ACE_OS::close (handle
);
146 handle
= ACE_INVALID_HANDLE
;
150 #else /* ACE_WIN32 && !ACE_HAS_PHARLAP */
151 handle
= ACE::handle_timed_open (timeout
,
152 remote_sap
.get_path_name (),
154 #endif /* !ACE_WIN32 || ACE_HAS_PHARLAP || ACE_HAS_WINCE */
156 new_io
.set_handle (handle
);
157 new_io
.remote_addr_
= remote_sap
; // class copy.
159 return handle
== ACE_INVALID_HANDLE
? -1 : 0;
162 ACE_END_VERSIONED_NAMESPACE_DECL