Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/reactive_socket_sendto_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_REACTIVE_SOCKET_SENDTO_OP_HPP
0012 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SENDTO_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 #include <boost/asio/detail/bind_handler.hpp>
0020 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
0021 #include <boost/asio/detail/fenced_block.hpp>
0022 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0023 #include <boost/asio/detail/handler_work.hpp>
0024 #include <boost/asio/detail/memory.hpp>
0025 #include <boost/asio/detail/reactor_op.hpp>
0026 #include <boost/asio/detail/socket_ops.hpp>
0027 
0028 #include <boost/asio/detail/push_options.hpp>
0029 
0030 namespace boost {
0031 namespace asio {
0032 namespace detail {
0033 
0034 template <typename ConstBufferSequence, typename Endpoint>
0035 class reactive_socket_sendto_op_base : public reactor_op
0036 {
0037 public:
0038   reactive_socket_sendto_op_base(const boost::system::error_code& success_ec,
0039       socket_type socket, const ConstBufferSequence& buffers,
0040       const Endpoint& endpoint, socket_base::message_flags flags,
0041       func_type complete_func)
0042     : reactor_op(success_ec,
0043         &reactive_socket_sendto_op_base::do_perform, complete_func),
0044       socket_(socket),
0045       buffers_(buffers),
0046       destination_(endpoint),
0047       flags_(flags)
0048   {
0049   }
0050 
0051   static status do_perform(reactor_op* base)
0052   {
0053     BOOST_ASIO_ASSUME(base != 0);
0054     reactive_socket_sendto_op_base* o(
0055         static_cast<reactive_socket_sendto_op_base*>(base));
0056 
0057     typedef buffer_sequence_adapter<boost::asio::const_buffer,
0058         ConstBufferSequence> bufs_type;
0059 
0060     status result;
0061     if (bufs_type::is_single_buffer)
0062     {
0063       result = socket_ops::non_blocking_sendto1(o->socket_,
0064           bufs_type::first(o->buffers_).data(),
0065           bufs_type::first(o->buffers_).size(), o->flags_,
0066           o->destination_.data(), o->destination_.size(),
0067           o->ec_, o->bytes_transferred_) ? done : not_done;
0068     }
0069     else
0070     {
0071       bufs_type bufs(o->buffers_);
0072       result = socket_ops::non_blocking_sendto(o->socket_,
0073           bufs.buffers(), bufs.count(), o->flags_,
0074           o->destination_.data(), o->destination_.size(),
0075           o->ec_, o->bytes_transferred_) ? done : not_done;
0076     }
0077 
0078     BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_sendto",
0079           o->ec_, o->bytes_transferred_));
0080 
0081     return result;
0082   }
0083 
0084 private:
0085   socket_type socket_;
0086   ConstBufferSequence buffers_;
0087   Endpoint destination_;
0088   socket_base::message_flags flags_;
0089 };
0090 
0091 template <typename ConstBufferSequence, typename Endpoint,
0092     typename Handler, typename IoExecutor>
0093 class reactive_socket_sendto_op :
0094   public reactive_socket_sendto_op_base<ConstBufferSequence, Endpoint>
0095 {
0096 public:
0097   typedef Handler handler_type;
0098   typedef IoExecutor io_executor_type;
0099 
0100   BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_sendto_op);
0101 
0102   reactive_socket_sendto_op(const boost::system::error_code& success_ec,
0103       socket_type socket, const ConstBufferSequence& buffers,
0104       const Endpoint& endpoint, socket_base::message_flags flags,
0105       Handler& handler, const IoExecutor& io_ex)
0106     : reactive_socket_sendto_op_base<ConstBufferSequence, Endpoint>(
0107         success_ec, socket, buffers, endpoint, flags,
0108         &reactive_socket_sendto_op::do_complete),
0109       handler_(static_cast<Handler&&>(handler)),
0110       work_(handler_, io_ex)
0111   {
0112   }
0113 
0114   static void do_complete(void* owner, operation* base,
0115       const boost::system::error_code& /*ec*/,
0116       std::size_t /*bytes_transferred*/)
0117   {
0118     // Take ownership of the handler object.
0119     BOOST_ASIO_ASSUME(base != 0);
0120     reactive_socket_sendto_op* o(static_cast<reactive_socket_sendto_op*>(base));
0121     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0122 
0123     BOOST_ASIO_HANDLER_COMPLETION((*o));
0124 
0125     // Take ownership of the operation's outstanding work.
0126     handler_work<Handler, IoExecutor> w(
0127         static_cast<handler_work<Handler, IoExecutor>&&>(
0128           o->work_));
0129 
0130     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0131 
0132     // Make a copy of the handler so that the memory can be deallocated before
0133     // the upcall is made. Even if we're not about to make an upcall, a
0134     // sub-object of the handler may be the true owner of the memory associated
0135     // with the handler. Consequently, a local copy of the handler is required
0136     // to ensure that any owning sub-object remains valid until after we have
0137     // deallocated the memory here.
0138     detail::binder2<Handler, boost::system::error_code, std::size_t>
0139       handler(o->handler_, o->ec_, o->bytes_transferred_);
0140     p.h = boost::asio::detail::addressof(handler.handler_);
0141     p.reset();
0142 
0143     // Make the upcall if required.
0144     if (owner)
0145     {
0146       fenced_block b(fenced_block::half);
0147       BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0148       w.complete(handler, handler.handler_);
0149       BOOST_ASIO_HANDLER_INVOCATION_END;
0150     }
0151   }
0152 
0153   static void do_immediate(operation* base, bool, const void* io_ex)
0154   {
0155     // Take ownership of the handler object.
0156     BOOST_ASIO_ASSUME(base != 0);
0157     reactive_socket_sendto_op* o(static_cast<reactive_socket_sendto_op*>(base));
0158     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0159 
0160     BOOST_ASIO_HANDLER_COMPLETION((*o));
0161 
0162     // Take ownership of the operation's outstanding work.
0163     immediate_handler_work<Handler, IoExecutor> w(
0164         static_cast<handler_work<Handler, IoExecutor>&&>(
0165           o->work_));
0166 
0167     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0168 
0169     // Make a copy of the handler so that the memory can be deallocated before
0170     // the upcall is made. Even if we're not about to make an upcall, a
0171     // sub-object of the handler may be the true owner of the memory associated
0172     // with the handler. Consequently, a local copy of the handler is required
0173     // to ensure that any owning sub-object remains valid until after we have
0174     // deallocated the memory here.
0175     detail::binder2<Handler, boost::system::error_code, std::size_t>
0176       handler(o->handler_, o->ec_, o->bytes_transferred_);
0177     p.h = boost::asio::detail::addressof(handler.handler_);
0178     p.reset();
0179 
0180     BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0181     w.complete(handler, handler.handler_, io_ex);
0182     BOOST_ASIO_HANDLER_INVOCATION_END;
0183   }
0184 
0185 private:
0186   Handler handler_;
0187   handler_work<Handler, IoExecutor> work_;
0188 };
0189 
0190 } // namespace detail
0191 } // namespace asio
0192 } // namespace boost
0193 
0194 #include <boost/asio/detail/pop_options.hpp>
0195 
0196 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SENDTO_OP_HPP