reactive_socket_accept_op.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // detail/reactive_socket_accept_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_REACTIVE_SOCKET_ACCEPT_OP_HPP
  11. #define ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_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. #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_holder.hpp"
  22. #include "asio/detail/socket_ops.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. template <typename Socket, typename Protocol>
  27. class reactive_socket_accept_op_base : public reactor_op
  28. {
  29. public:
  30. reactive_socket_accept_op_base(socket_type socket,
  31. socket_ops::state_type state, Socket& peer, const Protocol& protocol,
  32. typename Protocol::endpoint* peer_endpoint, func_type complete_func)
  33. : reactor_op(&reactive_socket_accept_op_base::do_perform, complete_func),
  34. socket_(socket),
  35. state_(state),
  36. peer_(peer),
  37. protocol_(protocol),
  38. peer_endpoint_(peer_endpoint)
  39. {
  40. }
  41. static bool do_perform(reactor_op* base)
  42. {
  43. reactive_socket_accept_op_base* o(
  44. static_cast<reactive_socket_accept_op_base*>(base));
  45. std::size_t addrlen = o->peer_endpoint_ ? o->peer_endpoint_->capacity() : 0;
  46. socket_type new_socket = invalid_socket;
  47. bool result = socket_ops::non_blocking_accept(o->socket_,
  48. o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
  49. o->peer_endpoint_ ? &addrlen : 0, o->ec_, new_socket);
  50. // On success, assign new connection to peer socket object.
  51. if (new_socket != invalid_socket)
  52. {
  53. socket_holder new_socket_holder(new_socket);
  54. if (o->peer_endpoint_)
  55. o->peer_endpoint_->resize(addrlen);
  56. if (!o->peer_.assign(o->protocol_, new_socket, o->ec_))
  57. new_socket_holder.release();
  58. }
  59. return result;
  60. }
  61. private:
  62. socket_type socket_;
  63. socket_ops::state_type state_;
  64. Socket& peer_;
  65. Protocol protocol_;
  66. typename Protocol::endpoint* peer_endpoint_;
  67. };
  68. template <typename Socket, typename Protocol, typename Handler>
  69. class reactive_socket_accept_op :
  70. public reactive_socket_accept_op_base<Socket, Protocol>
  71. {
  72. public:
  73. ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
  74. reactive_socket_accept_op(socket_type socket,
  75. socket_ops::state_type state, Socket& peer, const Protocol& protocol,
  76. typename Protocol::endpoint* peer_endpoint, Handler& handler)
  77. : reactive_socket_accept_op_base<Socket, Protocol>(socket, state, peer,
  78. protocol, peer_endpoint, &reactive_socket_accept_op::do_complete),
  79. handler_(ASIO_MOVE_CAST(Handler)(handler))
  80. {
  81. }
  82. static void do_complete(io_service_impl* owner, operation* base,
  83. const asio::error_code& /*ec*/,
  84. std::size_t /*bytes_transferred*/)
  85. {
  86. // Take ownership of the handler object.
  87. reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
  88. ptr p = { asio::detail::addressof(o->handler_), o, o };
  89. ASIO_HANDLER_COMPLETION((o));
  90. // Make a copy of the handler so that the memory can be deallocated before
  91. // the upcall is made. Even if we're not about to make an upcall, a
  92. // sub-object of the handler may be the true owner of the memory associated
  93. // with the handler. Consequently, a local copy of the handler is required
  94. // to ensure that any owning sub-object remains valid until after we have
  95. // deallocated the memory here.
  96. detail::binder1<Handler, asio::error_code>
  97. handler(o->handler_, o->ec_);
  98. p.h = asio::detail::addressof(handler.handler_);
  99. p.reset();
  100. // Make the upcall if required.
  101. if (owner)
  102. {
  103. fenced_block b(fenced_block::half);
  104. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  105. asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  106. ASIO_HANDLER_INVOCATION_END;
  107. }
  108. }
  109. private:
  110. Handler handler_;
  111. };
  112. } // namespace detail
  113. } // namespace asio
  114. #include "asio/detail/pop_options.hpp"
  115. #endif // ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP