Asynch_Acceptor.cpp

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 // $Id: Asynch_Acceptor.cpp 79134 2007-07-31 18:23:50Z johnnyw $
00003 
00004 #ifndef ACE_ASYNCH_ACCEPTOR_C
00005 #define ACE_ASYNCH_ACCEPTOR_C
00006 
00007 #include "ace/Asynch_Acceptor.h"
00008 
00009 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00010 # pragma once
00011 #endif /* ACE_LACKS_PRAGMA_ONCE */
00012 
00013 ACE_RCSID(ace, Asynch_Acceptor, "$Id: Asynch_Acceptor.cpp 79134 2007-07-31 18:23:50Z johnnyw $")
00014 
00015 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
00016 // This only works on platforms that support async i/o.
00017 
00018 #include "ace/OS_Errno.h"
00019 #include "ace/OS_Memory.h"
00020 #include "ace/OS_NS_sys_socket.h"
00021 #include "ace/Log_Msg.h"
00022 #include "ace/Message_Block.h"
00023 #include "ace/INET_Addr.h"
00024 #include "ace/SOCK_Stream.h"
00025 #include "ace/Sock_Connect.h"
00026 
00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00028 
00029 template <class HANDLER>
00030 ACE_Asynch_Acceptor<HANDLER>::ACE_Asynch_Acceptor (void)
00031   : listen_handle_ (ACE_INVALID_HANDLE),
00032     pass_addresses_ (0),
00033     validate_new_connection_ (0),
00034     reissue_accept_ (1),
00035     bytes_to_read_ (0)
00036 {
00037 }
00038 
00039 template <class HANDLER>
00040 ACE_Asynch_Acceptor<HANDLER>::~ACE_Asynch_Acceptor (void)
00041 {
00042   // Close down the listen socket
00043   if (this->listen_handle_ != ACE_INVALID_HANDLE)
00044     {
00045       ACE_OS::closesocket (this->listen_handle_);
00046       this->listen_handle_ = ACE_INVALID_HANDLE;
00047     }
00048 }
00049 
00050 template <class HANDLER> int
00051 ACE_Asynch_Acceptor<HANDLER>::open (const ACE_INET_Addr &address,
00052                                     size_t bytes_to_read,
00053                                     int pass_addresses,
00054                                     int backlog,
00055                                     int reuse_addr,
00056                                     ACE_Proactor *proactor,
00057                                     int validate_new_connection,
00058                                     int reissue_accept,
00059                                     int number_of_initial_accepts)
00060 {
00061   ACE_TRACE ("ACE_Asynch_Acceptor<>::open");
00062 
00063   this->proactor (proactor);
00064   this->pass_addresses_ = pass_addresses;
00065   this->bytes_to_read_ = bytes_to_read;
00066   this->validate_new_connection_ = validate_new_connection;
00067   this->reissue_accept_ = reissue_accept;
00068   this->addr_family_ = address.get_type ();
00069 
00070   // Create the listener socket
00071   this->listen_handle_ = ACE_OS::socket (address.get_type (), SOCK_STREAM, 0);
00072   if (this->listen_handle_ == ACE_INVALID_HANDLE)
00073     ACE_ERROR_RETURN ((LM_ERROR,
00074                        ACE_TEXT ("%p\n"),
00075                        ACE_TEXT ("ACE_OS::socket")),
00076                       -1);
00077   // Initialize the ACE_Asynch_Accept
00078   if (this->asynch_accept_.open (*this,
00079                                  this->listen_handle_,
00080                                  0,
00081                                  this->proactor ()) == -1)
00082     {
00083       ACE_Errno_Guard g (errno);
00084       ACE_ERROR ((LM_ERROR,
00085                   ACE_TEXT ("%p\n"),
00086                   ACE_TEXT ("ACE_Asynch_Accept::open")));
00087       ACE_OS::closesocket (this->listen_handle_);
00088       this->listen_handle_ = ACE_INVALID_HANDLE;
00089       return -1;
00090     }
00091 
00092   if (reuse_addr)
00093     {
00094       // Reuse the address
00095       int one = 1;
00096       if (ACE_OS::setsockopt (this->listen_handle_,
00097                               SOL_SOCKET,
00098                               SO_REUSEADDR,
00099                               (const char*) &one,
00100                               sizeof one) == -1)
00101         {
00102           ACE_Errno_Guard g (errno);
00103           ACE_ERROR ((LM_ERROR,
00104                       ACE_TEXT ("%p\n"),
00105                       ACE_TEXT ("ACE_OS::setsockopt")));
00106           ACE_OS::closesocket (this->listen_handle_);
00107           this->listen_handle_ = ACE_INVALID_HANDLE;
00108           return -1;
00109         }
00110     }
00111 
00112   // If port is not specified, bind to any port.
00113   static ACE_INET_Addr sa (ACE_sap_any_cast (const ACE_INET_Addr &));
00114 
00115   if (address == sa &&
00116       ACE::bind_port (this->listen_handle_,
00117                       INADDR_ANY,
00118                       address.get_type()) == -1)
00119     {
00120       ACE_Errno_Guard g (errno);
00121       ACE_ERROR ((LM_ERROR,
00122                   ACE_TEXT ("%p\n"),
00123                   ACE_TEXT ("ACE::bind_port")));
00124       ACE_OS::closesocket (this->listen_handle_);
00125       this->listen_handle_ = ACE_INVALID_HANDLE;
00126       return -1;
00127     }
00128 
00129   // Bind to the specified port.
00130   if (ACE_OS::bind (this->listen_handle_,
00131                     reinterpret_cast<sockaddr *> (address.get_addr ()),
00132                     address.get_size ()) == -1)
00133     {
00134       ACE_Errno_Guard g (errno);
00135       ACE_ERROR ((LM_ERROR,
00136                   ACE_TEXT ("%p\n"),
00137                   ACE_TEXT ("ACE_OS::bind")));
00138       ACE_OS::closesocket (this->listen_handle_);
00139       this->listen_handle_ = ACE_INVALID_HANDLE;
00140       return -1;
00141     }
00142 
00143   // Start listening.
00144   if (ACE_OS::listen (this->listen_handle_, backlog) == -1)
00145     {
00146       ACE_Errno_Guard g (errno);
00147       ACE_ERROR ((LM_ERROR,
00148                   ACE_TEXT ("%p\n"),
00149                   ACE_TEXT ("ACE_OS::listen")));
00150       ACE_OS::closesocket (this->listen_handle_);
00151       this->listen_handle_ = ACE_INVALID_HANDLE;
00152       return -1;
00153     }
00154 
00155   // For the number of <intial_accepts>.
00156   if (number_of_initial_accepts == -1)
00157     number_of_initial_accepts = backlog;
00158 
00159   for (int i = 0; i < number_of_initial_accepts; i++)
00160     {
00161       // Initiate accepts.
00162       if (this->accept (bytes_to_read) == -1)
00163         {
00164           ACE_Errno_Guard g (errno);
00165           ACE_ERROR ((LM_ERROR,
00166                       ACE_TEXT ("%p\n"),
00167                       ACE_TEXT ("ACE_Asynch_Acceptor::accept")));
00168           ACE_OS::closesocket (this->listen_handle_);
00169           this->listen_handle_ = ACE_INVALID_HANDLE;
00170           return -1;
00171         }
00172     }
00173 
00174   return 0;
00175 }
00176 
00177 template <class HANDLER> int
00178 ACE_Asynch_Acceptor<HANDLER>::set_handle (ACE_HANDLE listen_handle)
00179 {
00180   ACE_TRACE ("ACE_Asynch_Acceptor<>::set_handle");
00181 
00182   // Take ownership of the <listen_handle>
00183   this->listen_handle_ = listen_handle;
00184 
00185   // Reinitialize the ACE_Asynch_Accept
00186   if (this->asynch_accept_.open (*this,
00187                                  this->listen_handle_,
00188                                  0,
00189                                  this->proactor ()) == -1)
00190     ACE_ERROR_RETURN ((LM_ERROR,
00191                        ACE_TEXT ("%p\n"),
00192                        ACE_TEXT ("ACE_Asynch_Accept::open")),
00193                       -1);
00194   return 0;
00195 }
00196 
00197 template <class HANDLER> ACE_HANDLE
00198 ACE_Asynch_Acceptor<HANDLER>::get_handle (void) const
00199 {
00200   return this->listen_handle_;
00201 }
00202 
00203 template <class HANDLER> int
00204 ACE_Asynch_Acceptor<HANDLER>::accept (size_t bytes_to_read, const void *act)
00205 {
00206   ACE_TRACE ("ACE_Asynch_Acceptor<>::accept");
00207 
00208   ACE_Message_Block *message_block = 0;
00209   // The space_needed calculation is drive by needs of Windows. POSIX doesn't
00210   // need to extra 16 bytes, but it doesn't hurt.
00211   size_t space_needed = sizeof (sockaddr_in) + 16;
00212 #if defined (ACE_HAS_IPV6)
00213   if (PF_INET6 == this->addr_family_)
00214     space_needed = sizeof (sockaddr_in6) + 16;
00215 #endif /* ACE_HAS_IPV6 */
00216   space_needed = (2 * space_needed) + bytes_to_read;
00217 
00218   // Create a new message block big enough for the addresses and data
00219   ACE_NEW_RETURN (message_block,
00220                   ACE_Message_Block (space_needed),
00221                   -1);
00222 
00223   // Initiate asynchronous accepts
00224   if (this->asynch_accept_.accept (*message_block,
00225                                    bytes_to_read,
00226                                    ACE_INVALID_HANDLE,
00227                                    act,
00228                                    0,
00229                                    ACE_SIGRTMIN,
00230                                    this->addr_family_) == -1)
00231     {
00232       // Cleanup on error
00233       message_block->release ();
00234       return -1;
00235     }
00236   return 0;
00237 }
00238 
00239 template <class HANDLER> void
00240 ACE_Asynch_Acceptor<HANDLER>::handle_accept (const ACE_Asynch_Accept::Result &result)
00241 {
00242   ACE_TRACE ("ACE_Asynch_Acceptor<>::handle_accept");
00243 
00244   // Variable for error tracking
00245   int error = 0;
00246 
00247   // If the asynchronous accept fails.
00248   if (!result.success () || result.accept_handle () == ACE_INVALID_HANDLE)
00249     {
00250       error = 1;
00251     }
00252 
00253 #if defined (ACE_WIN32)
00254   // In order to use accept handle with other Window Sockets 1.1
00255   // functions, we call the setsockopt function with the
00256   // SO_UPDATE_ACCEPT_CONTEXT option. This option initializes the
00257   // socket so that other Windows Sockets routines to access the
00258   // socket correctly.
00259   if (!error &&
00260       ACE_OS::setsockopt (result.accept_handle (),
00261                           SOL_SOCKET,
00262                           SO_UPDATE_ACCEPT_CONTEXT,
00263                           (char *) &this->listen_handle_,
00264                           sizeof (this->listen_handle_)) == -1)
00265     {
00266       error = 1;
00267     }
00268 #endif /* ACE_WIN32 */
00269 
00270   // Parse address.
00271   ACE_INET_Addr local_address;
00272   ACE_INET_Addr remote_address;
00273   if (!error &&
00274       (this->validate_new_connection_ || this->pass_addresses_))
00275     // Parse the addresses.
00276     this->parse_address (result,
00277                          remote_address,
00278                          local_address);
00279 
00280   // Validate remote address
00281   if (!error &&
00282       this->validate_new_connection_ &&
00283       (this->validate_connection (result, remote_address, local_address) == -1
00284        || this->validate_new_connection (remote_address) == -1))
00285     {
00286       error = 1;
00287     }
00288 
00289   HANDLER *new_handler = 0;
00290   if (!error)
00291     {
00292       // The Template method
00293       new_handler = this->make_handler ();
00294       if (new_handler == 0)
00295         {
00296           error = 1;
00297         }
00298     }
00299 
00300   // If no errors
00301   if (!error)
00302     {
00303       // Update the Proactor.
00304       new_handler->proactor (this->proactor ());
00305 
00306       // Pass the addresses
00307       if (this->pass_addresses_)
00308         new_handler->addresses (remote_address,
00309                                 local_address);
00310 
00311       // Pass the ACT
00312       if (result.act () != 0)
00313         new_handler->act (result.act ());
00314 
00315       // Set up the handler's new handle value
00316       new_handler->handle (result.accept_handle ());
00317 
00318       // Initiate the handler
00319       new_handler->open (result.accept_handle (),
00320                          result.message_block ());
00321     }
00322 
00323   // On failure, no choice but to close the socket
00324   if (error &&
00325       result.accept_handle() != ACE_INVALID_HANDLE )
00326     ACE_OS::closesocket (result.accept_handle ());
00327 
00328   // Delete the dynamically allocated message_block
00329   result.message_block ().release ();
00330 
00331   // Start off another asynchronous accept to keep the backlog going,
00332   // unless we closed the listen socket already (from the destructor),
00333   // or this callback is the result of a canceled/aborted accept.
00334   if (this->should_reissue_accept () &&
00335       this->listen_handle_ != ACE_INVALID_HANDLE
00336 #if defined (ACE_WIN32)
00337       && result.error () != ERROR_OPERATION_ABORTED
00338 #else
00339       && result.error () != ECANCELED
00340 #endif
00341       )
00342     this->accept (this->bytes_to_read_);
00343 }
00344 
00345 template <class HANDLER> int
00346 ACE_Asynch_Acceptor<HANDLER>::validate_connection
00347   (const ACE_Asynch_Accept::Result& /* result */,
00348    const ACE_INET_Addr& /* remote */,
00349    const ACE_INET_Addr& /* local */)
00350 {
00351   // Default implementation always validates the remote address.
00352   return 0;
00353 }
00354 
00355 template <class HANDLER> int
00356 ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (const ACE_INET_Addr&)
00357 {
00358   // Default implementation always validates the remote address.
00359   return 0;
00360 }
00361 
00362 template <class HANDLER> int
00363 ACE_Asynch_Acceptor<HANDLER>::cancel (void)
00364 {
00365   ACE_TRACE ("ACE_Asynch_Acceptor<>::cancel");
00366 
00367   // All I/O operations that are canceled will complete with the error
00368   // ERROR_OPERATION_ABORTED. All completion notifications for the I/O
00369   // operations will occur normally.
00370 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) && \
00371     (defined (_MSC_VER) || defined (__BORLANDC__))
00372   return (int) ::CancelIo (this->listen_handle_);
00373 #else
00374   // Supported now
00375   return this->asynch_accept_.cancel();
00376 
00377 #endif /* defined (ACE_HAS_WIN32_OVERLAPPED_IO) && (defined (_MSC_VER)) || defined (__BORLANDC__)) */
00378 }
00379 
00380 template <class HANDLER> void
00381 ACE_Asynch_Acceptor<HANDLER>::parse_address (const
00382                                              ACE_Asynch_Accept::Result &result,
00383                                              ACE_INET_Addr &remote_address,
00384                                              ACE_INET_Addr &local_address)
00385 {
00386   ACE_TRACE ("ACE_Asynch_Acceptor<>::parse_address");
00387 
00388 #if defined (ACE_HAS_AIO_CALLS)
00389 
00390   // Use an ACE_SOCK to get the addresses - it knows how to deal with
00391   // ACE_INET_Addr objects and get IPv4/v6 addresses.
00392   ACE_SOCK_Stream str (result.accept_handle ());
00393   str.get_local_addr (local_address);
00394   str.get_remote_addr (remote_address);
00395 
00396 #elif defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00397 
00398   ACE_Message_Block &message_block = result.message_block ();
00399 
00400   sockaddr *local_addr = 0;
00401   sockaddr *remote_addr = 0;
00402   int local_size = 0;
00403   int remote_size = 0;
00404   // This matches setup in accept().
00405   size_t addr_size = sizeof (sockaddr_in) + 16;
00406 #if defined (ACE_HAS_IPV6)
00407   if (this->addr_family_ == PF_INET6)
00408     addr_size = sizeof (sockaddr_in6) + 16;
00409 #endif /* ACE_HAS_IPV6 */
00410 
00411   ::GetAcceptExSockaddrs (message_block.rd_ptr (),
00412                           static_cast<DWORD> (this->bytes_to_read_),
00413                           static_cast<DWORD> (addr_size),
00414                           static_cast<DWORD> (addr_size),
00415                           &local_addr,
00416                           &local_size,
00417                           &remote_addr,
00418                           &remote_size);
00419 
00420   local_address.set (reinterpret_cast<sockaddr_in *> (local_addr),
00421                      local_size);
00422   remote_address.set (reinterpret_cast<sockaddr_in *> (remote_addr),
00423                       remote_size);
00424 #else
00425   // just in case
00426   errno = ENOTSUP;
00427 #endif /* defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) */
00428   return;
00429 }
00430 
00431 template <class HANDLER> ACE_HANDLE
00432 ACE_Asynch_Acceptor<HANDLER>::handle (void) const
00433 {
00434   return this->listen_handle_;
00435 }
00436 
00437 template <class HANDLER> void
00438 ACE_Asynch_Acceptor<HANDLER>::handle (ACE_HANDLE h)
00439 {
00440   ACE_Handler::handle (h);
00441 }
00442 
00443 template <class HANDLER> ACE_Asynch_Accept &
00444 ACE_Asynch_Acceptor<HANDLER>::asynch_accept (void)
00445 {
00446   return this->asynch_accept_;
00447 }
00448 
00449 template <class HANDLER> HANDLER *
00450 ACE_Asynch_Acceptor<HANDLER>::make_handler (void)
00451 {
00452   // Default behavior
00453   HANDLER *handler = 0;
00454   ACE_NEW_RETURN (handler,
00455                   HANDLER,
00456                   0);
00457   return handler;
00458 }
00459 
00460 /* static */
00461 template <class HANDLER> size_t
00462 ACE_Asynch_Acceptor<HANDLER>::address_size (void)
00463 {
00464   return sizeof (sockaddr) + sizeof (sockaddr_in);
00465 }
00466 
00467 template <class HANDLER> int
00468 ACE_Asynch_Acceptor<HANDLER>::pass_addresses (void) const
00469 {
00470   return this->pass_addresses_;
00471 }
00472 
00473 template <class HANDLER> void
00474 ACE_Asynch_Acceptor<HANDLER>::pass_addresses (int new_value)
00475 {
00476   this->pass_addresses_ = new_value;
00477 }
00478 
00479 template <class HANDLER> int
00480 ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (void) const
00481 {
00482   return this->validate_new_connection_;
00483 }
00484 
00485 template <class HANDLER> void
00486 ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (int new_value)
00487 {
00488   this->validate_new_connection_ = new_value;
00489 }
00490 
00491 template <class HANDLER> int
00492 ACE_Asynch_Acceptor<HANDLER>::reissue_accept (void) const
00493 {
00494   return this->reissue_accept_;
00495 }
00496 
00497 template <class HANDLER> void
00498 ACE_Asynch_Acceptor<HANDLER>::reissue_accept (int new_value)
00499 {
00500   this->reissue_accept_ = new_value;
00501 }
00502 
00503 template <class HANDLER> size_t
00504 ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (void) const
00505 {
00506   return this->bytes_to_read_;
00507 }
00508 
00509 template <class HANDLER> void
00510 ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (size_t new_value)
00511 {
00512   this->bytes_to_read_ = new_value;
00513 }
00514 
00515 template <class HANDLER> int
00516 ACE_Asynch_Acceptor<HANDLER>::should_reissue_accept (void)
00517 {
00518   return this->reissue_accept_;
00519 }
00520 
00521 ACE_END_VERSIONED_NAMESPACE_DECL
00522 
00523 #endif /* ACE_WIN32 || ACE_HAS_AIO_CALLS */
00524 #endif /* ACE_ASYNCH_ACCEPTOR_C */

Generated on Sun Jan 27 12:05:20 2008 for ACE by doxygen 1.3.6