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

Pipe.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Pipe.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $
00003 
00004 #include "ace/Pipe.h"
00005 #include "ace/SOCK_Acceptor.h"
00006 #include "ace/SOCK_Connector.h"
00007 #include "ace/Log_Msg.h"
00008 
00009 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00010 #include "ace/Pipe.i"
00011 #endif
00012 
00013 ACE_RCSID(ace, Pipe, "$Id: Pipe.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00014 
00015 void
00016 ACE_Pipe::dump (void) const
00017 {
00018   ACE_TRACE ("ACE_Pipe::dump");
00019   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00020   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("handles_[0] = %d"), this->handles_[0]));
00021   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nhandles_[1] = %d"), this->handles_[1]));
00022   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00023   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00024 }
00025 
00026 int
00027 ACE_Pipe::open (int buffer_size)
00028 {
00029   ACE_TRACE ("ACE_Pipe::open");
00030 
00031 #if defined (ACE_WIN32) || defined (ACE_LACKS_SOCKETPAIR) || defined (__Lynx__)
00032   ACE_INET_Addr my_addr;
00033   ACE_SOCK_Acceptor acceptor;
00034   ACE_SOCK_Connector connector;
00035   ACE_SOCK_Stream reader;
00036   ACE_SOCK_Stream writer;
00037   int result = 0;
00038 
00039   // Bind listener to any port and then find out what the port was.
00040   if (acceptor.open (ACE_Addr::sap_any) == -1
00041       || acceptor.get_local_addr (my_addr) == -1)
00042     result = -1;
00043   else
00044     {
00045       ACE_INET_Addr sv_addr (my_addr.get_port_number (),
00046                              ACE_LOCALHOST);
00047 
00048       // Establish a connection within the same process.
00049       if (connector.connect (writer, sv_addr) == -1)
00050         result = -1;
00051       else if (acceptor.accept (reader) == -1)
00052         {
00053           writer.close ();
00054           result = -1;
00055         }
00056     }
00057 
00058   // Close down the acceptor endpoint since we don't need it anymore.
00059   acceptor.close ();
00060   if (result == -1)
00061     return -1;
00062 
00063   this->handles_[0] = reader.get_handle ();
00064   this->handles_[1] = writer.get_handle ();
00065 
00066 # if !defined (ACE_LACKS_TCP_NODELAY)
00067   int one = 1;
00068 
00069   // Make sure that the TCP stack doesn't try to buffer small writes.
00070   // Since this communication is purely local to the host it doesn't
00071   // affect network performance.
00072 
00073   if (writer.set_option (ACE_IPPROTO_TCP,
00074                          TCP_NODELAY,
00075                          &one,
00076                          sizeof one) == -1)
00077     {
00078       this->close ();
00079       return -1;
00080     }
00081 # endif /* ! ACE_LACKS_TCP_NODELAY */
00082 
00083 # if defined (ACE_LACKS_SOCKET_BUFSIZ)
00084     ACE_UNUSED_ARG (buffer_size);
00085 # else  /* ! ACE_LACKS_SOCKET_BUFSIZ */
00086   if (reader.set_option (SOL_SOCKET,
00087                          SO_SNDBUF,
00088                          ACE_reinterpret_cast (void *, &buffer_size),
00089                          sizeof (buffer_size)) == -1
00090       && errno != ENOTSUP)
00091     {
00092       this->close ();
00093       return -1;
00094     }
00095   else if (writer.set_option (SOL_SOCKET,
00096                               SO_RCVBUF,
00097                               ACE_reinterpret_cast (void *, &buffer_size),
00098                               sizeof (buffer_size)) == -1
00099            && errno != ENOTSUP)
00100     {
00101       this->close ();
00102       return -1;
00103     }
00104 # endif /* ! ACE_LACKS_SOCKET_BUFSIZ */
00105 
00106 #elif defined (ACE_HAS_STREAM_PIPES) || defined (__QNX__)
00107   ACE_UNUSED_ARG (buffer_size);
00108   if (ACE_OS::pipe (this->handles_) == -1)
00109     ACE_ERROR_RETURN ((LM_ERROR,
00110                        ACE_LIB_TEXT ("%p\n"),
00111                        ACE_LIB_TEXT ("pipe")),
00112                       -1);
00113 
00114 #if !defined(__QNX__)
00115   int arg = RMSGN;
00116 
00117   // Enable "msg no discard" mode, which ensures that record
00118   // boundaries are maintained when messages are sent and received.
00119   if (ACE_OS::ioctl (this->handles_[0],
00120                      I_SRDOPT,
00121                      (void *) arg) == -1
00122       || ACE_OS::ioctl (this->handles_[1],
00123                         I_SRDOPT,
00124                         (void *) arg) == -1)
00125     {
00126       this->close ();
00127       ACE_ERROR_RETURN ((LM_ERROR,
00128                          ACE_LIB_TEXT ("%p\n"),
00129                          ACE_LIB_TEXT ("ioctl")), -1);
00130     }
00131 #endif /* __QNX__ */
00132 
00133 #else  /* ! ACE_WIN32 && ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */
00134   if (ACE_OS::socketpair (AF_UNIX,
00135                           SOCK_STREAM,
00136                           0,
00137                           this->handles_) == -1)
00138     ACE_ERROR_RETURN ((LM_ERROR,
00139                        ACE_LIB_TEXT ("%p\n"),
00140                        ACE_LIB_TEXT ("socketpair")),
00141                       -1);
00142 # if defined (ACE_LACKS_SOCKET_BUFSIZ)
00143   ACE_UNUSED_ARG (buffer_size);
00144 # else  /* ! ACE_LACKS_SOCKET_BUFSIZ */
00145   if (ACE_OS::setsockopt (this->handles_[0],
00146                           SOL_SOCKET,
00147                           SO_RCVBUF,
00148                           ACE_reinterpret_cast (const char *,
00149                                                 &buffer_size),
00150                           sizeof (buffer_size)) == -1
00151       && errno != ENOTSUP)
00152     {
00153       this->close ();
00154       return -1;
00155     }
00156   if (ACE_OS::setsockopt (this->handles_[1],
00157                           SOL_SOCKET,
00158                           SO_SNDBUF,
00159                           ACE_reinterpret_cast (const char *, &buffer_size),
00160                           sizeof (buffer_size)) == -1
00161       && errno != ENOTSUP)
00162     {
00163       this->close ();
00164       return -1;
00165     }
00166 # endif /* ! ACE_LACKS_SOCKET_BUFSIZ */
00167 #endif  /* ! ACE_WIN32 && ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */
00168   // Point both the read and write HANDLES to the appropriate socket
00169   // HANDLEs.
00170 
00171   return 0;
00172 }
00173 
00174 int
00175 ACE_Pipe::open (ACE_HANDLE handles[2])
00176 {
00177   ACE_TRACE ("ACE_Pipe::open");
00178 
00179   if (this->open () == -1)
00180     return -1;
00181   else
00182     {
00183       handles[0] = this->handles_[0];
00184       handles[1] = this->handles_[1];
00185       return 0;
00186     }
00187 }
00188 
00189 // Do nothing...
00190 
00191 ACE_Pipe::ACE_Pipe (void)
00192 {
00193   ACE_TRACE ("ACE_Pipe::ACE_Pipe");
00194 
00195   this->handles_[0] = ACE_INVALID_HANDLE;
00196   this->handles_[1] = ACE_INVALID_HANDLE;
00197 }
00198 
00199 ACE_Pipe::ACE_Pipe (ACE_HANDLE handles[2])
00200 {
00201   ACE_TRACE ("ACE_Pipe::ACE_Pipe");
00202 
00203   if (this->open (handles) == -1)
00204     ACE_ERROR ((LM_ERROR,
00205                 ACE_LIB_TEXT ("ACE_Pipe::ACE_Pipe")));
00206 }
00207 
00208 ACE_Pipe::ACE_Pipe (ACE_HANDLE read,
00209                     ACE_HANDLE write)
00210 {
00211   ACE_TRACE ("ACE_Pipe::ACE_Pipe");
00212   this->handles_[0] = read;
00213   this->handles_[1] = write;
00214 }
00215 
00216 int
00217 ACE_Pipe::close (void)
00218 {
00219   ACE_TRACE ("ACE_Pipe::close");
00220 
00221   int result = 0;
00222 
00223   // Note that the following will work even if we aren't closing down
00224   // sockets because <ACE_OS::closesocket> will just call <::close> in
00225   // that case!
00226 
00227   if (this->handles_[0] != ACE_INVALID_HANDLE)
00228     result = ACE_OS::closesocket (this->handles_[0]);
00229   this->handles_[0] = ACE_INVALID_HANDLE;
00230 
00231   if (this->handles_[1] != ACE_INVALID_HANDLE)
00232     result |= ACE_OS::closesocket (this->handles_[1]);
00233   this->handles_[1] = ACE_INVALID_HANDLE;
00234 
00235   return result;
00236 }

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