win_iocp_socket_service.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. //
  2. // detail/win_iocp_socket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_IOCP)
  17. #include <cstring>
  18. #include "asio/error.hpp"
  19. #include "asio/io_service.hpp"
  20. #include "asio/socket_base.hpp"
  21. #include "asio/detail/addressof.hpp"
  22. #include "asio/detail/bind_handler.hpp"
  23. #include "asio/detail/buffer_sequence_adapter.hpp"
  24. #include "asio/detail/fenced_block.hpp"
  25. #include "asio/detail/handler_alloc_helpers.hpp"
  26. #include "asio/detail/handler_invoke_helpers.hpp"
  27. #include "asio/detail/mutex.hpp"
  28. #include "asio/detail/operation.hpp"
  29. #include "asio/detail/reactor.hpp"
  30. #include "asio/detail/reactor_op.hpp"
  31. #include "asio/detail/socket_holder.hpp"
  32. #include "asio/detail/socket_ops.hpp"
  33. #include "asio/detail/socket_types.hpp"
  34. #include "asio/detail/win_iocp_io_service.hpp"
  35. #include "asio/detail/win_iocp_null_buffers_op.hpp"
  36. #include "asio/detail/win_iocp_socket_accept_op.hpp"
  37. #include "asio/detail/win_iocp_socket_connect_op.hpp"
  38. #include "asio/detail/win_iocp_socket_recvfrom_op.hpp"
  39. #include "asio/detail/win_iocp_socket_send_op.hpp"
  40. #include "asio/detail/win_iocp_socket_service_base.hpp"
  41. #include "asio/detail/push_options.hpp"
  42. namespace asio {
  43. namespace detail {
  44. template <typename Protocol>
  45. class win_iocp_socket_service : public win_iocp_socket_service_base
  46. {
  47. public:
  48. // The protocol type.
  49. typedef Protocol protocol_type;
  50. // The endpoint type.
  51. typedef typename Protocol::endpoint endpoint_type;
  52. // The native type of a socket.
  53. class native_handle_type
  54. {
  55. public:
  56. native_handle_type(socket_type s)
  57. : socket_(s),
  58. have_remote_endpoint_(false)
  59. {
  60. }
  61. native_handle_type(socket_type s, const endpoint_type& ep)
  62. : socket_(s),
  63. have_remote_endpoint_(true),
  64. remote_endpoint_(ep)
  65. {
  66. }
  67. void operator=(socket_type s)
  68. {
  69. socket_ = s;
  70. have_remote_endpoint_ = false;
  71. remote_endpoint_ = endpoint_type();
  72. }
  73. operator socket_type() const
  74. {
  75. return socket_;
  76. }
  77. bool have_remote_endpoint() const
  78. {
  79. return have_remote_endpoint_;
  80. }
  81. endpoint_type remote_endpoint() const
  82. {
  83. return remote_endpoint_;
  84. }
  85. private:
  86. socket_type socket_;
  87. bool have_remote_endpoint_;
  88. endpoint_type remote_endpoint_;
  89. };
  90. // The implementation type of the socket.
  91. struct implementation_type :
  92. win_iocp_socket_service_base::base_implementation_type
  93. {
  94. // Default constructor.
  95. implementation_type()
  96. : protocol_(endpoint_type().protocol()),
  97. have_remote_endpoint_(false),
  98. remote_endpoint_()
  99. {
  100. }
  101. // The protocol associated with the socket.
  102. protocol_type protocol_;
  103. // Whether we have a cached remote endpoint.
  104. bool have_remote_endpoint_;
  105. // A cached remote endpoint.
  106. endpoint_type remote_endpoint_;
  107. };
  108. // Constructor.
  109. win_iocp_socket_service(asio::io_service& io_service)
  110. : win_iocp_socket_service_base(io_service)
  111. {
  112. }
  113. // Move-construct a new socket implementation.
  114. void move_construct(implementation_type& impl,
  115. implementation_type& other_impl)
  116. {
  117. this->base_move_construct(impl, other_impl);
  118. impl.protocol_ = other_impl.protocol_;
  119. other_impl.protocol_ = endpoint_type().protocol();
  120. impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
  121. other_impl.have_remote_endpoint_ = false;
  122. impl.remote_endpoint_ = other_impl.remote_endpoint_;
  123. other_impl.remote_endpoint_ = endpoint_type();
  124. }
  125. // Move-assign from another socket implementation.
  126. void move_assign(implementation_type& impl,
  127. win_iocp_socket_service_base& other_service,
  128. implementation_type& other_impl)
  129. {
  130. this->base_move_assign(impl, other_service, other_impl);
  131. impl.protocol_ = other_impl.protocol_;
  132. other_impl.protocol_ = endpoint_type().protocol();
  133. impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
  134. other_impl.have_remote_endpoint_ = false;
  135. impl.remote_endpoint_ = other_impl.remote_endpoint_;
  136. other_impl.remote_endpoint_ = endpoint_type();
  137. }
  138. // Move-construct a new socket implementation from another protocol type.
  139. template <typename Protocol1>
  140. void converting_move_construct(implementation_type& impl,
  141. typename win_iocp_socket_service<
  142. Protocol1>::implementation_type& other_impl)
  143. {
  144. this->base_move_construct(impl, other_impl);
  145. impl.protocol_ = protocol_type(other_impl.protocol_);
  146. other_impl.protocol_ = typename Protocol1::endpoint().protocol();
  147. impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
  148. other_impl.have_remote_endpoint_ = false;
  149. impl.remote_endpoint_ = other_impl.remote_endpoint_;
  150. other_impl.remote_endpoint_ = typename Protocol1::endpoint();
  151. }
  152. // Open a new socket implementation.
  153. asio::error_code open(implementation_type& impl,
  154. const protocol_type& protocol, asio::error_code& ec)
  155. {
  156. if (!do_open(impl, protocol.family(),
  157. protocol.type(), protocol.protocol(), ec))
  158. {
  159. impl.protocol_ = protocol;
  160. impl.have_remote_endpoint_ = false;
  161. impl.remote_endpoint_ = endpoint_type();
  162. }
  163. return ec;
  164. }
  165. // Assign a native socket to a socket implementation.
  166. asio::error_code assign(implementation_type& impl,
  167. const protocol_type& protocol, const native_handle_type& native_socket,
  168. asio::error_code& ec)
  169. {
  170. if (!do_assign(impl, protocol.type(), native_socket, ec))
  171. {
  172. impl.protocol_ = protocol;
  173. impl.have_remote_endpoint_ = native_socket.have_remote_endpoint();
  174. impl.remote_endpoint_ = native_socket.remote_endpoint();
  175. }
  176. return ec;
  177. }
  178. // Get the native socket representation.
  179. native_handle_type native_handle(implementation_type& impl)
  180. {
  181. if (impl.have_remote_endpoint_)
  182. return native_handle_type(impl.socket_, impl.remote_endpoint_);
  183. return native_handle_type(impl.socket_);
  184. }
  185. // Bind the socket to the specified local endpoint.
  186. asio::error_code bind(implementation_type& impl,
  187. const endpoint_type& endpoint, asio::error_code& ec)
  188. {
  189. socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
  190. return ec;
  191. }
  192. // Set a socket option.
  193. template <typename Option>
  194. asio::error_code set_option(implementation_type& impl,
  195. const Option& option, asio::error_code& ec)
  196. {
  197. socket_ops::setsockopt(impl.socket_, impl.state_,
  198. option.level(impl.protocol_), option.name(impl.protocol_),
  199. option.data(impl.protocol_), option.size(impl.protocol_), ec);
  200. return ec;
  201. }
  202. // Set a socket option.
  203. template <typename Option>
  204. asio::error_code get_option(const implementation_type& impl,
  205. Option& option, asio::error_code& ec) const
  206. {
  207. std::size_t size = option.size(impl.protocol_);
  208. socket_ops::getsockopt(impl.socket_, impl.state_,
  209. option.level(impl.protocol_), option.name(impl.protocol_),
  210. option.data(impl.protocol_), &size, ec);
  211. if (!ec)
  212. option.resize(impl.protocol_, size);
  213. return ec;
  214. }
  215. // Get the local endpoint.
  216. endpoint_type local_endpoint(const implementation_type& impl,
  217. asio::error_code& ec) const
  218. {
  219. endpoint_type endpoint;
  220. std::size_t addr_len = endpoint.capacity();
  221. if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
  222. return endpoint_type();
  223. endpoint.resize(addr_len);
  224. return endpoint;
  225. }
  226. // Get the remote endpoint.
  227. endpoint_type remote_endpoint(const implementation_type& impl,
  228. asio::error_code& ec) const
  229. {
  230. endpoint_type endpoint = impl.remote_endpoint_;
  231. std::size_t addr_len = endpoint.capacity();
  232. if (socket_ops::getpeername(impl.socket_, endpoint.data(),
  233. &addr_len, impl.have_remote_endpoint_, ec))
  234. return endpoint_type();
  235. endpoint.resize(addr_len);
  236. return endpoint;
  237. }
  238. // Send a datagram to the specified endpoint. Returns the number of bytes
  239. // sent.
  240. template <typename ConstBufferSequence>
  241. size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
  242. const endpoint_type& destination, socket_base::message_flags flags,
  243. asio::error_code& ec)
  244. {
  245. buffer_sequence_adapter<asio::const_buffer,
  246. ConstBufferSequence> bufs(buffers);
  247. return socket_ops::sync_sendto(impl.socket_, impl.state_,
  248. bufs.buffers(), bufs.count(), flags,
  249. destination.data(), destination.size(), ec);
  250. }
  251. // Wait until data can be sent without blocking.
  252. size_t send_to(implementation_type& impl, const null_buffers&,
  253. const endpoint_type&, socket_base::message_flags,
  254. asio::error_code& ec)
  255. {
  256. // Wait for socket to become ready.
  257. socket_ops::poll_write(impl.socket_, impl.state_, ec);
  258. return 0;
  259. }
  260. // Start an asynchronous send. The data being sent must be valid for the
  261. // lifetime of the asynchronous operation.
  262. template <typename ConstBufferSequence, typename Handler>
  263. void async_send_to(implementation_type& impl,
  264. const ConstBufferSequence& buffers, const endpoint_type& destination,
  265. socket_base::message_flags flags, Handler& handler)
  266. {
  267. // Allocate and construct an operation to wrap the handler.
  268. typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
  269. typename op::ptr p = { asio::detail::addressof(handler),
  270. asio_handler_alloc_helpers::allocate(
  271. sizeof(op), handler), 0 };
  272. p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
  273. ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send_to"));
  274. buffer_sequence_adapter<asio::const_buffer,
  275. ConstBufferSequence> bufs(buffers);
  276. start_send_to_op(impl, bufs.buffers(), bufs.count(),
  277. destination.data(), static_cast<int>(destination.size()),
  278. flags, p.p);
  279. p.v = p.p = 0;
  280. }
  281. // Start an asynchronous wait until data can be sent without blocking.
  282. template <typename Handler>
  283. void async_send_to(implementation_type& impl, const null_buffers&,
  284. const endpoint_type&, socket_base::message_flags, Handler& handler)
  285. {
  286. // Allocate and construct an operation to wrap the handler.
  287. typedef win_iocp_null_buffers_op<Handler> op;
  288. typename op::ptr p = { asio::detail::addressof(handler),
  289. asio_handler_alloc_helpers::allocate(
  290. sizeof(op), handler), 0 };
  291. p.p = new (p.v) op(impl.cancel_token_, handler);
  292. ASIO_HANDLER_CREATION((p.p, "socket",
  293. &impl, "async_send_to(null_buffers)"));
  294. start_reactor_op(impl, reactor::write_op, p.p);
  295. p.v = p.p = 0;
  296. }
  297. // Receive a datagram with the endpoint of the sender. Returns the number of
  298. // bytes received.
  299. template <typename MutableBufferSequence>
  300. size_t receive_from(implementation_type& impl,
  301. const MutableBufferSequence& buffers,
  302. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  303. asio::error_code& ec)
  304. {
  305. buffer_sequence_adapter<asio::mutable_buffer,
  306. MutableBufferSequence> bufs(buffers);
  307. std::size_t addr_len = sender_endpoint.capacity();
  308. std::size_t bytes_recvd = socket_ops::sync_recvfrom(
  309. impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
  310. flags, sender_endpoint.data(), &addr_len, ec);
  311. if (!ec)
  312. sender_endpoint.resize(addr_len);
  313. return bytes_recvd;
  314. }
  315. // Wait until data can be received without blocking.
  316. size_t receive_from(implementation_type& impl,
  317. const null_buffers&, endpoint_type& sender_endpoint,
  318. socket_base::message_flags, asio::error_code& ec)
  319. {
  320. // Wait for socket to become ready.
  321. socket_ops::poll_read(impl.socket_, impl.state_, ec);
  322. // Reset endpoint since it can be given no sensible value at this time.
  323. sender_endpoint = endpoint_type();
  324. return 0;
  325. }
  326. // Start an asynchronous receive. The buffer for the data being received and
  327. // the sender_endpoint object must both be valid for the lifetime of the
  328. // asynchronous operation.
  329. template <typename MutableBufferSequence, typename Handler>
  330. void async_receive_from(implementation_type& impl,
  331. const MutableBufferSequence& buffers, endpoint_type& sender_endp,
  332. socket_base::message_flags flags, Handler& handler)
  333. {
  334. // Allocate and construct an operation to wrap the handler.
  335. typedef win_iocp_socket_recvfrom_op<
  336. MutableBufferSequence, endpoint_type, Handler> op;
  337. typename op::ptr p = { asio::detail::addressof(handler),
  338. asio_handler_alloc_helpers::allocate(
  339. sizeof(op), handler), 0 };
  340. p.p = new (p.v) op(sender_endp, impl.cancel_token_, buffers, handler);
  341. ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive_from"));
  342. buffer_sequence_adapter<asio::mutable_buffer,
  343. MutableBufferSequence> bufs(buffers);
  344. start_receive_from_op(impl, bufs.buffers(), bufs.count(),
  345. sender_endp.data(), flags, &p.p->endpoint_size(), p.p);
  346. p.v = p.p = 0;
  347. }
  348. // Wait until data can be received without blocking.
  349. template <typename Handler>
  350. void async_receive_from(implementation_type& impl,
  351. const null_buffers&, endpoint_type& sender_endpoint,
  352. socket_base::message_flags flags, Handler& handler)
  353. {
  354. // Allocate and construct an operation to wrap the handler.
  355. typedef win_iocp_null_buffers_op<Handler> op;
  356. typename op::ptr p = { asio::detail::addressof(handler),
  357. asio_handler_alloc_helpers::allocate(
  358. sizeof(op), handler), 0 };
  359. p.p = new (p.v) op(impl.cancel_token_, handler);
  360. ASIO_HANDLER_CREATION((p.p, "socket", &impl,
  361. "async_receive_from(null_buffers)"));
  362. // Reset endpoint since it can be given no sensible value at this time.
  363. sender_endpoint = endpoint_type();
  364. start_null_buffers_receive_op(impl, flags, p.p);
  365. p.v = p.p = 0;
  366. }
  367. // Accept a new connection.
  368. template <typename Socket>
  369. asio::error_code accept(implementation_type& impl, Socket& peer,
  370. endpoint_type* peer_endpoint, asio::error_code& ec)
  371. {
  372. // We cannot accept a socket that is already open.
  373. if (peer.is_open())
  374. {
  375. ec = asio::error::already_open;
  376. return ec;
  377. }
  378. std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
  379. socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
  380. impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
  381. peer_endpoint ? &addr_len : 0, ec));
  382. // On success, assign new connection to peer socket object.
  383. if (new_socket.get() != invalid_socket)
  384. {
  385. if (peer_endpoint)
  386. peer_endpoint->resize(addr_len);
  387. if (!peer.assign(impl.protocol_, new_socket.get(), ec))
  388. new_socket.release();
  389. }
  390. return ec;
  391. }
  392. // Start an asynchronous accept. The peer and peer_endpoint objects
  393. // must be valid until the accept's handler is invoked.
  394. template <typename Socket, typename Handler>
  395. void async_accept(implementation_type& impl, Socket& peer,
  396. endpoint_type* peer_endpoint, Handler& handler)
  397. {
  398. // Allocate and construct an operation to wrap the handler.
  399. typedef win_iocp_socket_accept_op<Socket, protocol_type, Handler> op;
  400. typename op::ptr p = { asio::detail::addressof(handler),
  401. asio_handler_alloc_helpers::allocate(
  402. sizeof(op), handler), 0 };
  403. bool enable_connection_aborted =
  404. (impl.state_ & socket_ops::enable_connection_aborted) != 0;
  405. p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_,
  406. peer_endpoint, enable_connection_aborted, handler);
  407. ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_accept"));
  408. start_accept_op(impl, peer.is_open(), p.p->new_socket(),
  409. impl.protocol_.family(), impl.protocol_.type(),
  410. impl.protocol_.protocol(), p.p->output_buffer(),
  411. p.p->address_length(), p.p);
  412. p.v = p.p = 0;
  413. }
  414. // Connect the socket to the specified endpoint.
  415. asio::error_code connect(implementation_type& impl,
  416. const endpoint_type& peer_endpoint, asio::error_code& ec)
  417. {
  418. socket_ops::sync_connect(impl.socket_,
  419. peer_endpoint.data(), peer_endpoint.size(), ec);
  420. return ec;
  421. }
  422. // Start an asynchronous connect.
  423. template <typename Handler>
  424. void async_connect(implementation_type& impl,
  425. const endpoint_type& peer_endpoint, Handler& handler)
  426. {
  427. // Allocate and construct an operation to wrap the handler.
  428. typedef win_iocp_socket_connect_op<Handler> op;
  429. typename op::ptr p = { asio::detail::addressof(handler),
  430. asio_handler_alloc_helpers::allocate(
  431. sizeof(op), handler), 0 };
  432. p.p = new (p.v) op(impl.socket_, handler);
  433. ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect"));
  434. start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(),
  435. peer_endpoint.data(), static_cast<int>(peer_endpoint.size()), p.p);
  436. p.v = p.p = 0;
  437. }
  438. };
  439. } // namespace detail
  440. } // namespace asio
  441. #include "asio/detail/pop_options.hpp"
  442. #endif // defined(ASIO_HAS_IOCP)
  443. #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP