win_iocp_socket_service_base.ipp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. //
  2. // detail/impl/win_iocp_socket_service_base.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. BOOST_ASIO_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,
  364. const socket_addr_type* addr, int addrlen,
  365. socket_base::message_flags flags, operation* op)
  366. {
  367. update_cancellation_thread_id(impl);
  368. iocp_service_.work_started();
  369. if (!is_open(impl))
  370. iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
  371. else
  372. {
  373. DWORD bytes_transferred = 0;
  374. int result = ::WSASendTo(impl.socket_, buffers,
  375. static_cast<DWORD>(buffer_count),
  376. &bytes_transferred, flags, addr, addrlen, op, 0);
  377. DWORD last_error = ::WSAGetLastError();
  378. if (last_error == ERROR_PORT_UNREACHABLE)
  379. last_error = WSAECONNREFUSED;
  380. if (result != 0 && last_error != WSA_IO_PENDING)
  381. iocp_service_.on_completion(op, last_error, bytes_transferred);
  382. else
  383. iocp_service_.on_pending(op);
  384. }
  385. }
  386. void win_iocp_socket_service_base::start_receive_op(
  387. win_iocp_socket_service_base::base_implementation_type& impl,
  388. WSABUF* buffers, std::size_t buffer_count,
  389. socket_base::message_flags flags, bool noop, operation* op)
  390. {
  391. update_cancellation_thread_id(impl);
  392. iocp_service_.work_started();
  393. if (noop)
  394. iocp_service_.on_completion(op);
  395. else if (!is_open(impl))
  396. iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
  397. else
  398. {
  399. DWORD bytes_transferred = 0;
  400. DWORD recv_flags = flags;
  401. int result = ::WSARecv(impl.socket_, buffers,
  402. static_cast<DWORD>(buffer_count),
  403. &bytes_transferred, &recv_flags, op, 0);
  404. DWORD last_error = ::WSAGetLastError();
  405. if (last_error == ERROR_NETNAME_DELETED)
  406. last_error = WSAECONNRESET;
  407. else if (last_error == ERROR_PORT_UNREACHABLE)
  408. last_error = WSAECONNREFUSED;
  409. if (result != 0 && last_error != WSA_IO_PENDING)
  410. iocp_service_.on_completion(op, last_error, bytes_transferred);
  411. else
  412. iocp_service_.on_pending(op);
  413. }
  414. }
  415. void win_iocp_socket_service_base::start_null_buffers_receive_op(
  416. win_iocp_socket_service_base::base_implementation_type& impl,
  417. socket_base::message_flags flags, reactor_op* op)
  418. {
  419. if ((impl.state_ & socket_ops::stream_oriented) != 0)
  420. {
  421. // For stream sockets on Windows, we may issue a 0-byte overlapped
  422. // WSARecv to wait until there is data available on the socket.
  423. ::WSABUF buf = { 0, 0 };
  424. start_receive_op(impl, &buf, 1, flags, false, op);
  425. }
  426. else
  427. {
  428. start_reactor_op(impl,
  429. (flags & socket_base::message_out_of_band)
  430. ? select_reactor::except_op : select_reactor::read_op,
  431. op);
  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, socket_addr_type* 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),
  449. &bytes_transferred, &recv_flags, 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, operation* op)
  492. {
  493. new_socket.reset();
  494. iocp_service_.work_started();
  495. boost::system::error_code ec;
  496. new_socket.reset(socket_ops::socket(family, type, protocol, ec));
  497. if (new_socket.get() == invalid_socket)
  498. iocp_service_.on_completion(op, ec);
  499. else
  500. {
  501. DWORD bytes_read = 0;
  502. BOOL result = ::AcceptEx(s, new_socket.get(), output_buffer,
  503. 0, address_length, address_length, &bytes_read, op);
  504. DWORD last_error = ::WSAGetLastError();
  505. if (!result && last_error != WSA_IO_PENDING)
  506. iocp_service_.on_completion(op, last_error);
  507. else
  508. iocp_service_.on_pending(op);
  509. }
  510. }
  511. void win_iocp_socket_service_base::start_reactor_op(
  512. win_iocp_socket_service_base::base_implementation_type& impl,
  513. int op_type, reactor_op* op)
  514. {
  515. select_reactor& r = get_reactor();
  516. update_cancellation_thread_id(impl);
  517. if (is_open(impl))
  518. {
  519. r.start_op(op_type, impl.socket_, impl.reactor_data_, op, false, false);
  520. return;
  521. }
  522. else
  523. op->ec_ = boost::asio::error::bad_descriptor;
  524. iocp_service_.post_immediate_completion(op, false);
  525. }
  526. void win_iocp_socket_service_base::start_connect_op(
  527. win_iocp_socket_service_base::base_implementation_type& impl,
  528. int family, int type, const socket_addr_type* addr,
  529. std::size_t addrlen, win_iocp_socket_connect_op_base* op)
  530. {
  531. // If ConnectEx is available, use that.
  532. if (family == BOOST_ASIO_OS_DEF(AF_INET)
  533. || family == BOOST_ASIO_OS_DEF(AF_INET6))
  534. {
  535. if (connect_ex_fn connect_ex = get_connect_ex(impl, type))
  536. {
  537. union address_union
  538. {
  539. socket_addr_type base;
  540. sockaddr_in4_type v4;
  541. sockaddr_in6_type v6;
  542. } a;
  543. using namespace std; // For memset.
  544. memset(&a, 0, sizeof(a));
  545. a.base.sa_family = family;
  546. socket_ops::bind(impl.socket_, &a.base,
  547. family == BOOST_ASIO_OS_DEF(AF_INET)
  548. ? sizeof(a.v4) : sizeof(a.v6), op->ec_);
  549. if (op->ec_ && op->ec_ != boost::asio::error::invalid_argument)
  550. {
  551. iocp_service_.post_immediate_completion(op, false);
  552. return;
  553. }
  554. op->connect_ex_ = true;
  555. update_cancellation_thread_id(impl);
  556. iocp_service_.work_started();
  557. BOOL result = connect_ex(impl.socket_,
  558. addr, static_cast<int>(addrlen), 0, 0, 0, op);
  559. DWORD last_error = ::WSAGetLastError();
  560. if (!result && last_error != WSA_IO_PENDING)
  561. iocp_service_.on_completion(op, last_error);
  562. else
  563. iocp_service_.on_pending(op);
  564. return;
  565. }
  566. }
  567. // Otherwise, fall back to a reactor-based implementation.
  568. select_reactor& r = get_reactor();
  569. update_cancellation_thread_id(impl);
  570. if ((impl.state_ & socket_ops::non_blocking) != 0
  571. || socket_ops::set_internal_non_blocking(
  572. impl.socket_, impl.state_, true, op->ec_))
  573. {
  574. if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
  575. {
  576. if (op->ec_ == boost::asio::error::in_progress
  577. || op->ec_ == boost::asio::error::would_block)
  578. {
  579. op->ec_ = boost::system::error_code();
  580. r.start_op(select_reactor::connect_op, impl.socket_,
  581. impl.reactor_data_, op, false, false);
  582. return;
  583. }
  584. }
  585. }
  586. r.post_immediate_completion(op, false);
  587. }
  588. void win_iocp_socket_service_base::close_for_destruction(
  589. win_iocp_socket_service_base::base_implementation_type& impl)
  590. {
  591. if (is_open(impl))
  592. {
  593. BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
  594. "socket", &impl, impl.socket_, "close"));
  595. // Check if the reactor was created, in which case we need to close the
  596. // socket on the reactor as well to cancel any operations that might be
  597. // running there.
  598. select_reactor* r = static_cast<select_reactor*>(
  599. interlocked_compare_exchange_pointer(
  600. reinterpret_cast<void**>(&reactor_), 0, 0));
  601. if (r)
  602. r->deregister_descriptor(impl.socket_, impl.reactor_data_, true);
  603. boost::system::error_code ignored_ec;
  604. socket_ops::close(impl.socket_, impl.state_, true, ignored_ec);
  605. if (r)
  606. r->cleanup_descriptor_data(impl.reactor_data_);
  607. }
  608. impl.socket_ = invalid_socket;
  609. impl.state_ = 0;
  610. impl.cancel_token_.reset();
  611. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  612. impl.safe_cancellation_thread_id_ = 0;
  613. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  614. }
  615. void win_iocp_socket_service_base::update_cancellation_thread_id(
  616. win_iocp_socket_service_base::base_implementation_type& impl)
  617. {
  618. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  619. if (impl.safe_cancellation_thread_id_ == 0)
  620. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  621. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  622. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  623. #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
  624. (void)impl;
  625. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  626. }
  627. select_reactor& win_iocp_socket_service_base::get_reactor()
  628. {
  629. select_reactor* r = static_cast<select_reactor*>(
  630. interlocked_compare_exchange_pointer(
  631. reinterpret_cast<void**>(&reactor_), 0, 0));
  632. if (!r)
  633. {
  634. r = &(use_service<select_reactor>(context_));
  635. interlocked_exchange_pointer(reinterpret_cast<void**>(&reactor_), r);
  636. }
  637. return *r;
  638. }
  639. win_iocp_socket_service_base::connect_ex_fn
  640. win_iocp_socket_service_base::get_connect_ex(
  641. win_iocp_socket_service_base::base_implementation_type& impl, int type)
  642. {
  643. #if defined(BOOST_ASIO_DISABLE_CONNECTEX)
  644. (void)impl;
  645. (void)type;
  646. return 0;
  647. #else // defined(BOOST_ASIO_DISABLE_CONNECTEX)
  648. if (type != BOOST_ASIO_OS_DEF(SOCK_STREAM)
  649. && type != BOOST_ASIO_OS_DEF(SOCK_SEQPACKET))
  650. return 0;
  651. void* ptr = interlocked_compare_exchange_pointer(&connect_ex_, 0, 0);
  652. if (!ptr)
  653. {
  654. GUID guid = { 0x25a207b9, 0xddf3, 0x4660,
  655. { 0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e } };
  656. DWORD bytes = 0;
  657. if (::WSAIoctl(impl.socket_, SIO_GET_EXTENSION_FUNCTION_POINTER,
  658. &guid, sizeof(guid), &ptr, sizeof(ptr), &bytes, 0, 0) != 0)
  659. {
  660. // Set connect_ex_ to a special value to indicate that ConnectEx is
  661. // unavailable. That way we won't bother trying to look it up again.
  662. ptr = this;
  663. }
  664. interlocked_exchange_pointer(&connect_ex_, ptr);
  665. }
  666. return reinterpret_cast<connect_ex_fn>(ptr == this ? 0 : ptr);
  667. #endif // defined(BOOST_ASIO_DISABLE_CONNECTEX)
  668. }
  669. win_iocp_socket_service_base::nt_set_info_fn
  670. win_iocp_socket_service_base::get_nt_set_info()
  671. {
  672. void* ptr = interlocked_compare_exchange_pointer(&nt_set_info_, 0, 0);
  673. if (!ptr)
  674. {
  675. if (HMODULE h = ::GetModuleHandleA("NTDLL.DLL"))
  676. ptr = reinterpret_cast<void*>(GetProcAddress(h, "NtSetInformationFile"));
  677. // On failure, set nt_set_info_ to a special value to indicate that the
  678. // NtSetInformationFile function is unavailable. That way we won't bother
  679. // trying to look it up again.
  680. interlocked_exchange_pointer(&nt_set_info_, ptr ? ptr : this);
  681. }
  682. return reinterpret_cast<nt_set_info_fn>(ptr == this ? 0 : ptr);
  683. }
  684. void* win_iocp_socket_service_base::interlocked_compare_exchange_pointer(
  685. void** dest, void* exch, void* cmp)
  686. {
  687. #if defined(_M_IX86)
  688. return reinterpret_cast<void*>(InterlockedCompareExchange(
  689. reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(exch),
  690. reinterpret_cast<LONG>(cmp)));
  691. #else
  692. return InterlockedCompareExchangePointer(dest, exch, cmp);
  693. #endif
  694. }
  695. void* win_iocp_socket_service_base::interlocked_exchange_pointer(
  696. void** dest, void* val)
  697. {
  698. #if defined(_M_IX86)
  699. return reinterpret_cast<void*>(InterlockedExchange(
  700. reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(val)));
  701. #else
  702. return InterlockedExchangePointer(dest, val);
  703. #endif
  704. }
  705. } // namespace detail
  706. } // namespace asio
  707. } // namespace boost
  708. #include <boost/asio/detail/pop_options.hpp>
  709. #endif // defined(BOOST_ASIO_HAS_IOCP)
  710. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP