Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / ACE / ace / UPIPE_Connector.cpp
blob03c61ea5999dcd2fce6016fde6cd491f7a656843
1 #include "ace/UPIPE_Connector.h"
3 #if defined (ACE_HAS_THREADS)
5 #include "ace/Handle_Ops.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/OS_NS_stropts.h"
9 #if !defined (__ACE_INLINE__)
10 #include "ace/UPIPE_Connector.inl"
11 #endif /* __ACE_INLINE__ */
13 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
15 ACE_ALLOC_HOOK_DEFINE(ACE_UPIPE_Connector)
17 void
18 ACE_UPIPE_Connector::dump () const
20 #if defined (ACE_HAS_DUMP)
21 ACE_TRACE ("ACE_UPIPE_Connector::dump");
22 #endif /* ACE_HAS_DUMP */
25 ACE_UPIPE_Connector::ACE_UPIPE_Connector ()
27 ACE_TRACE ("ACE_UPIPE_Connector::ACE_UPIPE_Connector");
30 int
31 ACE_UPIPE_Connector::connect (ACE_UPIPE_Stream &new_stream,
32 const ACE_UPIPE_Addr &addr,
33 ACE_Time_Value *timeout,
34 const ACE_Addr & /* local_sap */,
35 int /* reuse_addr */,
36 int flags,
37 int perms)
39 ACE_TRACE ("ACE_UPIPE_Connector::connect");
40 ACE_ASSERT (new_stream.get_handle () == ACE_INVALID_HANDLE);
42 ACE_HANDLE handle = ACE::handle_timed_open (timeout,
43 addr.get_path_name (),
44 flags, perms);
46 if (handle == ACE_INVALID_HANDLE)
47 return -1;
48 #if !defined (ACE_WIN32)
49 else if (ACE_OS::isastream (handle) != 1)
50 return -1;
51 #endif
52 else // We're connected!
54 ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, new_stream.lock_, -1));
56 ACE_UPIPE_Stream *ustream = &new_stream;
58 new_stream.set_handle (handle);
59 new_stream.remote_addr_ = addr; // class copy.
60 new_stream.reference_count_++;
62 // Now send the address of our ACE_UPIPE_Stream over this pipe
63 // to our corresponding ACE_UPIPE_Acceptor, so he may link the
64 // two streams.
65 ssize_t result = ACE_OS::write (handle,
66 (const char *) &ustream,
67 sizeof ustream);
68 if (result == -1)
69 ACELIB_ERROR ((LM_ERROR,
70 ACE_TEXT ("ACE_UPIPE_Connector %p\n"),
71 ACE_TEXT ("write to pipe failed")));
73 // Wait for confirmation of stream linking.
74 ACE_Message_Block *mb_p = 0;
76 // Our part is done, wait for server to confirm connection.
77 result = new_stream.recv (mb_p, 0);
79 // Do *not* coalesce the following two checks for result == -1.
80 // They perform different checks and cannot be merged.
81 if (result == -1)
82 ACELIB_ERROR ((LM_ERROR,
83 ACE_TEXT ("ACE_UPIPE_Connector %p\n"),
84 ACE_TEXT ("no confirmation from server")));
85 else
86 // Close down the new_stream at this point in order to
87 // conserve handles. Note that we don't need the SPIPE
88 // connection anymore since we're linked via the Message_Queue
89 // now.
90 new_stream.ACE_SPIPE::close ();
91 return static_cast<int> (result);
95 ACE_END_VERSIONED_NAMESPACE_DECL
97 #endif /* ACE_HAS_THREADS */