reactive_socket_service.hpp 15 KB

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