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