Asynch_Acceptor.cpp

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 // $Id: Asynch_Acceptor.cpp 80826 2008-03-04 14:51:23Z wotte $
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 80826 2008-03-04 14:51:23Z wotte $")
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_ (false),
00033     validate_new_connection_ (false),
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                                     bool pass_addresses,
00054                                     int backlog,
00055                                     int reuse_addr,
00056                                     ACE_Proactor *proactor,
00057                                     bool 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     {
00285       error = 1;
00286     }
00287 
00288   HANDLER *new_handler = 0;
00289   if (!error)
00290     {
00291       // The Template method
00292       new_handler = this->make_handler ();
00293       if (new_handler == 0)
00294         {
00295           error = 1;
00296         }
00297     }
00298 
00299   // If no errors
00300   if (!error)
00301     {
00302       // Update the Proactor.
00303       new_handler->proactor (this->proactor ());
00304 
00305       // Pass the addresses
00306       if (this->pass_addresses_)
00307         new_handler->addresses (remote_address,
00308                                 local_address);
00309 
00310       // Pass the ACT
00311       if (result.act () != 0)
00312         new_handler->act (result.act ());
00313 
00314       // Set up the handler's new handle value
00315       new_handler->handle (result.accept_handle ());
00316 
00317       // Initiate the handler
00318       new_handler->open (result.accept_handle (),
00319                          result.message_block ());
00320     }
00321 
00322   // On failure, no choice but to close the socket
00323   if (error &&
00324       result.accept_handle() != ACE_INVALID_HANDLE )
00325     ACE_OS::closesocket (result.accept_handle ());
00326 
00327   // Delete the dynamically allocated message_block
00328   result.message_block ().release ();
00329 
00330   // Start off another asynchronous accept to keep the backlog going,
00331   // unless we closed the listen socket already (from the destructor),
00332   // or this callback is the result of a canceled/aborted accept.
00333   if (this->should_reissue_accept () &&
00334       this->listen_handle_ != ACE_INVALID_HANDLE
00335 #if defined (ACE_WIN32)
00336       && result.error () != ERROR_OPERATION_ABORTED
00337 #else
00338       && result.error () != ECANCELED
00339 #endif
00340       )
00341     this->accept (this->bytes_to_read_);
00342 }
00343 
00344 template <class HANDLER> int
00345 ACE_Asynch_Acceptor<HANDLER>::validate_connection
00346   (const ACE_Asynch_Accept::Result& /* result */,
00347    const ACE_INET_Addr& /* remote */,
00348    const ACE_INET_Addr& /* local */)
00349 {
00350   // Default implementation always validates the remote address.
00351   return 0;
00352 }
00353 
00354 template <class HANDLER> int
00355 ACE_Asynch_Acceptor<HANDLER>::cancel (void)
00356 {
00357   ACE_TRACE ("ACE_Asynch_Acceptor<>::cancel");
00358 
00359   // All I/O operations that are canceled will complete with the error
00360   // ERROR_OPERATION_ABORTED. All completion notifications for the I/O
00361   // operations will occur normally.
00362 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) && \
00363     (defined (_MSC_VER) || defined (__BORLANDC__))
00364   return (int) ::CancelIo (this->listen_handle_);
00365 #else
00366   // Supported now
00367   return this->asynch_accept_.cancel();
00368 
00369 #endif /* defined (ACE_HAS_WIN32_OVERLAPPED_IO) && (defined (_MSC_VER)) || defined (__BORLANDC__)) */
00370 }
00371 
00372 template <class HANDLER> void
00373 ACE_Asynch_Acceptor<HANDLER>::parse_address (const
00374                                              ACE_Asynch_Accept::Result &result,
00375                                              ACE_INET_Addr &remote_address,
00376                                              ACE_INET_Addr &local_address)
00377 {
00378   ACE_TRACE ("ACE_Asynch_Acceptor<>::parse_address");
00379 
00380 #if defined (ACE_HAS_AIO_CALLS)
00381 
00382   // Use an ACE_SOCK to get the addresses - it knows how to deal with
00383   // ACE_INET_Addr objects and get IPv4/v6 addresses.
00384   ACE_SOCK_Stream str (result.accept_handle ());
00385   str.get_local_addr (local_address);
00386   str.get_remote_addr (remote_address);
00387 
00388 #elif defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00389 
00390   ACE_Message_Block &message_block = result.message_block ();
00391 
00392   sockaddr *local_addr = 0;
00393   sockaddr *remote_addr = 0;
00394   int local_size = 0;
00395   int remote_size = 0;
00396   // This matches setup in accept().
00397   size_t addr_size = sizeof (sockaddr_in) + 16;
00398 #if defined (ACE_HAS_IPV6)
00399   if (this->addr_family_ == PF_INET6)
00400     addr_size = sizeof (sockaddr_in6) + 16;
00401 #endif /* ACE_HAS_IPV6 */
00402 
00403   ::GetAcceptExSockaddrs (message_block.rd_ptr (),
00404                           static_cast<DWORD> (this->bytes_to_read_),
00405                           static_cast<DWORD> (addr_size),
00406                           static_cast<DWORD> (addr_size),
00407                           &local_addr,
00408                           &local_size,
00409                           &remote_addr,
00410                           &remote_size);
00411 
00412   local_address.set (reinterpret_cast<sockaddr_in *> (local_addr),
00413                      local_size);
00414   remote_address.set (reinterpret_cast<sockaddr_in *> (remote_addr),
00415                       remote_size);
00416 #else
00417   // just in case
00418   errno = ENOTSUP;
00419 #endif /* defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) */
00420   return;
00421 }
00422 
00423 template <class HANDLER> ACE_HANDLE
00424 ACE_Asynch_Acceptor<HANDLER>::handle (void) const
00425 {
00426   return this->listen_handle_;
00427 }
00428 
00429 template <class HANDLER> void
00430 ACE_Asynch_Acceptor<HANDLER>::handle (ACE_HANDLE h)
00431 {
00432   ACE_Handler::handle (h);
00433 }
00434 
00435 template <class HANDLER> ACE_Asynch_Accept &
00436 ACE_Asynch_Acceptor<HANDLER>::asynch_accept (void)
00437 {
00438   return this->asynch_accept_;
00439 }
00440 
00441 template <class HANDLER> HANDLER *
00442 ACE_Asynch_Acceptor<HANDLER>::make_handler (void)
00443 {
00444   // Default behavior
00445   HANDLER *handler = 0;
00446   ACE_NEW_RETURN (handler,
00447                   HANDLER,
00448                   0);
00449   return handler;
00450 }
00451 
00452 /* static */
00453 template <class HANDLER> size_t
00454 ACE_Asynch_Acceptor<HANDLER>::address_size (void)
00455 {
00456   return sizeof (sockaddr) + sizeof (sockaddr_in);
00457 }
00458 
00459 template <class HANDLER> bool
00460 ACE_Asynch_Acceptor<HANDLER>::pass_addresses (void) const
00461 {
00462   return this->pass_addresses_;
00463 }
00464 
00465 template <class HANDLER> void
00466 ACE_Asynch_Acceptor<HANDLER>::pass_addresses (bool new_value)
00467 {
00468   this->pass_addresses_ = new_value;
00469 }
00470 
00471 template <class HANDLER> bool
00472 ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (void) const
00473 {
00474   return this->validate_new_connection_;
00475 }
00476 
00477 template <class HANDLER> void
00478 ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (bool new_value)
00479 {
00480   this->validate_new_connection_ = new_value;
00481 }
00482 
00483 template <class HANDLER> int
00484 ACE_Asynch_Acceptor<HANDLER>::reissue_accept (void) const
00485 {
00486   return this->reissue_accept_;
00487 }
00488 
00489 template <class HANDLER> void
00490 ACE_Asynch_Acceptor<HANDLER>::reissue_accept (int new_value)
00491 {
00492   this->reissue_accept_ = new_value;
00493 }
00494 
00495 template <class HANDLER> size_t
00496 ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (void) const
00497 {
00498   return this->bytes_to_read_;
00499 }
00500 
00501 template <class HANDLER> void
00502 ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (size_t new_value)
00503 {
00504   this->bytes_to_read_ = new_value;
00505 }
00506 
00507 template <class HANDLER> int
00508 ACE_Asynch_Acceptor<HANDLER>::should_reissue_accept (void)
00509 {
00510   return this->reissue_accept_;
00511 }
00512 
00513 ACE_END_VERSIONED_NAMESPACE_DECL
00514 
00515 #endif /* ACE_WIN32 || ACE_HAS_AIO_CALLS */
00516 #endif /* ACE_ASYNCH_ACCEPTOR_C */

Generated on Tue Feb 2 17:18:38 2010 for ACE by  doxygen 1.4.7