1 // 2 // detail/win_iocp_wait_op.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6 // 7 // Distributed under the Boost Software License, Version 1.0. (See accompanying 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9 // 10 11 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_WAIT_OP_HPP 12 #define BOOST_ASIO_DETAIL_WIN_IOCP_WAIT_OP_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <boost/asio/detail/config.hpp> 19 20 #if defined(BOOST_ASIO_HAS_IOCP) 21 22 #include <boost/asio/detail/bind_handler.hpp> 23 #include <boost/asio/detail/buffer_sequence_adapter.hpp> 24 #include <boost/asio/detail/fenced_block.hpp> 25 #include <boost/asio/detail/handler_alloc_helpers.hpp> 26 #include <boost/asio/detail/handler_invoke_helpers.hpp> 27 #include <boost/asio/detail/handler_work.hpp> 28 #include <boost/asio/detail/memory.hpp> 29 #include <boost/asio/detail/reactor_op.hpp> 30 #include <boost/asio/detail/socket_ops.hpp> 31 #include <boost/asio/error.hpp> 32 33 #include <boost/asio/detail/push_options.hpp> 34 35 namespace boost { 36 namespace asio { 37 namespace detail { 38 39 template <typename Handler, typename IoExecutor> 40 class win_iocp_wait_op : public reactor_op 41 { 42 public: 43 BOOST_ASIO_DEFINE_HANDLER_PTR(win_iocp_wait_op); 44 win_iocp_wait_op(socket_ops::weak_cancel_token_type cancel_token,Handler & handler,const IoExecutor & io_ex)45 win_iocp_wait_op(socket_ops::weak_cancel_token_type cancel_token, 46 Handler& handler, const IoExecutor& io_ex) 47 : reactor_op(boost::system::error_code(), 48 &win_iocp_wait_op::do_perform, 49 &win_iocp_wait_op::do_complete), 50 cancel_token_(cancel_token), 51 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), 52 work_(handler_, io_ex) 53 { 54 } 55 do_perform(reactor_op *)56 static status do_perform(reactor_op*) 57 { 58 return done; 59 } 60 do_complete(void * owner,operation * base,const boost::system::error_code & result_ec,std::size_t)61 static void do_complete(void* owner, operation* base, 62 const boost::system::error_code& result_ec, 63 std::size_t /*bytes_transferred*/) 64 { 65 boost::system::error_code ec(result_ec); 66 67 // Take ownership of the operation object. 68 win_iocp_wait_op* o(static_cast<win_iocp_wait_op*>(base)); 69 ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; 70 71 BOOST_ASIO_HANDLER_COMPLETION((*o)); 72 73 // Take ownership of the operation's outstanding work. 74 handler_work<Handler, IoExecutor> w( 75 BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)( 76 o->work_)); 77 78 // The reactor may have stored a result in the operation object. 79 if (o->ec_) 80 ec = o->ec_; 81 82 // Map non-portable errors to their portable counterparts. 83 if (ec.value() == ERROR_NETNAME_DELETED) 84 { 85 if (o->cancel_token_.expired()) 86 ec = boost::asio::error::operation_aborted; 87 else 88 ec = boost::asio::error::connection_reset; 89 } 90 else if (ec.value() == ERROR_PORT_UNREACHABLE) 91 { 92 ec = boost::asio::error::connection_refused; 93 } 94 95 // Make a copy of the handler so that the memory can be deallocated before 96 // the upcall is made. Even if we're not about to make an upcall, a 97 // sub-object of the handler may be the true owner of the memory associated 98 // with the handler. Consequently, a local copy of the handler is required 99 // to ensure that any owning sub-object remains valid until after we have 100 // deallocated the memory here. 101 detail::binder1<Handler, boost::system::error_code> 102 handler(o->handler_, ec); 103 p.h = boost::asio::detail::addressof(handler.handler_); 104 p.reset(); 105 106 // Make the upcall if required. 107 if (owner) 108 { 109 fenced_block b(fenced_block::half); 110 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_)); 111 w.complete(handler, handler.handler_); 112 BOOST_ASIO_HANDLER_INVOCATION_END; 113 } 114 } 115 116 private: 117 socket_ops::weak_cancel_token_type cancel_token_; 118 Handler handler_; 119 handler_work<Handler, IoExecutor> work_; 120 }; 121 122 } // namespace detail 123 } // namespace asio 124 } // namespace boost 125 126 #include <boost/asio/detail/pop_options.hpp> 127 128 #endif // defined(BOOST_ASIO_HAS_IOCP) 129 130 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_WAIT_OP_HPP 131