2 // detail/win_iocp_socket_service_base.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_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/io_service.hpp>
24 #include <boost/asio/socket_base.hpp>
25 #include <boost/asio/detail/addressof.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/mutex.hpp>
32 #include <boost/asio/detail/operation.hpp>
33 #include <boost/asio/detail/reactor.hpp>
34 #include <boost/asio/detail/reactor_op.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_service.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>
45 #include <boost/asio/detail/push_options.hpp>
51 class win_iocp_socket_service_base
54 // The implementation type of the socket.
55 struct base_implementation_type
57 // The native socket representation.
60 // The current state of the socket.
61 socket_ops::state_type state_;
63 // We use a shared pointer as a cancellation token here to work around the
64 // broken Windows support for cancellation. MSDN says that when you call
65 // closesocket any outstanding WSARecv or WSASend operations will complete
66 // with the error ERROR_OPERATION_ABORTED. In practice they complete with
67 // ERROR_NETNAME_DELETED, which means you can't tell the difference between
68 // a local cancellation and the socket being hard-closed by the peer.
69 socket_ops::shared_cancel_token_type cancel_token_;
71 // Per-descriptor data used by the reactor.
72 reactor::per_descriptor_data reactor_data_;
74 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
75 // The ID of the thread from which it is safe to cancel asynchronous
76 // operations. 0 means no asynchronous operations have been started yet.
77 // ~0 means asynchronous operations have been started from more than one
78 // thread, and cancellation is not supported for the socket.
79 DWORD safe_cancellation_thread_id_;
80 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
82 // Pointers to adjacent socket implementations in linked list.
83 base_implementation_type* next_;
84 base_implementation_type* prev_;
88 BOOST_ASIO_DECL win_iocp_socket_service_base(
89 boost::asio::io_service& io_service);
91 // Destroy all user-defined handler objects owned by the service.
92 BOOST_ASIO_DECL void shutdown_service();
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);
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 // Cancel all operations associated with the socket.
120 BOOST_ASIO_DECL boost::system::error_code cancel(
121 base_implementation_type& impl, boost::system::error_code& ec);
123 // Determine whether the socket is at the out-of-band data mark.
124 bool at_mark(const base_implementation_type& impl,
125 boost::system::error_code& ec) const
127 return socket_ops::sockatmark(impl.socket_, ec);
130 // Determine the number of bytes available for reading.
131 std::size_t available(const base_implementation_type& impl,
132 boost::system::error_code& ec) const
134 return socket_ops::available(impl.socket_, ec);
137 // Place the socket into the state where it will listen for new connections.
138 boost::system::error_code listen(base_implementation_type& impl,
139 int backlog, boost::system::error_code& ec)
141 socket_ops::listen(impl.socket_, backlog, ec);
145 // Perform an IO control command on the socket.
146 template <typename IO_Control_Command>
147 boost::system::error_code io_control(base_implementation_type& impl,
148 IO_Control_Command& command, boost::system::error_code& ec)
150 socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
151 static_cast<ioctl_arg_type*>(command.data()), ec);
155 // Gets the non-blocking mode of the socket.
156 bool non_blocking(const base_implementation_type& impl) const
158 return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
161 // Sets the non-blocking mode of the socket.
162 boost::system::error_code non_blocking(base_implementation_type& impl,
163 bool mode, boost::system::error_code& ec)
165 socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
169 // Gets the non-blocking mode of the native socket implementation.
170 bool native_non_blocking(const base_implementation_type& impl) const
172 return (impl.state_ & socket_ops::internal_non_blocking) != 0;
175 // Sets the non-blocking mode of the native socket implementation.
176 boost::system::error_code native_non_blocking(base_implementation_type& impl,
177 bool mode, boost::system::error_code& ec)
179 socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
183 // Disable sends or receives on the socket.
184 boost::system::error_code shutdown(base_implementation_type& impl,
185 socket_base::shutdown_type what, boost::system::error_code& ec)
187 socket_ops::shutdown(impl.socket_, what, ec);
191 // Send the given data to the peer. Returns the number of bytes sent.
192 template <typename ConstBufferSequence>
193 size_t send(base_implementation_type& impl,
194 const ConstBufferSequence& buffers,
195 socket_base::message_flags flags, boost::system::error_code& ec)
197 buffer_sequence_adapter<boost::asio::const_buffer,
198 ConstBufferSequence> bufs(buffers);
200 return socket_ops::sync_send(impl.socket_, impl.state_,
201 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
204 // Wait until data can be sent without blocking.
205 size_t send(base_implementation_type& impl, const null_buffers&,
206 socket_base::message_flags, boost::system::error_code& ec)
208 // Wait for socket to become ready.
209 socket_ops::poll_write(impl.socket_, impl.state_, ec);
214 // Start an asynchronous send. The data being sent must be valid for the
215 // lifetime of the asynchronous operation.
216 template <typename ConstBufferSequence, typename Handler>
217 void async_send(base_implementation_type& impl,
218 const ConstBufferSequence& buffers,
219 socket_base::message_flags flags, Handler& handler)
221 // Allocate and construct an operation to wrap the handler.
222 typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
223 typename op::ptr p = { boost::asio::detail::addressof(handler),
224 boost_asio_handler_alloc_helpers::allocate(
225 sizeof(op), handler), 0 };
226 p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
228 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send"));
230 buffer_sequence_adapter<boost::asio::const_buffer,
231 ConstBufferSequence> bufs(buffers);
233 start_send_op(impl, bufs.buffers(), bufs.count(), flags,
234 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
239 // Start an asynchronous wait until data can be sent without blocking.
240 template <typename Handler>
241 void async_send(base_implementation_type& impl, const null_buffers&,
242 socket_base::message_flags, Handler& handler)
244 // Allocate and construct an operation to wrap the handler.
245 typedef win_iocp_null_buffers_op<Handler> op;
246 typename op::ptr p = { boost::asio::detail::addressof(handler),
247 boost_asio_handler_alloc_helpers::allocate(
248 sizeof(op), handler), 0 };
249 p.p = new (p.v) op(impl.cancel_token_, handler);
251 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
252 &impl, "async_send(null_buffers)"));
254 start_reactor_op(impl, reactor::write_op, p.p);
258 // Receive some data from the peer. Returns the number of bytes received.
259 template <typename MutableBufferSequence>
260 size_t receive(base_implementation_type& impl,
261 const MutableBufferSequence& buffers,
262 socket_base::message_flags flags, boost::system::error_code& ec)
264 buffer_sequence_adapter<boost::asio::mutable_buffer,
265 MutableBufferSequence> bufs(buffers);
267 return socket_ops::sync_recv(impl.socket_, impl.state_,
268 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
271 // Wait until data can be received without blocking.
272 size_t receive(base_implementation_type& impl, const null_buffers&,
273 socket_base::message_flags, boost::system::error_code& ec)
275 // Wait for socket to become ready.
276 socket_ops::poll_read(impl.socket_, impl.state_, ec);
281 // Start an asynchronous receive. The buffer for the data being received
282 // must be valid for the lifetime of the asynchronous operation.
283 template <typename MutableBufferSequence, typename Handler>
284 void async_receive(base_implementation_type& impl,
285 const MutableBufferSequence& buffers,
286 socket_base::message_flags flags, Handler& handler)
288 // Allocate and construct an operation to wrap the handler.
289 typedef win_iocp_socket_recv_op<MutableBufferSequence, Handler> op;
290 typename op::ptr p = { boost::asio::detail::addressof(handler),
291 boost_asio_handler_alloc_helpers::allocate(
292 sizeof(op), handler), 0 };
293 p.p = new (p.v) op(impl.state_, impl.cancel_token_, buffers, handler);
295 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive"));
297 buffer_sequence_adapter<boost::asio::mutable_buffer,
298 MutableBufferSequence> bufs(buffers);
300 start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
301 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
306 // Wait until data can be received without blocking.
307 template <typename Handler>
308 void async_receive(base_implementation_type& impl, const null_buffers&,
309 socket_base::message_flags flags, Handler& handler)
311 // Allocate and construct an operation to wrap the handler.
312 typedef win_iocp_null_buffers_op<Handler> op;
313 typename op::ptr p = { boost::asio::detail::addressof(handler),
314 boost_asio_handler_alloc_helpers::allocate(
315 sizeof(op), handler), 0 };
316 p.p = new (p.v) op(impl.cancel_token_, handler);
318 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
319 &impl, "async_receive(null_buffers)"));
321 start_null_buffers_receive_op(impl, flags, p.p);
325 // Receive some data with associated flags. Returns the number of bytes
327 template <typename MutableBufferSequence>
328 size_t receive_with_flags(base_implementation_type& impl,
329 const MutableBufferSequence& buffers,
330 socket_base::message_flags in_flags,
331 socket_base::message_flags& out_flags, boost::system::error_code& ec)
333 buffer_sequence_adapter<boost::asio::mutable_buffer,
334 MutableBufferSequence> bufs(buffers);
336 return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
337 bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
340 // Wait until data can be received without blocking.
341 size_t receive_with_flags(base_implementation_type& impl,
342 const null_buffers&, socket_base::message_flags,
343 socket_base::message_flags& out_flags, boost::system::error_code& ec)
345 // Wait for socket to become ready.
346 socket_ops::poll_read(impl.socket_, impl.state_, ec);
348 // Clear out_flags, since we cannot give it any other sensible value when
349 // performing a null_buffers operation.
355 // Start an asynchronous receive. The buffer for the data being received
356 // must be valid for the lifetime of the asynchronous operation.
357 template <typename MutableBufferSequence, typename Handler>
358 void async_receive_with_flags(base_implementation_type& impl,
359 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
360 socket_base::message_flags& out_flags, Handler& handler)
362 // Allocate and construct an operation to wrap the handler.
363 typedef win_iocp_socket_recvmsg_op<MutableBufferSequence, Handler> op;
364 typename op::ptr p = { boost::asio::detail::addressof(handler),
365 boost_asio_handler_alloc_helpers::allocate(
366 sizeof(op), handler), 0 };
367 p.p = new (p.v) op(impl.cancel_token_, buffers, out_flags, handler);
369 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
370 &impl, "async_receive_with_flags"));
372 buffer_sequence_adapter<boost::asio::mutable_buffer,
373 MutableBufferSequence> bufs(buffers);
375 start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, p.p);
379 // Wait until data can be received without blocking.
380 template <typename Handler>
381 void async_receive_with_flags(base_implementation_type& impl,
382 const null_buffers&, socket_base::message_flags in_flags,
383 socket_base::message_flags& out_flags, Handler& handler)
385 // Allocate and construct an operation to wrap the handler.
386 typedef win_iocp_null_buffers_op<Handler> op;
387 typename op::ptr p = { boost::asio::detail::addressof(handler),
388 boost_asio_handler_alloc_helpers::allocate(
389 sizeof(op), handler), 0 };
390 p.p = new (p.v) op(impl.cancel_token_, handler);
392 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl,
393 "async_receive_with_flags(null_buffers)"));
395 // Reset out_flags since it can be given no sensible value at this time.
398 start_null_buffers_receive_op(impl, in_flags, p.p);
402 // Helper function to restart an asynchronous accept operation.
403 BOOST_ASIO_DECL void restart_accept_op(socket_type s,
404 socket_holder& new_socket, int family, int type, int protocol,
405 void* output_buffer, DWORD address_length, operation* op);
408 // Open a new socket implementation.
409 BOOST_ASIO_DECL boost::system::error_code do_open(
410 base_implementation_type& impl, int family, int type,
411 int protocol, boost::system::error_code& ec);
413 // Assign a native socket to a socket implementation.
414 BOOST_ASIO_DECL boost::system::error_code do_assign(
415 base_implementation_type& impl, int type,
416 socket_type native_socket, boost::system::error_code& ec);
418 // Helper function to start an asynchronous send operation.
419 BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
420 WSABUF* buffers, std::size_t buffer_count,
421 socket_base::message_flags flags, bool noop, operation* op);
423 // Helper function to start an asynchronous send_to operation.
424 BOOST_ASIO_DECL void start_send_to_op(base_implementation_type& impl,
425 WSABUF* buffers, std::size_t buffer_count,
426 const socket_addr_type* addr, int addrlen,
427 socket_base::message_flags flags, operation* op);
429 // Helper function to start an asynchronous receive operation.
430 BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
431 WSABUF* buffers, std::size_t buffer_count,
432 socket_base::message_flags flags, bool noop, operation* op);
434 // Helper function to start an asynchronous null_buffers receive operation.
435 BOOST_ASIO_DECL void start_null_buffers_receive_op(
436 base_implementation_type& impl,
437 socket_base::message_flags flags, reactor_op* op);
439 // Helper function to start an asynchronous receive_from operation.
440 BOOST_ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
441 WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
442 socket_base::message_flags flags, int* addrlen, operation* op);
444 // Helper function to start an asynchronous accept operation.
445 BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
446 bool peer_is_open, socket_holder& new_socket, int family, int type,
447 int protocol, void* output_buffer, DWORD address_length, operation* op);
449 // Start an asynchronous read or write operation using the reactor.
450 BOOST_ASIO_DECL void start_reactor_op(base_implementation_type& impl,
451 int op_type, reactor_op* op);
453 // Start the asynchronous connect operation using the reactor.
454 BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
455 int family, int type, const socket_addr_type* remote_addr,
456 std::size_t remote_addrlen, win_iocp_socket_connect_op_base* op);
458 // Helper function to close a socket when the associated object is being
460 BOOST_ASIO_DECL void close_for_destruction(base_implementation_type& impl);
462 // Update the ID of the thread from which cancellation is safe.
463 BOOST_ASIO_DECL void update_cancellation_thread_id(
464 base_implementation_type& impl);
466 // Helper function to get the reactor. If no reactor has been created yet, a
467 // new one is obtained from the io_service and a pointer to it is cached in
469 BOOST_ASIO_DECL reactor& get_reactor();
471 // The type of a ConnectEx function pointer, as old SDKs may not provide it.
472 typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
473 const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
475 // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
476 // been obtained yet, one is obtained using WSAIoctl and the pointer is
477 // cached. Returns a null pointer if ConnectEx is not available.
478 BOOST_ASIO_DECL connect_ex_fn get_connect_ex(
479 base_implementation_type& impl, int type);
481 // Helper function to emulate InterlockedCompareExchangePointer functionality
483 // - very old Platform SDKs; and
484 // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
485 BOOST_ASIO_DECL void* interlocked_compare_exchange_pointer(
486 void** dest, void* exch, void* cmp);
488 // Helper function to emulate InterlockedExchangePointer functionality for:
489 // - very old Platform SDKs; and
490 // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
491 BOOST_ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
493 // The io_service used to obtain the reactor, if required.
494 boost::asio::io_service& io_service_;
496 // The IOCP service used for running asynchronous operations and dispatching
498 win_iocp_io_service& iocp_service_;
500 // The reactor used for performing connect operations. This object is created
504 // Pointer to ConnectEx implementation.
507 // Mutex to protect access to the linked list of implementations.
508 boost::asio::detail::mutex mutex_;
510 // The head of a linked list of all implementations.
511 base_implementation_type* impl_list_;
514 } // namespace detail
518 #include <boost/asio/detail/pop_options.hpp>
520 #if defined(BOOST_ASIO_HEADER_ONLY)
521 # include <boost/asio/detail/impl/win_iocp_socket_service_base.ipp>
522 #endif // defined(BOOST_ASIO_HEADER_ONLY)
524 #endif // defined(BOOST_ASIO_HAS_IOCP)
526 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP