Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

SPIPE_Connector.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // SPIPE_Connector.cpp
00003 // $Id: SPIPE_Connector.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $
00004 
00005 #include "ace/SPIPE_Connector.h"
00006 #include "ace/Log_Msg.h"
00007 
00008 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00009 #include "ace/SPIPE_Connector.i"
00010 #endif
00011 
00012 ACE_RCSID(ace, SPIPE_Connector, "$Id: SPIPE_Connector.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00013 
00014 ACE_ALLOC_HOOK_DEFINE(ACE_SPIPE_Connector)
00015 
00016 // Creates a Local ACE_SPIPE.
00017 
00018 ACE_SPIPE_Connector::ACE_SPIPE_Connector (ACE_SPIPE_Stream &new_io,
00019                                           const ACE_SPIPE_Addr &remote_sap,
00020                                           ACE_Time_Value *timeout,
00021                                           const ACE_Addr & local_sap,
00022                                           int reuse_addr,
00023                                           int flags,
00024                                           int perms,
00025             LPSECURITY_ATTRIBUTES sa,
00026             int pipe_mode)
00027 {
00028   ACE_TRACE ("ACE_SPIPE_Connector::ACE_SPIPE_Connector");
00029   if (this->connect (new_io, remote_sap, timeout, local_sap,
00030                      reuse_addr, flags, perms, sa, pipe_mode) == -1
00031       && timeout != 0 && !(errno == EWOULDBLOCK || errno == ETIME))
00032     ACE_ERROR ((LM_ERROR, ACE_LIB_TEXT ("address %s, %p\n"),
00033                remote_sap.get_path_name (), ACE_LIB_TEXT ("ACE_SPIPE_Connector")));
00034 }
00035 
00036 void
00037 ACE_SPIPE_Connector::dump (void) const
00038 {
00039   ACE_TRACE ("ACE_SPIPE_Connector::dump");
00040 }
00041 
00042 ACE_SPIPE_Connector::ACE_SPIPE_Connector (void)
00043 {
00044   ACE_TRACE ("ACE_SPIPE_Connector::ACE_SPIPE_Connector");
00045 }
00046 
00047 int
00048 ACE_SPIPE_Connector::connect (ACE_SPIPE_Stream &new_io,
00049                               const ACE_SPIPE_Addr &remote_sap,
00050                               ACE_Time_Value *timeout,
00051                               const ACE_Addr & /* local_sap */,
00052                               int /* reuse_addr */,
00053                               int flags,
00054                               int perms,
00055                               LPSECURITY_ATTRIBUTES sa,
00056                               int pipe_mode)
00057 {
00058   ACE_TRACE ("ACE_SPIPE_Connector::connect");
00059   // Make darn sure that the O_CREAT flag is not set!
00060 #if ! defined (ACE_PSOS_DIAB_MIPS)
00061   ACE_CLR_BITS (flags, O_CREAT);
00062 # endif /* !ACE_PSOS_DIAB_MIPS */
00063 
00064   ACE_HANDLE handle;
00065 
00066   ACE_UNUSED_ARG (pipe_mode);
00067 #if defined (ACE_WIN32) && \
00068    !defined (ACE_HAS_PHARLAP) && !defined (ACE_HAS_WINCE)
00069   // We need to allow for more than one attempt to connect,
00070   // calculate the absolute time at which we give up.
00071   ACE_Time_Value absolute_time;
00072   if (timeout != 0)
00073     absolute_time = ACE_OS::gettimeofday () + *timeout;
00074 
00075   // Loop until success or failure.
00076   for (;;)
00077     {
00078       handle = ACE_OS::open (remote_sap.get_path_name(), flags, perms, sa);
00079       if (handle != ACE_INVALID_HANDLE)
00080         // Success!
00081         break;
00082 
00083       // Check if we have a busy pipe condition.
00084       if (::GetLastError() != ERROR_PIPE_BUSY)
00085         // Nope, this is a failure condition.
00086         break;
00087 
00088       // This will hold the time out value used in the ::WaitNamedPipe
00089       // call.
00090       DWORD time_out_value;
00091 
00092       // Check if we are to block until we connect.
00093       if (timeout == 0)
00094         // Wait for as long as it takes.
00095         time_out_value = NMPWAIT_WAIT_FOREVER;
00096       else
00097         {
00098           // Calculate the amount of time left to wait.
00099           ACE_Time_Value relative_time (absolute_time - ACE_OS::gettimeofday ());
00100           // Check if we have run out of time.
00101           if (relative_time <= ACE_Time_Value::zero)
00102             {
00103               // Mimick the errno value returned by
00104               // ACE_Handle_Ops::handle_timed_open.
00105               if (*timeout == ACE_Time_Value::zero)
00106                 errno = EWOULDBLOCK;
00107               else
00108                 errno = ETIMEDOUT;
00109               // Exit the connect loop with the failure.
00110               break;
00111             }
00112           // Get the amount of time remaining for ::WaitNamedPipe.
00113           time_out_value = relative_time.msec ();
00114 
00115         }
00116 
00117       // Wait for the named pipe to become available.
00118       ACE_TEXT_WaitNamedPipe (remote_sap.get_path_name (),
00119                               time_out_value);
00120 
00121       // Regardless of the return value, we'll do one more attempt to
00122       // connect to see if it is now available and to return
00123       // consistent error values.
00124     }
00125 
00126   // Set named pipe mode if we have a valid handle.
00127   if (handle != ACE_INVALID_HANDLE)
00128     {
00129       // Check if we are changing the pipe mode from the default.
00130       if (pipe_mode != (PIPE_READMODE_BYTE | PIPE_WAIT))
00131         {
00132           DWORD dword_pipe_mode = pipe_mode;
00133           if (!::SetNamedPipeHandleState (handle,
00134                                           &dword_pipe_mode,
00135                                           0,
00136                                           0))
00137             {
00138               // We were not able to put the pipe into the requested
00139               // mode.
00140               ACE_OS::close (handle);
00141               handle = ACE_INVALID_HANDLE;
00142             }
00143         }
00144     }
00145 #else /* ACE_WIN32 && !ACE_HAS_PHARLAP */
00146   handle = ACE_Handle_Ops::handle_timed_open (timeout,
00147                                               remote_sap.get_path_name (),
00148                                               flags, perms, sa);
00149 #endif /* !ACE_WIN32 || ACE_HAS_PHARLAP || ACE_HAS_WINCE */
00150 
00151   new_io.set_handle (handle);
00152   new_io.remote_addr_ = remote_sap; // class copy.
00153 
00154   return handle == ACE_INVALID_HANDLE ? -1 : 0;
00155 }

Generated on Mon Jun 16 11:21:22 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002