seq_packet_socket_service.hpp 11 KB

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