win_iocp_socket_service.hpp 17 KB

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