win_iocp_socket_service.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. //
  2. // detail/win_iocp_socket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 <boost/utility/addressof.hpp>
  19. #include "asio/error.hpp"
  20. #include "asio/io_service.hpp"
  21. #include "asio/socket_base.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/reactive_socket_connect_op.hpp"
  30. #include "asio/detail/reactor.hpp"
  31. #include "asio/detail/reactor_op.hpp"
  32. #include "asio/detail/socket_holder.hpp"
  33. #include "asio/detail/socket_ops.hpp"
  34. #include "asio/detail/socket_types.hpp"
  35. #include "asio/detail/win_iocp_io_service.hpp"
  36. #include "asio/detail/win_iocp_null_buffers_op.hpp"
  37. #include "asio/detail/win_iocp_socket_accept_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_type
  54. {
  55. public:
  56. native_type(socket_type s)
  57. : socket_(s),
  58. have_remote_endpoint_(false)
  59. {
  60. }
  61. native_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. // Open a new socket implementation.
  114. asio::error_code open(implementation_type& impl,
  115. const protocol_type& protocol, asio::error_code& ec)
  116. {
  117. if (!do_open(impl, protocol.family(),
  118. protocol.type(), protocol.protocol(), ec))
  119. {
  120. impl.protocol_ = protocol;
  121. impl.have_remote_endpoint_ = false;
  122. impl.remote_endpoint_ = endpoint_type();
  123. }
  124. return ec;
  125. }
  126. // Assign a native socket to a socket implementation.
  127. asio::error_code assign(implementation_type& impl,
  128. const protocol_type& protocol, const native_type& native_socket,
  129. asio::error_code& ec)
  130. {
  131. if (!do_assign(impl, protocol.type(), native_socket, ec))
  132. {
  133. impl.protocol_ = protocol;
  134. impl.have_remote_endpoint_ = native_socket.have_remote_endpoint();
  135. impl.remote_endpoint_ = native_socket.remote_endpoint();
  136. }
  137. return ec;
  138. }
  139. // Get the native socket representation.
  140. native_type native(implementation_type& impl)
  141. {
  142. if (impl.have_remote_endpoint_)
  143. return native_type(impl.socket_, impl.remote_endpoint_);
  144. return native_type(impl.socket_);
  145. }
  146. // Bind the socket to the specified local endpoint.
  147. asio::error_code bind(implementation_type& impl,
  148. const endpoint_type& endpoint, asio::error_code& ec)
  149. {
  150. socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
  151. return ec;
  152. }
  153. // Set a socket option.
  154. template <typename Option>
  155. asio::error_code set_option(implementation_type& impl,
  156. const Option& option, asio::error_code& ec)
  157. {
  158. socket_ops::setsockopt(impl.socket_, impl.state_,
  159. option.level(impl.protocol_), option.name(impl.protocol_),
  160. option.data(impl.protocol_), option.size(impl.protocol_), ec);
  161. return ec;
  162. }
  163. // Set a socket option.
  164. template <typename Option>
  165. asio::error_code get_option(const implementation_type& impl,
  166. Option& option, asio::error_code& ec) const
  167. {
  168. std::size_t size = option.size(impl.protocol_);
  169. socket_ops::getsockopt(impl.socket_, impl.state_,
  170. option.level(impl.protocol_), option.name(impl.protocol_),
  171. option.data(impl.protocol_), &size, ec);
  172. if (!ec)
  173. option.resize(impl.protocol_, size);
  174. return ec;
  175. }
  176. // Get the local endpoint.
  177. endpoint_type local_endpoint(const implementation_type& impl,
  178. asio::error_code& ec) const
  179. {
  180. endpoint_type endpoint;
  181. std::size_t addr_len = endpoint.capacity();
  182. if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
  183. return endpoint_type();
  184. endpoint.resize(addr_len);
  185. return endpoint;
  186. }
  187. // Get the remote endpoint.
  188. endpoint_type remote_endpoint(const implementation_type& impl,
  189. asio::error_code& ec) const
  190. {
  191. endpoint_type endpoint = impl.remote_endpoint_;
  192. std::size_t addr_len = endpoint.capacity();
  193. if (socket_ops::getpeername(impl.socket_, endpoint.data(),
  194. &addr_len, impl.have_remote_endpoint_, ec))
  195. return endpoint_type();
  196. endpoint.resize(addr_len);
  197. return endpoint;
  198. }
  199. // Send a datagram to the specified endpoint. Returns the number of bytes
  200. // sent.
  201. template <typename ConstBufferSequence>
  202. size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
  203. const endpoint_type& destination, socket_base::message_flags flags,
  204. asio::error_code& ec)
  205. {
  206. buffer_sequence_adapter<asio::const_buffer,
  207. ConstBufferSequence> bufs(buffers);
  208. return socket_ops::sync_sendto(impl.socket_, impl.state_,
  209. bufs.buffers(), bufs.count(), flags,
  210. destination.data(), destination.size(), ec);
  211. }
  212. // Wait until data can be sent without blocking.
  213. size_t send_to(implementation_type& impl, const null_buffers&,
  214. const endpoint_type&, socket_base::message_flags,
  215. asio::error_code& ec)
  216. {
  217. // Wait for socket to become ready.
  218. socket_ops::poll_write(impl.socket_, ec);
  219. return 0;
  220. }
  221. // Start an asynchronous send. The data being sent must be valid for the
  222. // lifetime of the asynchronous operation.
  223. template <typename ConstBufferSequence, typename Handler>
  224. void async_send_to(implementation_type& impl,
  225. const ConstBufferSequence& buffers, const endpoint_type& destination,
  226. socket_base::message_flags flags, Handler handler)
  227. {
  228. // Allocate and construct an operation to wrap the handler.
  229. typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
  230. typename op::ptr p = { boost::addressof(handler),
  231. asio_handler_alloc_helpers::allocate(
  232. sizeof(op), handler), 0 };
  233. p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
  234. buffer_sequence_adapter<asio::const_buffer,
  235. ConstBufferSequence> bufs(buffers);
  236. start_send_to_op(impl, bufs.buffers(), bufs.count(),
  237. destination.data(), static_cast<int>(destination.size()),
  238. flags, p.p);
  239. p.v = p.p = 0;
  240. }
  241. // Start an asynchronous wait until data can be sent without blocking.
  242. template <typename Handler>
  243. void async_send_to(implementation_type& impl, const null_buffers&,
  244. const endpoint_type&, socket_base::message_flags, Handler handler)
  245. {
  246. // Allocate and construct an operation to wrap the handler.
  247. typedef win_iocp_null_buffers_op<Handler> op;
  248. typename op::ptr p = { boost::addressof(handler),
  249. asio_handler_alloc_helpers::allocate(
  250. sizeof(op), handler), 0 };
  251. p.p = new (p.v) op(impl.cancel_token_, handler);
  252. start_reactor_op(impl, reactor::write_op, p.p);
  253. p.v = p.p = 0;
  254. }
  255. // Receive a datagram with the endpoint of the sender. Returns the number of
  256. // bytes received.
  257. template <typename MutableBufferSequence>
  258. size_t receive_from(implementation_type& impl,
  259. const MutableBufferSequence& buffers,
  260. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  261. asio::error_code& ec)
  262. {
  263. buffer_sequence_adapter<asio::mutable_buffer,
  264. MutableBufferSequence> bufs(buffers);
  265. std::size_t addr_len = sender_endpoint.capacity();
  266. std::size_t bytes_recvd = socket_ops::sync_recvfrom(
  267. impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
  268. flags, sender_endpoint.data(), &addr_len, ec);
  269. if (!ec)
  270. sender_endpoint.resize(addr_len);
  271. return bytes_recvd;
  272. }
  273. // Wait until data can be received without blocking.
  274. size_t receive_from(implementation_type& impl,
  275. const null_buffers&, endpoint_type& sender_endpoint,
  276. socket_base::message_flags, asio::error_code& ec)
  277. {
  278. // Wait for socket to become ready.
  279. socket_ops::poll_read(impl.socket_, ec);
  280. // Reset endpoint since it can be given no sensible value at this time.
  281. sender_endpoint = endpoint_type();
  282. return 0;
  283. }
  284. // Start an asynchronous receive. The buffer for the data being received and
  285. // the sender_endpoint object must both be valid for the lifetime of the
  286. // asynchronous operation.
  287. template <typename MutableBufferSequence, typename Handler>
  288. void async_receive_from(implementation_type& impl,
  289. const MutableBufferSequence& buffers, endpoint_type& sender_endp,
  290. socket_base::message_flags flags, Handler handler)
  291. {
  292. // Allocate and construct an operation to wrap the handler.
  293. typedef win_iocp_socket_recvfrom_op<
  294. MutableBufferSequence, endpoint_type, Handler> op;
  295. typename op::ptr p = { boost::addressof(handler),
  296. asio_handler_alloc_helpers::allocate(
  297. sizeof(op), handler), 0 };
  298. p.p = new (p.v) op(sender_endp, impl.cancel_token_, buffers, handler);
  299. buffer_sequence_adapter<asio::mutable_buffer,
  300. MutableBufferSequence> bufs(buffers);
  301. start_receive_from_op(impl, bufs.buffers(), bufs.count(),
  302. sender_endp.data(), flags, &p.p->endpoint_size(), p.p);
  303. p.v = p.p = 0;
  304. }
  305. // Wait until data can be received without blocking.
  306. template <typename Handler>
  307. void async_receive_from(implementation_type& impl,
  308. const null_buffers&, endpoint_type& sender_endpoint,
  309. socket_base::message_flags flags, Handler handler)
  310. {
  311. // Allocate and construct an operation to wrap the handler.
  312. typedef win_iocp_null_buffers_op<Handler> op;
  313. typename op::ptr p = { boost::addressof(handler),
  314. asio_handler_alloc_helpers::allocate(
  315. sizeof(op), handler), 0 };
  316. p.p = new (p.v) op(impl.cancel_token_, handler);
  317. // Reset endpoint since it can be given no sensible value at this time.
  318. sender_endpoint = endpoint_type();
  319. start_null_buffers_receive_op(impl, flags, p.p);
  320. p.v = p.p = 0;
  321. }
  322. // Accept a new connection.
  323. template <typename Socket>
  324. asio::error_code accept(implementation_type& impl, Socket& peer,
  325. endpoint_type* peer_endpoint, asio::error_code& ec)
  326. {
  327. // We cannot accept a socket that is already open.
  328. if (peer.is_open())
  329. {
  330. ec = asio::error::already_open;
  331. return ec;
  332. }
  333. std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
  334. socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
  335. impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
  336. peer_endpoint ? &addr_len : 0, ec));
  337. // On success, assign new connection to peer socket object.
  338. if (new_socket.get() >= 0)
  339. {
  340. if (peer_endpoint)
  341. peer_endpoint->resize(addr_len);
  342. if (!peer.assign(impl.protocol_, new_socket.get(), ec))
  343. new_socket.release();
  344. }
  345. return ec;
  346. }
  347. // Start an asynchronous accept. The peer and peer_endpoint objects
  348. // must be valid until the accept's handler is invoked.
  349. template <typename Socket, typename Handler>
  350. void async_accept(implementation_type& impl, Socket& peer,
  351. endpoint_type* peer_endpoint, Handler handler)
  352. {
  353. // Allocate and construct an operation to wrap the handler.
  354. typedef win_iocp_socket_accept_op<Socket, protocol_type, Handler> op;
  355. typename op::ptr p = { boost::addressof(handler),
  356. asio_handler_alloc_helpers::allocate(
  357. sizeof(op), handler), 0 };
  358. bool enable_connection_aborted =
  359. (impl.state_ & socket_ops::enable_connection_aborted) != 0;
  360. p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_,
  361. peer_endpoint, enable_connection_aborted, handler);
  362. start_accept_op(impl, peer.is_open(), p.p->new_socket(),
  363. impl.protocol_.family(), impl.protocol_.type(),
  364. impl.protocol_.protocol(), p.p->output_buffer(),
  365. p.p->address_length(), p.p);
  366. p.v = p.p = 0;
  367. }
  368. // Connect the socket to the specified endpoint.
  369. asio::error_code connect(implementation_type& impl,
  370. const endpoint_type& peer_endpoint, asio::error_code& ec)
  371. {
  372. socket_ops::sync_connect(impl.socket_,
  373. peer_endpoint.data(), peer_endpoint.size(), ec);
  374. return ec;
  375. }
  376. // Start an asynchronous connect.
  377. template <typename Handler>
  378. void async_connect(implementation_type& impl,
  379. const endpoint_type& peer_endpoint, Handler handler)
  380. {
  381. // Allocate and construct an operation to wrap the handler.
  382. typedef reactive_socket_connect_op<Handler> op;
  383. typename op::ptr p = { boost::addressof(handler),
  384. asio_handler_alloc_helpers::allocate(
  385. sizeof(op), handler), 0 };
  386. p.p = new (p.v) op(impl.socket_, handler);
  387. start_connect_op(impl, p.p, peer_endpoint.data(),
  388. static_cast<int>(peer_endpoint.size()));
  389. p.v = p.p = 0;
  390. }
  391. };
  392. } // namespace detail
  393. } // namespace asio
  394. #include "asio/detail/pop_options.hpp"
  395. #endif // defined(ASIO_HAS_IOCP)
  396. #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP