2 // detail/win_iocp_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_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)
23 #include <boost/asio/error.hpp>
24 #include <boost/asio/io_context.hpp>
25 #include <boost/asio/socket_base.hpp>
26 #include <boost/asio/detail/bind_handler.hpp>
27 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
28 #include <boost/asio/detail/fenced_block.hpp>
29 #include <boost/asio/detail/handler_alloc_helpers.hpp>
30 #include <boost/asio/detail/handler_invoke_helpers.hpp>
31 #include <boost/asio/detail/memory.hpp>
32 #include <boost/asio/detail/mutex.hpp>
33 #include <boost/asio/detail/operation.hpp>
34 #include <boost/asio/detail/reactor_op.hpp>
35 #include <boost/asio/detail/select_reactor.hpp>
36 #include <boost/asio/detail/socket_holder.hpp>
37 #include <boost/asio/detail/socket_ops.hpp>
38 #include <boost/asio/detail/socket_types.hpp>
39 #include <boost/asio/detail/win_iocp_io_context.hpp>
40 #include <boost/asio/detail/win_iocp_null_buffers_op.hpp>
41 #include <boost/asio/detail/win_iocp_socket_accept_op.hpp>
42 #include <boost/asio/detail/win_iocp_socket_connect_op.hpp>
43 #include <boost/asio/detail/win_iocp_socket_recvfrom_op.hpp>
44 #include <boost/asio/detail/win_iocp_socket_send_op.hpp>
45 #include <boost/asio/detail/win_iocp_socket_service_base.hpp>
47 #include <boost/asio/detail/push_options.hpp>
53 template <typename Protocol>
54 class win_iocp_socket_service :
55 public service_base<win_iocp_socket_service<Protocol> >,
56 public win_iocp_socket_service_base
60 typedef Protocol protocol_type;
63 typedef typename Protocol::endpoint endpoint_type;
65 // The native type of a socket.
66 class native_handle_type
69 native_handle_type(socket_type s)
71 have_remote_endpoint_(false)
75 native_handle_type(socket_type s, const endpoint_type& ep)
77 have_remote_endpoint_(true),
82 void operator=(socket_type s)
85 have_remote_endpoint_ = false;
86 remote_endpoint_ = endpoint_type();
89 operator socket_type() const
94 bool have_remote_endpoint() const
96 return have_remote_endpoint_;
99 endpoint_type remote_endpoint() const
101 return remote_endpoint_;
106 bool have_remote_endpoint_;
107 endpoint_type remote_endpoint_;
110 // The implementation type of the socket.
111 struct implementation_type :
112 win_iocp_socket_service_base::base_implementation_type
114 // Default constructor.
115 implementation_type()
116 : protocol_(endpoint_type().protocol()),
117 have_remote_endpoint_(false),
122 // The protocol associated with the socket.
123 protocol_type protocol_;
125 // Whether we have a cached remote endpoint.
126 bool have_remote_endpoint_;
128 // A cached remote endpoint.
129 endpoint_type remote_endpoint_;
133 win_iocp_socket_service(boost::asio::io_context& io_context)
134 : service_base<win_iocp_socket_service<Protocol> >(io_context),
135 win_iocp_socket_service_base(io_context)
139 // Destroy all user-defined handler objects owned by the service.
142 this->base_shutdown();
145 // Move-construct a new socket implementation.
146 void move_construct(implementation_type& impl,
147 implementation_type& other_impl)
149 this->base_move_construct(impl, other_impl);
151 impl.protocol_ = other_impl.protocol_;
152 other_impl.protocol_ = endpoint_type().protocol();
154 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
155 other_impl.have_remote_endpoint_ = false;
157 impl.remote_endpoint_ = other_impl.remote_endpoint_;
158 other_impl.remote_endpoint_ = endpoint_type();
161 // Move-assign from another socket implementation.
162 void move_assign(implementation_type& impl,
163 win_iocp_socket_service_base& other_service,
164 implementation_type& other_impl)
166 this->base_move_assign(impl, other_service, other_impl);
168 impl.protocol_ = other_impl.protocol_;
169 other_impl.protocol_ = endpoint_type().protocol();
171 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
172 other_impl.have_remote_endpoint_ = false;
174 impl.remote_endpoint_ = other_impl.remote_endpoint_;
175 other_impl.remote_endpoint_ = endpoint_type();
178 // Move-construct a new socket implementation from another protocol type.
179 template <typename Protocol1>
180 void converting_move_construct(implementation_type& impl,
181 win_iocp_socket_service<Protocol1>&,
182 typename win_iocp_socket_service<
183 Protocol1>::implementation_type& other_impl)
185 this->base_move_construct(impl, other_impl);
187 impl.protocol_ = protocol_type(other_impl.protocol_);
188 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
190 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
191 other_impl.have_remote_endpoint_ = false;
193 impl.remote_endpoint_ = other_impl.remote_endpoint_;
194 other_impl.remote_endpoint_ = typename Protocol1::endpoint();
197 // Open a new socket implementation.
198 boost::system::error_code open(implementation_type& impl,
199 const protocol_type& protocol, boost::system::error_code& ec)
201 if (!do_open(impl, protocol.family(),
202 protocol.type(), protocol.protocol(), ec))
204 impl.protocol_ = protocol;
205 impl.have_remote_endpoint_ = false;
206 impl.remote_endpoint_ = endpoint_type();
211 // Assign a native socket to a socket implementation.
212 boost::system::error_code assign(implementation_type& impl,
213 const protocol_type& protocol, const native_handle_type& native_socket,
214 boost::system::error_code& ec)
216 if (!do_assign(impl, protocol.type(), native_socket, ec))
218 impl.protocol_ = protocol;
219 impl.have_remote_endpoint_ = native_socket.have_remote_endpoint();
220 impl.remote_endpoint_ = native_socket.remote_endpoint();
225 // Get the native socket representation.
226 native_handle_type native_handle(implementation_type& impl)
228 if (impl.have_remote_endpoint_)
229 return native_handle_type(impl.socket_, impl.remote_endpoint_);
230 return native_handle_type(impl.socket_);
233 // Bind the socket to the specified local endpoint.
234 boost::system::error_code bind(implementation_type& impl,
235 const endpoint_type& endpoint, boost::system::error_code& ec)
237 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
241 // Set a socket option.
242 template <typename Option>
243 boost::system::error_code set_option(implementation_type& impl,
244 const Option& option, boost::system::error_code& ec)
246 socket_ops::setsockopt(impl.socket_, impl.state_,
247 option.level(impl.protocol_), option.name(impl.protocol_),
248 option.data(impl.protocol_), option.size(impl.protocol_), ec);
252 // Set a socket option.
253 template <typename Option>
254 boost::system::error_code get_option(const implementation_type& impl,
255 Option& option, boost::system::error_code& ec) const
257 std::size_t size = option.size(impl.protocol_);
258 socket_ops::getsockopt(impl.socket_, impl.state_,
259 option.level(impl.protocol_), option.name(impl.protocol_),
260 option.data(impl.protocol_), &size, ec);
262 option.resize(impl.protocol_, size);
266 // Get the local endpoint.
267 endpoint_type local_endpoint(const implementation_type& impl,
268 boost::system::error_code& ec) const
270 endpoint_type endpoint;
271 std::size_t addr_len = endpoint.capacity();
272 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
273 return endpoint_type();
274 endpoint.resize(addr_len);
278 // Get the remote endpoint.
279 endpoint_type remote_endpoint(const implementation_type& impl,
280 boost::system::error_code& ec) const
282 endpoint_type endpoint = impl.remote_endpoint_;
283 std::size_t addr_len = endpoint.capacity();
284 if (socket_ops::getpeername(impl.socket_, endpoint.data(),
285 &addr_len, impl.have_remote_endpoint_, ec))
286 return endpoint_type();
287 endpoint.resize(addr_len);
291 // Disable sends or receives on the socket.
292 boost::system::error_code shutdown(base_implementation_type& impl,
293 socket_base::shutdown_type what, boost::system::error_code& ec)
295 socket_ops::shutdown(impl.socket_, what, ec);
299 // Send a datagram to the specified endpoint. Returns the number of bytes
301 template <typename ConstBufferSequence>
302 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
303 const endpoint_type& destination, socket_base::message_flags flags,
304 boost::system::error_code& ec)
306 buffer_sequence_adapter<boost::asio::const_buffer,
307 ConstBufferSequence> bufs(buffers);
309 return socket_ops::sync_sendto(impl.socket_, impl.state_,
310 bufs.buffers(), bufs.count(), flags,
311 destination.data(), destination.size(), ec);
314 // Wait until data can be sent without blocking.
315 size_t send_to(implementation_type& impl, const null_buffers&,
316 const endpoint_type&, socket_base::message_flags,
317 boost::system::error_code& ec)
319 // Wait for socket to become ready.
320 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
325 // Start an asynchronous send. The data being sent must be valid for the
326 // lifetime of the asynchronous operation.
327 template <typename ConstBufferSequence, typename Handler>
328 void async_send_to(implementation_type& impl,
329 const ConstBufferSequence& buffers, const endpoint_type& destination,
330 socket_base::message_flags flags, Handler& handler)
332 // Allocate and construct an operation to wrap the handler.
333 typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
334 typename op::ptr p = { boost::asio::detail::addressof(handler),
335 op::ptr::allocate(handler), 0 };
336 p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
338 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
339 &impl, impl.socket_, "async_send_to"));
341 buffer_sequence_adapter<boost::asio::const_buffer,
342 ConstBufferSequence> bufs(buffers);
344 start_send_to_op(impl, bufs.buffers(), bufs.count(),
345 destination.data(), static_cast<int>(destination.size()),
350 // Start an asynchronous wait until data can be sent without blocking.
351 template <typename Handler>
352 void async_send_to(implementation_type& impl, const null_buffers&,
353 const endpoint_type&, socket_base::message_flags, Handler& handler)
355 // Allocate and construct an operation to wrap the handler.
356 typedef win_iocp_null_buffers_op<Handler> op;
357 typename op::ptr p = { boost::asio::detail::addressof(handler),
358 op::ptr::allocate(handler), 0 };
359 p.p = new (p.v) op(impl.cancel_token_, handler);
361 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
362 &impl, impl.socket_, "async_send_to(null_buffers)"));
364 start_reactor_op(impl, select_reactor::write_op, p.p);
368 // Receive a datagram with the endpoint of the sender. Returns the number of
370 template <typename MutableBufferSequence>
371 size_t receive_from(implementation_type& impl,
372 const MutableBufferSequence& buffers,
373 endpoint_type& sender_endpoint, socket_base::message_flags flags,
374 boost::system::error_code& ec)
376 buffer_sequence_adapter<boost::asio::mutable_buffer,
377 MutableBufferSequence> bufs(buffers);
379 std::size_t addr_len = sender_endpoint.capacity();
380 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
381 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
382 flags, sender_endpoint.data(), &addr_len, ec);
385 sender_endpoint.resize(addr_len);
390 // Wait until data can be received without blocking.
391 size_t receive_from(implementation_type& impl,
392 const null_buffers&, endpoint_type& sender_endpoint,
393 socket_base::message_flags, boost::system::error_code& ec)
395 // Wait for socket to become ready.
396 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
398 // Reset endpoint since it can be given no sensible value at this time.
399 sender_endpoint = endpoint_type();
404 // Start an asynchronous receive. The buffer for the data being received and
405 // the sender_endpoint object must both be valid for the lifetime of the
406 // asynchronous operation.
407 template <typename MutableBufferSequence, typename Handler>
408 void async_receive_from(implementation_type& impl,
409 const MutableBufferSequence& buffers, endpoint_type& sender_endp,
410 socket_base::message_flags flags, Handler& handler)
412 // Allocate and construct an operation to wrap the handler.
413 typedef win_iocp_socket_recvfrom_op<
414 MutableBufferSequence, endpoint_type, Handler> op;
415 typename op::ptr p = { boost::asio::detail::addressof(handler),
416 op::ptr::allocate(handler), 0 };
417 p.p = new (p.v) op(sender_endp, impl.cancel_token_, buffers, handler);
419 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
420 &impl, impl.socket_, "async_receive_from"));
422 buffer_sequence_adapter<boost::asio::mutable_buffer,
423 MutableBufferSequence> bufs(buffers);
425 start_receive_from_op(impl, bufs.buffers(), bufs.count(),
426 sender_endp.data(), flags, &p.p->endpoint_size(), p.p);
430 // Wait until data can be received without blocking.
431 template <typename Handler>
432 void async_receive_from(implementation_type& impl,
433 const null_buffers&, endpoint_type& sender_endpoint,
434 socket_base::message_flags flags, Handler& handler)
436 // Allocate and construct an operation to wrap the handler.
437 typedef win_iocp_null_buffers_op<Handler> op;
438 typename op::ptr p = { boost::asio::detail::addressof(handler),
439 op::ptr::allocate(handler), 0 };
440 p.p = new (p.v) op(impl.cancel_token_, handler);
442 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
443 &impl, impl.socket_, "async_receive_from(null_buffers)"));
445 // Reset endpoint since it can be given no sensible value at this time.
446 sender_endpoint = endpoint_type();
448 start_null_buffers_receive_op(impl, flags, p.p);
452 // Accept a new connection.
453 template <typename Socket>
454 boost::system::error_code accept(implementation_type& impl, Socket& peer,
455 endpoint_type* peer_endpoint, boost::system::error_code& ec)
457 // We cannot accept a socket that is already open.
460 ec = boost::asio::error::already_open;
464 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
465 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
466 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
467 peer_endpoint ? &addr_len : 0, ec));
469 // On success, assign new connection to peer socket object.
470 if (new_socket.get() != invalid_socket)
473 peer_endpoint->resize(addr_len);
474 peer.assign(impl.protocol_, new_socket.get(), ec);
476 new_socket.release();
482 #if defined(BOOST_ASIO_HAS_MOVE)
483 // Accept a new connection.
484 typename Protocol::socket accept(implementation_type& impl,
485 io_context* peer_io_context, endpoint_type* peer_endpoint,
486 boost::system::error_code& ec)
488 typename Protocol::socket peer(
489 peer_io_context ? *peer_io_context : io_context_);
491 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
492 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
493 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
494 peer_endpoint ? &addr_len : 0, ec));
496 // On success, assign new connection to peer socket object.
497 if (new_socket.get() != invalid_socket)
500 peer_endpoint->resize(addr_len);
501 peer.assign(impl.protocol_, new_socket.get(), ec);
503 new_socket.release();
508 #endif // defined(BOOST_ASIO_HAS_MOVE)
510 // Start an asynchronous accept. The peer and peer_endpoint objects
511 // must be valid until the accept's handler is invoked.
512 template <typename Socket, typename Handler>
513 void async_accept(implementation_type& impl, Socket& peer,
514 endpoint_type* peer_endpoint, Handler& handler)
516 // Allocate and construct an operation to wrap the handler.
517 typedef win_iocp_socket_accept_op<Socket, protocol_type, Handler> op;
518 typename op::ptr p = { boost::asio::detail::addressof(handler),
519 op::ptr::allocate(handler), 0 };
520 bool enable_connection_aborted =
521 (impl.state_ & socket_ops::enable_connection_aborted) != 0;
522 p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_,
523 peer_endpoint, enable_connection_aborted, handler);
525 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
526 &impl, impl.socket_, "async_accept"));
528 start_accept_op(impl, peer.is_open(), p.p->new_socket(),
529 impl.protocol_.family(), impl.protocol_.type(),
530 impl.protocol_.protocol(), p.p->output_buffer(),
531 p.p->address_length(), p.p);
535 #if defined(BOOST_ASIO_HAS_MOVE)
536 // Start an asynchronous accept. The peer and peer_endpoint objects
537 // must be valid until the accept's handler is invoked.
538 template <typename Handler>
539 void async_accept(implementation_type& impl,
540 boost::asio::io_context* peer_io_context,
541 endpoint_type* peer_endpoint, Handler& handler)
543 // Allocate and construct an operation to wrap the handler.
544 typedef win_iocp_socket_move_accept_op<protocol_type, Handler> op;
545 typename op::ptr p = { boost::asio::detail::addressof(handler),
546 op::ptr::allocate(handler), 0 };
547 bool enable_connection_aborted =
548 (impl.state_ & socket_ops::enable_connection_aborted) != 0;
549 p.p = new (p.v) op(*this, impl.socket_, impl.protocol_,
550 peer_io_context ? *peer_io_context : io_context_,
551 peer_endpoint, enable_connection_aborted, handler);
553 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
554 &impl, impl.socket_, "async_accept"));
556 start_accept_op(impl, false, p.p->new_socket(),
557 impl.protocol_.family(), impl.protocol_.type(),
558 impl.protocol_.protocol(), p.p->output_buffer(),
559 p.p->address_length(), p.p);
562 #endif // defined(BOOST_ASIO_HAS_MOVE)
564 // Connect the socket to the specified endpoint.
565 boost::system::error_code connect(implementation_type& impl,
566 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
568 socket_ops::sync_connect(impl.socket_,
569 peer_endpoint.data(), peer_endpoint.size(), ec);
573 // Start an asynchronous connect.
574 template <typename Handler>
575 void async_connect(implementation_type& impl,
576 const endpoint_type& peer_endpoint, Handler& handler)
578 // Allocate and construct an operation to wrap the handler.
579 typedef win_iocp_socket_connect_op<Handler> op;
580 typename op::ptr p = { boost::asio::detail::addressof(handler),
581 op::ptr::allocate(handler), 0 };
582 p.p = new (p.v) op(impl.socket_, handler);
584 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
585 &impl, impl.socket_, "async_connect"));
587 start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(),
588 peer_endpoint.data(), static_cast<int>(peer_endpoint.size()), p.p);
593 } // namespace detail
597 #include <boost/asio/detail/pop_options.hpp>
599 #endif // defined(BOOST_ASIO_HAS_IOCP)
601 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP