win_iocp_socket_service.hpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. //
  2. // win_iocp_socket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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_WIN_IOCP_SOCKET_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/win_iocp_io_service_fwd.hpp>
  17. #if defined(BOOST_ASIO_HAS_IOCP)
  18. #include <boost/asio/detail/push_options.hpp>
  19. #include <cstring>
  20. #include <boost/shared_ptr.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. #include <boost/weak_ptr.hpp>
  23. #include <boost/asio/detail/pop_options.hpp>
  24. #include <boost/asio/buffer.hpp>
  25. #include <boost/asio/error.hpp>
  26. #include <boost/asio/io_service.hpp>
  27. #include <boost/asio/socket_base.hpp>
  28. #include <boost/asio/detail/bind_handler.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/select_reactor.hpp>
  33. #include <boost/asio/detail/socket_holder.hpp>
  34. #include <boost/asio/detail/socket_ops.hpp>
  35. #include <boost/asio/detail/socket_types.hpp>
  36. #include <boost/asio/detail/win_iocp_io_service.hpp>
  37. namespace boost {
  38. namespace asio {
  39. namespace detail {
  40. template <typename Protocol>
  41. class win_iocp_socket_service
  42. : public boost::asio::detail::service_base<win_iocp_socket_service<Protocol> >
  43. {
  44. public:
  45. // The protocol type.
  46. typedef Protocol protocol_type;
  47. // The endpoint type.
  48. typedef typename Protocol::endpoint endpoint_type;
  49. // Base class for all operations.
  50. typedef win_iocp_io_service::operation operation;
  51. struct noop_deleter { void operator()(void*) {} };
  52. typedef boost::shared_ptr<void> shared_cancel_token_type;
  53. typedef boost::weak_ptr<void> weak_cancel_token_type;
  54. // The native type of a socket.
  55. class native_type
  56. {
  57. public:
  58. native_type(socket_type s)
  59. : socket_(s),
  60. have_remote_endpoint_(false)
  61. {
  62. }
  63. native_type(socket_type s, const endpoint_type& ep)
  64. : socket_(s),
  65. have_remote_endpoint_(true),
  66. remote_endpoint_(ep)
  67. {
  68. }
  69. void operator=(socket_type s)
  70. {
  71. socket_ = s;
  72. have_remote_endpoint_ = false;
  73. remote_endpoint_ = endpoint_type();
  74. }
  75. operator socket_type() const
  76. {
  77. return socket_;
  78. }
  79. HANDLE as_handle() const
  80. {
  81. return reinterpret_cast<HANDLE>(socket_);
  82. }
  83. bool have_remote_endpoint() const
  84. {
  85. return have_remote_endpoint_;
  86. }
  87. endpoint_type remote_endpoint() const
  88. {
  89. return remote_endpoint_;
  90. }
  91. private:
  92. socket_type socket_;
  93. bool have_remote_endpoint_;
  94. endpoint_type remote_endpoint_;
  95. };
  96. // The type of the reactor used for connect operations.
  97. typedef detail::select_reactor<true> reactor_type;
  98. // The implementation type of the socket.
  99. class implementation_type
  100. {
  101. public:
  102. // Default constructor.
  103. implementation_type()
  104. : socket_(invalid_socket),
  105. flags_(0),
  106. cancel_token_(),
  107. protocol_(endpoint_type().protocol()),
  108. next_(0),
  109. prev_(0)
  110. {
  111. }
  112. private:
  113. // Only this service will have access to the internal values.
  114. friend class win_iocp_socket_service;
  115. // The native socket representation.
  116. native_type socket_;
  117. enum
  118. {
  119. enable_connection_aborted = 1, // User wants connection_aborted errors.
  120. close_might_block = 2, // User set linger option for blocking close.
  121. user_set_non_blocking = 4 // The user wants a non-blocking socket.
  122. };
  123. // Flags indicating the current state of the socket.
  124. unsigned char flags_;
  125. // We use a shared pointer as a cancellation token here to work around the
  126. // broken Windows support for cancellation. MSDN says that when you call
  127. // closesocket any outstanding WSARecv or WSASend operations will complete
  128. // with the error ERROR_OPERATION_ABORTED. In practice they complete with
  129. // ERROR_NETNAME_DELETED, which means you can't tell the difference between
  130. // a local cancellation and the socket being hard-closed by the peer.
  131. shared_cancel_token_type cancel_token_;
  132. // The protocol associated with the socket.
  133. protocol_type protocol_;
  134. // Per-descriptor data used by the reactor.
  135. reactor_type::per_descriptor_data reactor_data_;
  136. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  137. // The ID of the thread from which it is safe to cancel asynchronous
  138. // operations. 0 means no asynchronous operations have been started yet.
  139. // ~0 means asynchronous operations have been started from more than one
  140. // thread, and cancellation is not supported for the socket.
  141. DWORD safe_cancellation_thread_id_;
  142. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  143. // Pointers to adjacent socket implementations in linked list.
  144. implementation_type* next_;
  145. implementation_type* prev_;
  146. };
  147. // The maximum number of buffers to support in a single operation.
  148. enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len };
  149. // Constructor.
  150. win_iocp_socket_service(boost::asio::io_service& io_service)
  151. : boost::asio::detail::service_base<
  152. win_iocp_socket_service<Protocol> >(io_service),
  153. iocp_service_(boost::asio::use_service<win_iocp_io_service>(io_service)),
  154. reactor_(0),
  155. mutex_(),
  156. impl_list_(0)
  157. {
  158. }
  159. // Destroy all user-defined handler objects owned by the service.
  160. void shutdown_service()
  161. {
  162. // Close all implementations, causing all operations to complete.
  163. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  164. implementation_type* impl = impl_list_;
  165. while (impl)
  166. {
  167. boost::system::error_code ignored_ec;
  168. close_for_destruction(*impl);
  169. impl = impl->next_;
  170. }
  171. }
  172. // Construct a new socket implementation.
  173. void construct(implementation_type& impl)
  174. {
  175. impl.socket_ = invalid_socket;
  176. impl.flags_ = 0;
  177. impl.cancel_token_.reset();
  178. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  179. impl.safe_cancellation_thread_id_ = 0;
  180. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  181. // Insert implementation into linked list of all implementations.
  182. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  183. impl.next_ = impl_list_;
  184. impl.prev_ = 0;
  185. if (impl_list_)
  186. impl_list_->prev_ = &impl;
  187. impl_list_ = &impl;
  188. }
  189. // Destroy a socket implementation.
  190. void destroy(implementation_type& impl)
  191. {
  192. close_for_destruction(impl);
  193. // Remove implementation from linked list of all implementations.
  194. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  195. if (impl_list_ == &impl)
  196. impl_list_ = impl.next_;
  197. if (impl.prev_)
  198. impl.prev_->next_ = impl.next_;
  199. if (impl.next_)
  200. impl.next_->prev_= impl.prev_;
  201. impl.next_ = 0;
  202. impl.prev_ = 0;
  203. }
  204. // Open a new socket implementation.
  205. boost::system::error_code open(implementation_type& impl,
  206. const protocol_type& protocol, boost::system::error_code& ec)
  207. {
  208. if (is_open(impl))
  209. {
  210. ec = boost::asio::error::already_open;
  211. return ec;
  212. }
  213. socket_holder sock(socket_ops::socket(protocol.family(), protocol.type(),
  214. protocol.protocol(), ec));
  215. if (sock.get() == invalid_socket)
  216. return ec;
  217. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock.get());
  218. if (iocp_service_.register_handle(sock_as_handle, ec))
  219. return ec;
  220. impl.socket_ = sock.release();
  221. impl.flags_ = 0;
  222. impl.cancel_token_.reset(static_cast<void*>(0), noop_deleter());
  223. impl.protocol_ = protocol;
  224. ec = boost::system::error_code();
  225. return ec;
  226. }
  227. // Assign a native socket to a socket implementation.
  228. boost::system::error_code assign(implementation_type& impl,
  229. const protocol_type& protocol, const native_type& native_socket,
  230. boost::system::error_code& ec)
  231. {
  232. if (is_open(impl))
  233. {
  234. ec = boost::asio::error::already_open;
  235. return ec;
  236. }
  237. if (iocp_service_.register_handle(native_socket.as_handle(), ec))
  238. return ec;
  239. impl.socket_ = native_socket;
  240. impl.flags_ = 0;
  241. impl.cancel_token_.reset(static_cast<void*>(0), noop_deleter());
  242. impl.protocol_ = protocol;
  243. ec = boost::system::error_code();
  244. return ec;
  245. }
  246. // Determine whether the socket is open.
  247. bool is_open(const implementation_type& impl) const
  248. {
  249. return impl.socket_ != invalid_socket;
  250. }
  251. // Destroy a socket implementation.
  252. boost::system::error_code close(implementation_type& impl,
  253. boost::system::error_code& ec)
  254. {
  255. if (is_open(impl))
  256. {
  257. // Check if the reactor was created, in which case we need to close the
  258. // socket on the reactor as well to cancel any operations that might be
  259. // running there.
  260. reactor_type* reactor = static_cast<reactor_type*>(
  261. interlocked_compare_exchange_pointer(
  262. reinterpret_cast<void**>(&reactor_), 0, 0));
  263. if (reactor)
  264. reactor->close_descriptor(impl.socket_, impl.reactor_data_);
  265. if (socket_ops::close(impl.socket_, ec) == socket_error_retval)
  266. return ec;
  267. impl.socket_ = invalid_socket;
  268. impl.flags_ = 0;
  269. impl.cancel_token_.reset();
  270. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  271. impl.safe_cancellation_thread_id_ = 0;
  272. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  273. }
  274. ec = boost::system::error_code();
  275. return ec;
  276. }
  277. // Get the native socket representation.
  278. native_type native(implementation_type& impl)
  279. {
  280. return impl.socket_;
  281. }
  282. // Cancel all operations associated with the socket.
  283. boost::system::error_code cancel(implementation_type& impl,
  284. boost::system::error_code& ec)
  285. {
  286. if (!is_open(impl))
  287. {
  288. ec = boost::asio::error::bad_descriptor;
  289. return ec;
  290. }
  291. else if (FARPROC cancel_io_ex_ptr = ::GetProcAddress(
  292. ::GetModuleHandleA("KERNEL32"), "CancelIoEx"))
  293. {
  294. // The version of Windows supports cancellation from any thread.
  295. typedef BOOL (WINAPI* cancel_io_ex_t)(HANDLE, LPOVERLAPPED);
  296. cancel_io_ex_t cancel_io_ex = (cancel_io_ex_t)cancel_io_ex_ptr;
  297. socket_type sock = impl.socket_;
  298. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
  299. if (!cancel_io_ex(sock_as_handle, 0))
  300. {
  301. DWORD last_error = ::GetLastError();
  302. if (last_error == ERROR_NOT_FOUND)
  303. {
  304. // ERROR_NOT_FOUND means that there were no operations to be
  305. // cancelled. We swallow this error to match the behaviour on other
  306. // platforms.
  307. ec = boost::system::error_code();
  308. }
  309. else
  310. {
  311. ec = boost::system::error_code(last_error,
  312. boost::asio::error::get_system_category());
  313. }
  314. }
  315. else
  316. {
  317. ec = boost::system::error_code();
  318. }
  319. }
  320. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  321. else if (impl.safe_cancellation_thread_id_ == 0)
  322. {
  323. // No operations have been started, so there's nothing to cancel.
  324. ec = boost::system::error_code();
  325. }
  326. else if (impl.safe_cancellation_thread_id_ == ::GetCurrentThreadId())
  327. {
  328. // Asynchronous operations have been started from the current thread only,
  329. // so it is safe to try to cancel them using CancelIo.
  330. socket_type sock = impl.socket_;
  331. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
  332. if (!::CancelIo(sock_as_handle))
  333. {
  334. DWORD last_error = ::GetLastError();
  335. ec = boost::system::error_code(last_error,
  336. boost::asio::error::get_system_category());
  337. }
  338. else
  339. {
  340. ec = boost::system::error_code();
  341. }
  342. }
  343. else
  344. {
  345. // Asynchronous operations have been started from more than one thread,
  346. // so cancellation is not safe.
  347. ec = boost::asio::error::operation_not_supported;
  348. }
  349. #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
  350. else
  351. {
  352. // Cancellation is not supported as CancelIo may not be used.
  353. ec = boost::asio::error::operation_not_supported;
  354. }
  355. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  356. return ec;
  357. }
  358. // Determine whether the socket is at the out-of-band data mark.
  359. bool at_mark(const implementation_type& impl,
  360. boost::system::error_code& ec) const
  361. {
  362. if (!is_open(impl))
  363. {
  364. ec = boost::asio::error::bad_descriptor;
  365. return false;
  366. }
  367. boost::asio::detail::ioctl_arg_type value = 0;
  368. socket_ops::ioctl(impl.socket_, SIOCATMARK, &value, ec);
  369. return ec ? false : value != 0;
  370. }
  371. // Determine the number of bytes available for reading.
  372. std::size_t available(const implementation_type& impl,
  373. boost::system::error_code& ec) const
  374. {
  375. if (!is_open(impl))
  376. {
  377. ec = boost::asio::error::bad_descriptor;
  378. return 0;
  379. }
  380. boost::asio::detail::ioctl_arg_type value = 0;
  381. socket_ops::ioctl(impl.socket_, FIONREAD, &value, ec);
  382. return ec ? static_cast<std::size_t>(0) : static_cast<std::size_t>(value);
  383. }
  384. // Bind the socket to the specified local endpoint.
  385. boost::system::error_code bind(implementation_type& impl,
  386. const endpoint_type& endpoint, boost::system::error_code& ec)
  387. {
  388. if (!is_open(impl))
  389. {
  390. ec = boost::asio::error::bad_descriptor;
  391. return ec;
  392. }
  393. socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
  394. return ec;
  395. }
  396. // Place the socket into the state where it will listen for new connections.
  397. boost::system::error_code listen(implementation_type& impl, int backlog,
  398. boost::system::error_code& ec)
  399. {
  400. if (!is_open(impl))
  401. {
  402. ec = boost::asio::error::bad_descriptor;
  403. return ec;
  404. }
  405. socket_ops::listen(impl.socket_, backlog, ec);
  406. return ec;
  407. }
  408. // Set a socket option.
  409. template <typename Option>
  410. boost::system::error_code set_option(implementation_type& impl,
  411. const Option& option, boost::system::error_code& ec)
  412. {
  413. if (!is_open(impl))
  414. {
  415. ec = boost::asio::error::bad_descriptor;
  416. return ec;
  417. }
  418. if (option.level(impl.protocol_) == custom_socket_option_level
  419. && option.name(impl.protocol_) == enable_connection_aborted_option)
  420. {
  421. if (option.size(impl.protocol_) != sizeof(int))
  422. {
  423. ec = boost::asio::error::invalid_argument;
  424. }
  425. else
  426. {
  427. if (*reinterpret_cast<const int*>(option.data(impl.protocol_)))
  428. impl.flags_ |= implementation_type::enable_connection_aborted;
  429. else
  430. impl.flags_ &= ~implementation_type::enable_connection_aborted;
  431. ec = boost::system::error_code();
  432. }
  433. return ec;
  434. }
  435. else
  436. {
  437. if (option.level(impl.protocol_) == SOL_SOCKET
  438. && option.name(impl.protocol_) == SO_LINGER)
  439. {
  440. const ::linger* linger_option =
  441. reinterpret_cast<const ::linger*>(option.data(impl.protocol_));
  442. if (linger_option->l_onoff != 0 && linger_option->l_linger != 0)
  443. impl.flags_ |= implementation_type::close_might_block;
  444. else
  445. impl.flags_ &= ~implementation_type::close_might_block;
  446. }
  447. socket_ops::setsockopt(impl.socket_,
  448. option.level(impl.protocol_), option.name(impl.protocol_),
  449. option.data(impl.protocol_), option.size(impl.protocol_), ec);
  450. return ec;
  451. }
  452. }
  453. // Set a socket option.
  454. template <typename Option>
  455. boost::system::error_code get_option(const implementation_type& impl,
  456. Option& option, boost::system::error_code& ec) const
  457. {
  458. if (!is_open(impl))
  459. {
  460. ec = boost::asio::error::bad_descriptor;
  461. return ec;
  462. }
  463. if (option.level(impl.protocol_) == custom_socket_option_level
  464. && option.name(impl.protocol_) == enable_connection_aborted_option)
  465. {
  466. if (option.size(impl.protocol_) != sizeof(int))
  467. {
  468. ec = boost::asio::error::invalid_argument;
  469. }
  470. else
  471. {
  472. int* target = reinterpret_cast<int*>(option.data(impl.protocol_));
  473. if (impl.flags_ & implementation_type::enable_connection_aborted)
  474. *target = 1;
  475. else
  476. *target = 0;
  477. option.resize(impl.protocol_, sizeof(int));
  478. ec = boost::system::error_code();
  479. }
  480. return ec;
  481. }
  482. else
  483. {
  484. size_t size = option.size(impl.protocol_);
  485. socket_ops::getsockopt(impl.socket_,
  486. option.level(impl.protocol_), option.name(impl.protocol_),
  487. option.data(impl.protocol_), &size, ec);
  488. if (!ec)
  489. option.resize(impl.protocol_, size);
  490. return ec;
  491. }
  492. }
  493. // Perform an IO control command on the socket.
  494. template <typename IO_Control_Command>
  495. boost::system::error_code io_control(implementation_type& impl,
  496. IO_Control_Command& command, boost::system::error_code& ec)
  497. {
  498. if (!is_open(impl))
  499. {
  500. ec = boost::asio::error::bad_descriptor;
  501. return ec;
  502. }
  503. socket_ops::ioctl(impl.socket_, command.name(),
  504. static_cast<ioctl_arg_type*>(command.data()), ec);
  505. if (!ec && command.name() == static_cast<int>(FIONBIO))
  506. {
  507. if (*static_cast<ioctl_arg_type*>(command.data()))
  508. impl.flags_ |= implementation_type::user_set_non_blocking;
  509. else
  510. impl.flags_ &= ~implementation_type::user_set_non_blocking;
  511. }
  512. return ec;
  513. }
  514. // Get the local endpoint.
  515. endpoint_type local_endpoint(const implementation_type& impl,
  516. boost::system::error_code& ec) const
  517. {
  518. if (!is_open(impl))
  519. {
  520. ec = boost::asio::error::bad_descriptor;
  521. return endpoint_type();
  522. }
  523. endpoint_type endpoint;
  524. std::size_t addr_len = endpoint.capacity();
  525. if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
  526. return endpoint_type();
  527. endpoint.resize(addr_len);
  528. return endpoint;
  529. }
  530. // Get the remote endpoint.
  531. endpoint_type remote_endpoint(const implementation_type& impl,
  532. boost::system::error_code& ec) const
  533. {
  534. if (!is_open(impl))
  535. {
  536. ec = boost::asio::error::bad_descriptor;
  537. return endpoint_type();
  538. }
  539. if (impl.socket_.have_remote_endpoint())
  540. {
  541. // Check if socket is still connected.
  542. DWORD connect_time = 0;
  543. size_t connect_time_len = sizeof(connect_time);
  544. if (socket_ops::getsockopt(impl.socket_, SOL_SOCKET, SO_CONNECT_TIME,
  545. &connect_time, &connect_time_len, ec) == socket_error_retval)
  546. {
  547. return endpoint_type();
  548. }
  549. if (connect_time == 0xFFFFFFFF)
  550. {
  551. ec = boost::asio::error::not_connected;
  552. return endpoint_type();
  553. }
  554. ec = boost::system::error_code();
  555. return impl.socket_.remote_endpoint();
  556. }
  557. else
  558. {
  559. endpoint_type endpoint;
  560. std::size_t addr_len = endpoint.capacity();
  561. if (socket_ops::getpeername(impl.socket_, endpoint.data(), &addr_len, ec))
  562. return endpoint_type();
  563. endpoint.resize(addr_len);
  564. return endpoint;
  565. }
  566. }
  567. /// Disable sends or receives on the socket.
  568. boost::system::error_code shutdown(implementation_type& impl,
  569. socket_base::shutdown_type what, boost::system::error_code& ec)
  570. {
  571. if (!is_open(impl))
  572. {
  573. ec = boost::asio::error::bad_descriptor;
  574. return ec;
  575. }
  576. socket_ops::shutdown(impl.socket_, what, ec);
  577. return ec;
  578. }
  579. // Send the given data to the peer. Returns the number of bytes sent.
  580. template <typename ConstBufferSequence>
  581. size_t send(implementation_type& impl, const ConstBufferSequence& buffers,
  582. socket_base::message_flags flags, boost::system::error_code& ec)
  583. {
  584. if (!is_open(impl))
  585. {
  586. ec = boost::asio::error::bad_descriptor;
  587. return 0;
  588. }
  589. // Copy buffers into WSABUF array.
  590. ::WSABUF bufs[max_buffers];
  591. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  592. typename ConstBufferSequence::const_iterator end = buffers.end();
  593. DWORD i = 0;
  594. size_t total_buffer_size = 0;
  595. for (; iter != end && i < max_buffers; ++iter, ++i)
  596. {
  597. boost::asio::const_buffer buffer(*iter);
  598. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  599. bufs[i].buf = const_cast<char*>(
  600. boost::asio::buffer_cast<const char*>(buffer));
  601. total_buffer_size += boost::asio::buffer_size(buffer);
  602. }
  603. // A request to receive 0 bytes on a stream socket is a no-op.
  604. if (impl.protocol_.type() == SOCK_STREAM && total_buffer_size == 0)
  605. {
  606. ec = boost::system::error_code();
  607. return 0;
  608. }
  609. // Send the data.
  610. DWORD bytes_transferred = 0;
  611. int result = ::WSASend(impl.socket_, bufs,
  612. i, &bytes_transferred, flags, 0, 0);
  613. if (result != 0)
  614. {
  615. DWORD last_error = ::WSAGetLastError();
  616. if (last_error == ERROR_NETNAME_DELETED)
  617. last_error = WSAECONNRESET;
  618. else if (last_error == ERROR_PORT_UNREACHABLE)
  619. last_error = WSAECONNREFUSED;
  620. ec = boost::system::error_code(last_error,
  621. boost::asio::error::get_system_category());
  622. return 0;
  623. }
  624. ec = boost::system::error_code();
  625. return bytes_transferred;
  626. }
  627. // Wait until data can be sent without blocking.
  628. size_t send(implementation_type& impl, const null_buffers&,
  629. socket_base::message_flags, boost::system::error_code& ec)
  630. {
  631. if (!is_open(impl))
  632. {
  633. ec = boost::asio::error::bad_descriptor;
  634. return 0;
  635. }
  636. // Wait for socket to become ready.
  637. socket_ops::poll_write(impl.socket_, ec);
  638. return 0;
  639. }
  640. template <typename ConstBufferSequence, typename Handler>
  641. class send_operation
  642. : public operation
  643. {
  644. public:
  645. send_operation(win_iocp_io_service& io_service,
  646. weak_cancel_token_type cancel_token,
  647. const ConstBufferSequence& buffers, Handler handler)
  648. : operation(io_service,
  649. &send_operation<ConstBufferSequence, Handler>::do_completion_impl,
  650. &send_operation<ConstBufferSequence, Handler>::destroy_impl),
  651. work_(io_service.get_io_service()),
  652. cancel_token_(cancel_token),
  653. buffers_(buffers),
  654. handler_(handler)
  655. {
  656. }
  657. private:
  658. static void do_completion_impl(operation* op,
  659. DWORD last_error, size_t bytes_transferred)
  660. {
  661. // Take ownership of the operation object.
  662. typedef send_operation<ConstBufferSequence, Handler> op_type;
  663. op_type* handler_op(static_cast<op_type*>(op));
  664. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  665. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  666. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  667. // Check whether buffers are still valid.
  668. typename ConstBufferSequence::const_iterator iter
  669. = handler_op->buffers_.begin();
  670. typename ConstBufferSequence::const_iterator end
  671. = handler_op->buffers_.end();
  672. while (iter != end)
  673. {
  674. boost::asio::const_buffer buffer(*iter);
  675. boost::asio::buffer_cast<const char*>(buffer);
  676. ++iter;
  677. }
  678. #endif // defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  679. // Map non-portable errors to their portable counterparts.
  680. boost::system::error_code ec(last_error,
  681. boost::asio::error::get_system_category());
  682. if (ec.value() == ERROR_NETNAME_DELETED)
  683. {
  684. if (handler_op->cancel_token_.expired())
  685. ec = boost::asio::error::operation_aborted;
  686. else
  687. ec = boost::asio::error::connection_reset;
  688. }
  689. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  690. {
  691. ec = boost::asio::error::connection_refused;
  692. }
  693. // Make a copy of the handler so that the memory can be deallocated before
  694. // the upcall is made.
  695. Handler handler(handler_op->handler_);
  696. // Free the memory associated with the handler.
  697. ptr.reset();
  698. // Call the handler.
  699. boost_asio_handler_invoke_helpers::invoke(
  700. detail::bind_handler(handler, ec, bytes_transferred), &handler);
  701. }
  702. static void destroy_impl(operation* op)
  703. {
  704. // Take ownership of the operation object.
  705. typedef send_operation<ConstBufferSequence, Handler> op_type;
  706. op_type* handler_op(static_cast<op_type*>(op));
  707. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  708. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  709. // A sub-object of the handler may be the true owner of the memory
  710. // associated with the handler. Consequently, a local copy of the handler
  711. // is required to ensure that any owning sub-object remains valid until
  712. // after we have deallocated the memory here.
  713. Handler handler(handler_op->handler_);
  714. (void)handler;
  715. // Free the memory associated with the handler.
  716. ptr.reset();
  717. }
  718. boost::asio::io_service::work work_;
  719. weak_cancel_token_type cancel_token_;
  720. ConstBufferSequence buffers_;
  721. Handler handler_;
  722. };
  723. // Start an asynchronous send. The data being sent must be valid for the
  724. // lifetime of the asynchronous operation.
  725. template <typename ConstBufferSequence, typename Handler>
  726. void async_send(implementation_type& impl, const ConstBufferSequence& buffers,
  727. socket_base::message_flags flags, Handler handler)
  728. {
  729. if (!is_open(impl))
  730. {
  731. this->get_io_service().post(bind_handler(handler,
  732. boost::asio::error::bad_descriptor, 0));
  733. return;
  734. }
  735. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  736. // Update the ID of the thread from which cancellation is safe.
  737. if (impl.safe_cancellation_thread_id_ == 0)
  738. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  739. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  740. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  741. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  742. // Allocate and construct an operation to wrap the handler.
  743. typedef send_operation<ConstBufferSequence, Handler> value_type;
  744. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  745. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  746. handler_ptr<alloc_traits> ptr(raw_ptr, iocp_service_,
  747. impl.cancel_token_, buffers, handler);
  748. // Copy buffers into WSABUF array.
  749. ::WSABUF bufs[max_buffers];
  750. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  751. typename ConstBufferSequence::const_iterator end = buffers.end();
  752. DWORD i = 0;
  753. size_t total_buffer_size = 0;
  754. for (; iter != end && i < max_buffers; ++iter, ++i)
  755. {
  756. boost::asio::const_buffer buffer(*iter);
  757. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  758. bufs[i].buf = const_cast<char*>(
  759. boost::asio::buffer_cast<const char*>(buffer));
  760. total_buffer_size += boost::asio::buffer_size(buffer);
  761. }
  762. // A request to receive 0 bytes on a stream socket is a no-op.
  763. if (impl.protocol_.type() == SOCK_STREAM && total_buffer_size == 0)
  764. {
  765. boost::asio::io_service::work work(this->get_io_service());
  766. ptr.reset();
  767. boost::system::error_code error;
  768. iocp_service_.post(bind_handler(handler, error, 0));
  769. return;
  770. }
  771. // Send the data.
  772. DWORD bytes_transferred = 0;
  773. int result = ::WSASend(impl.socket_, bufs, i,
  774. &bytes_transferred, flags, ptr.get(), 0);
  775. DWORD last_error = ::WSAGetLastError();
  776. // Check if the operation completed immediately.
  777. if (result != 0 && last_error != WSA_IO_PENDING)
  778. {
  779. boost::asio::io_service::work work(this->get_io_service());
  780. ptr.reset();
  781. boost::system::error_code ec(last_error,
  782. boost::asio::error::get_system_category());
  783. iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
  784. }
  785. else
  786. {
  787. ptr.release();
  788. }
  789. }
  790. template <typename Handler>
  791. class null_buffers_operation
  792. {
  793. public:
  794. null_buffers_operation(boost::asio::io_service& io_service, Handler handler)
  795. : work_(io_service),
  796. handler_(handler)
  797. {
  798. }
  799. bool perform(boost::system::error_code&,
  800. std::size_t& bytes_transferred)
  801. {
  802. bytes_transferred = 0;
  803. return true;
  804. }
  805. void complete(const boost::system::error_code& ec,
  806. std::size_t bytes_transferred)
  807. {
  808. work_.get_io_service().post(bind_handler(
  809. handler_, ec, bytes_transferred));
  810. }
  811. private:
  812. boost::asio::io_service::work work_;
  813. Handler handler_;
  814. };
  815. // Start an asynchronous wait until data can be sent without blocking.
  816. template <typename Handler>
  817. void async_send(implementation_type& impl, const null_buffers&,
  818. socket_base::message_flags, Handler handler)
  819. {
  820. if (!is_open(impl))
  821. {
  822. this->get_io_service().post(bind_handler(handler,
  823. boost::asio::error::bad_descriptor, 0));
  824. }
  825. else
  826. {
  827. // Check if the reactor was already obtained from the io_service.
  828. reactor_type* reactor = static_cast<reactor_type*>(
  829. interlocked_compare_exchange_pointer(
  830. reinterpret_cast<void**>(&reactor_), 0, 0));
  831. if (!reactor)
  832. {
  833. reactor = &(boost::asio::use_service<reactor_type>(
  834. this->get_io_service()));
  835. interlocked_exchange_pointer(
  836. reinterpret_cast<void**>(&reactor_), reactor);
  837. }
  838. reactor->start_write_op(impl.socket_, impl.reactor_data_,
  839. null_buffers_operation<Handler>(this->get_io_service(), handler),
  840. false);
  841. }
  842. }
  843. // Send a datagram to the specified endpoint. Returns the number of bytes
  844. // sent.
  845. template <typename ConstBufferSequence>
  846. size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
  847. const endpoint_type& destination, socket_base::message_flags flags,
  848. boost::system::error_code& ec)
  849. {
  850. if (!is_open(impl))
  851. {
  852. ec = boost::asio::error::bad_descriptor;
  853. return 0;
  854. }
  855. // Copy buffers into WSABUF array.
  856. ::WSABUF bufs[max_buffers];
  857. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  858. typename ConstBufferSequence::const_iterator end = buffers.end();
  859. DWORD i = 0;
  860. for (; iter != end && i < max_buffers; ++iter, ++i)
  861. {
  862. boost::asio::const_buffer buffer(*iter);
  863. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  864. bufs[i].buf = const_cast<char*>(
  865. boost::asio::buffer_cast<const char*>(buffer));
  866. }
  867. // Send the data.
  868. DWORD bytes_transferred = 0;
  869. int result = ::WSASendTo(impl.socket_, bufs, i, &bytes_transferred,
  870. flags, destination.data(), static_cast<int>(destination.size()), 0, 0);
  871. if (result != 0)
  872. {
  873. DWORD last_error = ::WSAGetLastError();
  874. if (last_error == ERROR_PORT_UNREACHABLE)
  875. last_error = WSAECONNREFUSED;
  876. ec = boost::system::error_code(last_error,
  877. boost::asio::error::get_system_category());
  878. return 0;
  879. }
  880. ec = boost::system::error_code();
  881. return bytes_transferred;
  882. }
  883. // Wait until data can be sent without blocking.
  884. size_t send_to(implementation_type& impl, const null_buffers&,
  885. socket_base::message_flags, const endpoint_type&,
  886. boost::system::error_code& ec)
  887. {
  888. if (!is_open(impl))
  889. {
  890. ec = boost::asio::error::bad_descriptor;
  891. return 0;
  892. }
  893. // Wait for socket to become ready.
  894. socket_ops::poll_write(impl.socket_, ec);
  895. return 0;
  896. }
  897. template <typename ConstBufferSequence, typename Handler>
  898. class send_to_operation
  899. : public operation
  900. {
  901. public:
  902. send_to_operation(win_iocp_io_service& io_service,
  903. const ConstBufferSequence& buffers, Handler handler)
  904. : operation(io_service,
  905. &send_to_operation<ConstBufferSequence, Handler>::do_completion_impl,
  906. &send_to_operation<ConstBufferSequence, Handler>::destroy_impl),
  907. work_(io_service.get_io_service()),
  908. buffers_(buffers),
  909. handler_(handler)
  910. {
  911. }
  912. private:
  913. static void do_completion_impl(operation* op,
  914. DWORD last_error, size_t bytes_transferred)
  915. {
  916. // Take ownership of the operation object.
  917. typedef send_to_operation<ConstBufferSequence, Handler> op_type;
  918. op_type* handler_op(static_cast<op_type*>(op));
  919. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  920. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  921. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  922. // Check whether buffers are still valid.
  923. typename ConstBufferSequence::const_iterator iter
  924. = handler_op->buffers_.begin();
  925. typename ConstBufferSequence::const_iterator end
  926. = handler_op->buffers_.end();
  927. while (iter != end)
  928. {
  929. boost::asio::const_buffer buffer(*iter);
  930. boost::asio::buffer_cast<const char*>(buffer);
  931. ++iter;
  932. }
  933. #endif // defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  934. // Map non-portable errors to their portable counterparts.
  935. boost::system::error_code ec(last_error,
  936. boost::asio::error::get_system_category());
  937. if (ec.value() == ERROR_PORT_UNREACHABLE)
  938. {
  939. ec = boost::asio::error::connection_refused;
  940. }
  941. // Make a copy of the handler so that the memory can be deallocated before
  942. // the upcall is made.
  943. Handler handler(handler_op->handler_);
  944. // Free the memory associated with the handler.
  945. ptr.reset();
  946. // Call the handler.
  947. boost_asio_handler_invoke_helpers::invoke(
  948. detail::bind_handler(handler, ec, bytes_transferred), &handler);
  949. }
  950. static void destroy_impl(operation* op)
  951. {
  952. // Take ownership of the operation object.
  953. typedef send_to_operation<ConstBufferSequence, Handler> op_type;
  954. op_type* handler_op(static_cast<op_type*>(op));
  955. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  956. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  957. // A sub-object of the handler may be the true owner of the memory
  958. // associated with the handler. Consequently, a local copy of the handler
  959. // is required to ensure that any owning sub-object remains valid until
  960. // after we have deallocated the memory here.
  961. Handler handler(handler_op->handler_);
  962. (void)handler;
  963. // Free the memory associated with the handler.
  964. ptr.reset();
  965. }
  966. boost::asio::io_service::work work_;
  967. ConstBufferSequence buffers_;
  968. Handler handler_;
  969. };
  970. // Start an asynchronous send. The data being sent must be valid for the
  971. // lifetime of the asynchronous operation.
  972. template <typename ConstBufferSequence, typename Handler>
  973. void async_send_to(implementation_type& impl,
  974. const ConstBufferSequence& buffers, const endpoint_type& destination,
  975. socket_base::message_flags flags, Handler handler)
  976. {
  977. if (!is_open(impl))
  978. {
  979. this->get_io_service().post(bind_handler(handler,
  980. boost::asio::error::bad_descriptor, 0));
  981. return;
  982. }
  983. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  984. // Update the ID of the thread from which cancellation is safe.
  985. if (impl.safe_cancellation_thread_id_ == 0)
  986. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  987. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  988. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  989. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  990. // Allocate and construct an operation to wrap the handler.
  991. typedef send_to_operation<ConstBufferSequence, Handler> value_type;
  992. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  993. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  994. handler_ptr<alloc_traits> ptr(raw_ptr, iocp_service_, buffers, handler);
  995. // Copy buffers into WSABUF array.
  996. ::WSABUF bufs[max_buffers];
  997. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  998. typename ConstBufferSequence::const_iterator end = buffers.end();
  999. DWORD i = 0;
  1000. for (; iter != end && i < max_buffers; ++iter, ++i)
  1001. {
  1002. boost::asio::const_buffer buffer(*iter);
  1003. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  1004. bufs[i].buf = const_cast<char*>(
  1005. boost::asio::buffer_cast<const char*>(buffer));
  1006. }
  1007. // Send the data.
  1008. DWORD bytes_transferred = 0;
  1009. int result = ::WSASendTo(impl.socket_, bufs, i, &bytes_transferred, flags,
  1010. destination.data(), static_cast<int>(destination.size()), ptr.get(), 0);
  1011. DWORD last_error = ::WSAGetLastError();
  1012. // Check if the operation completed immediately.
  1013. if (result != 0 && last_error != WSA_IO_PENDING)
  1014. {
  1015. boost::asio::io_service::work work(this->get_io_service());
  1016. ptr.reset();
  1017. boost::system::error_code ec(last_error,
  1018. boost::asio::error::get_system_category());
  1019. iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
  1020. }
  1021. else
  1022. {
  1023. ptr.release();
  1024. }
  1025. }
  1026. // Start an asynchronous wait until data can be sent without blocking.
  1027. template <typename Handler>
  1028. void async_send_to(implementation_type& impl, const null_buffers&,
  1029. socket_base::message_flags, const endpoint_type&, Handler handler)
  1030. {
  1031. if (!is_open(impl))
  1032. {
  1033. this->get_io_service().post(bind_handler(handler,
  1034. boost::asio::error::bad_descriptor, 0));
  1035. }
  1036. else
  1037. {
  1038. // Check if the reactor was already obtained from the io_service.
  1039. reactor_type* reactor = static_cast<reactor_type*>(
  1040. interlocked_compare_exchange_pointer(
  1041. reinterpret_cast<void**>(&reactor_), 0, 0));
  1042. if (!reactor)
  1043. {
  1044. reactor = &(boost::asio::use_service<reactor_type>(
  1045. this->get_io_service()));
  1046. interlocked_exchange_pointer(
  1047. reinterpret_cast<void**>(&reactor_), reactor);
  1048. }
  1049. reactor->start_write_op(impl.socket_, impl.reactor_data_,
  1050. null_buffers_operation<Handler>(this->get_io_service(), handler),
  1051. false);
  1052. }
  1053. }
  1054. // Receive some data from the peer. Returns the number of bytes received.
  1055. template <typename MutableBufferSequence>
  1056. size_t receive(implementation_type& impl,
  1057. const MutableBufferSequence& buffers,
  1058. socket_base::message_flags flags, boost::system::error_code& ec)
  1059. {
  1060. if (!is_open(impl))
  1061. {
  1062. ec = boost::asio::error::bad_descriptor;
  1063. return 0;
  1064. }
  1065. // Copy buffers into WSABUF array.
  1066. ::WSABUF bufs[max_buffers];
  1067. typename MutableBufferSequence::const_iterator iter = buffers.begin();
  1068. typename MutableBufferSequence::const_iterator end = buffers.end();
  1069. DWORD i = 0;
  1070. size_t total_buffer_size = 0;
  1071. for (; iter != end && i < max_buffers; ++iter, ++i)
  1072. {
  1073. boost::asio::mutable_buffer buffer(*iter);
  1074. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  1075. bufs[i].buf = boost::asio::buffer_cast<char*>(buffer);
  1076. total_buffer_size += boost::asio::buffer_size(buffer);
  1077. }
  1078. // A request to receive 0 bytes on a stream socket is a no-op.
  1079. if (impl.protocol_.type() == SOCK_STREAM && total_buffer_size == 0)
  1080. {
  1081. ec = boost::system::error_code();
  1082. return 0;
  1083. }
  1084. // Receive some data.
  1085. DWORD bytes_transferred = 0;
  1086. DWORD recv_flags = flags;
  1087. int result = ::WSARecv(impl.socket_, bufs, i,
  1088. &bytes_transferred, &recv_flags, 0, 0);
  1089. if (result != 0)
  1090. {
  1091. DWORD last_error = ::WSAGetLastError();
  1092. if (last_error == ERROR_NETNAME_DELETED)
  1093. last_error = WSAECONNRESET;
  1094. else if (last_error == ERROR_PORT_UNREACHABLE)
  1095. last_error = WSAECONNREFUSED;
  1096. ec = boost::system::error_code(last_error,
  1097. boost::asio::error::get_system_category());
  1098. return 0;
  1099. }
  1100. if (bytes_transferred == 0 && impl.protocol_.type() == SOCK_STREAM)
  1101. {
  1102. ec = boost::asio::error::eof;
  1103. return 0;
  1104. }
  1105. ec = boost::system::error_code();
  1106. return bytes_transferred;
  1107. }
  1108. // Wait until data can be received without blocking.
  1109. size_t receive(implementation_type& impl, const null_buffers&,
  1110. socket_base::message_flags, boost::system::error_code& ec)
  1111. {
  1112. if (!is_open(impl))
  1113. {
  1114. ec = boost::asio::error::bad_descriptor;
  1115. return 0;
  1116. }
  1117. // Wait for socket to become ready.
  1118. socket_ops::poll_read(impl.socket_, ec);
  1119. return 0;
  1120. }
  1121. template <typename MutableBufferSequence, typename Handler>
  1122. class receive_operation
  1123. : public operation
  1124. {
  1125. public:
  1126. receive_operation(int protocol_type, win_iocp_io_service& io_service,
  1127. weak_cancel_token_type cancel_token,
  1128. const MutableBufferSequence& buffers, Handler handler)
  1129. : operation(io_service,
  1130. &receive_operation<
  1131. MutableBufferSequence, Handler>::do_completion_impl,
  1132. &receive_operation<
  1133. MutableBufferSequence, Handler>::destroy_impl),
  1134. protocol_type_(protocol_type),
  1135. work_(io_service.get_io_service()),
  1136. cancel_token_(cancel_token),
  1137. buffers_(buffers),
  1138. handler_(handler)
  1139. {
  1140. }
  1141. private:
  1142. static void do_completion_impl(operation* op,
  1143. DWORD last_error, size_t bytes_transferred)
  1144. {
  1145. // Take ownership of the operation object.
  1146. typedef receive_operation<MutableBufferSequence, Handler> op_type;
  1147. op_type* handler_op(static_cast<op_type*>(op));
  1148. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  1149. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  1150. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  1151. // Check whether buffers are still valid.
  1152. typename MutableBufferSequence::const_iterator iter
  1153. = handler_op->buffers_.begin();
  1154. typename MutableBufferSequence::const_iterator end
  1155. = handler_op->buffers_.end();
  1156. while (iter != end)
  1157. {
  1158. boost::asio::mutable_buffer buffer(*iter);
  1159. boost::asio::buffer_cast<char*>(buffer);
  1160. ++iter;
  1161. }
  1162. #endif // defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  1163. // Map non-portable errors to their portable counterparts.
  1164. boost::system::error_code ec(last_error,
  1165. boost::asio::error::get_system_category());
  1166. if (ec.value() == ERROR_NETNAME_DELETED)
  1167. {
  1168. if (handler_op->cancel_token_.expired())
  1169. ec = boost::asio::error::operation_aborted;
  1170. else
  1171. ec = boost::asio::error::connection_reset;
  1172. }
  1173. else if (ec.value() == ERROR_PORT_UNREACHABLE)
  1174. {
  1175. ec = boost::asio::error::connection_refused;
  1176. }
  1177. // Check for connection closed.
  1178. else if (!ec && bytes_transferred == 0
  1179. && handler_op->protocol_type_ == SOCK_STREAM
  1180. && !boost::is_same<MutableBufferSequence, null_buffers>::value)
  1181. {
  1182. ec = boost::asio::error::eof;
  1183. }
  1184. // Make a copy of the handler so that the memory can be deallocated before
  1185. // the upcall is made.
  1186. Handler handler(handler_op->handler_);
  1187. // Free the memory associated with the handler.
  1188. ptr.reset();
  1189. // Call the handler.
  1190. boost_asio_handler_invoke_helpers::invoke(
  1191. detail::bind_handler(handler, ec, bytes_transferred), &handler);
  1192. }
  1193. static void destroy_impl(operation* op)
  1194. {
  1195. // Take ownership of the operation object.
  1196. typedef receive_operation<MutableBufferSequence, Handler> op_type;
  1197. op_type* handler_op(static_cast<op_type*>(op));
  1198. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  1199. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  1200. // A sub-object of the handler may be the true owner of the memory
  1201. // associated with the handler. Consequently, a local copy of the handler
  1202. // is required to ensure that any owning sub-object remains valid until
  1203. // after we have deallocated the memory here.
  1204. Handler handler(handler_op->handler_);
  1205. (void)handler;
  1206. // Free the memory associated with the handler.
  1207. ptr.reset();
  1208. }
  1209. int protocol_type_;
  1210. boost::asio::io_service::work work_;
  1211. weak_cancel_token_type cancel_token_;
  1212. MutableBufferSequence buffers_;
  1213. Handler handler_;
  1214. };
  1215. // Start an asynchronous receive. The buffer for the data being received
  1216. // must be valid for the lifetime of the asynchronous operation.
  1217. template <typename MutableBufferSequence, typename Handler>
  1218. void async_receive(implementation_type& impl,
  1219. const MutableBufferSequence& buffers,
  1220. socket_base::message_flags flags, Handler handler)
  1221. {
  1222. if (!is_open(impl))
  1223. {
  1224. this->get_io_service().post(bind_handler(handler,
  1225. boost::asio::error::bad_descriptor, 0));
  1226. return;
  1227. }
  1228. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  1229. // Update the ID of the thread from which cancellation is safe.
  1230. if (impl.safe_cancellation_thread_id_ == 0)
  1231. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  1232. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  1233. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  1234. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  1235. // Allocate and construct an operation to wrap the handler.
  1236. typedef receive_operation<MutableBufferSequence, Handler> value_type;
  1237. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  1238. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  1239. int protocol_type = impl.protocol_.type();
  1240. handler_ptr<alloc_traits> ptr(raw_ptr, protocol_type,
  1241. iocp_service_, impl.cancel_token_, buffers, handler);
  1242. // Copy buffers into WSABUF array.
  1243. ::WSABUF bufs[max_buffers];
  1244. typename MutableBufferSequence::const_iterator iter = buffers.begin();
  1245. typename MutableBufferSequence::const_iterator end = buffers.end();
  1246. DWORD i = 0;
  1247. size_t total_buffer_size = 0;
  1248. for (; iter != end && i < max_buffers; ++iter, ++i)
  1249. {
  1250. boost::asio::mutable_buffer buffer(*iter);
  1251. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  1252. bufs[i].buf = boost::asio::buffer_cast<char*>(buffer);
  1253. total_buffer_size += boost::asio::buffer_size(buffer);
  1254. }
  1255. // A request to receive 0 bytes on a stream socket is a no-op.
  1256. if (impl.protocol_.type() == SOCK_STREAM && total_buffer_size == 0)
  1257. {
  1258. boost::asio::io_service::work work(this->get_io_service());
  1259. ptr.reset();
  1260. boost::system::error_code error;
  1261. iocp_service_.post(bind_handler(handler, error, 0));
  1262. return;
  1263. }
  1264. // Receive some data.
  1265. DWORD bytes_transferred = 0;
  1266. DWORD recv_flags = flags;
  1267. int result = ::WSARecv(impl.socket_, bufs, i,
  1268. &bytes_transferred, &recv_flags, ptr.get(), 0);
  1269. DWORD last_error = ::WSAGetLastError();
  1270. if (result != 0 && last_error != WSA_IO_PENDING)
  1271. {
  1272. boost::asio::io_service::work work(this->get_io_service());
  1273. ptr.reset();
  1274. boost::system::error_code ec(last_error,
  1275. boost::asio::error::get_system_category());
  1276. iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
  1277. }
  1278. else
  1279. {
  1280. ptr.release();
  1281. }
  1282. }
  1283. // Wait until data can be received without blocking.
  1284. template <typename Handler>
  1285. void async_receive(implementation_type& impl, const null_buffers& buffers,
  1286. socket_base::message_flags flags, Handler handler)
  1287. {
  1288. if (!is_open(impl))
  1289. {
  1290. this->get_io_service().post(bind_handler(handler,
  1291. boost::asio::error::bad_descriptor, 0));
  1292. }
  1293. else if (impl.protocol_.type() == SOCK_STREAM)
  1294. {
  1295. // For stream sockets on Windows, we may issue a 0-byte overlapped
  1296. // WSARecv to wait until there is data available on the socket.
  1297. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  1298. // Update the ID of the thread from which cancellation is safe.
  1299. if (impl.safe_cancellation_thread_id_ == 0)
  1300. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  1301. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  1302. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  1303. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  1304. // Allocate and construct an operation to wrap the handler.
  1305. typedef receive_operation<null_buffers, Handler> value_type;
  1306. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  1307. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  1308. int protocol_type = impl.protocol_.type();
  1309. handler_ptr<alloc_traits> ptr(raw_ptr, protocol_type,
  1310. iocp_service_, impl.cancel_token_, buffers, handler);
  1311. // Issue a receive operation with an empty buffer.
  1312. ::WSABUF buf = { 0, 0 };
  1313. DWORD bytes_transferred = 0;
  1314. DWORD recv_flags = flags;
  1315. int result = ::WSARecv(impl.socket_, &buf, 1,
  1316. &bytes_transferred, &recv_flags, ptr.get(), 0);
  1317. DWORD last_error = ::WSAGetLastError();
  1318. if (result != 0 && last_error != WSA_IO_PENDING)
  1319. {
  1320. boost::asio::io_service::work work(this->get_io_service());
  1321. ptr.reset();
  1322. boost::system::error_code ec(last_error,
  1323. boost::asio::error::get_system_category());
  1324. iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
  1325. }
  1326. else
  1327. {
  1328. ptr.release();
  1329. }
  1330. }
  1331. else
  1332. {
  1333. // Check if the reactor was already obtained from the io_service.
  1334. reactor_type* reactor = static_cast<reactor_type*>(
  1335. interlocked_compare_exchange_pointer(
  1336. reinterpret_cast<void**>(&reactor_), 0, 0));
  1337. if (!reactor)
  1338. {
  1339. reactor = &(boost::asio::use_service<reactor_type>(
  1340. this->get_io_service()));
  1341. interlocked_exchange_pointer(
  1342. reinterpret_cast<void**>(&reactor_), reactor);
  1343. }
  1344. if (flags & socket_base::message_out_of_band)
  1345. {
  1346. reactor->start_except_op(impl.socket_, impl.reactor_data_,
  1347. null_buffers_operation<Handler>(this->get_io_service(), handler));
  1348. }
  1349. else
  1350. {
  1351. reactor->start_read_op(impl.socket_, impl.reactor_data_,
  1352. null_buffers_operation<Handler>(this->get_io_service(), handler),
  1353. false);
  1354. }
  1355. }
  1356. }
  1357. // Receive a datagram with the endpoint of the sender. Returns the number of
  1358. // bytes received.
  1359. template <typename MutableBufferSequence>
  1360. size_t receive_from(implementation_type& impl,
  1361. const MutableBufferSequence& buffers,
  1362. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  1363. boost::system::error_code& ec)
  1364. {
  1365. if (!is_open(impl))
  1366. {
  1367. ec = boost::asio::error::bad_descriptor;
  1368. return 0;
  1369. }
  1370. // Copy buffers into WSABUF array.
  1371. ::WSABUF bufs[max_buffers];
  1372. typename MutableBufferSequence::const_iterator iter = buffers.begin();
  1373. typename MutableBufferSequence::const_iterator end = buffers.end();
  1374. DWORD i = 0;
  1375. for (; iter != end && i < max_buffers; ++iter, ++i)
  1376. {
  1377. boost::asio::mutable_buffer buffer(*iter);
  1378. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  1379. bufs[i].buf = boost::asio::buffer_cast<char*>(buffer);
  1380. }
  1381. // Receive some data.
  1382. DWORD bytes_transferred = 0;
  1383. DWORD recv_flags = flags;
  1384. int endpoint_size = static_cast<int>(sender_endpoint.capacity());
  1385. int result = ::WSARecvFrom(impl.socket_, bufs, i, &bytes_transferred,
  1386. &recv_flags, sender_endpoint.data(), &endpoint_size, 0, 0);
  1387. if (result != 0)
  1388. {
  1389. DWORD last_error = ::WSAGetLastError();
  1390. if (last_error == ERROR_PORT_UNREACHABLE)
  1391. last_error = WSAECONNREFUSED;
  1392. ec = boost::system::error_code(last_error,
  1393. boost::asio::error::get_system_category());
  1394. return 0;
  1395. }
  1396. if (bytes_transferred == 0 && impl.protocol_.type() == SOCK_STREAM)
  1397. {
  1398. ec = boost::asio::error::eof;
  1399. return 0;
  1400. }
  1401. sender_endpoint.resize(static_cast<std::size_t>(endpoint_size));
  1402. ec = boost::system::error_code();
  1403. return bytes_transferred;
  1404. }
  1405. // Wait until data can be received without blocking.
  1406. size_t receive_from(implementation_type& impl,
  1407. const null_buffers&, endpoint_type& sender_endpoint,
  1408. socket_base::message_flags, boost::system::error_code& ec)
  1409. {
  1410. if (!is_open(impl))
  1411. {
  1412. ec = boost::asio::error::bad_descriptor;
  1413. return 0;
  1414. }
  1415. // Wait for socket to become ready.
  1416. socket_ops::poll_read(impl.socket_, ec);
  1417. // Reset endpoint since it can be given no sensible value at this time.
  1418. sender_endpoint = endpoint_type();
  1419. return 0;
  1420. }
  1421. template <typename MutableBufferSequence, typename Handler>
  1422. class receive_from_operation
  1423. : public operation
  1424. {
  1425. public:
  1426. receive_from_operation(int protocol_type, win_iocp_io_service& io_service,
  1427. endpoint_type& endpoint, const MutableBufferSequence& buffers,
  1428. Handler handler)
  1429. : operation(io_service,
  1430. &receive_from_operation<
  1431. MutableBufferSequence, Handler>::do_completion_impl,
  1432. &receive_from_operation<
  1433. MutableBufferSequence, Handler>::destroy_impl),
  1434. protocol_type_(protocol_type),
  1435. endpoint_(endpoint),
  1436. endpoint_size_(static_cast<int>(endpoint.capacity())),
  1437. work_(io_service.get_io_service()),
  1438. buffers_(buffers),
  1439. handler_(handler)
  1440. {
  1441. }
  1442. int& endpoint_size()
  1443. {
  1444. return endpoint_size_;
  1445. }
  1446. private:
  1447. static void do_completion_impl(operation* op,
  1448. DWORD last_error, size_t bytes_transferred)
  1449. {
  1450. // Take ownership of the operation object.
  1451. typedef receive_from_operation<MutableBufferSequence, Handler> op_type;
  1452. op_type* handler_op(static_cast<op_type*>(op));
  1453. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  1454. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  1455. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  1456. // Check whether buffers are still valid.
  1457. typename MutableBufferSequence::const_iterator iter
  1458. = handler_op->buffers_.begin();
  1459. typename MutableBufferSequence::const_iterator end
  1460. = handler_op->buffers_.end();
  1461. while (iter != end)
  1462. {
  1463. boost::asio::mutable_buffer buffer(*iter);
  1464. boost::asio::buffer_cast<char*>(buffer);
  1465. ++iter;
  1466. }
  1467. #endif // defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  1468. // Map non-portable errors to their portable counterparts.
  1469. boost::system::error_code ec(last_error,
  1470. boost::asio::error::get_system_category());
  1471. if (ec.value() == ERROR_PORT_UNREACHABLE)
  1472. {
  1473. ec = boost::asio::error::connection_refused;
  1474. }
  1475. // Check for connection closed.
  1476. if (!ec && bytes_transferred == 0
  1477. && handler_op->protocol_type_ == SOCK_STREAM)
  1478. {
  1479. ec = boost::asio::error::eof;
  1480. }
  1481. // Record the size of the endpoint returned by the operation.
  1482. handler_op->endpoint_.resize(handler_op->endpoint_size_);
  1483. // Make a copy of the handler so that the memory can be deallocated before
  1484. // the upcall is made.
  1485. Handler handler(handler_op->handler_);
  1486. // Free the memory associated with the handler.
  1487. ptr.reset();
  1488. // Call the handler.
  1489. boost_asio_handler_invoke_helpers::invoke(
  1490. detail::bind_handler(handler, ec, bytes_transferred), &handler);
  1491. }
  1492. static void destroy_impl(operation* op)
  1493. {
  1494. // Take ownership of the operation object.
  1495. typedef receive_from_operation<MutableBufferSequence, Handler> op_type;
  1496. op_type* handler_op(static_cast<op_type*>(op));
  1497. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  1498. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  1499. // A sub-object of the handler may be the true owner of the memory
  1500. // associated with the handler. Consequently, a local copy of the handler
  1501. // is required to ensure that any owning sub-object remains valid until
  1502. // after we have deallocated the memory here.
  1503. Handler handler(handler_op->handler_);
  1504. (void)handler;
  1505. // Free the memory associated with the handler.
  1506. ptr.reset();
  1507. }
  1508. int protocol_type_;
  1509. endpoint_type& endpoint_;
  1510. int endpoint_size_;
  1511. boost::asio::io_service::work work_;
  1512. MutableBufferSequence buffers_;
  1513. Handler handler_;
  1514. };
  1515. // Start an asynchronous receive. The buffer for the data being received and
  1516. // the sender_endpoint object must both be valid for the lifetime of the
  1517. // asynchronous operation.
  1518. template <typename MutableBufferSequence, typename Handler>
  1519. void async_receive_from(implementation_type& impl,
  1520. const MutableBufferSequence& buffers, endpoint_type& sender_endp,
  1521. socket_base::message_flags flags, Handler handler)
  1522. {
  1523. if (!is_open(impl))
  1524. {
  1525. this->get_io_service().post(bind_handler(handler,
  1526. boost::asio::error::bad_descriptor, 0));
  1527. return;
  1528. }
  1529. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  1530. // Update the ID of the thread from which cancellation is safe.
  1531. if (impl.safe_cancellation_thread_id_ == 0)
  1532. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  1533. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  1534. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  1535. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  1536. // Allocate and construct an operation to wrap the handler.
  1537. typedef receive_from_operation<MutableBufferSequence, Handler> value_type;
  1538. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  1539. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  1540. int protocol_type = impl.protocol_.type();
  1541. handler_ptr<alloc_traits> ptr(raw_ptr, protocol_type,
  1542. iocp_service_, sender_endp, buffers, handler);
  1543. // Copy buffers into WSABUF array.
  1544. ::WSABUF bufs[max_buffers];
  1545. typename MutableBufferSequence::const_iterator iter = buffers.begin();
  1546. typename MutableBufferSequence::const_iterator end = buffers.end();
  1547. DWORD i = 0;
  1548. for (; iter != end && i < max_buffers; ++iter, ++i)
  1549. {
  1550. boost::asio::mutable_buffer buffer(*iter);
  1551. bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));
  1552. bufs[i].buf = boost::asio::buffer_cast<char*>(buffer);
  1553. }
  1554. // Receive some data.
  1555. DWORD bytes_transferred = 0;
  1556. DWORD recv_flags = flags;
  1557. int result = ::WSARecvFrom(impl.socket_, bufs, i, &bytes_transferred,
  1558. &recv_flags, sender_endp.data(), &ptr.get()->endpoint_size(),
  1559. ptr.get(), 0);
  1560. DWORD last_error = ::WSAGetLastError();
  1561. if (result != 0 && last_error != WSA_IO_PENDING)
  1562. {
  1563. boost::asio::io_service::work work(this->get_io_service());
  1564. ptr.reset();
  1565. boost::system::error_code ec(last_error,
  1566. boost::asio::error::get_system_category());
  1567. iocp_service_.post(bind_handler(handler, ec, bytes_transferred));
  1568. }
  1569. else
  1570. {
  1571. ptr.release();
  1572. }
  1573. }
  1574. // Wait until data can be received without blocking.
  1575. template <typename Handler>
  1576. void async_receive_from(implementation_type& impl,
  1577. const null_buffers&, endpoint_type& sender_endpoint,
  1578. socket_base::message_flags flags, Handler handler)
  1579. {
  1580. if (!is_open(impl))
  1581. {
  1582. this->get_io_service().post(bind_handler(handler,
  1583. boost::asio::error::bad_descriptor, 0));
  1584. }
  1585. else
  1586. {
  1587. // Check if the reactor was already obtained from the io_service.
  1588. reactor_type* reactor = static_cast<reactor_type*>(
  1589. interlocked_compare_exchange_pointer(
  1590. reinterpret_cast<void**>(&reactor_), 0, 0));
  1591. if (!reactor)
  1592. {
  1593. reactor = &(boost::asio::use_service<reactor_type>(
  1594. this->get_io_service()));
  1595. interlocked_exchange_pointer(
  1596. reinterpret_cast<void**>(&reactor_), reactor);
  1597. }
  1598. // Reset endpoint since it can be given no sensible value at this time.
  1599. sender_endpoint = endpoint_type();
  1600. if (flags & socket_base::message_out_of_band)
  1601. {
  1602. reactor->start_except_op(impl.socket_, impl.reactor_data_,
  1603. null_buffers_operation<Handler>(this->get_io_service(), handler));
  1604. }
  1605. else
  1606. {
  1607. reactor->start_read_op(impl.socket_, impl.reactor_data_,
  1608. null_buffers_operation<Handler>(this->get_io_service(), handler),
  1609. false);
  1610. }
  1611. }
  1612. }
  1613. // Accept a new connection.
  1614. template <typename Socket>
  1615. boost::system::error_code accept(implementation_type& impl, Socket& peer,
  1616. endpoint_type* peer_endpoint, boost::system::error_code& ec)
  1617. {
  1618. if (!is_open(impl))
  1619. {
  1620. ec = boost::asio::error::bad_descriptor;
  1621. return ec;
  1622. }
  1623. // We cannot accept a socket that is already open.
  1624. if (peer.is_open())
  1625. {
  1626. ec = boost::asio::error::already_open;
  1627. return ec;
  1628. }
  1629. for (;;)
  1630. {
  1631. socket_holder new_socket;
  1632. std::size_t addr_len = 0;
  1633. if (peer_endpoint)
  1634. {
  1635. addr_len = peer_endpoint->capacity();
  1636. new_socket.reset(socket_ops::accept(impl.socket_,
  1637. peer_endpoint->data(), &addr_len, ec));
  1638. }
  1639. else
  1640. {
  1641. new_socket.reset(socket_ops::accept(impl.socket_, 0, 0, ec));
  1642. }
  1643. if (ec)
  1644. {
  1645. if (ec == boost::asio::error::connection_aborted
  1646. && !(impl.flags_ & implementation_type::enable_connection_aborted))
  1647. {
  1648. // Retry accept operation.
  1649. continue;
  1650. }
  1651. else
  1652. {
  1653. return ec;
  1654. }
  1655. }
  1656. if (peer_endpoint)
  1657. peer_endpoint->resize(addr_len);
  1658. peer.assign(impl.protocol_, new_socket.get(), ec);
  1659. if (!ec)
  1660. new_socket.release();
  1661. return ec;
  1662. }
  1663. }
  1664. template <typename Socket, typename Handler>
  1665. class accept_operation
  1666. : public operation
  1667. {
  1668. public:
  1669. accept_operation(win_iocp_io_service& io_service,
  1670. socket_type socket, socket_type new_socket, Socket& peer,
  1671. const protocol_type& protocol, endpoint_type* peer_endpoint,
  1672. bool enable_connection_aborted, Handler handler)
  1673. : operation(io_service,
  1674. &accept_operation<Socket, Handler>::do_completion_impl,
  1675. &accept_operation<Socket, Handler>::destroy_impl),
  1676. io_service_(io_service),
  1677. socket_(socket),
  1678. new_socket_(new_socket),
  1679. peer_(peer),
  1680. protocol_(protocol),
  1681. peer_endpoint_(peer_endpoint),
  1682. work_(io_service.get_io_service()),
  1683. enable_connection_aborted_(enable_connection_aborted),
  1684. handler_(handler)
  1685. {
  1686. }
  1687. socket_type new_socket()
  1688. {
  1689. return new_socket_.get();
  1690. }
  1691. void* output_buffer()
  1692. {
  1693. return output_buffer_;
  1694. }
  1695. DWORD address_length()
  1696. {
  1697. return sizeof(sockaddr_storage_type) + 16;
  1698. }
  1699. private:
  1700. static void do_completion_impl(operation* op, DWORD last_error, size_t)
  1701. {
  1702. // Take ownership of the operation object.
  1703. typedef accept_operation<Socket, Handler> op_type;
  1704. op_type* handler_op(static_cast<op_type*>(op));
  1705. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  1706. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  1707. // Map Windows error ERROR_NETNAME_DELETED to connection_aborted.
  1708. if (last_error == ERROR_NETNAME_DELETED)
  1709. {
  1710. last_error = WSAECONNABORTED;
  1711. }
  1712. // Restart the accept operation if we got the connection_aborted error
  1713. // and the enable_connection_aborted socket option is not set.
  1714. if (last_error == WSAECONNABORTED
  1715. && !ptr.get()->enable_connection_aborted_)
  1716. {
  1717. // Reset OVERLAPPED structure.
  1718. ptr.get()->Internal = 0;
  1719. ptr.get()->InternalHigh = 0;
  1720. ptr.get()->Offset = 0;
  1721. ptr.get()->OffsetHigh = 0;
  1722. ptr.get()->hEvent = 0;
  1723. // Create a new socket for the next connection, since the AcceptEx call
  1724. // fails with WSAEINVAL if we try to reuse the same socket.
  1725. boost::system::error_code ec;
  1726. ptr.get()->new_socket_.reset();
  1727. ptr.get()->new_socket_.reset(socket_ops::socket(
  1728. ptr.get()->protocol_.family(), ptr.get()->protocol_.type(),
  1729. ptr.get()->protocol_.protocol(), ec));
  1730. if (ptr.get()->new_socket() != invalid_socket)
  1731. {
  1732. // Accept a connection.
  1733. DWORD bytes_read = 0;
  1734. BOOL result = ::AcceptEx(ptr.get()->socket_, ptr.get()->new_socket(),
  1735. ptr.get()->output_buffer(), 0, ptr.get()->address_length(),
  1736. ptr.get()->address_length(), &bytes_read, ptr.get());
  1737. last_error = ::WSAGetLastError();
  1738. // Check if the operation completed immediately.
  1739. if (!result && last_error != WSA_IO_PENDING)
  1740. {
  1741. if (last_error == ERROR_NETNAME_DELETED
  1742. || last_error == WSAECONNABORTED)
  1743. {
  1744. // Post this handler so that operation will be restarted again.
  1745. ptr.get()->io_service_.post_completion(ptr.get(), last_error, 0);
  1746. ptr.release();
  1747. return;
  1748. }
  1749. else
  1750. {
  1751. // Operation already complete. Continue with rest of this handler.
  1752. }
  1753. }
  1754. else
  1755. {
  1756. // Asynchronous operation has been successfully restarted.
  1757. ptr.release();
  1758. return;
  1759. }
  1760. }
  1761. }
  1762. // Get the address of the peer.
  1763. endpoint_type peer_endpoint;
  1764. if (last_error == 0)
  1765. {
  1766. LPSOCKADDR local_addr = 0;
  1767. int local_addr_length = 0;
  1768. LPSOCKADDR remote_addr = 0;
  1769. int remote_addr_length = 0;
  1770. GetAcceptExSockaddrs(handler_op->output_buffer(), 0,
  1771. handler_op->address_length(), handler_op->address_length(),
  1772. &local_addr, &local_addr_length, &remote_addr, &remote_addr_length);
  1773. if (static_cast<std::size_t>(remote_addr_length)
  1774. > peer_endpoint.capacity())
  1775. {
  1776. last_error = WSAEINVAL;
  1777. }
  1778. else
  1779. {
  1780. using namespace std; // For memcpy.
  1781. memcpy(peer_endpoint.data(), remote_addr, remote_addr_length);
  1782. peer_endpoint.resize(static_cast<std::size_t>(remote_addr_length));
  1783. }
  1784. }
  1785. // Need to set the SO_UPDATE_ACCEPT_CONTEXT option so that getsockname
  1786. // and getpeername will work on the accepted socket.
  1787. if (last_error == 0)
  1788. {
  1789. SOCKET update_ctx_param = handler_op->socket_;
  1790. boost::system::error_code ec;
  1791. if (socket_ops::setsockopt(handler_op->new_socket_.get(),
  1792. SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
  1793. &update_ctx_param, sizeof(SOCKET), ec) != 0)
  1794. {
  1795. last_error = ec.value();
  1796. }
  1797. }
  1798. // If the socket was successfully accepted, transfer ownership of the
  1799. // socket to the peer object.
  1800. if (last_error == 0)
  1801. {
  1802. boost::system::error_code ec;
  1803. handler_op->peer_.assign(handler_op->protocol_,
  1804. native_type(handler_op->new_socket_.get(), peer_endpoint), ec);
  1805. if (ec)
  1806. last_error = ec.value();
  1807. else
  1808. handler_op->new_socket_.release();
  1809. }
  1810. // Pass endpoint back to caller.
  1811. if (handler_op->peer_endpoint_)
  1812. *handler_op->peer_endpoint_ = peer_endpoint;
  1813. // Make a copy of the handler so that the memory can be deallocated before
  1814. // the upcall is made.
  1815. Handler handler(handler_op->handler_);
  1816. // Free the memory associated with the handler.
  1817. ptr.reset();
  1818. // Call the handler.
  1819. boost::system::error_code ec(last_error,
  1820. boost::asio::error::get_system_category());
  1821. boost_asio_handler_invoke_helpers::invoke(
  1822. detail::bind_handler(handler, ec), &handler);
  1823. }
  1824. static void destroy_impl(operation* op)
  1825. {
  1826. // Take ownership of the operation object.
  1827. typedef accept_operation<Socket, Handler> op_type;
  1828. op_type* handler_op(static_cast<op_type*>(op));
  1829. typedef handler_alloc_traits<Handler, op_type> alloc_traits;
  1830. handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
  1831. // A sub-object of the handler may be the true owner of the memory
  1832. // associated with the handler. Consequently, a local copy of the handler
  1833. // is required to ensure that any owning sub-object remains valid until
  1834. // after we have deallocated the memory here.
  1835. Handler handler(handler_op->handler_);
  1836. (void)handler;
  1837. // Free the memory associated with the handler.
  1838. ptr.reset();
  1839. }
  1840. win_iocp_io_service& io_service_;
  1841. socket_type socket_;
  1842. socket_holder new_socket_;
  1843. Socket& peer_;
  1844. protocol_type protocol_;
  1845. endpoint_type* peer_endpoint_;
  1846. boost::asio::io_service::work work_;
  1847. unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
  1848. bool enable_connection_aborted_;
  1849. Handler handler_;
  1850. };
  1851. // Start an asynchronous accept. The peer and peer_endpoint objects
  1852. // must be valid until the accept's handler is invoked.
  1853. template <typename Socket, typename Handler>
  1854. void async_accept(implementation_type& impl, Socket& peer,
  1855. endpoint_type* peer_endpoint, Handler handler)
  1856. {
  1857. // Check whether acceptor has been initialised.
  1858. if (!is_open(impl))
  1859. {
  1860. this->get_io_service().post(bind_handler(handler,
  1861. boost::asio::error::bad_descriptor));
  1862. return;
  1863. }
  1864. // Check that peer socket has not already been opened.
  1865. if (peer.is_open())
  1866. {
  1867. this->get_io_service().post(bind_handler(handler,
  1868. boost::asio::error::already_open));
  1869. return;
  1870. }
  1871. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  1872. // Update the ID of the thread from which cancellation is safe.
  1873. if (impl.safe_cancellation_thread_id_ == 0)
  1874. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  1875. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  1876. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  1877. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  1878. // Create a new socket for the connection.
  1879. boost::system::error_code ec;
  1880. socket_holder sock(socket_ops::socket(impl.protocol_.family(),
  1881. impl.protocol_.type(), impl.protocol_.protocol(), ec));
  1882. if (sock.get() == invalid_socket)
  1883. {
  1884. this->get_io_service().post(bind_handler(handler, ec));
  1885. return;
  1886. }
  1887. // Allocate and construct an operation to wrap the handler.
  1888. typedef accept_operation<Socket, Handler> value_type;
  1889. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  1890. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  1891. socket_type new_socket = sock.get();
  1892. bool enable_connection_aborted =
  1893. (impl.flags_ & implementation_type::enable_connection_aborted);
  1894. handler_ptr<alloc_traits> ptr(raw_ptr,
  1895. iocp_service_, impl.socket_, new_socket, peer, impl.protocol_,
  1896. peer_endpoint, enable_connection_aborted, handler);
  1897. sock.release();
  1898. // Accept a connection.
  1899. DWORD bytes_read = 0;
  1900. BOOL result = ::AcceptEx(impl.socket_, ptr.get()->new_socket(),
  1901. ptr.get()->output_buffer(), 0, ptr.get()->address_length(),
  1902. ptr.get()->address_length(), &bytes_read, ptr.get());
  1903. DWORD last_error = ::WSAGetLastError();
  1904. // Check if the operation completed immediately.
  1905. if (!result && last_error != WSA_IO_PENDING)
  1906. {
  1907. if (!enable_connection_aborted
  1908. && (last_error == ERROR_NETNAME_DELETED
  1909. || last_error == WSAECONNABORTED))
  1910. {
  1911. // Post handler so that operation will be restarted again. We do not
  1912. // perform the AcceptEx again here to avoid the possibility of starving
  1913. // other handlers.
  1914. iocp_service_.post_completion(ptr.get(), last_error, 0);
  1915. ptr.release();
  1916. }
  1917. else
  1918. {
  1919. boost::asio::io_service::work work(this->get_io_service());
  1920. ptr.reset();
  1921. boost::system::error_code ec(last_error,
  1922. boost::asio::error::get_system_category());
  1923. iocp_service_.post(bind_handler(handler, ec));
  1924. }
  1925. }
  1926. else
  1927. {
  1928. ptr.release();
  1929. }
  1930. }
  1931. // Connect the socket to the specified endpoint.
  1932. boost::system::error_code connect(implementation_type& impl,
  1933. const endpoint_type& peer_endpoint, boost::system::error_code& ec)
  1934. {
  1935. if (!is_open(impl))
  1936. {
  1937. ec = boost::asio::error::bad_descriptor;
  1938. return ec;
  1939. }
  1940. // Perform the connect operation.
  1941. socket_ops::connect(impl.socket_,
  1942. peer_endpoint.data(), peer_endpoint.size(), ec);
  1943. return ec;
  1944. }
  1945. template <typename Handler>
  1946. class connect_operation
  1947. {
  1948. public:
  1949. connect_operation(socket_type socket, bool user_set_non_blocking,
  1950. boost::asio::io_service& io_service, Handler handler)
  1951. : socket_(socket),
  1952. user_set_non_blocking_(user_set_non_blocking),
  1953. io_service_(io_service),
  1954. work_(io_service),
  1955. handler_(handler)
  1956. {
  1957. }
  1958. bool perform(boost::system::error_code& ec,
  1959. std::size_t& bytes_transferred)
  1960. {
  1961. bytes_transferred = 0;
  1962. // Check whether the operation was successful.
  1963. if (ec)
  1964. return true;
  1965. // Get the error code from the connect operation.
  1966. int connect_error = 0;
  1967. size_t connect_error_len = sizeof(connect_error);
  1968. if (socket_ops::getsockopt(socket_, SOL_SOCKET, SO_ERROR,
  1969. &connect_error, &connect_error_len, ec) == socket_error_retval)
  1970. return true;
  1971. // If connection failed then post the handler with the error code.
  1972. if (connect_error)
  1973. {
  1974. ec = boost::system::error_code(connect_error,
  1975. boost::asio::error::get_system_category());
  1976. return true;
  1977. }
  1978. // Revert socket to blocking mode unless the user requested otherwise.
  1979. if (!user_set_non_blocking_)
  1980. {
  1981. ioctl_arg_type non_blocking = 0;
  1982. if (socket_ops::ioctl(socket_, FIONBIO, &non_blocking, ec))
  1983. return true;
  1984. }
  1985. // Post the result of the successful connection operation.
  1986. ec = boost::system::error_code();
  1987. return true;
  1988. }
  1989. void complete(const boost::system::error_code& ec, std::size_t)
  1990. {
  1991. io_service_.post(bind_handler(handler_, ec));
  1992. }
  1993. private:
  1994. socket_type socket_;
  1995. bool user_set_non_blocking_;
  1996. boost::asio::io_service& io_service_;
  1997. boost::asio::io_service::work work_;
  1998. Handler handler_;
  1999. };
  2000. // Start an asynchronous connect.
  2001. template <typename Handler>
  2002. void async_connect(implementation_type& impl,
  2003. const endpoint_type& peer_endpoint, Handler handler)
  2004. {
  2005. if (!is_open(impl))
  2006. {
  2007. this->get_io_service().post(bind_handler(handler,
  2008. boost::asio::error::bad_descriptor));
  2009. return;
  2010. }
  2011. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  2012. // Update the ID of the thread from which cancellation is safe.
  2013. if (impl.safe_cancellation_thread_id_ == 0)
  2014. impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
  2015. else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
  2016. impl.safe_cancellation_thread_id_ = ~DWORD(0);
  2017. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  2018. // Check if the reactor was already obtained from the io_service.
  2019. reactor_type* reactor = static_cast<reactor_type*>(
  2020. interlocked_compare_exchange_pointer(
  2021. reinterpret_cast<void**>(&reactor_), 0, 0));
  2022. if (!reactor)
  2023. {
  2024. reactor = &(boost::asio::use_service<reactor_type>(
  2025. this->get_io_service()));
  2026. interlocked_exchange_pointer(
  2027. reinterpret_cast<void**>(&reactor_), reactor);
  2028. }
  2029. // Mark the socket as non-blocking so that the connection will take place
  2030. // asynchronously.
  2031. ioctl_arg_type non_blocking = 1;
  2032. boost::system::error_code ec;
  2033. if (socket_ops::ioctl(impl.socket_, FIONBIO, &non_blocking, ec))
  2034. {
  2035. this->get_io_service().post(bind_handler(handler, ec));
  2036. return;
  2037. }
  2038. // Start the connect operation.
  2039. if (socket_ops::connect(impl.socket_, peer_endpoint.data(),
  2040. peer_endpoint.size(), ec) == 0)
  2041. {
  2042. // Revert socket to blocking mode unless the user requested otherwise.
  2043. if (!(impl.flags_ & implementation_type::user_set_non_blocking))
  2044. {
  2045. non_blocking = 0;
  2046. socket_ops::ioctl(impl.socket_, FIONBIO, &non_blocking, ec);
  2047. }
  2048. // The connect operation has finished successfully so we need to post the
  2049. // handler immediately.
  2050. this->get_io_service().post(bind_handler(handler, ec));
  2051. }
  2052. else if (ec == boost::asio::error::in_progress
  2053. || ec == boost::asio::error::would_block)
  2054. {
  2055. // The connection is happening in the background, and we need to wait
  2056. // until the socket becomes writeable.
  2057. boost::shared_ptr<bool> completed(new bool(false));
  2058. reactor->start_connect_op(impl.socket_, impl.reactor_data_,
  2059. connect_operation<Handler>(
  2060. impl.socket_,
  2061. (impl.flags_ & implementation_type::user_set_non_blocking) != 0,
  2062. this->get_io_service(), handler));
  2063. }
  2064. else
  2065. {
  2066. // Revert socket to blocking mode unless the user requested otherwise.
  2067. if (!(impl.flags_ & implementation_type::user_set_non_blocking))
  2068. {
  2069. non_blocking = 0;
  2070. boost::system::error_code ignored_ec;
  2071. socket_ops::ioctl(impl.socket_, FIONBIO, &non_blocking, ignored_ec);
  2072. }
  2073. // The connect operation has failed, so post the handler immediately.
  2074. this->get_io_service().post(bind_handler(handler, ec));
  2075. }
  2076. }
  2077. private:
  2078. // Helper function to close a socket when the associated object is being
  2079. // destroyed.
  2080. void close_for_destruction(implementation_type& impl)
  2081. {
  2082. if (is_open(impl))
  2083. {
  2084. // Check if the reactor was created, in which case we need to close the
  2085. // socket on the reactor as well to cancel any operations that might be
  2086. // running there.
  2087. reactor_type* reactor = static_cast<reactor_type*>(
  2088. interlocked_compare_exchange_pointer(
  2089. reinterpret_cast<void**>(&reactor_), 0, 0));
  2090. if (reactor)
  2091. reactor->close_descriptor(impl.socket_, impl.reactor_data_);
  2092. // The socket destructor must not block. If the user has changed the
  2093. // linger option to block in the foreground, we will change it back to the
  2094. // default so that the closure is performed in the background.
  2095. if (impl.flags_ & implementation_type::close_might_block)
  2096. {
  2097. ::linger opt;
  2098. opt.l_onoff = 0;
  2099. opt.l_linger = 0;
  2100. boost::system::error_code ignored_ec;
  2101. socket_ops::setsockopt(impl.socket_,
  2102. SOL_SOCKET, SO_LINGER, &opt, sizeof(opt), ignored_ec);
  2103. }
  2104. boost::system::error_code ignored_ec;
  2105. socket_ops::close(impl.socket_, ignored_ec);
  2106. impl.socket_ = invalid_socket;
  2107. impl.flags_ = 0;
  2108. impl.cancel_token_.reset();
  2109. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  2110. impl.safe_cancellation_thread_id_ = 0;
  2111. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  2112. }
  2113. }
  2114. // Helper function to emulate InterlockedCompareExchangePointer functionality
  2115. // for:
  2116. // - very old Platform SDKs; and
  2117. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  2118. void* interlocked_compare_exchange_pointer(void** dest, void* exch, void* cmp)
  2119. {
  2120. #if defined(_M_IX86)
  2121. return reinterpret_cast<void*>(InterlockedCompareExchange(
  2122. reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(exch),
  2123. reinterpret_cast<LONG>(cmp)));
  2124. #else
  2125. return InterlockedCompareExchangePointer(dest, exch, cmp);
  2126. #endif
  2127. }
  2128. // Helper function to emulate InterlockedExchangePointer functionality for:
  2129. // - very old Platform SDKs; and
  2130. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  2131. void* interlocked_exchange_pointer(void** dest, void* val)
  2132. {
  2133. #if defined(_M_IX86)
  2134. return reinterpret_cast<void*>(InterlockedExchange(
  2135. reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(val)));
  2136. #else
  2137. return InterlockedExchangePointer(dest, val);
  2138. #endif
  2139. }
  2140. // The IOCP service used for running asynchronous operations and dispatching
  2141. // handlers.
  2142. win_iocp_io_service& iocp_service_;
  2143. // The reactor used for performing connect operations. This object is created
  2144. // only if needed.
  2145. reactor_type* reactor_;
  2146. // Mutex to protect access to the linked list of implementations.
  2147. boost::asio::detail::mutex mutex_;
  2148. // The head of a linked list of all implementations.
  2149. implementation_type* impl_list_;
  2150. };
  2151. } // namespace detail
  2152. } // namespace asio
  2153. } // namespace boost
  2154. #endif // defined(BOOST_ASIO_HAS_IOCP)
  2155. #include <boost/asio/detail/pop_options.hpp>
  2156. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP