UPIPE_Connector.cpp

Go to the documentation of this file.
00001 // UPIPE_Connector.cpp,v 4.19 2006/02/21 23:53:19 shuston Exp
00002 
00003 #include "ace/UPIPE_Connector.h"
00004 
00005 ACE_RCSID(ace, UPIPE_Connector, "UPIPE_Connector.cpp,v 4.19 2006/02/21 23:53:19 shuston Exp")
00006 
00007 #if defined (ACE_HAS_THREADS)
00008 
00009 #include "ace/Handle_Ops.h"
00010 #include "ace/OS_NS_unistd.h"
00011 #include "ace/OS_NS_stropts.h"
00012 
00013 #if !defined (__ACE_INLINE__)
00014 #include "ace/UPIPE_Connector.inl"
00015 #endif /* __ACE_INLINE__ */
00016 
00017 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 ACE_ALLOC_HOOK_DEFINE(ACE_UPIPE_Connector)
00020 
00021 void
00022 ACE_UPIPE_Connector::dump (void) const
00023 {
00024 #if defined (ACE_HAS_DUMP)
00025   ACE_TRACE ("ACE_UPIPE_Connector::dump");
00026 #endif /* ACE_HAS_DUMP */
00027 }
00028 
00029 ACE_UPIPE_Connector::ACE_UPIPE_Connector (void)
00030 {
00031   ACE_TRACE ("ACE_UPIPE_Connector::ACE_UPIPE_Connector");
00032 }
00033 
00034 int
00035 ACE_UPIPE_Connector::connect (ACE_UPIPE_Stream &new_stream,
00036                               const ACE_UPIPE_Addr &addr,
00037                               ACE_Time_Value *timeout,
00038                               const ACE_Addr & /* local_sap */,
00039                               int /* reuse_addr */,
00040                               int flags,
00041                               int perms)
00042 {
00043   ACE_TRACE ("ACE_UPIPE_Connector::connect");
00044   ACE_ASSERT (new_stream.get_handle () == ACE_INVALID_HANDLE);
00045 
00046   ACE_HANDLE handle = ACE::handle_timed_open (timeout,
00047                                               addr.get_path_name (),
00048                                               flags, perms);
00049 
00050   if (handle == ACE_INVALID_HANDLE)
00051     return -1;
00052 #if !defined (ACE_WIN32)
00053   else if (ACE_OS::isastream (handle) != 1)
00054     return -1;
00055 #endif
00056   else // We're connected!
00057     {
00058       ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, new_stream.lock_, -1));
00059 
00060       ACE_UPIPE_Stream *ustream = &new_stream;
00061 
00062       new_stream.set_handle (handle);
00063       new_stream.remote_addr_ = addr; // class copy.
00064       new_stream.reference_count_++;
00065 
00066       // Now send the address of our ACE_UPIPE_Stream over this pipe
00067       // to our corresponding ACE_UPIPE_Acceptor, so he may link the
00068       // two streams.
00069       ssize_t result = ACE_OS::write (handle,
00070                                       (const char *) &ustream,
00071                                       sizeof ustream);
00072       if (result == -1)
00073         ACE_ERROR ((LM_ERROR,
00074                     ACE_LIB_TEXT ("ACE_UPIPE_Connector %p\n"),
00075                     ACE_LIB_TEXT ("write to pipe failed")));
00076 
00077       // Wait for confirmation of stream linking.
00078       ACE_Message_Block *mb_p = 0;
00079 
00080       // Our part is done, wait for server to confirm connection.
00081       result = new_stream.recv (mb_p, 0);
00082 
00083       // Do *not* coalesce the following two checks for result == -1.
00084       // They perform different checks and cannot be merged.
00085       if (result == -1)
00086           ACE_ERROR ((LM_ERROR,
00087                       ACE_LIB_TEXT ("ACE_UPIPE_Connector %p\n"),
00088                       ACE_LIB_TEXT ("no confirmation from server")));
00089       else
00090         // Close down the new_stream at this point in order to
00091         // conserve handles.  Note that we don't need the SPIPE
00092         // connection anymore since we're linked via the Message_Queue
00093         // now.
00094         new_stream.ACE_SPIPE::close ();
00095       return static_cast<int> (result);
00096     }
00097 }
00098 
00099 ACE_END_VERSIONED_NAMESPACE_DECL
00100 
00101 #endif /* ACE_HAS_THREADS */

Generated on Thu Nov 9 09:42:09 2006 for ACE by doxygen 1.3.6