win_iocp_socket_service_base.ipp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. //
  2. // detail/impl/win_iocp_socket_service_base.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_IOCP)
  17. #include <boost/asio/detail/win_iocp_socket_service_base.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. win_iocp_socket_service_base::win_iocp_socket_service_base(
  23. execution_context& context)
  24. : context_(context),
  25. iocp_service_(use_service<win_iocp_io_context>(context)),
  26. reactor_(0),
  27. connect_ex_(0),
  28. nt_set_info_(0),
  29. mutex_(),
  30. impl_list_(0)
  31. {
  32. }
  33. void win_iocp_socket_service_base::base_shutdown()
  34. {
  35. // Close all implementations, causing all operations to complete.
  36. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  37. base_implementation_type* impl = impl_list_;
  38. while (impl)
  39. {
  40. close_for_destruction(*impl);
  41. impl = impl->next_;
  42. }
  43. }
  44. void win_iocp_socket_service_base::construct(
  45. win_iocp_socket_service_base::base_implementation_type& impl)
  46. {
  47. impl.socket_ = invalid_socket;
  48. impl.state_ = 0;
  49. impl.cancel_token_.reset();
  50. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  51. impl.safe_cancellation_thread_id_ = 0;
  52. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  53. // Insert implementation into linked list of all implementations.
  54. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  55. impl.next_ = impl_list_;
  56. impl.prev_ = 0;
  57. if (impl_list_)
  58. impl_list_->prev_ = &impl;
  59. impl_list_ = &impl;
  60. }
  61. void win_iocp_socket_service_base::base_move_construct(
  62. win_iocp_socket_service_base::base_implementation_type& impl,
  63. win_iocp_socket_service_base::base_implementation_type& other_impl)
  64. noexcept
  65. {
  66. impl.socket_ = other_impl.socket_;
  67. other_impl.socket_ = invalid_socket;
  68. impl.state_ = other_impl.state_;
  69. other_impl.state_ = 0;
  70. impl.cancel_token_ = other_impl.cancel_token_;
  71. other_impl.cancel_token_.reset();
  72. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  73. impl.safe_cancellation_thread_id_ = other_impl.safe_cancellation_thread_id_;
  74. other_impl.safe_cancellation_thread_id_ = 0;
  75. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  76. // Insert implementation into linked list of all implementations.
  77. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  78. impl.next_ = impl_list_;
  79. impl.prev_ = 0;
  80. if (impl_list_)
  81. impl_list_->prev_ = &impl;
  82. impl_list_ = &impl;
  83. }
  84. void win_iocp_socket_service_base::base_move_assign(
  85. win_iocp_socket_service_base::base_implementation_type& impl,
  86. win_iocp_socket_service_base& other_service,
  87. win_iocp_socket_service_base::base_implementation_type& other_impl)
  88. {
  89. close_for_destruction(impl);
  90. if (this != &other_service)
  91. {
  92. // Remove implementation from linked list of all implementations.
  93. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  94. if (impl_list_ == &impl)
  95. impl_list_ = impl.next_;
  96. if (impl.prev_)
  97. impl.prev_->next_ = impl.next_;
  98. if (impl.next_)
  99. impl.next_->prev_= impl.prev_;
  100. impl.next_ = 0;
  101. impl.prev_ = 0;
  102. }
  103. impl.socket_ = other_impl.socket_;
  104. other_impl.socket_ = invalid_socket;
  105. impl.state_ = other_impl.state_;
  106. other_impl.state_ = 0;
  107. impl.cancel_token_ = other_impl.cancel_token_;
  108. other_impl.cancel_token_.reset();
  109. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  110. impl.safe_cancellation_thread_id_ = other_impl.safe_cancellation_thread_id_;
  111. other_impl.safe_cancellation_thread_id_ = 0;
  112. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  113. if (this != &other_service)
  114. {
  115. // Insert implementation into linked list of all implementations.
  116. boost::asio::detail::mutex::scoped_lock lock(other_service.mutex_);
  117. impl.next_ = other_service.impl_list_;
  118. impl.prev_ = 0;
  119. if (other_service.impl_list_)
  120. other_service.impl_list_->prev_ = &impl;
  121. other_service.impl_list_ = &impl;
  122. }
  123. }
  124. void win_iocp_socket_service_base::destroy(
  125. win_iocp_socket_service_base::base_implementation_type& impl)
  126. {
  127. close_for_destruction(impl);
  128. // Remove implementation from linked list of all implementations.
  129. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  130. if (impl_list_ == &impl)
  131. impl_list_ = impl.next_;
  132. if (impl.prev_)
  133. impl.prev_->next_ = impl.next_;
  134. if (impl.next_)
  135. impl.next_->prev_= impl.prev_;
  136. impl.next_ = 0;
  137. impl.prev_ = 0;
  138. }
  139. boost::system::error_code win_iocp_socket_service_base::close(
  140. win_iocp_socket_service_base::base_implementation_type& impl,
  141. boost::system::error_code& ec)
  142. {
  143. if (is_open(impl))
  144. {
  145. BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
  146. "socket", &impl, impl.socket_, "close"));
  147. // Check if the reactor was created, in which case we need to close the
  148. // socket on the reactor as well to cancel any operations that might be
  149. // running there.
  150. select_reactor* r = static_cast<select_reactor*>(
  151. interlocked_compare_exchange_pointer(
  152. reinterpret_cast<void**>(&reactor_), 0, 0));
  153. if (r)
  154. r->deregister_descriptor(impl.socket_, impl.reactor_data_, true);
  155. socket_ops::close(impl.socket_, impl.state_, false, ec);
  156. if (r)
  157. r->cleanup_descriptor_data(impl.reactor_data_);
  158. }
  159. else
  160. {
  161. ec = boost::system::error_code();
  162. }
  163. impl.socket_ = invalid_socket;
  164. impl.state_ = 0;
  165. impl.cancel_token_.reset();
  166. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  167. impl.safe_cancellation_thread_id_ = 0;
  168. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  169. return ec;
  170. }
  171. socket_type win_iocp_socket_service_base::release(
  172. win_iocp_socket_service_base::base_implementation_type& impl,
  173. boost::system::error_code& ec)
  174. {
  175. if (!is_open(impl))
  176. return invalid_socket;
  177. cancel(impl, ec);
  178. if (ec)
  179. return invalid_socket;
  180. nt_set_info_fn fn = get_nt_set_info();
  181. if (fn == 0)
  182. {
  183. ec = boost::asio::error::operation_not_supported;
  184. return invalid_socket;
  185. }
  186. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(impl.socket_);
  187. ULONG_PTR iosb[2] = { 0, 0 };
  188. void* info[2] = { 0, 0 };
  189. if (fn(sock_as_handle, iosb, &info, sizeof(info),
  190. 61 /* FileReplaceCompletionInformation */))
  191. {
  192. ec = boost::asio::error::operation_not_supported;
  193. return invalid_socket;
  194. }
  195. socket_type tmp = impl.socket_;
  196. impl.socket_ = invalid_socket;
  197. return tmp;
  198. }
  199. boost::system::error_code win_iocp_socket_service_base::cancel(
  200. win_iocp_socket_service_base::base_implementation_type& impl,
  201. boost::system::error_code& ec)
  202. {
  203. if (!is_open(impl))
  204. {
  205. ec = boost::asio::error::bad_descriptor;
  206. return ec;
  207. }
  208. BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
  209. "socket", &impl, impl.socket_, "cancel"));
  210. if (FARPROC cancel_io_ex_ptr = ::GetProcAddress(
  211. ::GetModuleHandleA("KERNEL32"), "CancelIoEx"))
  212. {
  213. // The version of Windows supports cancellation from any thread.
  214. typedef BOOL (WINAPI* cancel_io_ex_t)(HANDLE, LPOVERLAPPED);
  215. cancel_io_ex_t cancel_io_ex = reinterpret_cast<cancel_io_ex_t>(
  216. reinterpret_cast<void*>(cancel_io_ex_ptr));
  217. socket_type sock = impl.socket_;
  218. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
  219. if (!cancel_io_ex(sock_as_handle, 0))
  220. {
  221. DWORD last_error = ::GetLastError();
  222. if (last_error == ERROR_NOT_FOUND)
  223. {
  224. // ERROR_NOT_FOUND means that there were no operations to be
  225. // cancelled. We swallow this error to match the behaviour on other
  226. // platforms.
  227. ec = boost::system::error_code();
  228. }
  229. else
  230. {
  231. ec = boost::system::error_code(last_error,
  232. boost::asio::error::get_system_category());
  233. }
  234. }
  235. else
  236. {
  237. ec = boost::system::error_code();
  238. }
  239. }
  240. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  241. else if (impl.safe_cancellation_thread_id_ == 0)
  242. {
  243. // No operations have been started, so there's nothing to cancel.
  244. ec = boost::system::error_code();
  245. }
  246. else if (impl.safe_cancellation_thread_id_ == ::GetCurrentThreadId())
  247. {
  248. // Asynchronous operations have been started from the current thread only,
  249. // so it is safe to try to cancel them using CancelIo.
  250. socket_type sock = impl.socket_;
  251. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
  252. if (!::CancelIo(sock_as_handle))
  253. {
  254. DWORD last_error = ::GetLastError();
  255. ec = boost::system::error_code(last_error,
  256. boost::asio::error::get_system_category());
  257. }
  258. else
  259. {
  260. ec = boost::system::error_code();
  261. }
  262. }
  263. else
  264. {
  265. // Asynchronous operations have been started from more than one thread,
  266. // so cancellation is not safe.
  267. ec = boost::asio::error::operation_not_supported;
  268. }
  269. #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
  270. else
  271. {
  272. // Cancellation is not supported as CancelIo may not be used.
  273. ec = boost::asio::error::operation_not_supported;
  274. }
  275. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  276. // Cancel any operations started via the reactor.
  277. if (!ec)
  278. {
  279. select_reactor* r = static_cast<select_reactor*>(
  280. interlocked_compare_exchange_pointer(
  281. reinterpret_cast<void**>(&reactor_), 0, 0));
  282. if (r)
  283. r->cancel_ops(impl.socket_, impl.reactor_data_);
  284. }
  285. return ec;
  286. }
  287. boost::system::error_code win_iocp_socket_service_base::do_open(
  288. win_iocp_socket_service_base::base_implementation_type& impl,
  289. int family, int type, int protocol, boost::system::error_code& ec)
  290. {
  291. if (is_open(impl))
  292. {
  293. ec = boost::asio::error::already_open;
  294. return ec;
  295. }
  296. socket_holder sock(socket_ops::socket(family, type, protocol, ec));
  297. if (sock.get() == invalid_socket)
  298. return ec;
  299. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock.get());
  300. if (iocp_service_.register_handle(sock_as_handle, ec))
  301. return ec;
  302. impl.socket_ = sock.release();
  303. switch (type)
  304. {
  305. case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
  306. case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
  307. default: impl.state_ = 0; break;
  308. }
  309. impl.cancel_token_.reset(static_cast<void*>(0), socket_ops::noop_deleter());
  310. ec = boost::system::error_code();
  311. return ec;
  312. }
  313. boost::system::error_code win_iocp_socket_service_base::do_assign(
  314. win_iocp_socket_service_base::base_implementation_type& impl,
  315. int type, socket_type native_socket, boost::system::error_code& ec)
  316. {
  317. if (is_open(impl))
  318. {
  319. ec = boost::asio::error::already_open;
  320. return ec;
  321. }
  322. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(native_socket);
  323. if (iocp_service_.register_handle(sock_as_handle, ec))
  324. return ec;
  325. impl.socket_ = native_socket;
  326. switch (type)
  327. {
  328. case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
  329. case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
  330. default: impl.state_ = 0; break;
  331. }
  332. impl.cancel_token_.reset(static_cast<void*>(0), socket_ops::noop_deleter());
  333. ec = boost::system::error_code();
  334. return ec;
  335. }
  336. void win_iocp_socket_service_base::start_send_op(
  337. win_iocp_socket_service_base::base_implementation_type& impl,
  338. WSABUF* buffers, std::size_t buffer_count,
  339. socket_base::message_flags flags, bool noop, operation* op)
  340. {
  341. update_cancellation_thread_id(impl);
  342. iocp_service_.work_started();
  343. if (noop)
  344. iocp_service_.on_completion(op);
  345. else if (!is_open(impl))
  346. iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
  347. else
  348. {
  349. DWORD bytes_transferred = 0;
  350. int result = ::WSASend(impl.socket_, buffers,
  351. static_cast<DWORD>(buffer_count), &bytes_transferred, flags, op, 0);
  352. DWORD last_error = ::WSAGetLastError();
  353. if (last_error == ERROR_PORT_UNREACHABLE)
  354. last_error = WSAECONNREFUSED;
  355. if (result != 0 && last_error != WSA_IO_PENDING)
  356. iocp_service_.on_completion(op, last_error, bytes_transferred);
  357. else
  358. iocp_service_.on_pending(op);
  359. }
  360. }
  361. void win_iocp_socket_service_base::start_send_to_op(
  362. win_iocp_socket_service_base::base_implementation_type& impl,
  363. WSABUF* buffers, std::size_t buffer_count, const void* addr,
  364. int addrlen, socket_base::message_flags flags, operation* op)
  365. {
  366. update_cancellation_thread_id(impl);
  367. iocp_service_.work_started();
  368. if (!is_open(impl))
  369. iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
  370. else
  371. {
  372. DWORD bytes_transferred = 0;
  373. int result = ::WSASendTo(impl.socket_, buffers,
  374. static_cast<DWORD>(buffer_count), &bytes_transferred, flags,
  375. static_cast<const socket_addr_type*>(addr), addrlen, op, 0);
  376. DWORD last_error = ::WSAGetLastError();
  377. if (last_error == ERROR_PORT_UNREACHABLE)
  378. last_error = WSAECONNREFUSED;
  379. if (result != 0 && last_error != WSA_IO_PENDING)
  380. iocp_service_.on_completion(op, last_error, bytes_transferred);
  381. else
  382. iocp_service_.on_pending(op);
  383. }
  384. }
  385. void win_iocp_socket_service_base::start_receive_op(
  386. win_iocp_socket_service_base::base_implementation_type& impl,
  387. WSABUF* buffers, std::size_t buffer_count,
  388. socket_base::message_flags flags, bool noop, operation* op)
  389. {
  390. update_cancellation_thread_id(impl);
  391. iocp_service_.work_started();
  392. if (noop)
  393. iocp_service_.on_completion(op);
  394. else if (!is_open(impl))
  395. iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
  396. else
  397. {
  398. DWORD bytes_transferred = 0;
  399. DWORD recv_flags = flags;
  400. int result = ::WSARecv(impl.socket_, buffers,
  401. static_cast<DWORD>(buffer_count),
  402. &bytes_transferred, &recv_flags, op, 0);
  403. DWORD last_error = ::WSAGetLastError();
  404. if (last_error == ERROR_NETNAME_DELETED)
  405. last_error = WSAECONNRESET;
  406. else if (last_error == ERROR_PORT_UNREACHABLE)
  407. last_error = WSAECONNREFUSED;
  408. if (result != 0 && last_error != WSA_IO_PENDING)
  409. iocp_service_.on_completion(op, last_error, bytes_transferred);
  410. else
  411. iocp_service_.on_pending(op);
  412. }
  413. }
  414. int win_iocp_socket_service_base::start_null_buffers_receive_op(
  415. win_iocp_socket_service_base::base_implementation_type& impl,
  416. socket_base::message_flags flags, reactor_op* op, operation* iocp_op)
  417. {
  418. if ((impl.state_ & socket_ops::stream_oriented) != 0)
  419. {
  420. // For stream sockets on Windows, we may issue a 0-byte overlapped
  421. // WSARecv to wait until there is data available on the socket.
  422. ::WSABUF buf = { 0, 0 };
  423. start_receive_op(impl, &buf, 1, flags, false, iocp_op);
  424. return -1;
  425. }
  426. else
  427. {
  428. int op_type = (flags & socket_base::message_out_of_band)
  429. ? select_reactor::except_op : select_reactor::read_op;
  430. start_reactor_op(impl, op_type, op);
  431. return op_type;
  432. }
  433. }
  434. void win_iocp_socket_service_base::start_receive_from_op(
  435. win_iocp_socket_service_base::base_implementation_type& impl,
  436. WSABUF* buffers, std::size_t buffer_count, void* addr,
  437. socket_base::message_flags flags, int* addrlen, operation* op)
  438. {
  439. update_cancellation_thread_id(impl);
  440. iocp_service_.work_started();
  441. if (!is_open(impl))
  442. iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
  443. else
  444. {
  445. DWORD bytes_transferred = 0;
  446. DWORD recv_flags = flags;
  447. int result = ::WSARecvFrom(impl.socket_, buffers,
  448. static_cast<DWORD>(buffer_count), &bytes_transferred, &recv_flags,
  449. static_cast<socket_addr_type*>(addr), addrlen, op, 0);
  450. DWORD last_error = ::WSAGetLastError();
  451. if (last_error == ERROR_PORT_UNREACHABLE)
  452. last_error = WSAECONNREFUSED;
  453. if (result != 0 && last_error != WSA_IO_PENDING)
  454. iocp_service_.on_completion(op, last_error, bytes_transferred);
  455. else
  456. iocp_service_.on_pending(op);
  457. }
  458. }
  459. void win_iocp_socket_service_base::start_accept_op(
  460. win_iocp_socket_service_base::base_implementation_type& impl,
  461. bool peer_is_open, socket_holder& new_socket, int family, int type,
  462. int protocol, void* output_buffer, DWORD address_length, operation* op)
  463. {
  464. update_cancellation_thread_id(impl);
  465. iocp_service_.work_started();
  466. if (!is_open(impl))
  467. iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
  468. else if (peer_is_open)
  469. iocp_service_.on_completion(op, boost::asio::error::already_open);
  470. else
  471. {
  472. boost::system::error_code ec;
  473. new_socket.reset(socket_ops::socket(family, type, protocol, ec));
  474. if (new_socket.get() == invalid_socket)
  475. iocp_service_.on_completion(op, ec);
  476. else
  477. {
  478. DWORD bytes_read = 0;
  479. BOOL result = ::AcceptEx(impl.socket_, new_socket.get(), output_buffer,
  480. 0, address_length, address_length, &bytes_read, op);
  481. DWORD last_error = ::WSAGetLastError();
  482. if (!result && last_error != WSA_IO_PENDING)
  483. iocp_service_.on_completion(op, last_error);
  484. else
  485. iocp_service_.on_pending(op);
  486. }
  487. }
  488. }
  489. void win_iocp_socket_service_base::restart_accept_op(
  490. socket_type s, socket_holder& new_socket, int family, int type,
  491. int protocol, void* output_buffer, DWORD address_length,
  492. long* cancel_requested, operation* op)
  493. {
  494. new_socket.reset();
  495. iocp_service_.work_started();
  496. // Check if we were cancelled after the first AcceptEx completed.
  497. if (cancel_requested)
  498. if (::InterlockedExchangeAdd(cancel_requested, 0) == 1)
  499. iocp_service_.on_completion(op, boost::asio::error::operation_aborted);
  500. boost::system::error_code ec;
  501. new_socket.reset(socket_ops::socket(family, type, protocol, ec));
  502. if (new_socket.get() == invalid_socket)
  503. iocp_service_.on_completion(op, ec);
  504. else
  505. {
  506. DWORD bytes_read = 0;
  507. BOOL result = ::AcceptEx(s, new_socket.get(), output_buffer,
  508. 0, address_length, address_length, &bytes_read, op);
  509. DWORD last_error = ::WSAGetLastError();
  510. if (!result && last_error != WSA_IO_PENDING)
  511. iocp_service_.on_completion(op, last_error);
  512. else
  513. {
  514. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  515. if (cancel_requested)
  516. {
  517. if (::InterlockedExchangeAdd(cancel_requested, 0) == 1)
  518. {
  519. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(s);
  520. ::CancelIoEx(sock_as_handle, op);
  521. }
  522. }
  523. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  524. iocp_service_.on_pending(op);
  525. }
  526. }
  527. }
  528. void win_iocp_socket_service_base::start_reactor_op(
  529. win_iocp_socket_service_base::base_implementation_type& impl,
  530. int op_type, reactor_op* op)
  531. {
  532. select_reactor& r = get_reactor();
  533. update_cancellation_thread_id(impl);
  534. if (is_open(impl))
  535. {
  536. r.start_op(op_type, impl.socket_, impl.reactor_data_, op, false, false);
  537. return;
  538. }
  539. else
  540. op->ec_ = boost::asio::error::bad_descriptor;
  541. iocp_service_.post_immediate_completion(op, false);
  542. }
  543. int win_iocp_socket_service_base::start_connect_op(
  544. win_iocp_socket_service_base::base_implementation_type& impl,
  545. int family, int type, const void* addr, std::size_t addrlen,
  546. win_iocp_socket_connect_op_base* op, operation* iocp_op)
  547. {
  548. // If ConnectEx is available, use that.
  549. if (family == BOOST_ASIO_OS_DEF(AF_INET)
  550. || family == BOOST_ASIO_OS_DEF(AF_INET6))
  551. {
  552. if (connect_ex_fn connect_ex = get_connect_ex(impl, type))
  553. {
  554. union address_union
  555. {
  556. socket_addr_type base;
  557. sockaddr_in4_type v4;
  558. sockaddr_in6_type v6;
  559. } a;
  560. using namespace std; // For memset.
  561. memset(&a, 0, sizeof(a));
  562. a.base.sa_family = family;
  563. socket_ops::bind(impl.socket_, &a.base,
  564. family == BOOST_ASIO_OS_DEF(AF_INET)
  565. ? sizeof(a.v4) : sizeof(a.v6), op->ec_);
  566. if (op->ec_ && op->ec_ != boost::asio::error::invalid_argument)
  567. {
  568. iocp_service_.post_immediate_completion(op, false);
  569. return -1;
  570. }
  571. op->connect_ex_ = true;
  572. update_cancellation_thread_id(impl);
  573. iocp_service_.work_started();
  574. BOOL result = connect_ex(impl.socket_,
  575. static_cast<const socket_addr_type*>(addr),
  576. static_cast<int>(addrlen), 0, 0, 0, iocp_op);
  577. DWORD last_error = ::WSAGetLastError();
  578. if (!result && last_error != WSA_IO_PENDING)
  579. iocp_service_.on_completion(iocp_op, last_error);
  580. else
  581. iocp_service_.on_pending(iocp_op);
  582. return -1;
  583. }
  584. }
  585. // Otherwise, fall back to a reactor-based implementation.
  586. select_reactor& r = get_reactor();
  587. update_cancellation_thread_id(impl);
  588. if ((impl.state_ & socket_ops::non_blocking) != 0
  589. || socket_ops::set_internal_non_blocking(
  590. impl.socket_, impl.state_, true, op->ec_))
  591. {
  592. if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
  593. {
  594. if (op->ec_ == boost::asio::error::in_progress
  595. || op->ec_ == boost::asio::error::would_block)
  596. {
  597. op->ec_ = boost::system::error_code();
  598. r.start_op(select_reactor::connect_op, impl.socket_,
  599. impl.reactor_data_, op, false, false);
  600. return select_reactor::connect_op;
  601. }
  602. }
  603. }
  604. r.post_immediate_completion(op, false);
  605. return -1;
  606. }
  607. void win_iocp_socket_service_base::close_for_destruction(
  608. win_iocp_socket_service_base::base_implementation_type& impl)
  609. {
  610. if (is_open(impl))
  611. {
  612. BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
  613. "socket", &impl, impl.socket_, "close"));
  614. // Check if the reactor was created, in which case we need to close the
  615. // socket on the reactor as well to cancel any operations that might be
  616. // running there.
  617. select_reactor* r = static_cast<select_reactor*>(
  618. interlocked_compare_exchange_pointer(
  619. reinterpret_cast<void**>(&reactor_), 0, 0));
  620. if (r)
  621. r->deregister_descriptor(impl.socket_, impl.reactor_data_, true);
  622. boost::system::error_code ignored_ec;
  623. socket_ops::close(impl.socket_, impl.state_, true, ignored_ec);
  624. if (r)
  625. r->cleanup_descriptor_data(impl.reactor_data_);
  626. }
  627. impl.socket_ = invalid_socket;
  628. impl.state_ = 0;
  629. impl.cancel_token_.reset();
  630. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  631. impl.safe_cancellation_thread_id_ = 0;
  632. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  633. }
  634. void win_iocp_socket_service_base::update_cancellation_thread_id(
  635. win_iocp_socket_service_base::base_implementation_type& impl)
  636. {
  637. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  638. if (impl.safe_cancellation_thread_id_ == 0)
  639. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  640. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  641. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  642. #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
  643. (void)impl;
  644. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  645. }
  646. select_reactor& win_iocp_socket_service_base::get_reactor()
  647. {
  648. select_reactor* r = static_cast<select_reactor*>(
  649. interlocked_compare_exchange_pointer(
  650. reinterpret_cast<void**>(&reactor_), 0, 0));
  651. if (!r)
  652. {
  653. r = &(use_service<select_reactor>(context_));
  654. interlocked_exchange_pointer(reinterpret_cast<void**>(&reactor_), r);
  655. }
  656. return *r;
  657. }
  658. win_iocp_socket_service_base::connect_ex_fn
  659. win_iocp_socket_service_base::get_connect_ex(
  660. win_iocp_socket_service_base::base_implementation_type& impl, int type)
  661. {
  662. #if defined(BOOST_ASIO_DISABLE_CONNECTEX)
  663. (void)impl;
  664. (void)type;
  665. return 0;
  666. #else // defined(BOOST_ASIO_DISABLE_CONNECTEX)
  667. if (type != BOOST_ASIO_OS_DEF(SOCK_STREAM)
  668. && type != BOOST_ASIO_OS_DEF(SOCK_SEQPACKET))
  669. return 0;
  670. void* ptr = interlocked_compare_exchange_pointer(&connect_ex_, 0, 0);
  671. if (!ptr)
  672. {
  673. GUID guid = { 0x25a207b9, 0xddf3, 0x4660,
  674. { 0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e } };
  675. DWORD bytes = 0;
  676. if (::WSAIoctl(impl.socket_, SIO_GET_EXTENSION_FUNCTION_POINTER,
  677. &guid, sizeof(guid), &ptr, sizeof(ptr), &bytes, 0, 0) != 0)
  678. {
  679. // Set connect_ex_ to a special value to indicate that ConnectEx is
  680. // unavailable. That way we won't bother trying to look it up again.
  681. ptr = this;
  682. }
  683. interlocked_exchange_pointer(&connect_ex_, ptr);
  684. }
  685. return reinterpret_cast<connect_ex_fn>(ptr == this ? 0 : ptr);
  686. #endif // defined(BOOST_ASIO_DISABLE_CONNECTEX)
  687. }
  688. win_iocp_socket_service_base::nt_set_info_fn
  689. win_iocp_socket_service_base::get_nt_set_info()
  690. {
  691. void* ptr = interlocked_compare_exchange_pointer(&nt_set_info_, 0, 0);
  692. if (!ptr)
  693. {
  694. if (HMODULE h = ::GetModuleHandleA("NTDLL.DLL"))
  695. ptr = reinterpret_cast<void*>(GetProcAddress(h, "NtSetInformationFile"));
  696. // On failure, set nt_set_info_ to a special value to indicate that the
  697. // NtSetInformationFile function is unavailable. That way we won't bother
  698. // trying to look it up again.
  699. interlocked_exchange_pointer(&nt_set_info_, ptr ? ptr : this);
  700. }
  701. return reinterpret_cast<nt_set_info_fn>(ptr == this ? 0 : ptr);
  702. }
  703. void* win_iocp_socket_service_base::interlocked_compare_exchange_pointer(
  704. void** dest, void* exch, void* cmp)
  705. {
  706. #if defined(_M_IX86)
  707. return reinterpret_cast<void*>(InterlockedCompareExchange(
  708. reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(exch),
  709. reinterpret_cast<LONG>(cmp)));
  710. #else
  711. return InterlockedCompareExchangePointer(dest, exch, cmp);
  712. #endif
  713. }
  714. void* win_iocp_socket_service_base::interlocked_exchange_pointer(
  715. void** dest, void* val)
  716. {
  717. #if defined(_M_IX86)
  718. return reinterpret_cast<void*>(InterlockedExchange(
  719. reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(val)));
  720. #else
  721. return InterlockedExchangePointer(dest, val);
  722. #endif
  723. }
  724. } // namespace detail
  725. } // namespace asio
  726. } // namespace boost
  727. #include <boost/asio/detail/pop_options.hpp>
  728. #endif // defined(BOOST_ASIO_HAS_IOCP)
  729. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP