Back to home page

EIC code displayed by LXR

 
 

    


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

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