Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / SPIPE_Connector.cpp
blobc11bb7d9b39da995885a66951251571389a467c2
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,
25 int reuse_addr,
26 int flags,
27 int perms,
28 LPSECURITY_ATTRIBUTES sa,
29 int pipe_mode)
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")));
39 void
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");
52 int
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 */,
57 int /* reuse_addr */,
58 int flags,
59 int perms,
60 LPSECURITY_ATTRIBUTES sa,
61 int pipe_mode)
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);
67 ACE_HANDLE handle;
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;
75 if (timeout != 0)
76 absolute_time = ACE_OS::gettimeofday () + *timeout;
78 // Loop until success or failure.
79 for (;;)
81 handle = ACE_OS::open (remote_sap.get_path_name(), flags, perms, sa);
82 if (handle != ACE_INVALID_HANDLE)
83 // Success!
84 break;
86 // Check if we have a busy pipe condition.
87 if (::GetLastError() != ERROR_PIPE_BUSY)
88 // Nope, this is a failure condition.
89 break;
91 // This will hold the time out value used in the ::WaitNamedPipe
92 // call.
93 DWORD time_out_value;
95 // Check if we are to block until we connect.
96 if (timeout == 0)
97 // Wait for as long as it takes.
98 time_out_value = NMPWAIT_WAIT_FOREVER;
99 else
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)
109 errno = EWOULDBLOCK;
110 else
111 errno = ETIMEDOUT;
112 // Exit the connect loop with the failure.
113 break;
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 (),
122 time_out_value);
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,
137 &dword_pipe_mode,
141 // We were not able to put the pipe into the requested
142 // mode.
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 (),
151 flags, perms, sa);
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