]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // detail/reactive_socket_recvmsg_op.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2016 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_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> | |
19 | #include <boost/asio/detail/addressof.hpp> | |
20 | #include <boost/asio/detail/bind_handler.hpp> | |
21 | #include <boost/asio/detail/buffer_sequence_adapter.hpp> | |
22 | #include <boost/asio/detail/fenced_block.hpp> | |
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 | ||
48 | static bool do_perform(reactor_op* base) | |
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 | ||
56 | return socket_ops::non_blocking_recvmsg(o->socket_, | |
57 | bufs.buffers(), bufs.count(), | |
58 | o->in_flags_, o->out_flags_, | |
59 | o->ec_, o->bytes_transferred_); | |
60 | } | |
61 | ||
62 | private: | |
63 | socket_type socket_; | |
64 | MutableBufferSequence buffers_; | |
65 | socket_base::message_flags in_flags_; | |
66 | socket_base::message_flags& out_flags_; | |
67 | }; | |
68 | ||
69 | template <typename MutableBufferSequence, typename Handler> | |
70 | class reactive_socket_recvmsg_op : | |
71 | public reactive_socket_recvmsg_op_base<MutableBufferSequence> | |
72 | { | |
73 | public: | |
74 | BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op); | |
75 | ||
76 | reactive_socket_recvmsg_op(socket_type socket, | |
77 | const MutableBufferSequence& buffers, socket_base::message_flags in_flags, | |
78 | socket_base::message_flags& out_flags, Handler& handler) | |
79 | : reactive_socket_recvmsg_op_base<MutableBufferSequence>(socket, buffers, | |
80 | in_flags, out_flags, &reactive_socket_recvmsg_op::do_complete), | |
81 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)) | |
82 | { | |
83 | } | |
84 | ||
85 | static void do_complete(io_service_impl* owner, operation* base, | |
86 | const boost::system::error_code& /*ec*/, | |
87 | std::size_t /*bytes_transferred*/) | |
88 | { | |
89 | // Take ownership of the handler object. | |
90 | reactive_socket_recvmsg_op* o( | |
91 | static_cast<reactive_socket_recvmsg_op*>(base)); | |
92 | ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; | |
93 | ||
94 | BOOST_ASIO_HANDLER_COMPLETION((o)); | |
95 | ||
96 | // Make a copy of the handler so that the memory can be deallocated before | |
97 | // the upcall is made. Even if we're not about to make an upcall, a | |
98 | // sub-object of the handler may be the true owner of the memory associated | |
99 | // with the handler. Consequently, a local copy of the handler is required | |
100 | // to ensure that any owning sub-object remains valid until after we have | |
101 | // deallocated the memory here. | |
102 | detail::binder2<Handler, boost::system::error_code, std::size_t> | |
103 | handler(o->handler_, o->ec_, o->bytes_transferred_); | |
104 | p.h = boost::asio::detail::addressof(handler.handler_); | |
105 | p.reset(); | |
106 | ||
107 | // Make the upcall if required. | |
108 | if (owner) | |
109 | { | |
110 | fenced_block b(fenced_block::half); | |
111 | BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); | |
112 | boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_); | |
113 | BOOST_ASIO_HANDLER_INVOCATION_END; | |
114 | } | |
115 | } | |
116 | ||
117 | private: | |
118 | Handler handler_; | |
119 | }; | |
120 | ||
121 | } // namespace detail | |
122 | } // namespace asio | |
123 | } // namespace boost | |
124 | ||
125 | #include <boost/asio/detail/pop_options.hpp> | |
126 | ||
127 | #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP |