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