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