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

SOCK_Acceptor.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // SOCK_Acceptor.cpp
00003 // $Id: SOCK_Acceptor.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $
00004 
00005 #include "ace/SOCK_Acceptor.h"
00006 #include "ace/Log_Msg.h"
00007 
00008 #if !defined (ACE_HAS_WINCE)
00009 #include "ace/OS_QoS.h"
00010 #endif  // ACE_HAS_WINCE
00011 
00012 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00013 #include "ace/SOCK_Acceptor.i"
00014 #endif /* ACE_LACKS_INLINE_FUNCTIONS */
00015 
00016 #include "ace/Synch.h"
00017 
00018 ACE_RCSID(ace, SOCK_Acceptor, "$Id: SOCK_Acceptor.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00019 
00020 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_Acceptor)
00021 
00022 // Do nothing routine for constructor.
00023 
00024 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (void)
00025 {
00026   ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00027 }
00028 
00029 // Performs the timed accept operation.
00030 
00031 int
00032 ACE_SOCK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
00033                                         int restart,
00034                                         int &in_blocking_mode) const
00035 {
00036   ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_start");
00037 
00038   ACE_HANDLE handle = this->get_handle ();
00039 
00040   // Handle the case where we're doing a timed <accept>.
00041   if (timeout != 0)
00042     {
00043       if (ACE::handle_timed_accept (handle,
00044                                     timeout,
00045                                     restart) == -1)
00046         return -1;
00047       else
00048         {
00049           in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
00050                                                ACE_NONBLOCK);
00051           // Set the handle into non-blocking mode if it's not already
00052           // in it.
00053           if (in_blocking_mode
00054               && ACE::set_flags (handle,
00055                                  ACE_NONBLOCK) == -1)
00056             return -1;
00057         }
00058     }
00059 
00060   return 0;
00061 }
00062 
00063 int
00064 ACE_SOCK_Acceptor::shared_accept_finish (ACE_SOCK_Stream new_stream,
00065                                          int in_blocking_mode,
00066                                          int reset_new_handle) const
00067 {
00068   ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_finish ()");
00069 
00070   ACE_HANDLE new_handle = new_stream.get_handle ();
00071 
00072   // Check to see if we were originally in blocking mode, and if so,
00073   // set the <new_stream>'s handle and <this> handle to be in blocking
00074   // mode.
00075   if (in_blocking_mode)
00076     {
00077       // Save/restore errno.
00078       ACE_Errno_Guard error (errno);
00079 
00080       // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
00081       // originally.
00082       ACE::clr_flags (this->get_handle (),
00083                       ACE_NONBLOCK);
00084       ACE::clr_flags (new_handle,
00085                       ACE_NONBLOCK);
00086     }
00087 
00088 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00089   if (reset_new_handle)
00090     // Reset the event association inherited by the new handle.
00091     ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
00092 #else
00093   ACE_UNUSED_ARG (reset_new_handle);
00094 #endif /* ACE_WIN32 */
00095 
00096   return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
00097 }
00098 
00099 // General purpose routine for accepting new connections.
00100 
00101 int
00102 ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream &new_stream,
00103                            ACE_Addr *remote_addr,
00104                            ACE_Time_Value *timeout,
00105                            int restart,
00106                            int reset_new_handle) const
00107 {
00108   ACE_TRACE ("ACE_SOCK_Acceptor::accept");
00109 
00110   int in_blocking_mode = 0;
00111   if (this->shared_accept_start (timeout,
00112                                  restart,
00113                                  in_blocking_mode) == -1)
00114     return -1;
00115   else
00116     {
00117       // On Win32 the third parameter to <accept> must be a NULL
00118       // pointer if we want to ignore the client's address.
00119       int *len_ptr = 0;
00120       sockaddr *addr = 0;
00121       int len = 0;
00122 
00123       if (remote_addr != 0)
00124         {
00125           len = remote_addr->get_size ();
00126           len_ptr = &len;
00127           addr = (sockaddr *) remote_addr->get_addr ();
00128         }
00129 
00130       do
00131         new_stream.set_handle (ACE_OS::accept (this->get_handle (),
00132                                                addr,
00133                                                len_ptr));
00134       while (new_stream.get_handle () == ACE_INVALID_HANDLE
00135              && restart != 0
00136              && errno == EINTR
00137              && timeout == 0);
00138 
00139       // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
00140       // is known.
00141       if (new_stream.get_handle () != ACE_INVALID_HANDLE
00142           && remote_addr != 0)
00143         {
00144           remote_addr->set_size (len);
00145           remote_addr->set_type (addr->sa_family);
00146         }
00147     }
00148 
00149   return this->shared_accept_finish (new_stream,
00150                                      in_blocking_mode,
00151                                      reset_new_handle);
00152 }
00153 
00154 #if !defined (ACE_HAS_WINCE)
00155 int
00156 ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream &new_stream,
00157                            ACE_Accept_QoS_Params qos_params,
00158                            ACE_Addr *remote_addr,
00159                            ACE_Time_Value *timeout,
00160                            int restart,
00161                            int reset_new_handle) const
00162 {
00163   ACE_TRACE ("ACE_SOCK_Acceptor::accept");
00164 
00165   int in_blocking_mode = 0;
00166   if (this->shared_accept_start (timeout,
00167                                  restart,
00168                                  in_blocking_mode) == -1)
00169     return -1;
00170   else
00171     {
00172       // On Win32 the third parameter to <accept> must be a NULL
00173       // pointer if we want to ignore the client's address.
00174       int *len_ptr = 0;
00175       int len = 0;
00176       sockaddr *addr = 0;
00177 
00178       if (remote_addr != 0)
00179         {
00180           len = remote_addr->get_size ();
00181           len_ptr = &len;
00182           addr = (sockaddr *) remote_addr->get_addr ();
00183         }
00184 
00185       do
00186         new_stream.set_handle (ACE_OS::accept (this->get_handle (),
00187                                                addr,
00188                                                len_ptr,
00189                                                qos_params));
00190       while (new_stream.get_handle () == ACE_INVALID_HANDLE
00191              && restart != 0
00192              && errno == EINTR
00193              && timeout == 0);
00194 
00195       // Reset the size of the addr, which is only necessary for UNIX
00196       // domain sockets.
00197       if (new_stream.get_handle () != ACE_INVALID_HANDLE
00198           && remote_addr != 0)
00199         remote_addr->set_size (len);
00200     }
00201 
00202   return this->shared_accept_finish (new_stream,
00203                                      in_blocking_mode,
00204                                      reset_new_handle);
00205 }
00206 #endif  // ACE_HAS_WINCE
00207 
00208 void
00209 ACE_SOCK_Acceptor::dump (void) const
00210 {
00211   ACE_TRACE ("ACE_SOCK_Acceptor::dump");
00212 }
00213 
00214 int
00215 ACE_SOCK_Acceptor::shared_open (const ACE_Addr &local_sap,
00216                                 int protocol_family,
00217                                 int backlog)
00218 {
00219   ACE_TRACE ("ACE_SOCK_Acceptor::shared_open");
00220   int error = 0;
00221 
00222 #if defined (ACE_HAS_IPV6)
00223   ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
00224 
00225   if (protocol_family == PF_INET6)
00226     {
00227       sockaddr_in6 local_inet6_addr;
00228       ACE_OS::memset (ACE_reinterpret_cast (void *,
00229                                             &local_inet6_addr),
00230                       0,
00231                       sizeof local_inet6_addr);
00232 
00233       if (local_sap == ACE_Addr::sap_any)
00234         {
00235           local_inet6_addr.sin6_family = AF_INET6;
00236           local_inet6_addr.sin6_port = 0;
00237           local_inet6_addr.sin6_addr = in6addr_any;
00238         }
00239       else
00240         local_inet6_addr = *ACE_reinterpret_cast (sockaddr_in6 *,
00241                                                   local_sap.get_addr ());
00242 
00243       // We probably don't need a bind_port written here.
00244       // There are currently no supported OS's that define
00245       // ACE_LACKS_WILDCARD_BIND.
00246       if (ACE_OS::bind (this->get_handle (),
00247                         ACE_reinterpret_cast (sockaddr *,
00248                                               &local_inet6_addr),
00249                         sizeof local_inet6_addr) == -1)
00250         error = 1;
00251     }
00252   else
00253 #endif
00254   if (protocol_family == PF_INET)
00255     {
00256       sockaddr_in local_inet_addr;
00257       ACE_OS::memset (ACE_reinterpret_cast (void *,
00258                                             &local_inet_addr),
00259                       0,
00260                       sizeof local_inet_addr);
00261 
00262       if (local_sap == ACE_Addr::sap_any)
00263         {
00264           local_inet_addr.sin_port = 0;
00265         }
00266       else
00267         local_inet_addr = *ACE_reinterpret_cast (sockaddr_in *,
00268                                                  local_sap.get_addr ());
00269       if (local_inet_addr.sin_port == 0)
00270         {
00271           if (ACE::bind_port (this->get_handle ()) == -1)
00272             error = 1;
00273         }
00274       else if (ACE_OS::bind (this->get_handle (),
00275                              ACE_reinterpret_cast (sockaddr *,
00276                                                    &local_inet_addr),
00277                              sizeof local_inet_addr) == -1)
00278         error = 1;
00279     }
00280   else if (ACE_OS::bind (this->get_handle (),
00281                          (sockaddr *) local_sap.get_addr (),
00282                          local_sap.get_size ()) == -1)
00283     error = 1;
00284 
00285   if (error != 0
00286       || ACE_OS::listen (this->get_handle (),
00287                          backlog) == -1)
00288     {
00289       error = 1;
00290       this->close ();
00291     }
00292 
00293   return error ? -1 : 0;
00294 }
00295 
00296 int
00297 ACE_SOCK_Acceptor::open (const ACE_Addr &local_sap,
00298                          ACE_Protocol_Info *protocolinfo,
00299                          ACE_SOCK_GROUP g,
00300                          u_long flags,
00301                          int reuse_addr,
00302                          int protocol_family,
00303                          int backlog,
00304                          int protocol)
00305 {
00306   ACE_TRACE ("ACE_SOCK_Acceptor::open");
00307 
00308   if (protocol_family == PF_UNSPEC)
00309     protocol_family = local_sap.get_type ();
00310 
00311   if (ACE_SOCK::open (SOCK_STREAM,
00312                       protocol_family,
00313                       protocol,
00314                       protocolinfo,
00315                       g,
00316                       flags,
00317                       reuse_addr) == -1)
00318     return -1;
00319   else
00320     return this->shared_open (local_sap,
00321                               protocol_family,
00322                               backlog);
00323 }
00324 
00325 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
00326                                       ACE_Protocol_Info *protocolinfo,
00327                                       ACE_SOCK_GROUP g,
00328                                       u_long flags,
00329                                       int reuse_addr,
00330                                       int protocol_family,
00331                                       int backlog,
00332                                       int protocol)
00333 {
00334   ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00335   if (this->open (local_sap,
00336                   protocolinfo,
00337                   g,
00338                   flags,
00339                   reuse_addr,
00340                   protocol_family,
00341                   backlog,
00342                   protocol) == -1)
00343     ACE_ERROR ((LM_ERROR,
00344                 ACE_LIB_TEXT ("%p\n"),
00345                 ACE_LIB_TEXT ("ACE_SOCK_Acceptor")));
00346 }
00347 
00348 // General purpose routine for performing server ACE_SOCK creation.
00349 
00350 int
00351 ACE_SOCK_Acceptor::open (const ACE_Addr &local_sap,
00352                          int reuse_addr,
00353                          int protocol_family,
00354                          int backlog,
00355                          int protocol)
00356 {
00357   ACE_TRACE ("ACE_SOCK_Acceptor::open");
00358 
00359   if (local_sap != ACE_Addr::sap_any)
00360     protocol_family = local_sap.get_type ();
00361   else if (protocol_family == PF_UNSPEC)
00362     {
00363 #if defined (ACE_HAS_IPV6)
00364       protocol_family = ACE_Sock_Connect::ipv6_enabled () ? PF_INET6 : PF_INET;
00365 #else
00366       protocol_family = PF_INET;
00367 #endif /* ACE_HAS_IPV6 */
00368     }
00369 
00370   if (ACE_SOCK::open (SOCK_STREAM,
00371                       protocol_family,
00372                       protocol,
00373                       reuse_addr) == -1)
00374     return -1;
00375   else
00376     return this->shared_open (local_sap,
00377                               protocol_family,
00378                               backlog);
00379 }
00380 
00381 // General purpose routine for performing server ACE_SOCK creation.
00382 
00383 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
00384                                       int reuse_addr,
00385                                       int protocol_family,
00386                                       int backlog,
00387                                       int protocol)
00388 {
00389   ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00390   if (this->open (local_sap,
00391                   reuse_addr,
00392                   protocol_family,
00393                   backlog,
00394                   protocol) == -1)
00395     ACE_ERROR ((LM_ERROR,
00396                 ACE_LIB_TEXT ("%p\n"),
00397                 ACE_LIB_TEXT ("ACE_SOCK_Acceptor")));
00398 }
00399 
00400 int
00401 ACE_SOCK_Acceptor::close (void)
00402 {
00403   return ACE_SOCK::close ();
00404 }

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