raw_socket_service.hpp 13 KB

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