00001 
00002 
00003 #include "ace/UPIPE_Connector.h"
00004 
00005 ACE_RCSID(ace, UPIPE_Connector, "$Id: UPIPE_Connector.cpp 79134 2007-07-31 18:23:50Z johnnyw $")
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 
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 
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 & ,
00039                               int ,
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 
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; 
00064       new_stream.reference_count_++;
00065 
00066       
00067       
00068       
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_TEXT ("ACE_UPIPE_Connector %p\n"),
00075                     ACE_TEXT ("write to pipe failed")));
00076 
00077       
00078       ACE_Message_Block *mb_p = 0;
00079 
00080       
00081       result = new_stream.recv (mb_p, 0);
00082 
00083       
00084       
00085       if (result == -1)
00086           ACE_ERROR ((LM_ERROR,
00087                       ACE_TEXT ("ACE_UPIPE_Connector %p\n"),
00088                       ACE_TEXT ("no confirmation from server")));
00089       else
00090         
00091         
00092         
00093         
00094         new_stream.ACE_SPIPE::close ();
00095       return static_cast<int> (result);
00096     }
00097 }
00098 
00099 ACE_END_VERSIONED_NAMESPACE_DECL
00100 
00101 #endif