00001
00002
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
00012
00013 ACE_RCSID(ace, Asynch_Acceptor, "$Id: Asynch_Acceptor.cpp 85213 2009-04-29 16:34:20Z shuston $")
00014
00015 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
00016
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
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
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
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
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
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
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
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
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
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
00183 this->listen_handle_ = listen_handle;
00184
00185
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
00210
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
00216 space_needed = (2 * space_needed) + bytes_to_read;
00217
00218
00219 ACE_NEW_RETURN (message_block,
00220 ACE_Message_Block (space_needed),
00221 -1);
00222
00223
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
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
00245 int error = 0;
00246
00247
00248 if (!result.success () || result.accept_handle () == ACE_INVALID_HANDLE)
00249 {
00250 error = 1;
00251 }
00252
00253 #if defined (ACE_WIN32)
00254
00255
00256
00257
00258
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
00269
00270
00271 ACE_INET_Addr local_address;
00272 ACE_INET_Addr remote_address;
00273 if (!error &&
00274 (this->validate_new_connection_ || this->pass_addresses_))
00275
00276 this->parse_address (result,
00277 remote_address,
00278 local_address);
00279
00280
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
00292 new_handler = this->make_handler ();
00293 if (new_handler == 0)
00294 {
00295 error = 1;
00296 }
00297 }
00298
00299
00300 if (!error)
00301 {
00302
00303 new_handler->proactor (this->proactor ());
00304
00305
00306 if (this->pass_addresses_)
00307 new_handler->addresses (remote_address,
00308 local_address);
00309
00310
00311 if (result.act () != 0)
00312 new_handler->act (result.act ());
00313
00314
00315 new_handler->handle (result.accept_handle ());
00316
00317
00318 new_handler->open (result.accept_handle (),
00319 result.message_block ());
00320 }
00321
00322
00323 if (error &&
00324 result.accept_handle() != ACE_INVALID_HANDLE )
00325 ACE_OS::closesocket (result.accept_handle ());
00326
00327
00328 result.message_block ().release ();
00329
00330
00331
00332
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_, result.act ());
00342 }
00343
00344 template <class HANDLER> int
00345 ACE_Asynch_Acceptor<HANDLER>::validate_connection
00346 (const ACE_Asynch_Accept::Result& ,
00347 const ACE_INET_Addr& ,
00348 const ACE_INET_Addr& )
00349 {
00350
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
00360
00361
00362 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
00363 return (int) ::CancelIo (this->listen_handle_);
00364 #else
00365
00366 return this->asynch_accept_.cancel();
00367 #endif
00368 }
00369
00370 template <class HANDLER> void
00371 ACE_Asynch_Acceptor<HANDLER>::parse_address (const
00372 ACE_Asynch_Accept::Result &result,
00373 ACE_INET_Addr &remote_address,
00374 ACE_INET_Addr &local_address)
00375 {
00376 ACE_TRACE ("ACE_Asynch_Acceptor<>::parse_address");
00377
00378 #if defined (ACE_HAS_AIO_CALLS)
00379
00380
00381
00382 ACE_SOCK_Stream str (result.accept_handle ());
00383 str.get_local_addr (local_address);
00384 str.get_remote_addr (remote_address);
00385
00386 #elif defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00387
00388 ACE_Message_Block &message_block = result.message_block ();
00389
00390 sockaddr *local_addr = 0;
00391 sockaddr *remote_addr = 0;
00392 int local_size = 0;
00393 int remote_size = 0;
00394
00395 size_t addr_size = sizeof (sockaddr_in) + 16;
00396 #if defined (ACE_HAS_IPV6)
00397 if (this->addr_family_ == PF_INET6)
00398 addr_size = sizeof (sockaddr_in6) + 16;
00399 #endif
00400
00401 ::GetAcceptExSockaddrs (message_block.rd_ptr (),
00402 static_cast<DWORD> (this->bytes_to_read_),
00403 static_cast<DWORD> (addr_size),
00404 static_cast<DWORD> (addr_size),
00405 &local_addr,
00406 &local_size,
00407 &remote_addr,
00408 &remote_size);
00409
00410 local_address.set (reinterpret_cast<sockaddr_in *> (local_addr),
00411 local_size);
00412 remote_address.set (reinterpret_cast<sockaddr_in *> (remote_addr),
00413 remote_size);
00414 #else
00415
00416 errno = ENOTSUP;
00417 #endif
00418 return;
00419 }
00420
00421 template <class HANDLER> ACE_HANDLE
00422 ACE_Asynch_Acceptor<HANDLER>::handle (void) const
00423 {
00424 return this->listen_handle_;
00425 }
00426
00427 template <class HANDLER> void
00428 ACE_Asynch_Acceptor<HANDLER>::handle (ACE_HANDLE h)
00429 {
00430 ACE_Handler::handle (h);
00431 }
00432
00433 template <class HANDLER> ACE_Asynch_Accept &
00434 ACE_Asynch_Acceptor<HANDLER>::asynch_accept (void)
00435 {
00436 return this->asynch_accept_;
00437 }
00438
00439 template <class HANDLER> HANDLER *
00440 ACE_Asynch_Acceptor<HANDLER>::make_handler (void)
00441 {
00442
00443 HANDLER *handler = 0;
00444 ACE_NEW_RETURN (handler,
00445 HANDLER,
00446 0);
00447 return handler;
00448 }
00449
00450
00451 template <class HANDLER> size_t
00452 ACE_Asynch_Acceptor<HANDLER>::address_size (void)
00453 {
00454 return sizeof (sockaddr) + sizeof (sockaddr_in);
00455 }
00456
00457 template <class HANDLER> bool
00458 ACE_Asynch_Acceptor<HANDLER>::pass_addresses (void) const
00459 {
00460 return this->pass_addresses_;
00461 }
00462
00463 template <class HANDLER> void
00464 ACE_Asynch_Acceptor<HANDLER>::pass_addresses (bool new_value)
00465 {
00466 this->pass_addresses_ = new_value;
00467 }
00468
00469 template <class HANDLER> bool
00470 ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (void) const
00471 {
00472 return this->validate_new_connection_;
00473 }
00474
00475 template <class HANDLER> void
00476 ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (bool new_value)
00477 {
00478 this->validate_new_connection_ = new_value;
00479 }
00480
00481 template <class HANDLER> int
00482 ACE_Asynch_Acceptor<HANDLER>::reissue_accept (void) const
00483 {
00484 return this->reissue_accept_;
00485 }
00486
00487 template <class HANDLER> void
00488 ACE_Asynch_Acceptor<HANDLER>::reissue_accept (int new_value)
00489 {
00490 this->reissue_accept_ = new_value;
00491 }
00492
00493 template <class HANDLER> size_t
00494 ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (void) const
00495 {
00496 return this->bytes_to_read_;
00497 }
00498
00499 template <class HANDLER> void
00500 ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (size_t new_value)
00501 {
00502 this->bytes_to_read_ = new_value;
00503 }
00504
00505 template <class HANDLER> int
00506 ACE_Asynch_Acceptor<HANDLER>::should_reissue_accept (void)
00507 {
00508 return this->reissue_accept_;
00509 }
00510
00511 ACE_END_VERSIONED_NAMESPACE_DECL
00512
00513 #endif
00514 #endif