/Src/Dependencies/Boost/boost/asio/detail/win_iocp_socket_accept_op.hpp

http://hadesmem.googlecode.com/ · C++ Header · 164 lines · 117 code · 25 blank · 22 comment · 9 complexity · a9e7a141ba87706e5880ff821a08d857 MD5 · raw file

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