10ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie//
20ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie// detail/reactive_socket_recvfrom_op.hpp
30ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie//
50ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
60ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie//
70ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie// Distributed under the Boost Software License, Version 1.0. (See accompanying
80ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
90ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie//
100ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
110ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#ifndef ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_OP_HPP
120ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#define ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_OP_HPP
130ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
140ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
150ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/config.hpp"
160ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/addressof.hpp"
170ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/bind_handler.hpp"
180ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/buffer_sequence_adapter.hpp"
190ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/fenced_block.hpp"
200ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/reactor_op.hpp"
210ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/socket_ops.hpp"
220ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
230ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/push_options.hpp"
240ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
250ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffienamespace asio {
260ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffienamespace detail {
270ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
280ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffietemplate <typename MutableBufferSequence, typename Endpoint>
290ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffieclass reactive_socket_recvfrom_op_base : public reactor_op
300ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie{
310ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffiepublic:
320ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  reactive_socket_recvfrom_op_base(socket_type socket, int protocol_type,
330ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      const MutableBufferSequence& buffers, Endpoint& endpoint,
340ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      socket_base::message_flags flags, func_type complete_func)
350ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    : reactor_op(&reactive_socket_recvfrom_op_base::do_perform, complete_func),
360ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      socket_(socket),
370ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      protocol_type_(protocol_type),
380ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      buffers_(buffers),
390ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      sender_endpoint_(endpoint),
400ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      flags_(flags)
410ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  {
420ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  }
430ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
440ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  static bool do_perform(reactor_op* base)
450ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  {
460ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    reactive_socket_recvfrom_op_base* o(
470ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        static_cast<reactive_socket_recvfrom_op_base*>(base));
480ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
490ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    buffer_sequence_adapter<asio::mutable_buffer,
500ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        MutableBufferSequence> bufs(o->buffers_);
510ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
520ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    std::size_t addr_len = o->sender_endpoint_.capacity();
530ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    bool result = socket_ops::non_blocking_recvfrom(o->socket_,
540ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        bufs.buffers(), bufs.count(), o->flags_,
550ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        o->sender_endpoint_.data(), &addr_len,
560ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        o->ec_, o->bytes_transferred_);
570ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
580ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    if (result && !o->ec_)
590ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      o->sender_endpoint_.resize(addr_len);
600ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
610ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    return result;
620ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  }
630ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
640ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffieprivate:
650ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  socket_type socket_;
660ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  int protocol_type_;
670ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  MutableBufferSequence buffers_;
680ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  Endpoint& sender_endpoint_;
690ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  socket_base::message_flags flags_;
700ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie};
710ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
720ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffietemplate <typename MutableBufferSequence, typename Endpoint, typename Handler>
730ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffieclass reactive_socket_recvfrom_op :
740ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  public reactive_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>
750ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie{
760ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffiepublic:
770ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvfrom_op);
780ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
790ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  reactive_socket_recvfrom_op(socket_type socket, int protocol_type,
800ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      const MutableBufferSequence& buffers, Endpoint& endpoint,
810ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      socket_base::message_flags flags, Handler& handler)
820ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    : reactive_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>(
830ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        socket, protocol_type, buffers, endpoint, flags,
840ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        &reactive_socket_recvfrom_op::do_complete),
850ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      handler_(ASIO_MOVE_CAST(Handler)(handler))
860ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  {
870ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  }
880ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
890ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  static void do_complete(io_service_impl* owner, operation* base,
900ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      const asio::error_code& /*ec*/,
910ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      std::size_t /*bytes_transferred*/)
920ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  {
930ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // Take ownership of the handler object.
940ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    reactive_socket_recvfrom_op* o(
950ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie        static_cast<reactive_socket_recvfrom_op*>(base));
960ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    ptr p = { asio::detail::addressof(o->handler_), o, o };
970ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
980ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    ASIO_HANDLER_COMPLETION((o));
990ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
1000ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // Make a copy of the handler so that the memory can be deallocated before
1010ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // the upcall is made. Even if we're not about to make an upcall, a
1020ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // sub-object of the handler may be the true owner of the memory associated
1030ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // with the handler. Consequently, a local copy of the handler is required
1040ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // to ensure that any owning sub-object remains valid until after we have
1050ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // deallocated the memory here.
1060ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    detail::binder2<Handler, asio::error_code, std::size_t>
1070ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      handler(o->handler_, o->ec_, o->bytes_transferred_);
1080ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    p.h = asio::detail::addressof(handler.handler_);
1090ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    p.reset();
1100ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
1110ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    // Make the upcall if required.
1120ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    if (owner)
1130ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    {
1140ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      fenced_block b(fenced_block::half);
1150ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
1160ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      asio_handler_invoke_helpers::invoke(handler, handler.handler_);
1170ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie      ASIO_HANDLER_INVOCATION_END;
1180ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie    }
1190ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  }
1200ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
1210ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffieprivate:
1220ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie  Handler handler_;
1230ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie};
1240ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
1250ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie} // namespace detail
1260ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie} // namespace asio
1270ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
1280ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#include "asio/detail/pop_options.hpp"
1290ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie
1300ee85db398be8ea33d67cc42f99a1468cd6c8180François Gaffie#endif // ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_OP_HPP
131