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

MEM_Acceptor.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // MEM_Acceptor.cpp
00003 // $Id: MEM_Acceptor.cpp,v 1.1.1.4.2.2 2003/04/04 16:30:48 taoadmin Exp $
00004 
00005 #include "ace/MEM_Acceptor.h"
00006 
00007 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
00008 
00009 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00010 #include "ace/MEM_Acceptor.i"
00011 #endif /* ACE_LACKS_INLINE_FUNCTIONS */
00012 
00013 ACE_RCSID(ace, MEM_Acceptor, "$Id: MEM_Acceptor.cpp,v 1.1.1.4.2.2 2003/04/04 16:30:48 taoadmin Exp $")
00014 
00015 ACE_ALLOC_HOOK_DEFINE(ACE_MEM_Acceptor)
00016 
00017 void
00018 ACE_MEM_Acceptor::dump (void) const
00019 {
00020   ACE_TRACE ("ACE_MEM_Acceptor::dump");
00021 }
00022 
00023 // Do nothing routine for constructor.
00024 
00025 ACE_MEM_Acceptor::ACE_MEM_Acceptor (void)
00026   : mmap_prefix_ (0),
00027     malloc_options_ (ACE_DEFAULT_BASE_ADDR, 0),
00028     preferred_strategy_ (ACE_MEM_IO::Reactive)
00029 {
00030   ACE_TRACE ("ACE_MEM_Acceptor::ACE_MEM_Acceptor");
00031 }
00032 
00033 ACE_MEM_Acceptor::~ACE_MEM_Acceptor (void)
00034 {
00035   ACE_TRACE ("ACE_MEM_Acceptor::~ACE_MEM_Acceptor");
00036   delete[] this->mmap_prefix_;
00037 }
00038 
00039 // General purpose routine for performing server ACE_SOCK creation.
00040 
00041 ACE_MEM_Acceptor::ACE_MEM_Acceptor (const ACE_MEM_Addr &remote_sap,
00042                                     int reuse_addr,
00043                                     int backlog,
00044                                     int protocol)
00045   : mmap_prefix_ (0),
00046     malloc_options_ (ACE_DEFAULT_BASE_ADDR, 0),
00047     preferred_strategy_ (ACE_MEM_IO::Reactive)
00048 {
00049   ACE_TRACE ("ACE_MEM_Acceptor::ACE_MEM_Acceptor");
00050   if (this->open (remote_sap,
00051                   reuse_addr,
00052                   backlog,
00053                   protocol) == -1)
00054     ACE_ERROR ((LM_ERROR,
00055                 ACE_LIB_TEXT ("ACE_MEM_Acceptor::ACE_MEM_Acceptor")));
00056 }
00057 
00058 int
00059 ACE_MEM_Acceptor::open (const ACE_MEM_Addr &remote_sap,
00060                         int reuse_addr,
00061                         int back_log,
00062                         int protocol)
00063 {
00064   ACE_TRACE ("ACE_MEM_Acceptor::open");
00065   return this->ACE_SOCK_Acceptor::open (remote_sap.get_local_addr (),
00066                                         reuse_addr,
00067                                         PF_INET,
00068                                         back_log,
00069                                         protocol);
00070 }
00071 
00072 // General purpose routine for accepting new connections.
00073 
00074 int
00075 ACE_MEM_Acceptor::accept (ACE_MEM_Stream &new_stream,
00076                           ACE_MEM_Addr *remote_sap,
00077                           ACE_Time_Value *timeout,
00078                           int restart,
00079                           int reset_new_handle)
00080 {
00081   ACE_TRACE ("ACE_MEM_Acceptor::accept");
00082 
00083   int *len_ptr = 0;
00084   sockaddr *addr = 0;
00085 
00086 #if defined (__hpux)
00087   // We need to allow the accept to block for HP-UX
00088   this->disable (ACE_NONBLOCK);
00089 #endif /* __hpux */
00090 
00091   int in_blocking_mode = 1;
00092   if (this->shared_accept_start (timeout,
00093                                  restart,
00094                                  in_blocking_mode) == -1)
00095     return -1;
00096   else
00097     {
00098       do
00099         new_stream.set_handle (ACE_OS::accept (this->get_handle (),
00100                                                addr,
00101                                                len_ptr));
00102       while (new_stream.get_handle () == ACE_INVALID_HANDLE
00103              && restart != 0
00104              && errno == EINTR
00105              && timeout == 0);
00106 
00107       if (remote_sap != 0)
00108         {
00109           ACE_INET_Addr temp (ACE_reinterpret_cast (sockaddr_in *, addr),
00110                               *len_ptr);
00111           remote_sap->set_port_number(temp.get_port_number ());
00112         }
00113     }
00114 
00115   if (this->shared_accept_finish (new_stream,
00116                                   in_blocking_mode,
00117                                   reset_new_handle) == -1)
00118     return -1;
00119 
00120   // Allocate 2 * MAXPATHLEN so we can accomodate the unique
00121   // name that gets appended later
00122   ACE_TCHAR buf [2 * MAXPATHLEN + 1];
00123 
00124   ACE_INET_Addr local_addr;
00125   if (new_stream.get_local_addr (local_addr) == -1)
00126     return -1;
00127 
00128   if (this->mmap_prefix_ != 0)
00129     {
00130       ACE_OS::sprintf (buf,
00131                        ACE_LIB_TEXT ("%s_%d_"),
00132                        this->mmap_prefix_,
00133                        local_addr.get_port_number ());
00134     }
00135   else
00136     {
00137       ACE_TCHAR name[25];
00138       // - 24 is so we can append name to the end.
00139       if (ACE_Lib_Find::get_temp_dir (buf, MAXPATHLEN - 24) == -1)
00140         {
00141           ACE_ERROR ((LM_ERROR,
00142                       ACE_LIB_TEXT ("Temporary path too long, ")
00143                       ACE_LIB_TEXT ("defaulting to current directory\n")));
00144           buf[0] = 0;
00145         }
00146 
00147       ACE_OS::sprintf (name,
00148                        ACE_LIB_TEXT ("MEM_Acceptor_%d_"),
00149                        local_addr.get_port_number ());
00150       ACE_OS::strcat (buf, name);
00151     }
00152   ACE_TCHAR unique [MAXPATHLEN];
00153   ACE_OS::unique_name (&new_stream, unique, MAXPATHLEN);
00154 
00155   ACE_OS::strcat (buf, unique);
00156 
00157   // Make sure we have a fresh start.
00158   ACE_OS::unlink (buf);
00159 
00160   new_stream.disable (ACE_NONBLOCK);
00161   ACE_HANDLE new_handle = new_stream.get_handle ();
00162 
00163   // Protocol negociation:
00164   //   Tell the client side what level of signaling strategy
00165   //   we support.
00166   ACE_INT16 client_signaling =
00167 #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM)
00168     this->preferred_strategy_;
00169 #else
00170     // We don't support MT.
00171     ACE_MEM_IO::Reactive;
00172 #endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */
00173   if (ACE::send (new_handle, &client_signaling,
00174                  sizeof (ACE_INT16)) == -1)
00175     ACE_ERROR_RETURN ((LM_DEBUG,
00176                        ACE_LIB_TEXT ("ACE_MEM_Acceptor::accept error sending strategy\n")),
00177                       -1);
00178 
00179   //   Now we get the signaling strategy the client support.
00180   if (ACE::recv (new_handle, &client_signaling,
00181                  sizeof (ACE_INT16)) == -1)
00182     ACE_ERROR_RETURN ((LM_DEBUG,
00183                        ACE_LIB_TEXT ("ACE_MEM_Acceptor::%p error receiving strategy\n"), "accept"),
00184                       -1);
00185 
00186   // Client will decide what signaling strategy to use.
00187 
00188   // Now set up the shared memory malloc pool.
00189   if (new_stream.init (buf, ACE_static_cast (ACE_MEM_IO::Signal_Strategy, client_signaling),
00190                        &this->malloc_options_) == -1)
00191     return -1;
00192 
00193   // @@ Need to handle timeout here.
00194   ACE_UINT16 buf_len = ACE_static_cast (ACE_UINT16,
00195                                         (ACE_OS::strlen (buf) + 1) *
00196                                          sizeof (ACE_TCHAR));
00197 
00198   // No need to worry about byte-order because both parties should always
00199   // be on the same machine.
00200   if (ACE::send (new_handle, &buf_len, sizeof (ACE_UINT16)) == -1)
00201     return -1;
00202 
00203   // Now send the pathname of the mmap file.
00204   if (ACE::send (new_handle, buf, buf_len) == -1)
00205     return -1;
00206   return 0;
00207 }
00208 
00209 int
00210 ACE_MEM_Acceptor::shared_accept_finish (ACE_MEM_Stream new_stream,
00211                                         int in_blocking_mode,
00212                                         int reset_new_handle) const
00213 {
00214   ACE_TRACE ("ACE_MEM_Acceptor::shared_accept_finish ()");
00215 
00216   ACE_HANDLE new_handle = new_stream.get_handle ();
00217 
00218   // Check to see if we were originally in blocking mode, and if so,
00219   // set the <new_stream>'s handle and <this> handle to be in blocking
00220   // mode.
00221   if (in_blocking_mode)
00222     {
00223       // Save/restore errno.
00224       ACE_Errno_Guard error (errno);
00225 
00226       // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
00227       // originally.
00228       ACE_Flag_Manip::clr_flags (this->get_handle (),
00229                                  ACE_NONBLOCK);
00230       ACE_Flag_Manip::clr_flags (new_handle,
00231                                  ACE_NONBLOCK);
00232     }
00233 
00234 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00235   if (reset_new_handle)
00236     // Reset the event association inherited by the new handle.
00237     ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
00238 #else
00239   ACE_UNUSED_ARG (reset_new_handle);
00240 #endif /* ACE_WIN32 */
00241   if (new_handle == ACE_INVALID_HANDLE)
00242     return -1;
00243 
00244   return 0;
00245 }
00246 
00247 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */

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