MEM_Connector.cpp

Go to the documentation of this file.
00001 // MEM_Connector.cpp
00002 // MEM_Connector.cpp,v 4.20 2005/10/28 16:14:53 ossama Exp
00003 
00004 #include "ace/MEM_Connector.h"
00005 
00006 ACE_RCSID(ace, MEM_Connector, "MEM_Connector.cpp,v 4.20 2005/10/28 16:14:53 ossama Exp")
00007 
00008 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
00009 
00010 #if !defined (__ACE_INLINE__)
00011 #include "ace/MEM_Connector.inl"
00012 #endif /* __ACE_INLINE__ */
00013 
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 ACE_ALLOC_HOOK_DEFINE(ACE_MEM_Connector)
00017 
00018 void
00019 ACE_MEM_Connector::dump (void) const
00020 {
00021 #if defined (ACE_HAS_DUMP)
00022   ACE_TRACE ("ACE_MEM_Connector::dump");
00023 
00024   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00025   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("\n")));
00026   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00027 #endif /* ACE_HAS_DUMP */
00028 }
00029 
00030 ACE_MEM_Connector::ACE_MEM_Connector (void)
00031   : malloc_options_ (ACE_DEFAULT_BASE_ADDR, 0),
00032     preferred_strategy_ (ACE_MEM_IO::Reactive)
00033 {
00034   ACE_TRACE ("ACE_MEM_Connector::ACE_MEM_Connector");
00035 }
00036 
00037 // Establish a connection.
00038 ACE_MEM_Connector::ACE_MEM_Connector (ACE_MEM_Stream &new_stream,
00039                                       const ACE_INET_Addr &remote_sap,
00040                                       ACE_Time_Value *timeout,
00041                                       const ACE_Addr &local_sap,
00042                                       int reuse_addr,
00043                                       int flags,
00044                                       int perms)
00045   : malloc_options_ (ACE_DEFAULT_BASE_ADDR, 0),
00046     preferred_strategy_ (ACE_MEM_IO::Reactive)
00047 {
00048   ACE_TRACE ("ACE_MEM_Connector::ACE_MEM_Connector");
00049   // This is necessary due to the weird inheritance relationships of
00050   // ACE_MEM_Stream.
00051   this->connect (new_stream,
00052                  remote_sap,
00053                  timeout,
00054                  local_sap,
00055                  reuse_addr,
00056                  flags,
00057                  perms);
00058 }
00059 
00060 int
00061 ACE_MEM_Connector::connect (ACE_MEM_Stream &new_stream,
00062                             const ACE_INET_Addr &remote_sap,
00063                             ACE_Time_Value *timeout,
00064                             const ACE_Addr &local_sap,
00065                             int reuse_addr,
00066                             int flags,
00067                             int perms)
00068 {
00069   ACE_TRACE ("ACE_MEM_Connector::connect");
00070 
00071   if (!this->address_.same_host (remote_sap))
00072     ACE_ERROR_RETURN ((LM_ERROR,
00073                        ACE_LIB_TEXT ("(%P|%t) MEM_Connector can't connect ")
00074                        ACE_LIB_TEXT ("to %s:%d which is not a local endpoint ")
00075                        ACE_LIB_TEXT ("(local address is %s:%d)\n"),
00076                        ACE_TEXT_CHAR_TO_TCHAR (remote_sap.get_host_name ()),
00077                        remote_sap.get_port_number (),
00078                        ACE_TEXT_CHAR_TO_TCHAR (this->address_.get_host_name ()),
00079                        this->address_.get_port_number ()),
00080                       -1);
00081   else
00082     this->address_.set_port_number (remote_sap.get_port_number ());
00083 
00084 
00085   ACE_SOCK_Stream temp_stream;
00086 
00087   if (ACE_SOCK_Connector::connect (temp_stream,
00088                                    this->address_.get_local_addr (),
00089                                    timeout, local_sap,
00090                                    reuse_addr, flags, perms) == -1)
00091     ACE_ERROR_RETURN ((LM_DEBUG,
00092                        ACE_LIB_TEXT ("%p\n"),
00093                        ACE_LIB_TEXT ("ACE_MEM_Connector::connect")),
00094                       -1);
00095 
00096 
00097   ACE_HANDLE new_handle = temp_stream.get_handle ();
00098   new_stream.set_handle (new_handle);
00099   new_stream.disable (ACE_NONBLOCK);
00100   // Do not close the handle.
00101 
00102   // now we should setup the mmap malloc.
00103   ACE_TCHAR buf[MAXPATHLEN];
00104 
00105   // @@ Need to handle timeout here.
00106   ACE_INT16 server_strategy = ACE_MEM_IO::Reactive;
00107   // Receive the signaling strategy theserver support.
00108   if (ACE::recv (new_handle, &server_strategy,
00109                  sizeof (ACE_INT16)) == -1)
00110     ACE_ERROR_RETURN ((LM_DEBUG,
00111                        ACE_LIB_TEXT ("ACE_MEM_Connector::connect error receiving strategy\n")),
00112                       -1);
00113 
00114     // If either side don't support MT, we will not use it.
00115 #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM)
00116   if (! (this->preferred_strategy_ == ACE_MEM_IO::MT &&
00117          server_strategy == ACE_MEM_IO::MT))
00118 #endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */
00119     server_strategy = ACE_MEM_IO::Reactive;
00120 
00121   if (ACE::send (new_handle, &server_strategy,
00122                  sizeof (ACE_INT16)) == -1)
00123     ACE_ERROR_RETURN ((LM_DEBUG,
00124                        ACE_LIB_TEXT ("ACE_MEM_Connector::connect error sending strategy\n")),
00125                       -1);
00126 
00127   ACE_INT16 buf_len;
00128   // Byte-order is not a problem for this read.
00129   if (ACE::recv (new_handle, &buf_len, sizeof (buf_len)) == -1)
00130     ACE_ERROR_RETURN ((LM_DEBUG,
00131                        ACE_LIB_TEXT ("ACE_MEM_Connector::connect error receiving shm filename length\n")),
00132                       -1);
00133 
00134   if (ACE::recv (new_handle, buf, buf_len) == -1)
00135     ACE_ERROR_RETURN ((LM_DEBUG,
00136                        ACE_LIB_TEXT ("ACE_MEM_Connector::connect error receiving shm filename.\n")),
00137                       -1);
00138 
00139   if (new_stream.init (buf,
00140                        static_cast<ACE_MEM_IO::Signal_Strategy> (server_strategy),
00141                        &this->malloc_options_) == -1)
00142     return -1;
00143 
00144   return 0;
00145 }
00146 
00147 ACE_END_VERSIONED_NAMESPACE_DECL
00148 
00149 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */

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