1 // 2 // detail/descriptor_read_op.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2016 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_DESCRIPTOR_READ_OP_HPP 12 #define ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include "asio/detail/config.hpp" 19 20 #if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__) 21 22 #include "asio/detail/bind_handler.hpp" 23 #include "asio/detail/buffer_sequence_adapter.hpp" 24 #include "asio/detail/descriptor_ops.hpp" 25 #include "asio/detail/fenced_block.hpp" 26 #include "asio/detail/handler_work.hpp" 27 #include "asio/detail/memory.hpp" 28 #include "asio/detail/reactor_op.hpp" 29 30 #include "asio/detail/push_options.hpp" 31 32 namespace asio { 33 namespace detail { 34 35 template <typename MutableBufferSequence> 36 class descriptor_read_op_base : public reactor_op 37 { 38 public: descriptor_read_op_base(int descriptor,const MutableBufferSequence & buffers,func_type complete_func)39 descriptor_read_op_base(int descriptor, 40 const MutableBufferSequence& buffers, func_type complete_func) 41 : reactor_op(&descriptor_read_op_base::do_perform, complete_func), 42 descriptor_(descriptor), 43 buffers_(buffers) 44 { 45 } 46 do_perform(reactor_op * base)47 static status do_perform(reactor_op* base) 48 { 49 descriptor_read_op_base* o(static_cast<descriptor_read_op_base*>(base)); 50 51 buffer_sequence_adapter<asio::mutable_buffer, 52 MutableBufferSequence> bufs(o->buffers_); 53 54 status result = descriptor_ops::non_blocking_read(o->descriptor_, 55 bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_) 56 ? done : not_done; 57 58 ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_read", 59 o->ec_, o->bytes_transferred_)); 60 61 return result; 62 } 63 64 private: 65 int descriptor_; 66 MutableBufferSequence buffers_; 67 }; 68 69 template <typename MutableBufferSequence, typename Handler> 70 class descriptor_read_op 71 : public descriptor_read_op_base<MutableBufferSequence> 72 { 73 public: 74 ASIO_DEFINE_HANDLER_PTR(descriptor_read_op); 75 descriptor_read_op(int descriptor,const MutableBufferSequence & buffers,Handler & handler)76 descriptor_read_op(int descriptor, 77 const MutableBufferSequence& buffers, Handler& handler) 78 : descriptor_read_op_base<MutableBufferSequence>( 79 descriptor, buffers, &descriptor_read_op::do_complete), 80 handler_(ASIO_MOVE_CAST(Handler)(handler)) 81 { 82 handler_work<Handler>::start(handler_); 83 } 84 do_complete(void * owner,operation * base,const asio::error_code &,std::size_t)85 static void do_complete(void* owner, operation* base, 86 const asio::error_code& /*ec*/, 87 std::size_t /*bytes_transferred*/) 88 { 89 // Take ownership of the handler object. 90 descriptor_read_op* o(static_cast<descriptor_read_op*>(base)); 91 ptr p = { asio::detail::addressof(o->handler_), o, o }; 92 handler_work<Handler> w(o->handler_); 93 94 ASIO_HANDLER_COMPLETION((*o)); 95 96 // Make a copy of the handler so that the memory can be deallocated before 97 // the upcall is made. Even if we're not about to make an upcall, a 98 // sub-object of the handler may be the true owner of the memory associated 99 // with the handler. Consequently, a local copy of the handler is required 100 // to ensure that any owning sub-object remains valid until after we have 101 // deallocated the memory here. 102 detail::binder2<Handler, asio::error_code, std::size_t> 103 handler(o->handler_, o->ec_, o->bytes_transferred_); 104 p.h = asio::detail::addressof(handler.handler_); 105 p.reset(); 106 107 // Make the upcall if required. 108 if (owner) 109 { 110 fenced_block b(fenced_block::half); 111 ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); 112 w.complete(handler, handler.handler_); 113 ASIO_HANDLER_INVOCATION_END; 114 } 115 } 116 117 private: 118 Handler handler_; 119 }; 120 121 } // namespace detail 122 } // namespace asio 123 124 #include "asio/detail/pop_options.hpp" 125 126 #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__) 127 128 #endif // ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP 129