/Src/Dependencies/Boost/boost/asio/detail/win_iocp_socket_service.hpp

http://hadesmem.googlecode.com/ · C++ Header · 509 lines · 373 code · 78 blank · 58 comment · 13 complexity · 45175a9fd187b2fb9c28d1d547faea56 MD5 · raw file

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