]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/reactive_socket_recvmsg_op.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
92f5a8d4 | 5 | // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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_RECVMSG_OP_HPP | |
12 | #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_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> | |
7c673cae FG |
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> | |
b32b8144 | 22 | #include <boost/asio/detail/memory.hpp> |
7c673cae FG |
23 | #include <boost/asio/detail/reactor_op.hpp> |
24 | #include <boost/asio/detail/socket_ops.hpp> | |
25 | #include <boost/asio/socket_base.hpp> | |
26 | ||
27 | #include <boost/asio/detail/push_options.hpp> | |
28 | ||
29 | namespace boost { | |
30 | namespace asio { | |
31 | namespace detail { | |
32 | ||
33 | template <typename MutableBufferSequence> | |
34 | class reactive_socket_recvmsg_op_base : public reactor_op | |
35 | { | |
36 | public: | |
37 | reactive_socket_recvmsg_op_base(socket_type socket, | |
38 | const MutableBufferSequence& buffers, socket_base::message_flags in_flags, | |
39 | socket_base::message_flags& out_flags, func_type complete_func) | |
40 | : reactor_op(&reactive_socket_recvmsg_op_base::do_perform, complete_func), | |
41 | socket_(socket), | |
42 | buffers_(buffers), | |
43 | in_flags_(in_flags), | |
44 | out_flags_(out_flags) | |
45 | { | |
46 | } | |
47 | ||
b32b8144 | 48 | static status do_perform(reactor_op* base) |
7c673cae FG |
49 | { |
50 | reactive_socket_recvmsg_op_base* o( | |
51 | static_cast<reactive_socket_recvmsg_op_base*>(base)); | |
52 | ||
53 | buffer_sequence_adapter<boost::asio::mutable_buffer, | |
54 | MutableBufferSequence> bufs(o->buffers_); | |
55 | ||
b32b8144 | 56 | status result = socket_ops::non_blocking_recvmsg(o->socket_, |
7c673cae FG |
57 | bufs.buffers(), bufs.count(), |
58 | o->in_flags_, o->out_flags_, | |
b32b8144 FG |
59 | o->ec_, o->bytes_transferred_) ? done : not_done; |
60 | ||
61 | BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recvmsg", | |
62 | o->ec_, o->bytes_transferred_)); | |
63 | ||
64 | return result; | |
7c673cae FG |
65 | } |
66 | ||
67 | private: | |
68 | socket_type socket_; | |
69 | MutableBufferSequence buffers_; | |
70 | socket_base::message_flags in_flags_; | |
71 | socket_base::message_flags& out_flags_; | |
72 | }; | |
73 | ||
92f5a8d4 | 74 | template <typename MutableBufferSequence, typename Handler, typename IoExecutor> |
7c673cae FG |
75 | class reactive_socket_recvmsg_op : |
76 | public reactive_socket_recvmsg_op_base<MutableBufferSequence> | |
77 | { | |
78 | public: | |
79 | BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op); | |
80 | ||
81 | reactive_socket_recvmsg_op(socket_type socket, | |
82 | const MutableBufferSequence& buffers, socket_base::message_flags in_flags, | |
92f5a8d4 TL |
83 | socket_base::message_flags& out_flags, Handler& handler, |
84 | const IoExecutor& io_ex) | |
7c673cae FG |
85 | : reactive_socket_recvmsg_op_base<MutableBufferSequence>(socket, buffers, |
86 | in_flags, out_flags, &reactive_socket_recvmsg_op::do_complete), | |
92f5a8d4 TL |
87 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), |
88 | io_executor_(io_ex) | |
7c673cae | 89 | { |
92f5a8d4 | 90 | handler_work<Handler, IoExecutor>::start(handler_, io_executor_); |
7c673cae FG |
91 | } |
92 | ||
b32b8144 | 93 | static void do_complete(void* owner, operation* base, |
7c673cae FG |
94 | const boost::system::error_code& /*ec*/, |
95 | std::size_t /*bytes_transferred*/) | |
96 | { | |
97 | // Take ownership of the handler object. | |
98 | reactive_socket_recvmsg_op* o( | |
99 | static_cast<reactive_socket_recvmsg_op*>(base)); | |
100 | ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; | |
92f5a8d4 | 101 | handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_); |
7c673cae | 102 | |
b32b8144 | 103 | BOOST_ASIO_HANDLER_COMPLETION((*o)); |
7c673cae FG |
104 | |
105 | // Make a copy of the handler so that the memory can be deallocated before | |
106 | // the upcall is made. Even if we're not about to make an upcall, a | |
107 | // sub-object of the handler may be the true owner of the memory associated | |
108 | // with the handler. Consequently, a local copy of the handler is required | |
109 | // to ensure that any owning sub-object remains valid until after we have | |
110 | // deallocated the memory here. | |
111 | detail::binder2<Handler, boost::system::error_code, std::size_t> | |
112 | handler(o->handler_, o->ec_, o->bytes_transferred_); | |
113 | p.h = boost::asio::detail::addressof(handler.handler_); | |
114 | p.reset(); | |
115 | ||
116 | // Make the upcall if required. | |
117 | if (owner) | |
118 | { | |
119 | fenced_block b(fenced_block::half); | |
120 | BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); | |
b32b8144 | 121 | w.complete(handler, handler.handler_); |
7c673cae FG |
122 | BOOST_ASIO_HANDLER_INVOCATION_END; |
123 | } | |
124 | } | |
125 | ||
126 | private: | |
127 | Handler handler_; | |
92f5a8d4 | 128 | IoExecutor io_executor_; |
7c673cae FG |
129 | }; |
130 | ||
131 | } // namespace detail | |
132 | } // namespace asio | |
133 | } // namespace boost | |
134 | ||
135 | #include <boost/asio/detail/pop_options.hpp> | |
136 | ||
137 | #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP |