Chris@16
|
1 //
|
Chris@16
|
2 // detail/reactive_socket_recvmsg_op.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/bind_handler.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/fenced_block.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/reactor_op.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/socket_ops.hpp>
|
Chris@16
|
25 #include <boost/asio/socket_base.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 namespace boost {
|
Chris@16
|
30 namespace asio {
|
Chris@16
|
31 namespace detail {
|
Chris@16
|
32
|
Chris@16
|
33 template <typename MutableBufferSequence>
|
Chris@16
|
34 class reactive_socket_recvmsg_op_base : public reactor_op
|
Chris@16
|
35 {
|
Chris@16
|
36 public:
|
Chris@16
|
37 reactive_socket_recvmsg_op_base(socket_type socket,
|
Chris@16
|
38 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
|
Chris@16
|
39 socket_base::message_flags& out_flags, func_type complete_func)
|
Chris@16
|
40 : reactor_op(&reactive_socket_recvmsg_op_base::do_perform, complete_func),
|
Chris@16
|
41 socket_(socket),
|
Chris@16
|
42 buffers_(buffers),
|
Chris@16
|
43 in_flags_(in_flags),
|
Chris@16
|
44 out_flags_(out_flags)
|
Chris@16
|
45 {
|
Chris@16
|
46 }
|
Chris@16
|
47
|
Chris@16
|
48 static bool do_perform(reactor_op* base)
|
Chris@16
|
49 {
|
Chris@16
|
50 reactive_socket_recvmsg_op_base* o(
|
Chris@16
|
51 static_cast<reactive_socket_recvmsg_op_base*>(base));
|
Chris@16
|
52
|
Chris@16
|
53 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
54 MutableBufferSequence> bufs(o->buffers_);
|
Chris@16
|
55
|
Chris@16
|
56 return socket_ops::non_blocking_recvmsg(o->socket_,
|
Chris@16
|
57 bufs.buffers(), bufs.count(),
|
Chris@16
|
58 o->in_flags_, o->out_flags_,
|
Chris@16
|
59 o->ec_, o->bytes_transferred_);
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 private:
|
Chris@16
|
63 socket_type socket_;
|
Chris@16
|
64 MutableBufferSequence buffers_;
|
Chris@16
|
65 socket_base::message_flags in_flags_;
|
Chris@16
|
66 socket_base::message_flags& out_flags_;
|
Chris@16
|
67 };
|
Chris@16
|
68
|
Chris@16
|
69 template <typename MutableBufferSequence, typename Handler>
|
Chris@16
|
70 class reactive_socket_recvmsg_op :
|
Chris@16
|
71 public reactive_socket_recvmsg_op_base<MutableBufferSequence>
|
Chris@16
|
72 {
|
Chris@16
|
73 public:
|
Chris@16
|
74 BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op);
|
Chris@16
|
75
|
Chris@16
|
76 reactive_socket_recvmsg_op(socket_type socket,
|
Chris@16
|
77 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
|
Chris@16
|
78 socket_base::message_flags& out_flags, Handler& handler)
|
Chris@16
|
79 : reactive_socket_recvmsg_op_base<MutableBufferSequence>(socket, buffers,
|
Chris@16
|
80 in_flags, out_flags, &reactive_socket_recvmsg_op::do_complete),
|
Chris@16
|
81 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
|
Chris@16
|
82 {
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 static void do_complete(io_service_impl* owner, operation* base,
|
Chris@16
|
86 const boost::system::error_code& /*ec*/,
|
Chris@16
|
87 std::size_t /*bytes_transferred*/)
|
Chris@16
|
88 {
|
Chris@16
|
89 // Take ownership of the handler object.
|
Chris@16
|
90 reactive_socket_recvmsg_op* o(
|
Chris@16
|
91 static_cast<reactive_socket_recvmsg_op*>(base));
|
Chris@16
|
92 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
|
Chris@16
|
93
|
Chris@16
|
94 BOOST_ASIO_HANDLER_COMPLETION((o));
|
Chris@16
|
95
|
Chris@16
|
96 // Make a copy of the handler so that the memory can be deallocated before
|
Chris@16
|
97 // the upcall is made. Even if we're not about to make an upcall, a
|
Chris@16
|
98 // sub-object of the handler may be the true owner of the memory associated
|
Chris@16
|
99 // with the handler. Consequently, a local copy of the handler is required
|
Chris@16
|
100 // to ensure that any owning sub-object remains valid until after we have
|
Chris@16
|
101 // deallocated the memory here.
|
Chris@16
|
102 detail::binder2<Handler, boost::system::error_code, std::size_t>
|
Chris@16
|
103 handler(o->handler_, o->ec_, o->bytes_transferred_);
|
Chris@16
|
104 p.h = boost::asio::detail::addressof(handler.handler_);
|
Chris@16
|
105 p.reset();
|
Chris@16
|
106
|
Chris@16
|
107 // Make the upcall if required.
|
Chris@16
|
108 if (owner)
|
Chris@16
|
109 {
|
Chris@16
|
110 fenced_block b(fenced_block::half);
|
Chris@16
|
111 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
|
Chris@16
|
112 boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
|
Chris@16
|
113 BOOST_ASIO_HANDLER_INVOCATION_END;
|
Chris@16
|
114 }
|
Chris@16
|
115 }
|
Chris@16
|
116
|
Chris@16
|
117 private:
|
Chris@16
|
118 Handler handler_;
|
Chris@16
|
119 };
|
Chris@16
|
120
|
Chris@16
|
121 } // namespace detail
|
Chris@16
|
122 } // namespace asio
|
Chris@16
|
123 } // namespace boost
|
Chris@16
|
124
|
Chris@16
|
125 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
126
|
Chris@16
|
127 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
|