1//
2// detail/reactive_socket_sendto_op.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 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 ASIO_DETAIL_REACTIVE_SOCKET_SENDTO_OP_HPP
12#define ASIO_DETAIL_REACTIVE_SOCKET_SENDTO_OP_HPP
13
14
15#include "asio/detail/config.hpp"
16#include "asio/detail/addressof.hpp"
17#include "asio/detail/bind_handler.hpp"
18#include "asio/detail/buffer_sequence_adapter.hpp"
19#include "asio/detail/fenced_block.hpp"
20#include "asio/detail/reactor_op.hpp"
21#include "asio/detail/socket_ops.hpp"
22
23#include "asio/detail/push_options.hpp"
24
25namespace asio {
26namespace detail {
27
28template <typename ConstBufferSequence, typename Endpoint>
29class reactive_socket_sendto_op_base : public reactor_op
30{
31public:
32  reactive_socket_sendto_op_base(socket_type socket,
33      const ConstBufferSequence& buffers, const Endpoint& endpoint,
34      socket_base::message_flags flags, func_type complete_func)
35    : reactor_op(&reactive_socket_sendto_op_base::do_perform, complete_func),
36      socket_(socket),
37      buffers_(buffers),
38      destination_(endpoint),
39      flags_(flags)
40  {
41  }
42
43  static bool do_perform(reactor_op* base)
44  {
45    reactive_socket_sendto_op_base* o(
46        static_cast<reactive_socket_sendto_op_base*>(base));
47
48    buffer_sequence_adapter<asio::const_buffer,
49        ConstBufferSequence> bufs(o->buffers_);
50
51    return socket_ops::non_blocking_sendto(o->socket_,
52          bufs.buffers(), bufs.count(), o->flags_,
53          o->destination_.data(), o->destination_.size(),
54          o->ec_, o->bytes_transferred_);
55  }
56
57private:
58  socket_type socket_;
59  ConstBufferSequence buffers_;
60  Endpoint destination_;
61  socket_base::message_flags flags_;
62};
63
64template <typename ConstBufferSequence, typename Endpoint, typename Handler>
65class reactive_socket_sendto_op :
66  public reactive_socket_sendto_op_base<ConstBufferSequence, Endpoint>
67{
68public:
69  ASIO_DEFINE_HANDLER_PTR(reactive_socket_sendto_op);
70
71  reactive_socket_sendto_op(socket_type socket,
72      const ConstBufferSequence& buffers, const Endpoint& endpoint,
73      socket_base::message_flags flags, Handler& handler)
74    : reactive_socket_sendto_op_base<ConstBufferSequence, Endpoint>(socket,
75        buffers, endpoint, flags, &reactive_socket_sendto_op::do_complete),
76      handler_(ASIO_MOVE_CAST(Handler)(handler))
77  {
78  }
79
80  static void do_complete(io_service_impl* owner, operation* base,
81      const asio::error_code& /*ec*/,
82      std::size_t /*bytes_transferred*/)
83  {
84    // Take ownership of the handler object.
85    reactive_socket_sendto_op* o(static_cast<reactive_socket_sendto_op*>(base));
86    ptr p = { asio::detail::addressof(o->handler_), o, o };
87
88    ASIO_HANDLER_COMPLETION((o));
89
90    // Make a copy of the handler so that the memory can be deallocated before
91    // the upcall is made. Even if we're not about to make an upcall, a
92    // sub-object of the handler may be the true owner of the memory associated
93    // with the handler. Consequently, a local copy of the handler is required
94    // to ensure that any owning sub-object remains valid until after we have
95    // deallocated the memory here.
96    detail::binder2<Handler, asio::error_code, std::size_t>
97      handler(o->handler_, o->ec_, o->bytes_transferred_);
98    p.h = asio::detail::addressof(handler.handler_);
99    p.reset();
100
101    // Make the upcall if required.
102    if (owner)
103    {
104      fenced_block b(fenced_block::half);
105      ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
106      asio_handler_invoke_helpers::invoke(handler, handler.handler_);
107      ASIO_HANDLER_INVOCATION_END;
108    }
109  }
110
111private:
112  Handler handler_;
113};
114
115} // namespace detail
116} // namespace asio
117
118#include "asio/detail/pop_options.hpp"
119
120#endif // ASIO_DETAIL_REACTIVE_SOCKET_SENDTO_OP_HPP
121