Back to home page

EIC code displayed by LXR

 
 

    


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

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