2 // detail/win_iocp_socket_service_base.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_SERVICE_BASE_HPP
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_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/error.hpp>
23 #include <boost/asio/execution_context.hpp>
24 #include <boost/asio/socket_base.hpp>
25 #include <boost/asio/detail/bind_handler.hpp>
26 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
27 #include <boost/asio/detail/fenced_block.hpp>
28 #include <boost/asio/detail/handler_alloc_helpers.hpp>
29 #include <boost/asio/detail/handler_invoke_helpers.hpp>
30 #include <boost/asio/detail/memory.hpp>
31 #include <boost/asio/detail/mutex.hpp>
32 #include <boost/asio/detail/operation.hpp>
33 #include <boost/asio/detail/reactor_op.hpp>
34 #include <boost/asio/detail/select_reactor.hpp>
35 #include <boost/asio/detail/socket_holder.hpp>
36 #include <boost/asio/detail/socket_ops.hpp>
37 #include <boost/asio/detail/socket_types.hpp>
38 #include <boost/asio/detail/win_iocp_io_context.hpp>
39 #include <boost/asio/detail/win_iocp_null_buffers_op.hpp>
40 #include <boost/asio/detail/win_iocp_socket_connect_op.hpp>
41 #include <boost/asio/detail/win_iocp_socket_send_op.hpp>
42 #include <boost/asio/detail/win_iocp_socket_recv_op.hpp>
43 #include <boost/asio/detail/win_iocp_socket_recvmsg_op.hpp>
44 #include <boost/asio/detail/win_iocp_wait_op.hpp>
46 #include <boost/asio/detail/push_options.hpp>
52 class win_iocp_socket_service_base
55 // The implementation type of the socket.
56 struct base_implementation_type
58 // The native socket representation.
61 // The current state of the socket.
62 socket_ops::state_type state_;
64 // We use a shared pointer as a cancellation token here to work around the
65 // broken Windows support for cancellation. MSDN says that when you call
66 // closesocket any outstanding WSARecv or WSASend operations will complete
67 // with the error ERROR_OPERATION_ABORTED. In practice they complete with
68 // ERROR_NETNAME_DELETED, which means you can't tell the difference between
69 // a local cancellation and the socket being hard-closed by the peer.
70 socket_ops::shared_cancel_token_type cancel_token_;
72 // Per-descriptor data used by the reactor.
73 select_reactor::per_descriptor_data reactor_data_;
75 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
76 // The ID of the thread from which it is safe to cancel asynchronous
77 // operations. 0 means no asynchronous operations have been started yet.
78 // ~0 means asynchronous operations have been started from more than one
79 // thread, and cancellation is not supported for the socket.
80 DWORD safe_cancellation_thread_id_;
81 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
83 // Pointers to adjacent socket implementations in linked list.
84 base_implementation_type* next_;
85 base_implementation_type* prev_;
89 BOOST_ASIO_DECL win_iocp_socket_service_base(execution_context& context);
91 // Destroy all user-defined handler objects owned by the service.
92 BOOST_ASIO_DECL void base_shutdown();
94 // Construct a new socket implementation.
95 BOOST_ASIO_DECL void construct(base_implementation_type& impl);
97 // Move-construct a new socket implementation.
98 BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
99 base_implementation_type& other_impl) BOOST_ASIO_NOEXCEPT;
101 // Move-assign from another socket implementation.
102 BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
103 win_iocp_socket_service_base& other_service,
104 base_implementation_type& other_impl);
106 // Destroy a socket implementation.
107 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
109 // Determine whether the socket is open.
110 bool is_open(const base_implementation_type& impl) const
112 return impl.socket_ != invalid_socket;
115 // Destroy a socket implementation.
116 BOOST_ASIO_DECL boost::system::error_code close(
117 base_implementation_type& impl, boost::system::error_code& ec);
119 // Release ownership of the socket.
120 BOOST_ASIO_DECL socket_type release(
121 base_implementation_type& impl, boost::system::error_code& ec);
123 // Cancel all operations associated with the socket.
124 BOOST_ASIO_DECL boost::system::error_code cancel(
125 base_implementation_type& impl, boost::system::error_code& ec);
127 // Determine whether the socket is at the out-of-band data mark.
128 bool at_mark(const base_implementation_type& impl,
129 boost::system::error_code& ec) const
131 return socket_ops::sockatmark(impl.socket_, ec);
134 // Determine the number of bytes available for reading.
135 std::size_t available(const base_implementation_type& impl,
136 boost::system::error_code& ec) const
138 return socket_ops::available(impl.socket_, ec);
141 // Place the socket into the state where it will listen for new connections.
142 boost::system::error_code listen(base_implementation_type& impl,
143 int backlog, boost::system::error_code& ec)
145 socket_ops::listen(impl.socket_, backlog, ec);
149 // Perform an IO control command on the socket.
150 template <typename IO_Control_Command>
151 boost::system::error_code io_control(base_implementation_type& impl,
152 IO_Control_Command& command, boost::system::error_code& ec)
154 socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
155 static_cast<ioctl_arg_type*>(command.data()), ec);
159 // Gets the non-blocking mode of the socket.
160 bool non_blocking(const base_implementation_type& impl) const
162 return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
165 // Sets the non-blocking mode of the socket.
166 boost::system::error_code non_blocking(base_implementation_type& impl,
167 bool mode, boost::system::error_code& ec)
169 socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
173 // Gets the non-blocking mode of the native socket implementation.
174 bool native_non_blocking(const base_implementation_type& impl) const
176 return (impl.state_ & socket_ops::internal_non_blocking) != 0;
179 // Sets the non-blocking mode of the native socket implementation.
180 boost::system::error_code native_non_blocking(base_implementation_type& impl,
181 bool mode, boost::system::error_code& ec)
183 socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
187 // Wait for the socket to become ready to read, ready to write, or to have
188 // pending error conditions.
189 boost::system::error_code wait(base_implementation_type& impl,
190 socket_base::wait_type w, boost::system::error_code& ec)
194 case socket_base::wait_read:
195 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
197 case socket_base::wait_write:
198 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
200 case socket_base::wait_error:
201 socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
204 ec = boost::asio::error::invalid_argument;
211 // Asynchronously wait for the socket to become ready to read, ready to
212 // write, or to have pending error conditions.
213 template <typename Handler, typename IoExecutor>
214 void async_wait(base_implementation_type& impl,
215 socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex)
217 bool is_continuation =
218 boost_asio_handler_cont_helpers::is_continuation(handler);
220 // Allocate and construct an operation to wrap the handler.
221 typedef win_iocp_wait_op<Handler, IoExecutor> op;
222 typename op::ptr p = { boost::asio::detail::addressof(handler),
223 op::ptr::allocate(handler), 0 };
224 p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
226 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
227 &impl, impl.socket_, "async_wait"));
231 case socket_base::wait_read:
232 start_null_buffers_receive_op(impl, 0, p.p);
234 case socket_base::wait_write:
235 start_reactor_op(impl, select_reactor::write_op, p.p);
237 case socket_base::wait_error:
238 start_reactor_op(impl, select_reactor::except_op, p.p);
241 p.p->ec_ = boost::asio::error::invalid_argument;
242 iocp_service_.post_immediate_completion(p.p, is_continuation);
249 // Send the given data to the peer. Returns the number of bytes sent.
250 template <typename ConstBufferSequence>
251 size_t send(base_implementation_type& impl,
252 const ConstBufferSequence& buffers,
253 socket_base::message_flags flags, boost::system::error_code& ec)
255 buffer_sequence_adapter<boost::asio::const_buffer,
256 ConstBufferSequence> bufs(buffers);
258 return socket_ops::sync_send(impl.socket_, impl.state_,
259 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
262 // Wait until data can be sent without blocking.
263 size_t send(base_implementation_type& impl, const null_buffers&,
264 socket_base::message_flags, boost::system::error_code& ec)
266 // Wait for socket to become ready.
267 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
272 // Start an asynchronous send. The data being sent must be valid for the
273 // lifetime of the asynchronous operation.
274 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
275 void async_send(base_implementation_type& impl,
276 const ConstBufferSequence& buffers, socket_base::message_flags flags,
277 Handler& handler, const IoExecutor& io_ex)
279 // Allocate and construct an operation to wrap the handler.
280 typedef win_iocp_socket_send_op<
281 ConstBufferSequence, Handler, IoExecutor> op;
282 typename op::ptr p = { boost::asio::detail::addressof(handler),
283 op::ptr::allocate(handler), 0 };
284 p.p = new (p.v) op(impl.cancel_token_, buffers, handler, io_ex);
286 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
287 &impl, impl.socket_, "async_send"));
289 buffer_sequence_adapter<boost::asio::const_buffer,
290 ConstBufferSequence> bufs(buffers);
292 start_send_op(impl, bufs.buffers(), bufs.count(), flags,
293 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
298 // Start an asynchronous wait until data can be sent without blocking.
299 template <typename Handler, typename IoExecutor>
300 void async_send(base_implementation_type& impl, const null_buffers&,
301 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
303 // Allocate and construct an operation to wrap the handler.
304 typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
305 typename op::ptr p = { boost::asio::detail::addressof(handler),
306 op::ptr::allocate(handler), 0 };
307 p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
309 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
310 &impl, impl.socket_, "async_send(null_buffers)"));
312 start_reactor_op(impl, select_reactor::write_op, p.p);
316 // Receive some data from the peer. Returns the number of bytes received.
317 template <typename MutableBufferSequence>
318 size_t receive(base_implementation_type& impl,
319 const MutableBufferSequence& buffers,
320 socket_base::message_flags flags, boost::system::error_code& ec)
322 buffer_sequence_adapter<boost::asio::mutable_buffer,
323 MutableBufferSequence> bufs(buffers);
325 return socket_ops::sync_recv(impl.socket_, impl.state_,
326 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
329 // Wait until data can be received without blocking.
330 size_t receive(base_implementation_type& impl, const null_buffers&,
331 socket_base::message_flags, boost::system::error_code& ec)
333 // Wait for socket to become ready.
334 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
339 // Start an asynchronous receive. The buffer for the data being received
340 // must be valid for the lifetime of the asynchronous operation.
341 template <typename MutableBufferSequence,
342 typename Handler, typename IoExecutor>
343 void async_receive(base_implementation_type& impl,
344 const MutableBufferSequence& buffers, socket_base::message_flags flags,
345 Handler& handler, const IoExecutor& io_ex)
347 // Allocate and construct an operation to wrap the handler.
348 typedef win_iocp_socket_recv_op<
349 MutableBufferSequence, Handler, IoExecutor> op;
350 typename op::ptr p = { boost::asio::detail::addressof(handler),
351 op::ptr::allocate(handler), 0 };
352 p.p = new (p.v) op(impl.state_, impl.cancel_token_,
353 buffers, handler, io_ex);
355 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
356 &impl, impl.socket_, "async_receive"));
358 buffer_sequence_adapter<boost::asio::mutable_buffer,
359 MutableBufferSequence> bufs(buffers);
361 start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
362 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
367 // Wait until data can be received without blocking.
368 template <typename Handler, typename IoExecutor>
369 void async_receive(base_implementation_type& impl,
370 const null_buffers&, socket_base::message_flags flags,
371 Handler& handler, const IoExecutor& io_ex)
373 // Allocate and construct an operation to wrap the handler.
374 typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
375 typename op::ptr p = { boost::asio::detail::addressof(handler),
376 op::ptr::allocate(handler), 0 };
377 p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
379 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
380 &impl, impl.socket_, "async_receive(null_buffers)"));
382 start_null_buffers_receive_op(impl, flags, p.p);
386 // Receive some data with associated flags. Returns the number of bytes
388 template <typename MutableBufferSequence>
389 size_t receive_with_flags(base_implementation_type& impl,
390 const MutableBufferSequence& buffers,
391 socket_base::message_flags in_flags,
392 socket_base::message_flags& out_flags, boost::system::error_code& ec)
394 buffer_sequence_adapter<boost::asio::mutable_buffer,
395 MutableBufferSequence> bufs(buffers);
397 return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
398 bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
401 // Wait until data can be received without blocking.
402 size_t receive_with_flags(base_implementation_type& impl,
403 const null_buffers&, socket_base::message_flags,
404 socket_base::message_flags& out_flags, boost::system::error_code& ec)
406 // Wait for socket to become ready.
407 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
409 // Clear out_flags, since we cannot give it any other sensible value when
410 // performing a null_buffers operation.
416 // Start an asynchronous receive. The buffer for the data being received
417 // must be valid for the lifetime of the asynchronous operation.
418 template <typename MutableBufferSequence,
419 typename Handler, typename IoExecutor>
420 void async_receive_with_flags(base_implementation_type& impl,
421 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
422 socket_base::message_flags& out_flags, Handler& handler,
423 const IoExecutor& io_ex)
425 // Allocate and construct an operation to wrap the handler.
426 typedef win_iocp_socket_recvmsg_op<
427 MutableBufferSequence, Handler, IoExecutor> op;
428 typename op::ptr p = { boost::asio::detail::addressof(handler),
429 op::ptr::allocate(handler), 0 };
430 p.p = new (p.v) op(impl.cancel_token_,
431 buffers, out_flags, handler, io_ex);
433 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
434 &impl, impl.socket_, "async_receive_with_flags"));
436 buffer_sequence_adapter<boost::asio::mutable_buffer,
437 MutableBufferSequence> bufs(buffers);
439 start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, p.p);
443 // Wait until data can be received without blocking.
444 template <typename Handler, typename IoExecutor>
445 void async_receive_with_flags(base_implementation_type& impl,
446 const null_buffers&, socket_base::message_flags in_flags,
447 socket_base::message_flags& out_flags, Handler& handler,
448 const IoExecutor& io_ex)
450 // Allocate and construct an operation to wrap the handler.
451 typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
452 typename op::ptr p = { boost::asio::detail::addressof(handler),
453 op::ptr::allocate(handler), 0 };
454 p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
456 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
457 &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
459 // Reset out_flags since it can be given no sensible value at this time.
462 start_null_buffers_receive_op(impl, in_flags, p.p);
466 // Helper function to restart an asynchronous accept operation.
467 BOOST_ASIO_DECL void restart_accept_op(socket_type s,
468 socket_holder& new_socket, int family, int type, int protocol,
469 void* output_buffer, DWORD address_length, operation* op);
472 // Open a new socket implementation.
473 BOOST_ASIO_DECL boost::system::error_code do_open(
474 base_implementation_type& impl, int family, int type,
475 int protocol, boost::system::error_code& ec);
477 // Assign a native socket to a socket implementation.
478 BOOST_ASIO_DECL boost::system::error_code do_assign(
479 base_implementation_type& impl, int type,
480 socket_type native_socket, boost::system::error_code& ec);
482 // Helper function to start an asynchronous send operation.
483 BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
484 WSABUF* buffers, std::size_t buffer_count,
485 socket_base::message_flags flags, bool noop, operation* op);
487 // Helper function to start an asynchronous send_to operation.
488 BOOST_ASIO_DECL void start_send_to_op(base_implementation_type& impl,
489 WSABUF* buffers, std::size_t buffer_count,
490 const socket_addr_type* addr, int addrlen,
491 socket_base::message_flags flags, operation* op);
493 // Helper function to start an asynchronous receive operation.
494 BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
495 WSABUF* buffers, std::size_t buffer_count,
496 socket_base::message_flags flags, bool noop, operation* op);
498 // Helper function to start an asynchronous null_buffers receive operation.
499 BOOST_ASIO_DECL void start_null_buffers_receive_op(
500 base_implementation_type& impl,
501 socket_base::message_flags flags, reactor_op* op);
503 // Helper function to start an asynchronous receive_from operation.
504 BOOST_ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
505 WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
506 socket_base::message_flags flags, int* addrlen, operation* op);
508 // Helper function to start an asynchronous accept operation.
509 BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
510 bool peer_is_open, socket_holder& new_socket, int family, int type,
511 int protocol, void* output_buffer, DWORD address_length, operation* op);
513 // Start an asynchronous read or write operation using the reactor.
514 BOOST_ASIO_DECL void start_reactor_op(base_implementation_type& impl,
515 int op_type, reactor_op* op);
517 // Start the asynchronous connect operation using the reactor.
518 BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
519 int family, int type, const socket_addr_type* remote_addr,
520 std::size_t remote_addrlen, win_iocp_socket_connect_op_base* op);
522 // Helper function to close a socket when the associated object is being
524 BOOST_ASIO_DECL void close_for_destruction(base_implementation_type& impl);
526 // Update the ID of the thread from which cancellation is safe.
527 BOOST_ASIO_DECL void update_cancellation_thread_id(
528 base_implementation_type& impl);
530 // Helper function to get the reactor. If no reactor has been created yet, a
531 // new one is obtained from the execution context and a pointer to it is
532 // cached in this service.
533 BOOST_ASIO_DECL select_reactor& get_reactor();
535 // The type of a ConnectEx function pointer, as old SDKs may not provide it.
536 typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
537 const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
539 // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
540 // been obtained yet, one is obtained using WSAIoctl and the pointer is
541 // cached. Returns a null pointer if ConnectEx is not available.
542 BOOST_ASIO_DECL connect_ex_fn get_connect_ex(
543 base_implementation_type& impl, int type);
545 // The type of a NtSetInformationFile function pointer.
546 typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG);
548 // Helper function to get the NtSetInformationFile function pointer. If no
549 // NtSetInformationFile pointer has been obtained yet, one is obtained using
550 // GetProcAddress and the pointer is cached. Returns a null pointer if
551 // NtSetInformationFile is not available.
552 BOOST_ASIO_DECL nt_set_info_fn get_nt_set_info();
554 // Helper function to emulate InterlockedCompareExchangePointer functionality
556 // - very old Platform SDKs; and
557 // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
558 BOOST_ASIO_DECL void* interlocked_compare_exchange_pointer(
559 void** dest, void* exch, void* cmp);
561 // Helper function to emulate InterlockedExchangePointer functionality for:
562 // - very old Platform SDKs; and
563 // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
564 BOOST_ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
566 // The execution context used to obtain the reactor, if required.
567 execution_context& context_;
569 // The IOCP service used for running asynchronous operations and dispatching
571 win_iocp_io_context& iocp_service_;
573 // The reactor used for performing connect operations. This object is created
575 select_reactor* reactor_;
577 // Pointer to ConnectEx implementation.
580 // Pointer to NtSetInformationFile implementation.
583 // Mutex to protect access to the linked list of implementations.
584 boost::asio::detail::mutex mutex_;
586 // The head of a linked list of all implementations.
587 base_implementation_type* impl_list_;
590 } // namespace detail
594 #include <boost/asio/detail/pop_options.hpp>
596 #if defined(BOOST_ASIO_HEADER_ONLY)
597 # include <boost/asio/detail/impl/win_iocp_socket_service_base.ipp>
598 #endif // defined(BOOST_ASIO_HEADER_ONLY)
600 #endif // defined(BOOST_ASIO_HAS_IOCP)
602 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP