1 // 2 // detail/win_iocp_socket_service_base.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6 // 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) 9 // 10 11 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP 12 #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <boost/asio/detail/config.hpp> 19 20 #if defined(BOOST_ASIO_HAS_IOCP) 21 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> 45 46 #include <boost/asio/detail/push_options.hpp> 47 48 namespace boost { 49 namespace asio { 50 namespace detail { 51 52 class win_iocp_socket_service_base 53 { 54 public: 55 // The implementation type of the socket. 56 struct base_implementation_type 57 { 58 // The native socket representation. 59 socket_type socket_; 60 61 // The current state of the socket. 62 socket_ops::state_type state_; 63 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_; 71 72 // Per-descriptor data used by the reactor. 73 select_reactor::per_descriptor_data reactor_data_; 74 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) 82 83 // Pointers to adjacent socket implementations in linked list. 84 base_implementation_type* next_; 85 base_implementation_type* prev_; 86 }; 87 88 // Constructor. 89 BOOST_ASIO_DECL win_iocp_socket_service_base(execution_context& context); 90 91 // Destroy all user-defined handler objects owned by the service. 92 BOOST_ASIO_DECL void base_shutdown(); 93 94 // Construct a new socket implementation. 95 BOOST_ASIO_DECL void construct(base_implementation_type& impl); 96 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; 100 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); 105 106 // Destroy a socket implementation. 107 BOOST_ASIO_DECL void destroy(base_implementation_type& impl); 108 109 // Determine whether the socket is open. is_open(const base_implementation_type & impl) const110 bool is_open(const base_implementation_type& impl) const 111 { 112 return impl.socket_ != invalid_socket; 113 } 114 115 // Destroy a socket implementation. 116 BOOST_ASIO_DECL boost::system::error_code close( 117 base_implementation_type& impl, boost::system::error_code& ec); 118 119 // Release ownership of the socket. 120 BOOST_ASIO_DECL socket_type release( 121 base_implementation_type& impl, boost::system::error_code& ec); 122 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); 126 127 // Determine whether the socket is at the out-of-band data mark. at_mark(const base_implementation_type & impl,boost::system::error_code & ec) const128 bool at_mark(const base_implementation_type& impl, 129 boost::system::error_code& ec) const 130 { 131 return socket_ops::sockatmark(impl.socket_, ec); 132 } 133 134 // Determine the number of bytes available for reading. available(const base_implementation_type & impl,boost::system::error_code & ec) const135 std::size_t available(const base_implementation_type& impl, 136 boost::system::error_code& ec) const 137 { 138 return socket_ops::available(impl.socket_, ec); 139 } 140 141 // Place the socket into the state where it will listen for new connections. listen(base_implementation_type & impl,int backlog,boost::system::error_code & ec)142 boost::system::error_code listen(base_implementation_type& impl, 143 int backlog, boost::system::error_code& ec) 144 { 145 socket_ops::listen(impl.socket_, backlog, ec); 146 return ec; 147 } 148 149 // Perform an IO control command on the socket. 150 template <typename IO_Control_Command> io_control(base_implementation_type & impl,IO_Control_Command & command,boost::system::error_code & ec)151 boost::system::error_code io_control(base_implementation_type& impl, 152 IO_Control_Command& command, boost::system::error_code& ec) 153 { 154 socket_ops::ioctl(impl.socket_, impl.state_, command.name(), 155 static_cast<ioctl_arg_type*>(command.data()), ec); 156 return ec; 157 } 158 159 // Gets the non-blocking mode of the socket. non_blocking(const base_implementation_type & impl) const160 bool non_blocking(const base_implementation_type& impl) const 161 { 162 return (impl.state_ & socket_ops::user_set_non_blocking) != 0; 163 } 164 165 // Sets the non-blocking mode of the socket. non_blocking(base_implementation_type & impl,bool mode,boost::system::error_code & ec)166 boost::system::error_code non_blocking(base_implementation_type& impl, 167 bool mode, boost::system::error_code& ec) 168 { 169 socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec); 170 return ec; 171 } 172 173 // Gets the non-blocking mode of the native socket implementation. native_non_blocking(const base_implementation_type & impl) const174 bool native_non_blocking(const base_implementation_type& impl) const 175 { 176 return (impl.state_ & socket_ops::internal_non_blocking) != 0; 177 } 178 179 // Sets the non-blocking mode of the native socket implementation. native_non_blocking(base_implementation_type & impl,bool mode,boost::system::error_code & ec)180 boost::system::error_code native_non_blocking(base_implementation_type& impl, 181 bool mode, boost::system::error_code& ec) 182 { 183 socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec); 184 return ec; 185 } 186 187 // Wait for the socket to become ready to read, ready to write, or to have 188 // pending error conditions. wait(base_implementation_type & impl,socket_base::wait_type w,boost::system::error_code & ec)189 boost::system::error_code wait(base_implementation_type& impl, 190 socket_base::wait_type w, boost::system::error_code& ec) 191 { 192 switch (w) 193 { 194 case socket_base::wait_read: 195 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec); 196 break; 197 case socket_base::wait_write: 198 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec); 199 break; 200 case socket_base::wait_error: 201 socket_ops::poll_error(impl.socket_, impl.state_, -1, ec); 202 break; 203 default: 204 ec = boost::asio::error::invalid_argument; 205 break; 206 } 207 208 return ec; 209 } 210 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> async_wait(base_implementation_type & impl,socket_base::wait_type w,Handler & handler,const IoExecutor & io_ex)214 void async_wait(base_implementation_type& impl, 215 socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex) 216 { 217 bool is_continuation = 218 boost_asio_handler_cont_helpers::is_continuation(handler); 219 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); 225 226 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket", 227 &impl, impl.socket_, "async_wait")); 228 229 switch (w) 230 { 231 case socket_base::wait_read: 232 start_null_buffers_receive_op(impl, 0, p.p); 233 break; 234 case socket_base::wait_write: 235 start_reactor_op(impl, select_reactor::write_op, p.p); 236 break; 237 case socket_base::wait_error: 238 start_reactor_op(impl, select_reactor::except_op, p.p); 239 break; 240 default: 241 p.p->ec_ = boost::asio::error::invalid_argument; 242 iocp_service_.post_immediate_completion(p.p, is_continuation); 243 break; 244 } 245 246 p.v = p.p = 0; 247 } 248 249 // Send the given data to the peer. Returns the number of bytes sent. 250 template <typename ConstBufferSequence> send(base_implementation_type & impl,const ConstBufferSequence & buffers,socket_base::message_flags flags,boost::system::error_code & ec)251 size_t send(base_implementation_type& impl, 252 const ConstBufferSequence& buffers, 253 socket_base::message_flags flags, boost::system::error_code& ec) 254 { 255 buffer_sequence_adapter<boost::asio::const_buffer, 256 ConstBufferSequence> bufs(buffers); 257 258 return socket_ops::sync_send(impl.socket_, impl.state_, 259 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec); 260 } 261 262 // Wait until data can be sent without blocking. send(base_implementation_type & impl,const null_buffers &,socket_base::message_flags,boost::system::error_code & ec)263 size_t send(base_implementation_type& impl, const null_buffers&, 264 socket_base::message_flags, boost::system::error_code& ec) 265 { 266 // Wait for socket to become ready. 267 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec); 268 269 return 0; 270 } 271 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> async_send(base_implementation_type & impl,const ConstBufferSequence & buffers,socket_base::message_flags flags,Handler & handler,const IoExecutor & io_ex)275 void async_send(base_implementation_type& impl, 276 const ConstBufferSequence& buffers, socket_base::message_flags flags, 277 Handler& handler, const IoExecutor& io_ex) 278 { 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); 285 286 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket", 287 &impl, impl.socket_, "async_send")); 288 289 buffer_sequence_adapter<boost::asio::const_buffer, 290 ConstBufferSequence> bufs(buffers); 291 292 start_send_op(impl, bufs.buffers(), bufs.count(), flags, 293 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(), 294 p.p); 295 p.v = p.p = 0; 296 } 297 298 // Start an asynchronous wait until data can be sent without blocking. 299 template <typename Handler, typename IoExecutor> async_send(base_implementation_type & impl,const null_buffers &,socket_base::message_flags,Handler & handler,const IoExecutor & io_ex)300 void async_send(base_implementation_type& impl, const null_buffers&, 301 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) 302 { 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); 308 309 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket", 310 &impl, impl.socket_, "async_send(null_buffers)")); 311 312 start_reactor_op(impl, select_reactor::write_op, p.p); 313 p.v = p.p = 0; 314 } 315 316 // Receive some data from the peer. Returns the number of bytes received. 317 template <typename MutableBufferSequence> receive(base_implementation_type & impl,const MutableBufferSequence & buffers,socket_base::message_flags flags,boost::system::error_code & ec)318 size_t receive(base_implementation_type& impl, 319 const MutableBufferSequence& buffers, 320 socket_base::message_flags flags, boost::system::error_code& ec) 321 { 322 buffer_sequence_adapter<boost::asio::mutable_buffer, 323 MutableBufferSequence> bufs(buffers); 324 325 return socket_ops::sync_recv(impl.socket_, impl.state_, 326 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec); 327 } 328 329 // Wait until data can be received without blocking. receive(base_implementation_type & impl,const null_buffers &,socket_base::message_flags,boost::system::error_code & ec)330 size_t receive(base_implementation_type& impl, const null_buffers&, 331 socket_base::message_flags, boost::system::error_code& ec) 332 { 333 // Wait for socket to become ready. 334 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec); 335 336 return 0; 337 } 338 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> async_receive(base_implementation_type & impl,const MutableBufferSequence & buffers,socket_base::message_flags flags,Handler & handler,const IoExecutor & io_ex)343 void async_receive(base_implementation_type& impl, 344 const MutableBufferSequence& buffers, socket_base::message_flags flags, 345 Handler& handler, const IoExecutor& io_ex) 346 { 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); 354 355 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket", 356 &impl, impl.socket_, "async_receive")); 357 358 buffer_sequence_adapter<boost::asio::mutable_buffer, 359 MutableBufferSequence> bufs(buffers); 360 361 start_receive_op(impl, bufs.buffers(), bufs.count(), flags, 362 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(), 363 p.p); 364 p.v = p.p = 0; 365 } 366 367 // Wait until data can be received without blocking. 368 template <typename Handler, typename IoExecutor> async_receive(base_implementation_type & impl,const null_buffers &,socket_base::message_flags flags,Handler & handler,const IoExecutor & io_ex)369 void async_receive(base_implementation_type& impl, 370 const null_buffers&, socket_base::message_flags flags, 371 Handler& handler, const IoExecutor& io_ex) 372 { 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); 378 379 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket", 380 &impl, impl.socket_, "async_receive(null_buffers)")); 381 382 start_null_buffers_receive_op(impl, flags, p.p); 383 p.v = p.p = 0; 384 } 385 386 // Receive some data with associated flags. Returns the number of bytes 387 // received. 388 template <typename MutableBufferSequence> receive_with_flags(base_implementation_type & impl,const MutableBufferSequence & buffers,socket_base::message_flags in_flags,socket_base::message_flags & out_flags,boost::system::error_code & ec)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) 393 { 394 buffer_sequence_adapter<boost::asio::mutable_buffer, 395 MutableBufferSequence> bufs(buffers); 396 397 return socket_ops::sync_recvmsg(impl.socket_, impl.state_, 398 bufs.buffers(), bufs.count(), in_flags, out_flags, ec); 399 } 400 401 // Wait until data can be received without blocking. receive_with_flags(base_implementation_type & impl,const null_buffers &,socket_base::message_flags,socket_base::message_flags & out_flags,boost::system::error_code & ec)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) 405 { 406 // Wait for socket to become ready. 407 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec); 408 409 // Clear out_flags, since we cannot give it any other sensible value when 410 // performing a null_buffers operation. 411 out_flags = 0; 412 413 return 0; 414 } 415 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> async_receive_with_flags(base_implementation_type & impl,const MutableBufferSequence & buffers,socket_base::message_flags in_flags,socket_base::message_flags & out_flags,Handler & handler,const IoExecutor & io_ex)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) 424 { 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); 432 433 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket", 434 &impl, impl.socket_, "async_receive_with_flags")); 435 436 buffer_sequence_adapter<boost::asio::mutable_buffer, 437 MutableBufferSequence> bufs(buffers); 438 439 start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, p.p); 440 p.v = p.p = 0; 441 } 442 443 // Wait until data can be received without blocking. 444 template <typename Handler, typename IoExecutor> async_receive_with_flags(base_implementation_type & impl,const null_buffers &,socket_base::message_flags in_flags,socket_base::message_flags & out_flags,Handler & handler,const IoExecutor & io_ex)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) 449 { 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); 455 456 BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket", 457 &impl, impl.socket_, "async_receive_with_flags(null_buffers)")); 458 459 // Reset out_flags since it can be given no sensible value at this time. 460 out_flags = 0; 461 462 start_null_buffers_receive_op(impl, in_flags, p.p); 463 p.v = p.p = 0; 464 } 465 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); 470 471 protected: 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); 476 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); 481 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); 486 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); 492 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); 497 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); 502 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); 507 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); 512 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); 516 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); 521 522 // Helper function to close a socket when the associated object is being 523 // destroyed. 524 BOOST_ASIO_DECL void close_for_destruction(base_implementation_type& impl); 525 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); 529 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(); 534 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*); 538 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); 544 545 // The type of a NtSetInformationFile function pointer. 546 typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG); 547 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(); 553 554 // Helper function to emulate InterlockedCompareExchangePointer functionality 555 // for: 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); 560 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); 565 566 // The execution context used to obtain the reactor, if required. 567 execution_context& context_; 568 569 // The IOCP service used for running asynchronous operations and dispatching 570 // handlers. 571 win_iocp_io_context& iocp_service_; 572 573 // The reactor used for performing connect operations. This object is created 574 // only if needed. 575 select_reactor* reactor_; 576 577 // Pointer to ConnectEx implementation. 578 void* connect_ex_; 579 580 // Pointer to NtSetInformationFile implementation. 581 void* nt_set_info_; 582 583 // Mutex to protect access to the linked list of implementations. 584 boost::asio::detail::mutex mutex_; 585 586 // The head of a linked list of all implementations. 587 base_implementation_type* impl_list_; 588 }; 589 590 } // namespace detail 591 } // namespace asio 592 } // namespace boost 593 594 #include <boost/asio/detail/pop_options.hpp> 595 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) 599 600 #endif // defined(BOOST_ASIO_HAS_IOCP) 601 602 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP 603