seq_packet_socket_service.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // seq_packet_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_SEQ_PACKET_SOCKET_SERVICE_HPP
  11. #define BOOST_ASIO_SEQ_PACKET_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. #include <cstddef>
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/io_service.hpp>
  21. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  22. # include <boost/asio/detail/null_socket_service.hpp>
  23. #elif defined(BOOST_ASIO_HAS_IOCP)
  24. # include <boost/asio/detail/win_iocp_socket_service.hpp>
  25. #else
  26. # include <boost/asio/detail/reactive_socket_service.hpp>
  27. #endif
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. /// Default service implementation for a sequenced packet socket.
  32. template <typename Protocol>
  33. class seq_packet_socket_service
  34. #if defined(GENERATING_DOCUMENTATION)
  35. : public boost::asio::io_service::service
  36. #else
  37. : public boost::asio::detail::service_base<
  38. seq_packet_socket_service<Protocol> >
  39. #endif
  40. {
  41. public:
  42. #if defined(GENERATING_DOCUMENTATION)
  43. /// The unique service identifier.
  44. static boost::asio::io_service::id id;
  45. #endif
  46. /// The protocol type.
  47. typedef Protocol protocol_type;
  48. /// The endpoint type.
  49. typedef typename Protocol::endpoint endpoint_type;
  50. private:
  51. // The type of the platform-specific implementation.
  52. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  53. typedef detail::null_socket_service<Protocol> service_impl_type;
  54. #elif defined(BOOST_ASIO_HAS_IOCP)
  55. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  56. #else
  57. typedef detail::reactive_socket_service<Protocol> service_impl_type;
  58. #endif
  59. public:
  60. /// The type of a sequenced packet socket implementation.
  61. #if defined(GENERATING_DOCUMENTATION)
  62. typedef implementation_defined implementation_type;
  63. #else
  64. typedef typename service_impl_type::implementation_type implementation_type;
  65. #endif
  66. /// (Deprecated: Use native_handle_type.) The native socket type.
  67. #if defined(GENERATING_DOCUMENTATION)
  68. typedef implementation_defined native_type;
  69. #else
  70. typedef typename service_impl_type::native_handle_type native_type;
  71. #endif
  72. /// The native socket type.
  73. #if defined(GENERATING_DOCUMENTATION)
  74. typedef implementation_defined native_handle_type;
  75. #else
  76. typedef typename service_impl_type::native_handle_type native_handle_type;
  77. #endif
  78. /// Construct a new sequenced packet socket service for the specified
  79. /// io_service.
  80. explicit seq_packet_socket_service(boost::asio::io_service& io_service)
  81. : boost::asio::detail::service_base<
  82. seq_packet_socket_service<Protocol> >(io_service),
  83. service_impl_(io_service)
  84. {
  85. }
  86. /// Construct a new sequenced packet socket implementation.
  87. void construct(implementation_type& impl)
  88. {
  89. service_impl_.construct(impl);
  90. }
  91. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  92. /// Move-construct a new sequenced packet socket implementation.
  93. void move_construct(implementation_type& impl,
  94. implementation_type& other_impl)
  95. {
  96. service_impl_.move_construct(impl, other_impl);
  97. }
  98. /// Move-assign from another sequenced packet socket implementation.
  99. void move_assign(implementation_type& impl,
  100. seq_packet_socket_service& other_service,
  101. implementation_type& other_impl)
  102. {
  103. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  104. }
  105. /// Move-construct a new sequenced packet socket implementation from another
  106. /// protocol type.
  107. template <typename Protocol1>
  108. void converting_move_construct(implementation_type& impl,
  109. typename seq_packet_socket_service<
  110. Protocol1>::implementation_type& other_impl,
  111. typename enable_if<is_convertible<
  112. Protocol1, Protocol>::value>::type* = 0)
  113. {
  114. service_impl_.template converting_move_construct<Protocol1>(
  115. impl, other_impl);
  116. }
  117. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  118. /// Destroy a sequenced packet socket implementation.
  119. void destroy(implementation_type& impl)
  120. {
  121. service_impl_.destroy(impl);
  122. }
  123. /// Open a sequenced packet socket.
  124. boost::system::error_code open(implementation_type& impl,
  125. const protocol_type& protocol, boost::system::error_code& ec)
  126. {
  127. if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_SEQPACKET))
  128. service_impl_.open(impl, protocol, ec);
  129. else
  130. ec = boost::asio::error::invalid_argument;
  131. return ec;
  132. }
  133. /// Assign an existing native socket to a sequenced packet socket.
  134. boost::system::error_code assign(implementation_type& impl,
  135. const protocol_type& protocol, const native_handle_type& native_socket,
  136. boost::system::error_code& ec)
  137. {
  138. return service_impl_.assign(impl, protocol, native_socket, ec);
  139. }
  140. /// Determine whether the socket is open.
  141. bool is_open(const implementation_type& impl) const
  142. {
  143. return service_impl_.is_open(impl);
  144. }
  145. /// Close a sequenced packet socket implementation.
  146. boost::system::error_code close(implementation_type& impl,
  147. boost::system::error_code& ec)
  148. {
  149. return service_impl_.close(impl, ec);
  150. }
  151. /// (Deprecated: Use native_handle().) Get the native socket implementation.
  152. native_type native(implementation_type& impl)
  153. {
  154. return service_impl_.native_handle(impl);
  155. }
  156. /// Get the native socket implementation.
  157. native_handle_type native_handle(implementation_type& impl)
  158. {
  159. return service_impl_.native_handle(impl);
  160. }
  161. /// Cancel all asynchronous operations associated with the socket.
  162. boost::system::error_code cancel(implementation_type& impl,
  163. boost::system::error_code& ec)
  164. {
  165. return service_impl_.cancel(impl, ec);
  166. }
  167. /// Determine whether the socket is at the out-of-band data mark.
  168. bool at_mark(const implementation_type& impl,
  169. boost::system::error_code& ec) const
  170. {
  171. return service_impl_.at_mark(impl, ec);
  172. }
  173. /// Determine the number of bytes available for reading.
  174. std::size_t available(const implementation_type& impl,
  175. boost::system::error_code& ec) const
  176. {
  177. return service_impl_.available(impl, ec);
  178. }
  179. /// Bind the sequenced packet socket to the specified local endpoint.
  180. boost::system::error_code bind(implementation_type& impl,
  181. const endpoint_type& endpoint, boost::system::error_code& ec)
  182. {
  183. return service_impl_.bind(impl, endpoint, ec);
  184. }
  185. /// Connect the sequenced packet socket to the specified endpoint.
  186. boost::system::error_code connect(implementation_type& impl,
  187. const endpoint_type& peer_endpoint, boost::system::error_code& ec)
  188. {
  189. return service_impl_.connect(impl, peer_endpoint, ec);
  190. }
  191. /// Start an asynchronous connect.
  192. template <typename ConnectHandler>
  193. BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
  194. void (boost::system::error_code))
  195. async_connect(implementation_type& impl,
  196. const endpoint_type& peer_endpoint,
  197. BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
  198. {
  199. detail::async_result_init<
  200. ConnectHandler, void (boost::system::error_code)> init(
  201. BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
  202. service_impl_.async_connect(impl, peer_endpoint, init.handler);
  203. return init.result.get();
  204. }
  205. /// Set a socket option.
  206. template <typename SettableSocketOption>
  207. boost::system::error_code set_option(implementation_type& impl,
  208. const SettableSocketOption& option, boost::system::error_code& ec)
  209. {
  210. return service_impl_.set_option(impl, option, ec);
  211. }
  212. /// Get a socket option.
  213. template <typename GettableSocketOption>
  214. boost::system::error_code get_option(const implementation_type& impl,
  215. GettableSocketOption& option, boost::system::error_code& ec) const
  216. {
  217. return service_impl_.get_option(impl, option, ec);
  218. }
  219. /// Perform an IO control command on the socket.
  220. template <typename IoControlCommand>
  221. boost::system::error_code io_control(implementation_type& impl,
  222. IoControlCommand& command, boost::system::error_code& ec)
  223. {
  224. return service_impl_.io_control(impl, command, ec);
  225. }
  226. /// Gets the non-blocking mode of the socket.
  227. bool non_blocking(const implementation_type& impl) const
  228. {
  229. return service_impl_.non_blocking(impl);
  230. }
  231. /// Sets the non-blocking mode of the socket.
  232. boost::system::error_code non_blocking(implementation_type& impl,
  233. bool mode, boost::system::error_code& ec)
  234. {
  235. return service_impl_.non_blocking(impl, mode, ec);
  236. }
  237. /// Gets the non-blocking mode of the native socket implementation.
  238. bool native_non_blocking(const implementation_type& impl) const
  239. {
  240. return service_impl_.native_non_blocking(impl);
  241. }
  242. /// Sets the non-blocking mode of the native socket implementation.
  243. boost::system::error_code native_non_blocking(implementation_type& impl,
  244. bool mode, boost::system::error_code& ec)
  245. {
  246. return service_impl_.native_non_blocking(impl, mode, ec);
  247. }
  248. /// Get the local endpoint.
  249. endpoint_type local_endpoint(const implementation_type& impl,
  250. boost::system::error_code& ec) const
  251. {
  252. return service_impl_.local_endpoint(impl, ec);
  253. }
  254. /// Get the remote endpoint.
  255. endpoint_type remote_endpoint(const implementation_type& impl,
  256. boost::system::error_code& ec) const
  257. {
  258. return service_impl_.remote_endpoint(impl, ec);
  259. }
  260. /// Disable sends or receives on the socket.
  261. boost::system::error_code shutdown(implementation_type& impl,
  262. socket_base::shutdown_type what, boost::system::error_code& ec)
  263. {
  264. return service_impl_.shutdown(impl, what, ec);
  265. }
  266. /// Send the given data to the peer.
  267. template <typename ConstBufferSequence>
  268. std::size_t send(implementation_type& impl,
  269. const ConstBufferSequence& buffers,
  270. socket_base::message_flags flags, boost::system::error_code& ec)
  271. {
  272. return service_impl_.send(impl, buffers, flags, ec);
  273. }
  274. /// Start an asynchronous send.
  275. template <typename ConstBufferSequence, typename WriteHandler>
  276. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  277. void (boost::system::error_code, std::size_t))
  278. async_send(implementation_type& impl,
  279. const ConstBufferSequence& buffers,
  280. socket_base::message_flags flags,
  281. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  282. {
  283. detail::async_result_init<
  284. WriteHandler, void (boost::system::error_code, std::size_t)> init(
  285. BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  286. service_impl_.async_send(impl, buffers, flags, init.handler);
  287. return init.result.get();
  288. }
  289. /// Receive some data from the peer.
  290. template <typename MutableBufferSequence>
  291. std::size_t receive(implementation_type& impl,
  292. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  293. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  294. {
  295. return service_impl_.receive_with_flags(impl,
  296. buffers, in_flags, out_flags, ec);
  297. }
  298. /// Start an asynchronous receive.
  299. template <typename MutableBufferSequence, typename ReadHandler>
  300. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  301. void (boost::system::error_code, std::size_t))
  302. async_receive(implementation_type& impl,
  303. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  304. socket_base::message_flags& out_flags,
  305. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  306. {
  307. detail::async_result_init<
  308. ReadHandler, void (boost::system::error_code, std::size_t)> init(
  309. BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  310. service_impl_.async_receive_with_flags(impl,
  311. buffers, in_flags, out_flags, init.handler);
  312. return init.result.get();
  313. }
  314. private:
  315. // Destroy all user-defined handler objects owned by the service.
  316. void shutdown_service()
  317. {
  318. service_impl_.shutdown_service();
  319. }
  320. // The platform-specific implementation.
  321. service_impl_type service_impl_;
  322. };
  323. } // namespace asio
  324. } // namespace boost
  325. #include <boost/asio/detail/pop_options.hpp>
  326. #endif // BOOST_ASIO_SEQ_PACKET_SOCKET_SERVICE_HPP