Removed ACE_HAS_BSTRING, not used
[ACE_TAO.git] / ACE / ace / SPIPE_Connector.cpp
blob3b29e4fcb198845065ccfbf8e4d61b0a451d216e
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,
27 int reuse_addr,
28 int flags,
29 int perms,
30 LPSECURITY_ATTRIBUTES sa,
31 int pipe_mode)
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")));
41 void
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");
54 int
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 */,
59 int /* reuse_addr */,
60 int flags,
61 int perms,
62 LPSECURITY_ATTRIBUTES sa,
63 int pipe_mode)
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);
69 ACE_HANDLE handle;
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;
77 if (timeout != 0)
78 absolute_time = ACE_OS::gettimeofday () + *timeout;
80 // Loop until success or failure.
81 for (;;)
83 handle = ACE_OS::open (remote_sap.get_path_name(), flags, perms, sa);
84 if (handle != ACE_INVALID_HANDLE)
85 // Success!
86 break;
88 // Check if we have a busy pipe condition.
89 if (::GetLastError() != ERROR_PIPE_BUSY)
90 // Nope, this is a failure condition.
91 break;
93 // This will hold the time out value used in the ::WaitNamedPipe
94 // call.
95 DWORD time_out_value;
97 // Check if we are to block until we connect.
98 if (timeout == 0)
99 // Wait for as long as it takes.
100 time_out_value = NMPWAIT_WAIT_FOREVER;
101 else
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)
111 errno = EWOULDBLOCK;
112 else
113 errno = ETIMEDOUT;
114 // Exit the connect loop with the failure.
115 break;
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 (),
124 time_out_value);
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,
139 &dword_pipe_mode,
143 // We were not able to put the pipe into the requested
144 // mode.
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 (),
153 flags, perms, sa);
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