1 //
2 // detail/completion_handler.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_COMPLETION_HANDLER_HPP
12 #define BOOST_ASIO_DETAIL_COMPLETION_HANDLER_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/fenced_block.hpp>
20 #include <boost/asio/detail/handler_alloc_helpers.hpp>
21 #include <boost/asio/detail/handler_work.hpp>
22 #include <boost/asio/detail/memory.hpp>
23 #include <boost/asio/detail/operation.hpp>
24 
25 #include <boost/asio/detail/push_options.hpp>
26 
27 namespace boost {
28 namespace asio {
29 namespace detail {
30 
31 template <typename Handler, typename IoExecutor>
32 class completion_handler : public operation
33 {
34 public:
35   BOOST_ASIO_DEFINE_HANDLER_PTR(completion_handler);
36 
completion_handler(Handler & h,const IoExecutor & io_ex)37   completion_handler(Handler& h, const IoExecutor& io_ex)
38     : operation(&completion_handler::do_complete),
39       handler_(BOOST_ASIO_MOVE_CAST(Handler)(h)),
40       work_(handler_, io_ex)
41   {
42   }
43 
do_complete(void * owner,operation * base,const boost::system::error_code &,std::size_t)44   static void do_complete(void* owner, operation* base,
45       const boost::system::error_code& /*ec*/,
46       std::size_t /*bytes_transferred*/)
47   {
48     // Take ownership of the handler object.
49     completion_handler* h(static_cast<completion_handler*>(base));
50     ptr p = { boost::asio::detail::addressof(h->handler_), h, h };
51 
52     BOOST_ASIO_HANDLER_COMPLETION((*h));
53 
54     // Take ownership of the operation's outstanding work.
55     handler_work<Handler, IoExecutor> w(
56         BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
57           h->work_));
58 
59     // Make a copy of the handler so that the memory can be deallocated before
60     // the upcall is made. Even if we're not about to make an upcall, a
61     // sub-object of the handler may be the true owner of the memory associated
62     // with the handler. Consequently, a local copy of the handler is required
63     // to ensure that any owning sub-object remains valid until after we have
64     // deallocated the memory here.
65     Handler handler(BOOST_ASIO_MOVE_CAST(Handler)(h->handler_));
66     p.h = boost::asio::detail::addressof(handler);
67     p.reset();
68 
69     // Make the upcall if required.
70     if (owner)
71     {
72       fenced_block b(fenced_block::half);
73       BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
74       w.complete(handler, handler);
75       BOOST_ASIO_HANDLER_INVOCATION_END;
76     }
77   }
78 
79 private:
80   Handler handler_;
81   handler_work<Handler, IoExecutor> work_;
82 };
83 
84 } // namespace detail
85 } // namespace asio
86 } // namespace boost
87 
88 #include <boost/asio/detail/pop_options.hpp>
89 
90 #endif // BOOST_ASIO_DETAIL_COMPLETION_HANDLER_HPP
91