2 // detail/win_iocp_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_service.hpp>
25 #include <boost/asio/socket_base.hpp>
26 #include <boost/asio/detail/addressof.hpp>
27 #include <boost/asio/detail/bind_handler.hpp>
28 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
29 #include <boost/asio/detail/fenced_block.hpp>
30 #include <boost/asio/detail/handler_alloc_helpers.hpp>
31 #include <boost/asio/detail/handler_invoke_helpers.hpp>
32 #include <boost/asio/detail/mutex.hpp>
33 #include <boost/asio/detail/operation.hpp>
34 #include <boost/asio/detail/reactor.hpp>
35 #include <boost/asio/detail/reactor_op.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_service.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 : public win_iocp_socket_service_base
58 typedef Protocol protocol_type;
61 typedef typename Protocol::endpoint endpoint_type;
63 // The native type of a socket.
64 class native_handle_type
67 native_handle_type(socket_type s)
69 have_remote_endpoint_(false)
73 native_handle_type(socket_type s, const endpoint_type& ep)
75 have_remote_endpoint_(true),
80 void operator=(socket_type s)
83 have_remote_endpoint_ = false;
84 remote_endpoint_ = endpoint_type();
87 operator socket_type() const
92 bool have_remote_endpoint() const
94 return have_remote_endpoint_;
97 endpoint_type remote_endpoint() const
99 return remote_endpoint_;
104 bool have_remote_endpoint_;
105 endpoint_type remote_endpoint_;
108 // The implementation type of the socket.
109 struct implementation_type :
110 win_iocp_socket_service_base::base_implementation_type
112 // Default constructor.
113 implementation_type()
114 : protocol_(endpoint_type().protocol()),
115 have_remote_endpoint_(false),
120 // The protocol associated with the socket.
121 protocol_type protocol_;
123 // Whether we have a cached remote endpoint.
124 bool have_remote_endpoint_;
126 // A cached remote endpoint.
127 endpoint_type remote_endpoint_;
131 win_iocp_socket_service(boost::asio::io_service& io_service)
132 : win_iocp_socket_service_base(io_service)
136 // Move-construct a new socket implementation.
137 void move_construct(implementation_type& impl,
138 implementation_type& other_impl)
140 this->base_move_construct(impl, other_impl);
142 impl.protocol_ = other_impl.protocol_;
143 other_impl.protocol_ = endpoint_type().protocol();
145 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
146 other_impl.have_remote_endpoint_ = false;
148 impl.remote_endpoint_ = other_impl.remote_endpoint_;
149 other_impl.remote_endpoint_ = endpoint_type();
152 // Move-assign from another socket implementation.
153 void move_assign(implementation_type& impl,
154 win_iocp_socket_service_base& other_service,
155 implementation_type& other_impl)
157 this->base_move_assign(impl, other_service, other_impl);
159 impl.protocol_ = other_impl.protocol_;
160 other_impl.protocol_ = endpoint_type().protocol();
162 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
163 other_impl.have_remote_endpoint_ = false;
165 impl.remote_endpoint_ = other_impl.remote_endpoint_;
166 other_impl.remote_endpoint_ = endpoint_type();
169 // Move-construct a new socket implementation from another protocol type.
170 template <typename Protocol1>
171 void converting_move_construct(implementation_type& impl,
172 typename win_iocp_socket_service<
173 Protocol1>::implementation_type& other_impl)
175 this->base_move_construct(impl, other_impl);
177 impl.protocol_ = protocol_type(other_impl.protocol_);
178 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
180 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
181 other_impl.have_remote_endpoint_ = false;
183 impl.remote_endpoint_ = other_impl.remote_endpoint_;
184 other_impl.remote_endpoint_ = typename Protocol1::endpoint();
187 // Open a new socket implementation.
188 boost::system::error_code open(implementation_type& impl,
189 const protocol_type& protocol, boost::system::error_code& ec)
191 if (!do_open(impl, protocol.family(),
192 protocol.type(), protocol.protocol(), ec))
194 impl.protocol_ = protocol;
195 impl.have_remote_endpoint_ = false;
196 impl.remote_endpoint_ = endpoint_type();
201 // Assign a native socket to a socket implementation.
202 boost::system::error_code assign(implementation_type& impl,
203 const protocol_type& protocol, const native_handle_type& native_socket,
204 boost::system::error_code& ec)
206 if (!do_assign(impl, protocol.type(), native_socket, ec))
208 impl.protocol_ = protocol;
209 impl.have_remote_endpoint_ = native_socket.have_remote_endpoint();
210 impl.remote_endpoint_ = native_socket.remote_endpoint();
215 // Get the native socket representation.
216 native_handle_type native_handle(implementation_type& impl)
218 if (impl.have_remote_endpoint_)
219 return native_handle_type(impl.socket_, impl.remote_endpoint_);
220 return native_handle_type(impl.socket_);
223 // Bind the socket to the specified local endpoint.
224 boost::system::error_code bind(implementation_type& impl,
225 const endpoint_type& endpoint, boost::system::error_code& ec)
227 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
231 // Set a socket option.
232 template <typename Option>
233 boost::system::error_code set_option(implementation_type& impl,
234 const Option& option, boost::system::error_code& ec)
236 socket_ops::setsockopt(impl.socket_, impl.state_,
237 option.level(impl.protocol_), option.name(impl.protocol_),
238 option.data(impl.protocol_), option.size(impl.protocol_), ec);
242 // Set a socket option.
243 template <typename Option>
244 boost::system::error_code get_option(const implementation_type& impl,
245 Option& option, boost::system::error_code& ec) const
247 std::size_t size = option.size(impl.protocol_);
248 socket_ops::getsockopt(impl.socket_, impl.state_,
249 option.level(impl.protocol_), option.name(impl.protocol_),
250 option.data(impl.protocol_), &size, ec);
252 option.resize(impl.protocol_, size);
256 // Get the local endpoint.
257 endpoint_type local_endpoint(const implementation_type& impl,
258 boost::system::error_code& ec) const
260 endpoint_type endpoint;
261 std::size_t addr_len = endpoint.capacity();
262 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
263 return endpoint_type();
264 endpoint.resize(addr_len);
268 // Get the remote endpoint.
269 endpoint_type remote_endpoint(const implementation_type& impl,
270 boost::system::error_code& ec) const
272 endpoint_type endpoint = impl.remote_endpoint_;
273 std::size_t addr_len = endpoint.capacity();
274 if (socket_ops::getpeername(impl.socket_, endpoint.data(),
275 &addr_len, impl.have_remote_endpoint_, ec))
276 return endpoint_type();
277 endpoint.resize(addr_len);
281 // Send a datagram to the specified endpoint. Returns the number of bytes
283 template <typename ConstBufferSequence>
284 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
285 const endpoint_type& destination, socket_base::message_flags flags,
286 boost::system::error_code& ec)
288 buffer_sequence_adapter<boost::asio::const_buffer,
289 ConstBufferSequence> bufs(buffers);
291 return socket_ops::sync_sendto(impl.socket_, impl.state_,
292 bufs.buffers(), bufs.count(), flags,
293 destination.data(), destination.size(), ec);
296 // Wait until data can be sent without blocking.
297 size_t send_to(implementation_type& impl, const null_buffers&,
298 const endpoint_type&, socket_base::message_flags,
299 boost::system::error_code& ec)
301 // Wait for socket to become ready.
302 socket_ops::poll_write(impl.socket_, impl.state_, ec);
307 // Start an asynchronous send. The data being sent must be valid for the
308 // lifetime of the asynchronous operation.
309 template <typename ConstBufferSequence, typename Handler>
310 void async_send_to(implementation_type& impl,
311 const ConstBufferSequence& buffers, const endpoint_type& destination,
312 socket_base::message_flags flags, Handler& handler)
314 // Allocate and construct an operation to wrap the handler.
315 typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
316 typename op::ptr p = { boost::asio::detail::addressof(handler),
317 boost_asio_handler_alloc_helpers::allocate(
318 sizeof(op), handler), 0 };
319 p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
321 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send_to"));
323 buffer_sequence_adapter<boost::asio::const_buffer,
324 ConstBufferSequence> bufs(buffers);
326 start_send_to_op(impl, bufs.buffers(), bufs.count(),
327 destination.data(), static_cast<int>(destination.size()),
332 // Start an asynchronous wait until data can be sent without blocking.
333 template <typename Handler>
334 void async_send_to(implementation_type& impl, const null_buffers&,
335 const endpoint_type&, socket_base::message_flags, Handler& handler)
337 // Allocate and construct an operation to wrap the handler.
338 typedef win_iocp_null_buffers_op<Handler> op;
339 typename op::ptr p = { boost::asio::detail::addressof(handler),
340 boost_asio_handler_alloc_helpers::allocate(
341 sizeof(op), handler), 0 };
342 p.p = new (p.v) op(impl.cancel_token_, handler);
344 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
345 &impl, "async_send_to(null_buffers)"));
347 start_reactor_op(impl, reactor::write_op, p.p);
351 // Receive a datagram with the endpoint of the sender. Returns the number of
353 template <typename MutableBufferSequence>
354 size_t receive_from(implementation_type& impl,
355 const MutableBufferSequence& buffers,
356 endpoint_type& sender_endpoint, socket_base::message_flags flags,
357 boost::system::error_code& ec)
359 buffer_sequence_adapter<boost::asio::mutable_buffer,
360 MutableBufferSequence> bufs(buffers);
362 std::size_t addr_len = sender_endpoint.capacity();
363 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
364 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
365 flags, sender_endpoint.data(), &addr_len, ec);
368 sender_endpoint.resize(addr_len);
373 // Wait until data can be received without blocking.
374 size_t receive_from(implementation_type& impl,
375 const null_buffers&, endpoint_type& sender_endpoint,
376 socket_base::message_flags, boost::system::error_code& ec)
378 // Wait for socket to become ready.
379 socket_ops::poll_read(impl.socket_, impl.state_, ec);
381 // Reset endpoint since it can be given no sensible value at this time.
382 sender_endpoint = endpoint_type();
387 // Start an asynchronous receive. The buffer for the data being received and
388 // the sender_endpoint object must both be valid for the lifetime of the
389 // asynchronous operation.
390 template <typename MutableBufferSequence, typename Handler>
391 void async_receive_from(implementation_type& impl,
392 const MutableBufferSequence& buffers, endpoint_type& sender_endp,
393 socket_base::message_flags flags, Handler& handler)
395 // Allocate and construct an operation to wrap the handler.
396 typedef win_iocp_socket_recvfrom_op<
397 MutableBufferSequence, endpoint_type, Handler> op;
398 typename op::ptr p = { boost::asio::detail::addressof(handler),
399 boost_asio_handler_alloc_helpers::allocate(
400 sizeof(op), handler), 0 };
401 p.p = new (p.v) op(sender_endp, impl.cancel_token_, buffers, handler);
403 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive_from"));
405 buffer_sequence_adapter<boost::asio::mutable_buffer,
406 MutableBufferSequence> bufs(buffers);
408 start_receive_from_op(impl, bufs.buffers(), bufs.count(),
409 sender_endp.data(), flags, &p.p->endpoint_size(), p.p);
413 // Wait until data can be received without blocking.
414 template <typename Handler>
415 void async_receive_from(implementation_type& impl,
416 const null_buffers&, endpoint_type& sender_endpoint,
417 socket_base::message_flags flags, Handler& handler)
419 // Allocate and construct an operation to wrap the handler.
420 typedef win_iocp_null_buffers_op<Handler> op;
421 typename op::ptr p = { boost::asio::detail::addressof(handler),
422 boost_asio_handler_alloc_helpers::allocate(
423 sizeof(op), handler), 0 };
424 p.p = new (p.v) op(impl.cancel_token_, handler);
426 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl,
427 "async_receive_from(null_buffers)"));
429 // Reset endpoint since it can be given no sensible value at this time.
430 sender_endpoint = endpoint_type();
432 start_null_buffers_receive_op(impl, flags, p.p);
436 // Accept a new connection.
437 template <typename Socket>
438 boost::system::error_code accept(implementation_type& impl, Socket& peer,
439 endpoint_type* peer_endpoint, boost::system::error_code& ec)
441 // We cannot accept a socket that is already open.
444 ec = boost::asio::error::already_open;
448 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
449 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
450 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
451 peer_endpoint ? &addr_len : 0, ec));
453 // On success, assign new connection to peer socket object.
454 if (new_socket.get() != invalid_socket)
457 peer_endpoint->resize(addr_len);
458 if (!peer.assign(impl.protocol_, new_socket.get(), ec))
459 new_socket.release();
465 // Start an asynchronous accept. The peer and peer_endpoint objects
466 // must be valid until the accept's handler is invoked.
467 template <typename Socket, typename Handler>
468 void async_accept(implementation_type& impl, Socket& peer,
469 endpoint_type* peer_endpoint, Handler& handler)
471 // Allocate and construct an operation to wrap the handler.
472 typedef win_iocp_socket_accept_op<Socket, protocol_type, Handler> op;
473 typename op::ptr p = { boost::asio::detail::addressof(handler),
474 boost_asio_handler_alloc_helpers::allocate(
475 sizeof(op), handler), 0 };
476 bool enable_connection_aborted =
477 (impl.state_ & socket_ops::enable_connection_aborted) != 0;
478 p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_,
479 peer_endpoint, enable_connection_aborted, handler);
481 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_accept"));
483 start_accept_op(impl, peer.is_open(), p.p->new_socket(),
484 impl.protocol_.family(), impl.protocol_.type(),
485 impl.protocol_.protocol(), p.p->output_buffer(),
486 p.p->address_length(), p.p);
490 // Connect the socket to the specified endpoint.
491 boost::system::error_code connect(implementation_type& impl,
492 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
494 socket_ops::sync_connect(impl.socket_,
495 peer_endpoint.data(), peer_endpoint.size(), ec);
499 // Start an asynchronous connect.
500 template <typename Handler>
501 void async_connect(implementation_type& impl,
502 const endpoint_type& peer_endpoint, Handler& handler)
504 // Allocate and construct an operation to wrap the handler.
505 typedef win_iocp_socket_connect_op<Handler> op;
506 typename op::ptr p = { boost::asio::detail::addressof(handler),
507 boost_asio_handler_alloc_helpers::allocate(
508 sizeof(op), handler), 0 };
509 p.p = new (p.v) op(impl.socket_, handler);
511 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect"));
513 start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(),
514 peer_endpoint.data(), static_cast<int>(peer_endpoint.size()), p.p);
519 } // namespace detail
523 #include <boost/asio/detail/pop_options.hpp>
525 #endif // defined(BOOST_ASIO_HAS_IOCP)
527 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP