Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:36

0001 //
0002 // detail/impl/win_iocp_socket_service_base.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_HAS_IOCP)
0021 
0022 #include <boost/asio/detail/win_iocp_socket_service_base.hpp>
0023 
0024 #include <boost/asio/detail/push_options.hpp>
0025 
0026 namespace boost {
0027 namespace asio {
0028 namespace detail {
0029 
0030 win_iocp_socket_service_base::win_iocp_socket_service_base(
0031     execution_context& context)
0032   : context_(context),
0033     iocp_service_(use_service<win_iocp_io_context>(context)),
0034     reactor_(0),
0035     connect_ex_(0),
0036     nt_set_info_(0),
0037     mutex_(),
0038     impl_list_(0)
0039 {
0040 }
0041 
0042 void win_iocp_socket_service_base::base_shutdown()
0043 {
0044   // Close all implementations, causing all operations to complete.
0045   boost::asio::detail::mutex::scoped_lock lock(mutex_);
0046   base_implementation_type* impl = impl_list_;
0047   while (impl)
0048   {
0049     close_for_destruction(*impl);
0050     impl = impl->next_;
0051   }
0052 }
0053 
0054 void win_iocp_socket_service_base::construct(
0055     win_iocp_socket_service_base::base_implementation_type& impl)
0056 {
0057   impl.socket_ = invalid_socket;
0058   impl.state_ = 0;
0059   impl.cancel_token_.reset();
0060 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
0061   impl.safe_cancellation_thread_id_ = 0;
0062 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
0063 
0064   // Insert implementation into linked list of all implementations.
0065   boost::asio::detail::mutex::scoped_lock lock(mutex_);
0066   impl.next_ = impl_list_;
0067   impl.prev_ = 0;
0068   if (impl_list_)
0069     impl_list_->prev_ = &impl;
0070   impl_list_ = &impl;
0071 }
0072 
0073 void win_iocp_socket_service_base::base_move_construct(
0074     win_iocp_socket_service_base::base_implementation_type& impl,
0075     win_iocp_socket_service_base::base_implementation_type& other_impl)
0076   noexcept
0077 {
0078   impl.socket_ = other_impl.socket_;
0079   other_impl.socket_ = invalid_socket;
0080 
0081   impl.state_ = other_impl.state_;
0082   other_impl.state_ = 0;
0083 
0084   impl.cancel_token_ = other_impl.cancel_token_;
0085   other_impl.cancel_token_.reset();
0086 
0087 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
0088   impl.safe_cancellation_thread_id_ = other_impl.safe_cancellation_thread_id_;
0089   other_impl.safe_cancellation_thread_id_ = 0;
0090 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
0091 
0092   // Insert implementation into linked list of all implementations.
0093   boost::asio::detail::mutex::scoped_lock lock(mutex_);
0094   impl.next_ = impl_list_;
0095   impl.prev_ = 0;
0096   if (impl_list_)
0097     impl_list_->prev_ = &impl;
0098   impl_list_ = &impl;
0099 }
0100 
0101 void win_iocp_socket_service_base::base_move_assign(
0102     win_iocp_socket_service_base::base_implementation_type& impl,
0103     win_iocp_socket_service_base& other_service,
0104     win_iocp_socket_service_base::base_implementation_type& other_impl)
0105 {
0106   close_for_destruction(impl);
0107 
0108   if (this != &other_service)
0109   {
0110     // Remove implementation from linked list of all implementations.
0111     boost::asio::detail::mutex::scoped_lock lock(mutex_);
0112     if (impl_list_ == &impl)
0113       impl_list_ = impl.next_;
0114     if (impl.prev_)
0115       impl.prev_->next_ = impl.next_;
0116     if (impl.next_)
0117       impl.next_->prev_= impl.prev_;
0118     impl.next_ = 0;
0119     impl.prev_ = 0;
0120   }
0121 
0122   impl.socket_ = other_impl.socket_;
0123   other_impl.socket_ = invalid_socket;
0124 
0125   impl.state_ = other_impl.state_;
0126   other_impl.state_ = 0;
0127 
0128   impl.cancel_token_ = other_impl.cancel_token_;
0129   other_impl.cancel_token_.reset();
0130 
0131 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
0132   impl.safe_cancellation_thread_id_ = other_impl.safe_cancellation_thread_id_;
0133   other_impl.safe_cancellation_thread_id_ = 0;
0134 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
0135 
0136   if (this != &other_service)
0137   {
0138     // Insert implementation into linked list of all implementations.
0139     boost::asio::detail::mutex::scoped_lock lock(other_service.mutex_);
0140     impl.next_ = other_service.impl_list_;
0141     impl.prev_ = 0;
0142     if (other_service.impl_list_)
0143       other_service.impl_list_->prev_ = &impl;
0144     other_service.impl_list_ = &impl;
0145   }
0146 }
0147 
0148 void win_iocp_socket_service_base::destroy(
0149     win_iocp_socket_service_base::base_implementation_type& impl)
0150 {
0151   close_for_destruction(impl);
0152 
0153   // Remove implementation from linked list of all implementations.
0154   boost::asio::detail::mutex::scoped_lock lock(mutex_);
0155   if (impl_list_ == &impl)
0156     impl_list_ = impl.next_;
0157   if (impl.prev_)
0158     impl.prev_->next_ = impl.next_;
0159   if (impl.next_)
0160     impl.next_->prev_= impl.prev_;
0161   impl.next_ = 0;
0162   impl.prev_ = 0;
0163 }
0164 
0165 boost::system::error_code win_iocp_socket_service_base::close(
0166     win_iocp_socket_service_base::base_implementation_type& impl,
0167     boost::system::error_code& ec)
0168 {
0169   if (is_open(impl))
0170   {
0171     BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
0172           "socket", &impl, impl.socket_, "close"));
0173 
0174     // Check if the reactor was created, in which case we need to close the
0175     // socket on the reactor as well to cancel any operations that might be
0176     // running there.
0177     select_reactor* r = static_cast<select_reactor*>(
0178           interlocked_compare_exchange_pointer(
0179             reinterpret_cast<void**>(&reactor_), 0, 0));
0180     if (r)
0181       r->deregister_descriptor(impl.socket_, impl.reactor_data_, true);
0182 
0183     socket_ops::close(impl.socket_, impl.state_, false, ec);
0184 
0185     if (r)
0186       r->cleanup_descriptor_data(impl.reactor_data_);
0187   }
0188   else
0189   {
0190     ec = boost::system::error_code();
0191   }
0192 
0193   impl.socket_ = invalid_socket;
0194   impl.state_ = 0;
0195   impl.cancel_token_.reset();
0196 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
0197   impl.safe_cancellation_thread_id_ = 0;
0198 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
0199 
0200   return ec;
0201 }
0202 
0203 socket_type win_iocp_socket_service_base::release(
0204     win_iocp_socket_service_base::base_implementation_type& impl,
0205     boost::system::error_code& ec)
0206 {
0207   if (!is_open(impl))
0208     return invalid_socket;
0209 
0210   cancel(impl, ec);
0211   if (ec)
0212     return invalid_socket;
0213 
0214   nt_set_info_fn fn = get_nt_set_info();
0215   if (fn == 0)
0216   {
0217     ec = boost::asio::error::operation_not_supported;
0218     return invalid_socket;
0219   }
0220 
0221   HANDLE sock_as_handle = reinterpret_cast<HANDLE>(impl.socket_);
0222   ULONG_PTR iosb[2] = { 0, 0 };
0223   void* info[2] = { 0, 0 };
0224   if (fn(sock_as_handle, iosb, &info, sizeof(info),
0225         61 /* FileReplaceCompletionInformation */))
0226   {
0227     ec = boost::asio::error::operation_not_supported;
0228     return invalid_socket;
0229   }
0230 
0231   socket_type tmp = impl.socket_;
0232   impl.socket_ = invalid_socket;
0233   return tmp;
0234 }
0235 
0236 boost::system::error_code win_iocp_socket_service_base::cancel(
0237     win_iocp_socket_service_base::base_implementation_type& impl,
0238     boost::system::error_code& ec)
0239 {
0240   if (!is_open(impl))
0241   {
0242     ec = boost::asio::error::bad_descriptor;
0243     return ec;
0244   }
0245 
0246   BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
0247         "socket", &impl, impl.socket_, "cancel"));
0248 
0249   if (FARPROC cancel_io_ex_ptr = ::GetProcAddress(
0250         ::GetModuleHandleA("KERNEL32"), "CancelIoEx"))
0251   {
0252     // The version of Windows supports cancellation from any thread.
0253     typedef BOOL (WINAPI* cancel_io_ex_t)(HANDLE, LPOVERLAPPED);
0254     cancel_io_ex_t cancel_io_ex = reinterpret_cast<cancel_io_ex_t>(
0255         reinterpret_cast<void*>(cancel_io_ex_ptr));
0256     socket_type sock = impl.socket_;
0257     HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
0258     if (!cancel_io_ex(sock_as_handle, 0))
0259     {
0260       DWORD last_error = ::GetLastError();
0261       if (last_error == ERROR_NOT_FOUND)
0262       {
0263         // ERROR_NOT_FOUND means that there were no operations to be
0264         // cancelled. We swallow this error to match the behaviour on other
0265         // platforms.
0266         ec = boost::system::error_code();
0267       }
0268       else
0269       {
0270         ec = boost::system::error_code(last_error,
0271             boost::asio::error::get_system_category());
0272       }
0273     }
0274     else
0275     {
0276       ec = boost::system::error_code();
0277     }
0278   }
0279 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
0280   else if (impl.safe_cancellation_thread_id_ == 0)
0281   {
0282     // No operations have been started, so there's nothing to cancel.
0283     ec = boost::system::error_code();
0284   }
0285   else if (impl.safe_cancellation_thread_id_ == ::GetCurrentThreadId())
0286   {
0287     // Asynchronous operations have been started from the current thread only,
0288     // so it is safe to try to cancel them using CancelIo.
0289     socket_type sock = impl.socket_;
0290     HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
0291     if (!::CancelIo(sock_as_handle))
0292     {
0293       DWORD last_error = ::GetLastError();
0294       ec = boost::system::error_code(last_error,
0295           boost::asio::error::get_system_category());
0296     }
0297     else
0298     {
0299       ec = boost::system::error_code();
0300     }
0301   }
0302   else
0303   {
0304     // Asynchronous operations have been started from more than one thread,
0305     // so cancellation is not safe.
0306     ec = boost::asio::error::operation_not_supported;
0307   }
0308 #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
0309   else
0310   {
0311     // Cancellation is not supported as CancelIo may not be used.
0312     ec = boost::asio::error::operation_not_supported;
0313   }
0314 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
0315 
0316   // Cancel any operations started via the reactor.
0317   if (!ec)
0318   {
0319     select_reactor* r = static_cast<select_reactor*>(
0320           interlocked_compare_exchange_pointer(
0321             reinterpret_cast<void**>(&reactor_), 0, 0));
0322     if (r)
0323       r->cancel_ops(impl.socket_, impl.reactor_data_);
0324   }
0325 
0326   return ec;
0327 }
0328 
0329 boost::system::error_code win_iocp_socket_service_base::do_open(
0330     win_iocp_socket_service_base::base_implementation_type& impl,
0331     int family, int type, int protocol, boost::system::error_code& ec)
0332 {
0333   if (is_open(impl))
0334   {
0335     ec = boost::asio::error::already_open;
0336     return ec;
0337   }
0338 
0339   socket_holder sock(socket_ops::socket(family, type, protocol, ec));
0340   if (sock.get() == invalid_socket)
0341     return ec;
0342 
0343   HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock.get());
0344   if (iocp_service_.register_handle(sock_as_handle, ec))
0345     return ec;
0346 
0347   impl.socket_ = sock.release();
0348   switch (type)
0349   {
0350   case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
0351   case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
0352   default: impl.state_ = 0; break;
0353   }
0354   impl.cancel_token_.reset(static_cast<void*>(0), socket_ops::noop_deleter());
0355   ec = boost::system::error_code();
0356   return ec;
0357 }
0358 
0359 boost::system::error_code win_iocp_socket_service_base::do_assign(
0360     win_iocp_socket_service_base::base_implementation_type& impl,
0361     int type, socket_type native_socket, boost::system::error_code& ec)
0362 {
0363   if (is_open(impl))
0364   {
0365     ec = boost::asio::error::already_open;
0366     return ec;
0367   }
0368 
0369   HANDLE sock_as_handle = reinterpret_cast<HANDLE>(native_socket);
0370   if (iocp_service_.register_handle(sock_as_handle, ec))
0371     return ec;
0372 
0373   impl.socket_ = native_socket;
0374   switch (type)
0375   {
0376   case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
0377   case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
0378   default: impl.state_ = 0; break;
0379   }
0380   impl.cancel_token_.reset(static_cast<void*>(0), socket_ops::noop_deleter());
0381   ec = boost::system::error_code();
0382   return ec;
0383 }
0384 
0385 void win_iocp_socket_service_base::start_send_op(
0386     win_iocp_socket_service_base::base_implementation_type& impl,
0387     WSABUF* buffers, std::size_t buffer_count,
0388     socket_base::message_flags flags, bool noop, operation* op)
0389 {
0390   update_cancellation_thread_id(impl);
0391   iocp_service_.work_started();
0392 
0393   if (noop)
0394     iocp_service_.on_completion(op);
0395   else if (!is_open(impl))
0396     iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
0397   else
0398   {
0399     DWORD bytes_transferred = 0;
0400     int result = ::WSASend(impl.socket_, buffers,
0401         static_cast<DWORD>(buffer_count), &bytes_transferred, flags, op, 0);
0402     DWORD last_error = ::WSAGetLastError();
0403     if (last_error == ERROR_PORT_UNREACHABLE)
0404       last_error = WSAECONNREFUSED;
0405     if (result != 0 && last_error != WSA_IO_PENDING)
0406       iocp_service_.on_completion(op, last_error, bytes_transferred);
0407     else
0408       iocp_service_.on_pending(op);
0409   }
0410 }
0411 
0412 void win_iocp_socket_service_base::start_send_to_op(
0413     win_iocp_socket_service_base::base_implementation_type& impl,
0414     WSABUF* buffers, std::size_t buffer_count, const void* addr,
0415     int addrlen, socket_base::message_flags flags, operation* op)
0416 {
0417   update_cancellation_thread_id(impl);
0418   iocp_service_.work_started();
0419 
0420   if (!is_open(impl))
0421     iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
0422   else
0423   {
0424     DWORD bytes_transferred = 0;
0425     int result = ::WSASendTo(impl.socket_, buffers,
0426         static_cast<DWORD>(buffer_count), &bytes_transferred, flags,
0427         static_cast<const socket_addr_type*>(addr), addrlen, op, 0);
0428     DWORD last_error = ::WSAGetLastError();
0429     if (last_error == ERROR_PORT_UNREACHABLE)
0430       last_error = WSAECONNREFUSED;
0431     if (result != 0 && last_error != WSA_IO_PENDING)
0432       iocp_service_.on_completion(op, last_error, bytes_transferred);
0433     else
0434       iocp_service_.on_pending(op);
0435   }
0436 }
0437 
0438 void win_iocp_socket_service_base::start_receive_op(
0439     win_iocp_socket_service_base::base_implementation_type& impl,
0440     WSABUF* buffers, std::size_t buffer_count,
0441     socket_base::message_flags flags, bool noop, operation* op)
0442 {
0443   update_cancellation_thread_id(impl);
0444   iocp_service_.work_started();
0445 
0446   if (noop)
0447     iocp_service_.on_completion(op);
0448   else if (!is_open(impl))
0449     iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
0450   else
0451   {
0452     DWORD bytes_transferred = 0;
0453     DWORD recv_flags = flags;
0454     int result = ::WSARecv(impl.socket_, buffers,
0455         static_cast<DWORD>(buffer_count),
0456         &bytes_transferred, &recv_flags, op, 0);
0457     DWORD last_error = ::WSAGetLastError();
0458     if (last_error == ERROR_NETNAME_DELETED)
0459       last_error = WSAECONNRESET;
0460     else if (last_error == ERROR_PORT_UNREACHABLE)
0461       last_error = WSAECONNREFUSED;
0462     if (result != 0 && last_error != WSA_IO_PENDING)
0463       iocp_service_.on_completion(op, last_error, bytes_transferred);
0464     else
0465       iocp_service_.on_pending(op);
0466   }
0467 }
0468 
0469 int win_iocp_socket_service_base::start_null_buffers_receive_op(
0470     win_iocp_socket_service_base::base_implementation_type& impl,
0471     socket_base::message_flags flags, reactor_op* op, operation* iocp_op)
0472 {
0473   if ((impl.state_ & socket_ops::stream_oriented) != 0)
0474   {
0475     // For stream sockets on Windows, we may issue a 0-byte overlapped
0476     // WSARecv to wait until there is data available on the socket.
0477     ::WSABUF buf = { 0, 0 };
0478     start_receive_op(impl, &buf, 1, flags, false, iocp_op);
0479     return -1;
0480   }
0481   else
0482   {
0483     int op_type = (flags & socket_base::message_out_of_band)
0484       ? select_reactor::except_op : select_reactor::read_op;
0485     start_reactor_op(impl, op_type, op);
0486     return op_type;
0487   }
0488 }
0489 
0490 void win_iocp_socket_service_base::start_receive_from_op(
0491     win_iocp_socket_service_base::base_implementation_type& impl,
0492     WSABUF* buffers, std::size_t buffer_count, void* addr,
0493     socket_base::message_flags flags, int* addrlen, operation* op)
0494 {
0495   update_cancellation_thread_id(impl);
0496   iocp_service_.work_started();
0497 
0498   if (!is_open(impl))
0499     iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
0500   else
0501   {
0502     DWORD bytes_transferred = 0;
0503     DWORD recv_flags = flags;
0504     int result = ::WSARecvFrom(impl.socket_, buffers,
0505         static_cast<DWORD>(buffer_count), &bytes_transferred, &recv_flags,
0506         static_cast<socket_addr_type*>(addr), addrlen, op, 0);
0507     DWORD last_error = ::WSAGetLastError();
0508     if (last_error == ERROR_PORT_UNREACHABLE)
0509       last_error = WSAECONNREFUSED;
0510     if (result != 0 && last_error != WSA_IO_PENDING)
0511       iocp_service_.on_completion(op, last_error, bytes_transferred);
0512     else
0513       iocp_service_.on_pending(op);
0514   }
0515 }
0516 
0517 void win_iocp_socket_service_base::start_accept_op(
0518     win_iocp_socket_service_base::base_implementation_type& impl,
0519     bool peer_is_open, socket_holder& new_socket, int family, int type,
0520     int protocol, void* output_buffer, DWORD address_length, operation* op)
0521 {
0522   update_cancellation_thread_id(impl);
0523   iocp_service_.work_started();
0524 
0525   if (!is_open(impl))
0526     iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
0527   else if (peer_is_open)
0528     iocp_service_.on_completion(op, boost::asio::error::already_open);
0529   else
0530   {
0531     boost::system::error_code ec;
0532     new_socket.reset(socket_ops::socket(family, type, protocol, ec));
0533     if (new_socket.get() == invalid_socket)
0534       iocp_service_.on_completion(op, ec);
0535     else
0536     {
0537       DWORD bytes_read = 0;
0538       BOOL result = ::AcceptEx(impl.socket_, new_socket.get(), output_buffer,
0539           0, address_length, address_length, &bytes_read, op);
0540       DWORD last_error = ::WSAGetLastError();
0541       if (!result && last_error != WSA_IO_PENDING)
0542         iocp_service_.on_completion(op, last_error);
0543       else
0544         iocp_service_.on_pending(op);
0545     }
0546   }
0547 }
0548 
0549 void win_iocp_socket_service_base::restart_accept_op(
0550     socket_type s, socket_holder& new_socket, int family, int type,
0551     int protocol, void* output_buffer, DWORD address_length,
0552     long* cancel_requested, operation* op)
0553 {
0554   new_socket.reset();
0555   iocp_service_.work_started();
0556 
0557   // Check if we were cancelled after the first AcceptEx completed.
0558   if (cancel_requested)
0559     if (::InterlockedExchangeAdd(cancel_requested, 0) == 1)
0560       iocp_service_.on_completion(op, boost::asio::error::operation_aborted);
0561 
0562   boost::system::error_code ec;
0563   new_socket.reset(socket_ops::socket(family, type, protocol, ec));
0564   if (new_socket.get() == invalid_socket)
0565     iocp_service_.on_completion(op, ec);
0566   else
0567   {
0568     DWORD bytes_read = 0;
0569     BOOL result = ::AcceptEx(s, new_socket.get(), output_buffer,
0570         0, address_length, address_length, &bytes_read, op);
0571     DWORD last_error = ::WSAGetLastError();
0572     if (!result && last_error != WSA_IO_PENDING)
0573       iocp_service_.on_completion(op, last_error);
0574     else
0575     {
0576 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
0577       if (cancel_requested)
0578       {
0579         if (::InterlockedExchangeAdd(cancel_requested, 0) == 1)
0580         {
0581           HANDLE sock_as_handle = reinterpret_cast<HANDLE>(s);
0582           ::CancelIoEx(sock_as_handle, op);
0583         }
0584       }
0585 #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
0586       iocp_service_.on_pending(op);
0587     }
0588   }
0589 }
0590 
0591 void win_iocp_socket_service_base::start_reactor_op(
0592     win_iocp_socket_service_base::base_implementation_type& impl,
0593     int op_type, reactor_op* op)
0594 {
0595   select_reactor& r = get_reactor();
0596   update_cancellation_thread_id(impl);
0597 
0598   if (is_open(impl))
0599   {
0600     r.start_op(op_type, impl.socket_, impl.reactor_data_, op, false, false);
0601     return;
0602   }
0603   else
0604     op->ec_ = boost::asio::error::bad_descriptor;
0605 
0606   iocp_service_.post_immediate_completion(op, false);
0607 }
0608 
0609 int win_iocp_socket_service_base::start_connect_op(
0610     win_iocp_socket_service_base::base_implementation_type& impl,
0611     int family, int type, const void* addr, std::size_t addrlen,
0612     win_iocp_socket_connect_op_base* op, operation* iocp_op)
0613 {
0614   // If ConnectEx is available, use that.
0615   if (family == BOOST_ASIO_OS_DEF(AF_INET)
0616       || family == BOOST_ASIO_OS_DEF(AF_INET6))
0617   {
0618     if (connect_ex_fn connect_ex = get_connect_ex(impl, type))
0619     {
0620       union address_union
0621       {
0622         socket_addr_type base;
0623         sockaddr_in4_type v4;
0624         sockaddr_in6_type v6;
0625       } a;
0626 
0627       using namespace std; // For memset.
0628       memset(&a, 0, sizeof(a));
0629       a.base.sa_family = family;
0630 
0631       socket_ops::bind(impl.socket_, &a.base,
0632           family == BOOST_ASIO_OS_DEF(AF_INET)
0633           ? sizeof(a.v4) : sizeof(a.v6), op->ec_);
0634       if (op->ec_ && op->ec_ != boost::asio::error::invalid_argument)
0635       {
0636         iocp_service_.post_immediate_completion(op, false);
0637         return -1;
0638       }
0639 
0640       op->connect_ex_ = true;
0641       update_cancellation_thread_id(impl);
0642       iocp_service_.work_started();
0643 
0644       BOOL result = connect_ex(impl.socket_,
0645           static_cast<const socket_addr_type*>(addr),
0646           static_cast<int>(addrlen), 0, 0, 0, iocp_op);
0647       DWORD last_error = ::WSAGetLastError();
0648       if (!result && last_error != WSA_IO_PENDING)
0649         iocp_service_.on_completion(iocp_op, last_error);
0650       else
0651         iocp_service_.on_pending(iocp_op);
0652       return -1;
0653     }
0654   }
0655 
0656   // Otherwise, fall back to a reactor-based implementation.
0657   select_reactor& r = get_reactor();
0658   update_cancellation_thread_id(impl);
0659 
0660   if ((impl.state_ & socket_ops::non_blocking) != 0
0661       || socket_ops::set_internal_non_blocking(
0662         impl.socket_, impl.state_, true, op->ec_))
0663   {
0664     if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
0665     {
0666       if (op->ec_ == boost::asio::error::in_progress
0667           || op->ec_ == boost::asio::error::would_block)
0668       {
0669         op->ec_ = boost::system::error_code();
0670         r.start_op(select_reactor::connect_op, impl.socket_,
0671             impl.reactor_data_, op, false, false);
0672         return select_reactor::connect_op;
0673       }
0674     }
0675   }
0676 
0677   r.post_immediate_completion(op, false);
0678   return -1;
0679 }
0680 
0681 void win_iocp_socket_service_base::close_for_destruction(
0682     win_iocp_socket_service_base::base_implementation_type& impl)
0683 {
0684   if (is_open(impl))
0685   {
0686     BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
0687           "socket", &impl, impl.socket_, "close"));
0688 
0689     // Check if the reactor was created, in which case we need to close the
0690     // socket on the reactor as well to cancel any operations that might be
0691     // running there.
0692     select_reactor* r = static_cast<select_reactor*>(
0693           interlocked_compare_exchange_pointer(
0694             reinterpret_cast<void**>(&reactor_), 0, 0));
0695     if (r)
0696       r->deregister_descriptor(impl.socket_, impl.reactor_data_, true);
0697 
0698     boost::system::error_code ignored_ec;
0699     socket_ops::close(impl.socket_, impl.state_, true, ignored_ec);
0700 
0701     if (r)
0702       r->cleanup_descriptor_data(impl.reactor_data_);
0703   }
0704 
0705   impl.socket_ = invalid_socket;
0706   impl.state_ = 0;
0707   impl.cancel_token_.reset();
0708 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
0709   impl.safe_cancellation_thread_id_ = 0;
0710 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
0711 }
0712 
0713 void win_iocp_socket_service_base::update_cancellation_thread_id(
0714     win_iocp_socket_service_base::base_implementation_type& impl)
0715 {
0716 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
0717   if (impl.safe_cancellation_thread_id_ == 0)
0718     impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
0719   else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
0720     impl.safe_cancellation_thread_id_ = ~DWORD(0);
0721 #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
0722   (void)impl;
0723 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
0724 }
0725 
0726 select_reactor& win_iocp_socket_service_base::get_reactor()
0727 {
0728   select_reactor* r = static_cast<select_reactor*>(
0729         interlocked_compare_exchange_pointer(
0730           reinterpret_cast<void**>(&reactor_), 0, 0));
0731   if (!r)
0732   {
0733     r = &(use_service<select_reactor>(context_));
0734     interlocked_exchange_pointer(reinterpret_cast<void**>(&reactor_), r);
0735   }
0736   return *r;
0737 }
0738 
0739 win_iocp_socket_service_base::connect_ex_fn
0740 win_iocp_socket_service_base::get_connect_ex(
0741     win_iocp_socket_service_base::base_implementation_type& impl, int type)
0742 {
0743 #if defined(BOOST_ASIO_DISABLE_CONNECTEX)
0744   (void)impl;
0745   (void)type;
0746   return 0;
0747 #else // defined(BOOST_ASIO_DISABLE_CONNECTEX)
0748   if (type != BOOST_ASIO_OS_DEF(SOCK_STREAM)
0749       && type != BOOST_ASIO_OS_DEF(SOCK_SEQPACKET))
0750     return 0;
0751 
0752   void* ptr = interlocked_compare_exchange_pointer(&connect_ex_, 0, 0);
0753   if (!ptr)
0754   {
0755     GUID guid = { 0x25a207b9, 0xddf3, 0x4660,
0756       { 0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e } };
0757 
0758     DWORD bytes = 0;
0759     if (::WSAIoctl(impl.socket_, SIO_GET_EXTENSION_FUNCTION_POINTER,
0760           &guid, sizeof(guid), &ptr, sizeof(ptr), &bytes, 0, 0) != 0)
0761     {
0762       // Set connect_ex_ to a special value to indicate that ConnectEx is
0763       // unavailable. That way we won't bother trying to look it up again.
0764       ptr = this;
0765     }
0766 
0767     interlocked_exchange_pointer(&connect_ex_, ptr);
0768   }
0769 
0770   return reinterpret_cast<connect_ex_fn>(ptr == this ? 0 : ptr);
0771 #endif // defined(BOOST_ASIO_DISABLE_CONNECTEX)
0772 }
0773 
0774 win_iocp_socket_service_base::nt_set_info_fn
0775 win_iocp_socket_service_base::get_nt_set_info()
0776 {
0777   void* ptr = interlocked_compare_exchange_pointer(&nt_set_info_, 0, 0);
0778   if (!ptr)
0779   {
0780     if (HMODULE h = ::GetModuleHandleA("NTDLL.DLL"))
0781       ptr = reinterpret_cast<void*>(GetProcAddress(h, "NtSetInformationFile"));
0782 
0783     // On failure, set nt_set_info_ to a special value to indicate that the
0784     // NtSetInformationFile function is unavailable. That way we won't bother
0785     // trying to look it up again.
0786     interlocked_exchange_pointer(&nt_set_info_, ptr ? ptr : this);
0787   }
0788 
0789   return reinterpret_cast<nt_set_info_fn>(ptr == this ? 0 : ptr);
0790 }
0791 
0792 void* win_iocp_socket_service_base::interlocked_compare_exchange_pointer(
0793     void** dest, void* exch, void* cmp)
0794 {
0795 #if defined(_M_IX86)
0796   return reinterpret_cast<void*>(InterlockedCompareExchange(
0797         reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(exch),
0798         reinterpret_cast<LONG>(cmp)));
0799 #else
0800   return InterlockedCompareExchangePointer(dest, exch, cmp);
0801 #endif
0802 }
0803 
0804 void* win_iocp_socket_service_base::interlocked_exchange_pointer(
0805     void** dest, void* val)
0806 {
0807 #if defined(_M_IX86)
0808   return reinterpret_cast<void*>(InterlockedExchange(
0809         reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(val)));
0810 #else
0811   return InterlockedExchangePointer(dest, val);
0812 #endif
0813 }
0814 
0815 } // namespace detail
0816 } // namespace asio
0817 } // namespace boost
0818 
0819 #include <boost/asio/detail/pop_options.hpp>
0820 
0821 #endif // defined(BOOST_ASIO_HAS_IOCP)
0822 
0823 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP