2 // detail/win_iocp_socket_accept_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_IOCP)
22 #include <boost/asio/detail/bind_handler.hpp>
23 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
24 #include <boost/asio/detail/fenced_block.hpp>
25 #include <boost/asio/detail/handler_alloc_helpers.hpp>
26 #include <boost/asio/detail/handler_invoke_helpers.hpp>
27 #include <boost/asio/detail/memory.hpp>
28 #include <boost/asio/detail/operation.hpp>
29 #include <boost/asio/detail/socket_ops.hpp>
30 #include <boost/asio/detail/win_iocp_socket_service_base.hpp>
31 #include <boost/asio/error.hpp>
33 #include <boost/asio/detail/push_options.hpp>
39 template <typename Socket, typename Protocol,
40 typename Handler, typename IoExecutor>
41 class win_iocp_socket_accept_op : public operation
44 BOOST_ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_accept_op);
46 win_iocp_socket_accept_op(win_iocp_socket_service_base& socket_service,
47 socket_type socket, Socket& peer, const Protocol& protocol,
48 typename Protocol::endpoint* peer_endpoint,
49 bool enable_connection_aborted, Handler& handler, const IoExecutor& io_ex)
50 : operation(&win_iocp_socket_accept_op::do_complete),
51 socket_service_(socket_service),
55 peer_endpoint_(peer_endpoint),
56 enable_connection_aborted_(enable_connection_aborted),
57 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
60 handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
63 socket_holder& new_socket()
70 return output_buffer_;
73 DWORD address_length()
75 return sizeof(sockaddr_storage_type) + 16;
78 static void do_complete(void* owner, operation* base,
79 const boost::system::error_code& result_ec,
80 std::size_t /*bytes_transferred*/)
82 boost::system::error_code ec(result_ec);
84 // Take ownership of the operation object.
85 win_iocp_socket_accept_op* o(static_cast<win_iocp_socket_accept_op*>(base));
86 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
87 handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
91 typename Protocol::endpoint peer_endpoint;
92 std::size_t addr_len = peer_endpoint.capacity();
93 socket_ops::complete_iocp_accept(o->socket_,
94 o->output_buffer(), o->address_length(),
95 peer_endpoint.data(), &addr_len,
96 o->new_socket_.get(), ec);
98 // Restart the accept operation if we got the connection_aborted error
99 // and the enable_connection_aborted socket option is not set.
100 if (ec == boost::asio::error::connection_aborted
101 && !o->enable_connection_aborted_)
103 handler_work<Handler, IoExecutor>::start(o->handler_, o->io_executor_);
105 o->socket_service_.restart_accept_op(o->socket_,
106 o->new_socket_, o->protocol_.family(),
107 o->protocol_.type(), o->protocol_.protocol(),
108 o->output_buffer(), o->address_length(), o);
113 // If the socket was successfully accepted, transfer ownership of the
114 // socket to the peer object.
117 o->peer_.assign(o->protocol_,
118 typename Socket::native_handle_type(
119 o->new_socket_.get(), peer_endpoint), ec);
121 o->new_socket_.release();
124 // Pass endpoint back to caller.
125 if (o->peer_endpoint_)
126 *o->peer_endpoint_ = peer_endpoint;
129 BOOST_ASIO_HANDLER_COMPLETION((*o));
131 // Make a copy of the handler so that the memory can be deallocated before
132 // the upcall is made. Even if we're not about to make an upcall, a
133 // sub-object of the handler may be the true owner of the memory associated
134 // with the handler. Consequently, a local copy of the handler is required
135 // to ensure that any owning sub-object remains valid until after we have
136 // deallocated the memory here.
137 detail::binder1<Handler, boost::system::error_code>
138 handler(o->handler_, ec);
139 p.h = boost::asio::detail::addressof(handler.handler_);
142 // Make the upcall if required.
145 fenced_block b(fenced_block::half);
146 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
147 w.complete(handler, handler.handler_);
148 BOOST_ASIO_HANDLER_INVOCATION_END;
153 win_iocp_socket_service_base& socket_service_;
155 socket_holder new_socket_;
158 typename Protocol::endpoint* peer_endpoint_;
159 unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
160 bool enable_connection_aborted_;
162 IoExecutor io_executor_;
165 #if defined(BOOST_ASIO_HAS_MOVE)
167 template <typename Protocol, typename PeerIoExecutor,
168 typename Handler, typename IoExecutor>
169 class win_iocp_socket_move_accept_op : public operation
172 BOOST_ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_move_accept_op);
174 win_iocp_socket_move_accept_op(
175 win_iocp_socket_service_base& socket_service, socket_type socket,
176 const Protocol& protocol, const PeerIoExecutor& peer_io_ex,
177 typename Protocol::endpoint* peer_endpoint,
178 bool enable_connection_aborted, Handler& handler, const IoExecutor& io_ex)
179 : operation(&win_iocp_socket_move_accept_op::do_complete),
180 socket_service_(socket_service),
184 peer_endpoint_(peer_endpoint),
185 enable_connection_aborted_(enable_connection_aborted),
186 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
189 handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
192 socket_holder& new_socket()
197 void* output_buffer()
199 return output_buffer_;
202 DWORD address_length()
204 return sizeof(sockaddr_storage_type) + 16;
207 static void do_complete(void* owner, operation* base,
208 const boost::system::error_code& result_ec,
209 std::size_t /*bytes_transferred*/)
211 boost::system::error_code ec(result_ec);
213 // Take ownership of the operation object.
214 win_iocp_socket_move_accept_op* o(
215 static_cast<win_iocp_socket_move_accept_op*>(base));
216 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
217 handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
221 typename Protocol::endpoint peer_endpoint;
222 std::size_t addr_len = peer_endpoint.capacity();
223 socket_ops::complete_iocp_accept(o->socket_,
224 o->output_buffer(), o->address_length(),
225 peer_endpoint.data(), &addr_len,
226 o->new_socket_.get(), ec);
228 // Restart the accept operation if we got the connection_aborted error
229 // and the enable_connection_aborted socket option is not set.
230 if (ec == boost::asio::error::connection_aborted
231 && !o->enable_connection_aborted_)
233 handler_work<Handler, IoExecutor>::start(o->handler_, o->io_executor_);
235 o->socket_service_.restart_accept_op(o->socket_,
236 o->new_socket_, o->protocol_.family(),
237 o->protocol_.type(), o->protocol_.protocol(),
238 o->output_buffer(), o->address_length(), o);
243 // If the socket was successfully accepted, transfer ownership of the
244 // socket to the peer object.
247 o->peer_.assign(o->protocol_,
248 typename Protocol::socket::native_handle_type(
249 o->new_socket_.get(), peer_endpoint), ec);
251 o->new_socket_.release();
254 // Pass endpoint back to caller.
255 if (o->peer_endpoint_)
256 *o->peer_endpoint_ = peer_endpoint;
259 BOOST_ASIO_HANDLER_COMPLETION((*o));
261 // Make a copy of the handler so that the memory can be deallocated before
262 // the upcall is made. Even if we're not about to make an upcall, a
263 // sub-object of the handler may be the true owner of the memory associated
264 // with the handler. Consequently, a local copy of the handler is required
265 // to ensure that any owning sub-object remains valid until after we have
266 // deallocated the memory here.
267 detail::move_binder2<Handler,
268 boost::system::error_code, peer_socket_type>
269 handler(0, BOOST_ASIO_MOVE_CAST(Handler)(o->handler_), ec,
270 BOOST_ASIO_MOVE_CAST(peer_socket_type)(o->peer_));
271 p.h = boost::asio::detail::addressof(handler.handler_);
274 // Make the upcall if required.
277 fenced_block b(fenced_block::half);
278 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
279 w.complete(handler, handler.handler_);
280 BOOST_ASIO_HANDLER_INVOCATION_END;
285 typedef typename Protocol::socket::template
286 rebind_executor<PeerIoExecutor>::other peer_socket_type;
288 win_iocp_socket_service_base& socket_service_;
290 socket_holder new_socket_;
291 peer_socket_type peer_;
293 typename Protocol::endpoint* peer_endpoint_;
294 unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
295 bool enable_connection_aborted_;
297 IoExecutor io_executor_;
300 #endif // defined(BOOST_ASIO_HAS_MOVE)
302 } // namespace detail
306 #include <boost/asio/detail/pop_options.hpp>
308 #endif // defined(BOOST_ASIO_HAS_IOCP)
310 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP