Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/win_iocp_null_buffers_op.hpp
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_WIN_IOCP_NULL_BUFFERS_OP_HPP
0012 #define BOOST_ASIO_DETAIL_WIN_IOCP_NULL_BUFFERS_OP_HPP
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/bind_handler.hpp>
0023 #include <boost/asio/detail/fenced_block.hpp>
0024 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0025 #include <boost/asio/detail/handler_work.hpp>
0026 #include <boost/asio/detail/memory.hpp>
0027 #include <boost/asio/detail/reactor_op.hpp>
0028 #include <boost/asio/detail/socket_ops.hpp>
0029 #include <boost/asio/error.hpp>
0030 
0031 #include <boost/asio/detail/push_options.hpp>
0032 
0033 namespace boost {
0034 namespace asio {
0035 namespace detail {
0036 
0037 template <typename Handler, typename IoExecutor>
0038 class win_iocp_null_buffers_op : public reactor_op
0039 {
0040 public:
0041   BOOST_ASIO_DEFINE_HANDLER_PTR(win_iocp_null_buffers_op);
0042 
0043   win_iocp_null_buffers_op(socket_ops::weak_cancel_token_type cancel_token,
0044       Handler& handler, const IoExecutor& io_ex)
0045     : reactor_op(boost::system::error_code(),
0046         &win_iocp_null_buffers_op::do_perform,
0047         &win_iocp_null_buffers_op::do_complete),
0048       cancel_token_(cancel_token),
0049       handler_(static_cast<Handler&&>(handler)),
0050       work_(handler_, io_ex)
0051   {
0052   }
0053 
0054   static status do_perform(reactor_op*)
0055   {
0056     return done;
0057   }
0058 
0059   static void do_complete(void* owner, operation* base,
0060       const boost::system::error_code& result_ec,
0061       std::size_t bytes_transferred)
0062   {
0063     boost::system::error_code ec(result_ec);
0064 
0065     // Take ownership of the operation object.
0066     BOOST_ASIO_ASSUME(base != 0);
0067     win_iocp_null_buffers_op* o(static_cast<win_iocp_null_buffers_op*>(base));
0068     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0069 
0070     BOOST_ASIO_HANDLER_COMPLETION((*o));
0071 
0072     // Take ownership of the operation's outstanding work.
0073     handler_work<Handler, IoExecutor> w(
0074         static_cast<handler_work<Handler, IoExecutor>&&>(
0075           o->work_));
0076 
0077     // The reactor may have stored a result in the operation object.
0078     if (o->ec_)
0079       ec = o->ec_;
0080 
0081     // Map non-portable errors to their portable counterparts.
0082     if (ec.value() == ERROR_NETNAME_DELETED)
0083     {
0084       if (o->cancel_token_.expired())
0085         ec = boost::asio::error::operation_aborted;
0086       else
0087         ec = boost::asio::error::connection_reset;
0088     }
0089     else if (ec.value() == ERROR_PORT_UNREACHABLE)
0090     {
0091       ec = boost::asio::error::connection_refused;
0092     }
0093 
0094     BOOST_ASIO_ERROR_LOCATION(ec);
0095 
0096     // Make a copy of the handler so that the memory can be deallocated before
0097     // the upcall is made. Even if we're not about to make an upcall, a
0098     // sub-object of the handler may be the true owner of the memory associated
0099     // with the handler. Consequently, a local copy of the handler is required
0100     // to ensure that any owning sub-object remains valid until after we have
0101     // deallocated the memory here.
0102     detail::binder2<Handler, boost::system::error_code, std::size_t>
0103       handler(o->handler_, ec, bytes_transferred);
0104     p.h = boost::asio::detail::addressof(handler.handler_);
0105     p.reset();
0106 
0107     // Make the upcall if required.
0108     if (owner)
0109     {
0110       fenced_block b(fenced_block::half);
0111       BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0112       w.complete(handler, handler.handler_);
0113       BOOST_ASIO_HANDLER_INVOCATION_END;
0114     }
0115   }
0116 
0117 private:
0118   socket_ops::weak_cancel_token_type cancel_token_;
0119   Handler handler_;
0120   handler_work<Handler, IoExecutor> work_;
0121 };
0122 
0123 } // namespace detail
0124 } // namespace asio
0125 } // namespace boost
0126 
0127 #include <boost/asio/detail/pop_options.hpp>
0128 
0129 #endif // defined(BOOST_ASIO_HAS_IOCP)
0130 
0131 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_NULL_BUFFERS_OP_HPP