1 // 2 // detail/reactive_socket_send_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_REACTIVE_SOCKET_SEND_OP_HPP 12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SEND_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 #include <boost/asio/detail/bind_handler.hpp> 20 #include <boost/asio/detail/buffer_sequence_adapter.hpp> 21 #include <boost/asio/detail/fenced_block.hpp> 22 #include <boost/asio/detail/handler_alloc_helpers.hpp> 23 #include <boost/asio/detail/handler_invoke_helpers.hpp> 24 #include <boost/asio/detail/handler_work.hpp> 25 #include <boost/asio/detail/memory.hpp> 26 #include <boost/asio/detail/reactor_op.hpp> 27 #include <boost/asio/detail/socket_ops.hpp> 28 29 #include <boost/asio/detail/push_options.hpp> 30 31 namespace boost { 32 namespace asio { 33 namespace detail { 34 35 template <typename ConstBufferSequence> 36 class reactive_socket_send_op_base : public reactor_op 37 { 38 public: reactive_socket_send_op_base(const boost::system::error_code & success_ec,socket_type socket,socket_ops::state_type state,const ConstBufferSequence & buffers,socket_base::message_flags flags,func_type complete_func)39 reactive_socket_send_op_base(const boost::system::error_code& success_ec, 40 socket_type socket, socket_ops::state_type state, 41 const ConstBufferSequence& buffers, 42 socket_base::message_flags flags, func_type complete_func) 43 : reactor_op(success_ec, 44 &reactive_socket_send_op_base::do_perform, complete_func), 45 socket_(socket), 46 state_(state), 47 buffers_(buffers), 48 flags_(flags) 49 { 50 } 51 do_perform(reactor_op * base)52 static status do_perform(reactor_op* base) 53 { 54 reactive_socket_send_op_base* o( 55 static_cast<reactive_socket_send_op_base*>(base)); 56 57 typedef buffer_sequence_adapter<boost::asio::const_buffer, 58 ConstBufferSequence> bufs_type; 59 60 status result; 61 if (bufs_type::is_single_buffer) 62 { 63 result = socket_ops::non_blocking_send1(o->socket_, 64 bufs_type::first(o->buffers_).data(), 65 bufs_type::first(o->buffers_).size(), o->flags_, 66 o->ec_, o->bytes_transferred_) ? done : not_done; 67 68 if (result == done) 69 if ((o->state_ & socket_ops::stream_oriented) != 0) 70 if (o->bytes_transferred_ < bufs_type::first(o->buffers_).size()) 71 result = done_and_exhausted; 72 } 73 else 74 { 75 bufs_type bufs(o->buffers_); 76 result = socket_ops::non_blocking_send(o->socket_, 77 bufs.buffers(), bufs.count(), o->flags_, 78 o->ec_, o->bytes_transferred_) ? done : not_done; 79 80 if (result == done) 81 if ((o->state_ & socket_ops::stream_oriented) != 0) 82 if (o->bytes_transferred_ < bufs.total_size()) 83 result = done_and_exhausted; 84 } 85 86 BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_send", 87 o->ec_, o->bytes_transferred_)); 88 89 return result; 90 } 91 92 private: 93 socket_type socket_; 94 socket_ops::state_type state_; 95 ConstBufferSequence buffers_; 96 socket_base::message_flags flags_; 97 }; 98 99 template <typename ConstBufferSequence, typename Handler, typename IoExecutor> 100 class reactive_socket_send_op : 101 public reactive_socket_send_op_base<ConstBufferSequence> 102 { 103 public: 104 BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_send_op); 105 reactive_socket_send_op(const boost::system::error_code & success_ec,socket_type socket,socket_ops::state_type state,const ConstBufferSequence & buffers,socket_base::message_flags flags,Handler & handler,const IoExecutor & io_ex)106 reactive_socket_send_op(const boost::system::error_code& success_ec, 107 socket_type socket, socket_ops::state_type state, 108 const ConstBufferSequence& buffers, socket_base::message_flags flags, 109 Handler& handler, const IoExecutor& io_ex) 110 : reactive_socket_send_op_base<ConstBufferSequence>(success_ec, socket, 111 state, buffers, flags, &reactive_socket_send_op::do_complete), 112 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), 113 work_(handler_, io_ex) 114 { 115 } 116 do_complete(void * owner,operation * base,const boost::system::error_code &,std::size_t)117 static void do_complete(void* owner, operation* base, 118 const boost::system::error_code& /*ec*/, 119 std::size_t /*bytes_transferred*/) 120 { 121 // Take ownership of the handler object. 122 reactive_socket_send_op* o(static_cast<reactive_socket_send_op*>(base)); 123 ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; 124 125 BOOST_ASIO_HANDLER_COMPLETION((*o)); 126 127 // Take ownership of the operation's outstanding work. 128 handler_work<Handler, IoExecutor> w( 129 BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)( 130 o->work_)); 131 132 // Make a copy of the handler so that the memory can be deallocated before 133 // the upcall is made. Even if we're not about to make an upcall, a 134 // sub-object of the handler may be the true owner of the memory associated 135 // with the handler. Consequently, a local copy of the handler is required 136 // to ensure that any owning sub-object remains valid until after we have 137 // deallocated the memory here. 138 detail::binder2<Handler, boost::system::error_code, std::size_t> 139 handler(o->handler_, o->ec_, o->bytes_transferred_); 140 p.h = boost::asio::detail::addressof(handler.handler_); 141 p.reset(); 142 143 // Make the upcall if required. 144 if (owner) 145 { 146 fenced_block b(fenced_block::half); 147 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); 148 w.complete(handler, handler.handler_); 149 BOOST_ASIO_HANDLER_INVOCATION_END; 150 } 151 } 152 153 private: 154 Handler handler_; 155 handler_work<Handler, IoExecutor> work_; 156 }; 157 158 } // namespace detail 159 } // namespace asio 160 } // namespace boost 161 162 #include <boost/asio/detail/pop_options.hpp> 163 164 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SEND_OP_HPP 165