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