descriptor_read_op.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // detail/descriptor_read_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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. #ifndef ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP
  11. #define ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  17. #include <boost/utility/addressof.hpp>
  18. #include "asio/detail/bind_handler.hpp"
  19. #include "asio/detail/buffer_sequence_adapter.hpp"
  20. #include "asio/detail/descriptor_ops.hpp"
  21. #include "asio/detail/fenced_block.hpp"
  22. #include "asio/detail/reactor_op.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. template <typename MutableBufferSequence>
  27. class descriptor_read_op_base : public reactor_op
  28. {
  29. public:
  30. descriptor_read_op_base(int descriptor,
  31. const MutableBufferSequence& buffers, func_type complete_func)
  32. : reactor_op(&descriptor_read_op_base::do_perform, complete_func),
  33. descriptor_(descriptor),
  34. buffers_(buffers)
  35. {
  36. }
  37. static bool do_perform(reactor_op* base)
  38. {
  39. descriptor_read_op_base* o(static_cast<descriptor_read_op_base*>(base));
  40. buffer_sequence_adapter<asio::mutable_buffer,
  41. MutableBufferSequence> bufs(o->buffers_);
  42. return descriptor_ops::non_blocking_read(o->descriptor_,
  43. bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_);
  44. }
  45. private:
  46. int descriptor_;
  47. MutableBufferSequence buffers_;
  48. };
  49. template <typename MutableBufferSequence, typename Handler>
  50. class descriptor_read_op
  51. : public descriptor_read_op_base<MutableBufferSequence>
  52. {
  53. public:
  54. ASIO_DEFINE_HANDLER_PTR(descriptor_read_op);
  55. descriptor_read_op(int descriptor,
  56. const MutableBufferSequence& buffers, Handler handler)
  57. : descriptor_read_op_base<MutableBufferSequence>(
  58. descriptor, buffers, &descriptor_read_op::do_complete),
  59. handler_(handler)
  60. {
  61. }
  62. static void do_complete(io_service_impl* owner, operation* base,
  63. asio::error_code /*ec*/, std::size_t /*bytes_transferred*/)
  64. {
  65. // Take ownership of the handler object.
  66. descriptor_read_op* o(static_cast<descriptor_read_op*>(base));
  67. ptr p = { boost::addressof(o->handler_), o, o };
  68. // Make a copy of the handler so that the memory can be deallocated before
  69. // the upcall is made. Even if we're not about to make an upcall, a
  70. // sub-object of the handler may be the true owner of the memory associated
  71. // with the handler. Consequently, a local copy of the handler is required
  72. // to ensure that any owning sub-object remains valid until after we have
  73. // deallocated the memory here.
  74. detail::binder2<Handler, asio::error_code, std::size_t>
  75. handler(o->handler_, o->ec_, o->bytes_transferred_);
  76. p.h = boost::addressof(handler.handler_);
  77. p.reset();
  78. // Make the upcall if required.
  79. if (owner)
  80. {
  81. asio::detail::fenced_block b;
  82. asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  83. }
  84. }
  85. private:
  86. Handler handler_;
  87. };
  88. } // namespace detail
  89. } // namespace asio
  90. #include "asio/detail/pop_options.hpp"
  91. #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  92. #endif // ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP