win_iocp_socket_accept_op.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // detail/win_iocp_socket_accept_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_WIN_IOCP_SOCKET_ACCEPT_OP_HPP
  11. #define ASIO_DETAIL_WIN_IOCP_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. #if defined(ASIO_HAS_IOCP)
  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/fenced_block.hpp"
  21. #include "asio/detail/handler_alloc_helpers.hpp"
  22. #include "asio/detail/handler_invoke_helpers.hpp"
  23. #include "asio/detail/operation.hpp"
  24. #include "asio/detail/socket_ops.hpp"
  25. #include "asio/detail/win_iocp_socket_service_base.hpp"
  26. #include "asio/error.hpp"
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. namespace detail {
  30. template <typename Socket, typename Protocol, typename Handler>
  31. class win_iocp_socket_accept_op : public operation
  32. {
  33. public:
  34. ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_accept_op);
  35. win_iocp_socket_accept_op(win_iocp_socket_service_base& socket_service,
  36. socket_type socket, Socket& peer, const Protocol& protocol,
  37. typename Protocol::endpoint* peer_endpoint,
  38. bool enable_connection_aborted, Handler handler)
  39. : operation(&win_iocp_socket_accept_op::do_complete),
  40. socket_service_(socket_service),
  41. socket_(socket),
  42. peer_(peer),
  43. protocol_(protocol),
  44. peer_endpoint_(peer_endpoint),
  45. enable_connection_aborted_(enable_connection_aborted),
  46. handler_(handler)
  47. {
  48. }
  49. socket_holder& new_socket()
  50. {
  51. return new_socket_;
  52. }
  53. void* output_buffer()
  54. {
  55. return output_buffer_;
  56. }
  57. DWORD address_length()
  58. {
  59. return sizeof(sockaddr_storage_type) + 16;
  60. }
  61. static void do_complete(io_service_impl* owner, operation* base,
  62. asio::error_code ec, std::size_t /*bytes_transferred*/)
  63. {
  64. // Take ownership of the operation object.
  65. win_iocp_socket_accept_op* o(static_cast<win_iocp_socket_accept_op*>(base));
  66. ptr p = { boost::addressof(o->handler_), o, o };
  67. if (owner)
  68. {
  69. typename Protocol::endpoint peer_endpoint;
  70. std::size_t addr_len = peer_endpoint.capacity();
  71. socket_ops::complete_iocp_accept(o->socket_,
  72. o->output_buffer(), o->address_length(),
  73. peer_endpoint.data(), &addr_len,
  74. o->new_socket_.get(), ec);
  75. // Restart the accept operation if we got the connection_aborted error
  76. // and the enable_connection_aborted socket option is not set.
  77. if (ec == asio::error::connection_aborted
  78. && !o->enable_connection_aborted_)
  79. {
  80. o->reset();
  81. o->socket_service_.restart_accept_op(o->socket_,
  82. o->new_socket_, o->protocol_.family(),
  83. o->protocol_.type(), o->protocol_.protocol(),
  84. o->output_buffer(), o->address_length(), o);
  85. p.v = p.p = 0;
  86. return;
  87. }
  88. // If the socket was successfully accepted, transfer ownership of the
  89. // socket to the peer object.
  90. if (!ec)
  91. {
  92. o->peer_.assign(o->protocol_,
  93. typename Socket::native_type(
  94. o->new_socket_.get(), peer_endpoint), ec);
  95. if (!ec)
  96. o->new_socket_.release();
  97. }
  98. // Pass endpoint back to caller.
  99. if (o->peer_endpoint_)
  100. *o->peer_endpoint_ = peer_endpoint;
  101. }
  102. // Make a copy of the handler so that the memory can be deallocated before
  103. // the upcall is made. Even if we're not about to make an upcall, a
  104. // sub-object of the handler may be the true owner of the memory associated
  105. // with the handler. Consequently, a local copy of the handler is required
  106. // to ensure that any owning sub-object remains valid until after we have
  107. // deallocated the memory here.
  108. detail::binder1<Handler, asio::error_code>
  109. handler(o->handler_, ec);
  110. p.h = boost::addressof(handler.handler_);
  111. p.reset();
  112. // Make the upcall if required.
  113. if (owner)
  114. {
  115. asio::detail::fenced_block b;
  116. asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  117. }
  118. }
  119. private:
  120. win_iocp_socket_service_base& socket_service_;
  121. socket_type socket_;
  122. socket_holder new_socket_;
  123. Socket& peer_;
  124. Protocol protocol_;
  125. typename Protocol::endpoint* peer_endpoint_;
  126. unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
  127. bool enable_connection_aborted_;
  128. Handler handler_;
  129. };
  130. } // namespace detail
  131. } // namespace asio
  132. #include "asio/detail/pop_options.hpp"
  133. #endif // defined(ASIO_HAS_IOCP)
  134. #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP