1 // $Id: SPIPE_Connector.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/SPIPE_Connector.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_sys_time.h"
6 #include "ace/OS_NS_fcntl.h"
7 #include "ace/OS_NS_unistd.h"
9 #if !defined (__ACE_INLINE__)
10 #include "ace/SPIPE_Connector.inl"
11 #endif /* __ACE_INLINE__ */
13 ACE_RCSID(ace
, SPIPE_Connector
, "$Id: SPIPE_Connector.cpp 80826 2008-03-04 14:51:23Z wotte $")
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 ACE_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 (void) 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 (void)
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 !defined (ACE_HAS_PHARLAP) && !defined (ACE_HAS_WINCE)
72 // We need to allow for more than one attempt to connect,
73 // calculate the absolute time at which we give up.
74 ACE_Time_Value absolute_time
;
76 absolute_time
= ACE_OS::gettimeofday () + *timeout
;
78 // Loop until success or failure.
81 handle
= ACE_OS::open (remote_sap
.get_path_name(), flags
, perms
, sa
);
82 if (handle
!= ACE_INVALID_HANDLE
)
86 // Check if we have a busy pipe condition.
87 if (::GetLastError() != ERROR_PIPE_BUSY
)
88 // Nope, this is a failure condition.
91 // This will hold the time out value used in the ::WaitNamedPipe
95 // Check if we are to block until we connect.
97 // Wait for as long as it takes.
98 time_out_value
= NMPWAIT_WAIT_FOREVER
;
101 // Calculate the amount of time left to wait.
102 ACE_Time_Value
relative_time (absolute_time
- ACE_OS::gettimeofday ());
103 // Check if we have run out of time.
104 if (relative_time
<= ACE_Time_Value::zero
)
106 // Mimick the errno value returned by
107 // ACE::handle_timed_open.
108 if (*timeout
== ACE_Time_Value::zero
)
112 // Exit the connect loop with the failure.
115 // Get the amount of time remaining for ::WaitNamedPipe.
116 time_out_value
= relative_time
.msec ();
120 // Wait for the named pipe to become available.
121 ACE_TEXT_WaitNamedPipe (remote_sap
.get_path_name (),
124 // Regardless of the return value, we'll do one more attempt to
125 // connect to see if it is now available and to return
126 // consistent error values.
129 // Set named pipe mode if we have a valid handle.
130 if (handle
!= ACE_INVALID_HANDLE
)
132 // Check if we are changing the pipe mode from the default.
133 if (pipe_mode
!= (PIPE_READMODE_BYTE
| PIPE_WAIT
))
135 DWORD dword_pipe_mode
= pipe_mode
;
136 if (!::SetNamedPipeHandleState (handle
,
141 // We were not able to put the pipe into the requested
143 ACE_OS::close (handle
);
144 handle
= ACE_INVALID_HANDLE
;
148 #else /* ACE_WIN32 && !ACE_HAS_PHARLAP */
149 handle
= ACE::handle_timed_open (timeout
,
150 remote_sap
.get_path_name (),
152 #endif /* !ACE_WIN32 || ACE_HAS_PHARLAP || ACE_HAS_WINCE */
154 new_io
.set_handle (handle
);
155 new_io
.remote_addr_
= remote_sap
; // class copy.
157 return handle
== ACE_INVALID_HANDLE
? -1 : 0;
160 ACE_END_VERSIONED_NAMESPACE_DECL