descriptor_write_op.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // detail/descriptor_write_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. #ifndef ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP
  11. #define ASIO_DETAIL_DESCRIPTOR_WRITE_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(ASIO_WINDOWS) && !defined(__CYGWIN__)
  17. #include "asio/detail/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 ConstBufferSequence>
  27. class descriptor_write_op_base : public reactor_op
  28. {
  29. public:
  30. descriptor_write_op_base(int descriptor,
  31. const ConstBufferSequence& buffers, func_type complete_func)
  32. : reactor_op(&descriptor_write_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_write_op_base* o(static_cast<descriptor_write_op_base*>(base));
  40. buffer_sequence_adapter<asio::const_buffer,
  41. ConstBufferSequence> bufs(o->buffers_);
  42. return descriptor_ops::non_blocking_write(o->descriptor_,
  43. bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_);
  44. }
  45. private:
  46. int descriptor_;
  47. ConstBufferSequence buffers_;
  48. };
  49. template <typename ConstBufferSequence, typename Handler>
  50. class descriptor_write_op
  51. : public descriptor_write_op_base<ConstBufferSequence>
  52. {
  53. public:
  54. ASIO_DEFINE_HANDLER_PTR(descriptor_write_op);
  55. descriptor_write_op(int descriptor,
  56. const ConstBufferSequence& buffers, Handler& handler)
  57. : descriptor_write_op_base<ConstBufferSequence>(
  58. descriptor, buffers, &descriptor_write_op::do_complete),
  59. handler_(ASIO_MOVE_CAST(Handler)(handler))
  60. {
  61. }
  62. static void do_complete(io_service_impl* owner, operation* base,
  63. const asio::error_code& /*ec*/,
  64. std::size_t /*bytes_transferred*/)
  65. {
  66. // Take ownership of the handler object.
  67. descriptor_write_op* o(static_cast<descriptor_write_op*>(base));
  68. ptr p = { asio::detail::addressof(o->handler_), o, o };
  69. ASIO_HANDLER_COMPLETION((o));
  70. // Make a copy of the handler so that the memory can be deallocated before
  71. // the upcall is made. Even if we're not about to make an upcall, a
  72. // sub-object of the handler may be the true owner of the memory associated
  73. // with the handler. Consequently, a local copy of the handler is required
  74. // to ensure that any owning sub-object remains valid until after we have
  75. // deallocated the memory here.
  76. detail::binder2<Handler, asio::error_code, std::size_t>
  77. handler(o->handler_, o->ec_, o->bytes_transferred_);
  78. p.h = asio::detail::addressof(handler.handler_);
  79. p.reset();
  80. // Make the upcall if required.
  81. if (owner)
  82. {
  83. fenced_block b(fenced_block::half);
  84. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  85. asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  86. ASIO_HANDLER_INVOCATION_END;
  87. }
  88. }
  89. private:
  90. Handler handler_;
  91. };
  92. } // namespace detail
  93. } // namespace asio
  94. #include "asio/detail/pop_options.hpp"
  95. #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  96. #endif // ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP