raw_socket_service.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // raw_socket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include "asio/detail/pop_options.hpp"
  20. #include "asio/error.hpp"
  21. #include "asio/io_service.hpp"
  22. #include "asio/detail/service_base.hpp"
  23. #if 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. namespace asio {
  29. /// Default service implementation for a raw socket.
  30. template <typename Protocol>
  31. class raw_socket_service
  32. #if defined(GENERATING_DOCUMENTATION)
  33. : public asio::io_service::service
  34. #else
  35. : public asio::detail::service_base<raw_socket_service<Protocol> >
  36. #endif
  37. {
  38. public:
  39. #if defined(GENERATING_DOCUMENTATION)
  40. /// The unique service identifier.
  41. static asio::io_service::id id;
  42. #endif
  43. /// The protocol type.
  44. typedef Protocol protocol_type;
  45. /// The endpoint type.
  46. typedef typename Protocol::endpoint endpoint_type;
  47. private:
  48. // The type of the platform-specific implementation.
  49. #if defined(ASIO_HAS_IOCP)
  50. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  51. #else
  52. typedef detail::reactive_socket_service<Protocol> service_impl_type;
  53. #endif
  54. public:
  55. /// The type of a raw socket.
  56. #if defined(GENERATING_DOCUMENTATION)
  57. typedef implementation_defined implementation_type;
  58. #else
  59. typedef typename service_impl_type::implementation_type implementation_type;
  60. #endif
  61. /// The native socket type.
  62. #if defined(GENERATING_DOCUMENTATION)
  63. typedef implementation_defined native_type;
  64. #else
  65. typedef typename service_impl_type::native_type native_type;
  66. #endif
  67. /// Construct a new raw socket service for the specified io_service.
  68. explicit raw_socket_service(asio::io_service& io_service)
  69. : asio::detail::service_base<
  70. raw_socket_service<Protocol> >(io_service),
  71. service_impl_(io_service)
  72. {
  73. }
  74. /// Destroy all user-defined handler objects owned by the service.
  75. void shutdown_service()
  76. {
  77. service_impl_.shutdown_service();
  78. }
  79. /// Construct a new raw socket implementation.
  80. void construct(implementation_type& impl)
  81. {
  82. service_impl_.construct(impl);
  83. }
  84. /// Destroy a raw socket implementation.
  85. void destroy(implementation_type& impl)
  86. {
  87. service_impl_.destroy(impl);
  88. }
  89. // Open a new raw socket implementation.
  90. asio::error_code open(implementation_type& impl,
  91. const protocol_type& protocol, asio::error_code& ec)
  92. {
  93. if (protocol.type() == SOCK_RAW)
  94. service_impl_.open(impl, protocol, ec);
  95. else
  96. ec = asio::error::invalid_argument;
  97. return ec;
  98. }
  99. /// Assign an existing native socket to a raw socket.
  100. asio::error_code assign(implementation_type& impl,
  101. const protocol_type& protocol, const native_type& native_socket,
  102. asio::error_code& ec)
  103. {
  104. return service_impl_.assign(impl, protocol, native_socket, ec);
  105. }
  106. /// Determine whether the socket is open.
  107. bool is_open(const implementation_type& impl) const
  108. {
  109. return service_impl_.is_open(impl);
  110. }
  111. /// Close a raw socket implementation.
  112. asio::error_code close(implementation_type& impl,
  113. asio::error_code& ec)
  114. {
  115. return service_impl_.close(impl, ec);
  116. }
  117. /// Get the native socket implementation.
  118. native_type native(implementation_type& impl)
  119. {
  120. return service_impl_.native(impl);
  121. }
  122. /// Cancel all asynchronous operations associated with the socket.
  123. asio::error_code cancel(implementation_type& impl,
  124. asio::error_code& ec)
  125. {
  126. return service_impl_.cancel(impl, ec);
  127. }
  128. /// Determine whether the socket is at the out-of-band data mark.
  129. bool at_mark(const implementation_type& impl,
  130. asio::error_code& ec) const
  131. {
  132. return service_impl_.at_mark(impl, ec);
  133. }
  134. /// Determine the number of bytes available for reading.
  135. std::size_t available(const implementation_type& impl,
  136. asio::error_code& ec) const
  137. {
  138. return service_impl_.available(impl, ec);
  139. }
  140. // Bind the raw socket to the specified local endpoint.
  141. asio::error_code bind(implementation_type& impl,
  142. const endpoint_type& endpoint, asio::error_code& ec)
  143. {
  144. return service_impl_.bind(impl, endpoint, ec);
  145. }
  146. /// Connect the raw socket to the specified endpoint.
  147. asio::error_code connect(implementation_type& impl,
  148. const endpoint_type& peer_endpoint, asio::error_code& ec)
  149. {
  150. return service_impl_.connect(impl, peer_endpoint, ec);
  151. }
  152. /// Start an asynchronous connect.
  153. template <typename ConnectHandler>
  154. void async_connect(implementation_type& impl,
  155. const endpoint_type& peer_endpoint, ConnectHandler handler)
  156. {
  157. service_impl_.async_connect(impl, peer_endpoint, handler);
  158. }
  159. /// Set a socket option.
  160. template <typename SettableSocketOption>
  161. asio::error_code set_option(implementation_type& impl,
  162. const SettableSocketOption& option, asio::error_code& ec)
  163. {
  164. return service_impl_.set_option(impl, option, ec);
  165. }
  166. /// Get a socket option.
  167. template <typename GettableSocketOption>
  168. asio::error_code get_option(const implementation_type& impl,
  169. GettableSocketOption& option, asio::error_code& ec) const
  170. {
  171. return service_impl_.get_option(impl, option, ec);
  172. }
  173. /// Perform an IO control command on the socket.
  174. template <typename IoControlCommand>
  175. asio::error_code io_control(implementation_type& impl,
  176. IoControlCommand& command, asio::error_code& ec)
  177. {
  178. return service_impl_.io_control(impl, command, ec);
  179. }
  180. /// Get the local endpoint.
  181. endpoint_type local_endpoint(const implementation_type& impl,
  182. asio::error_code& ec) const
  183. {
  184. return service_impl_.local_endpoint(impl, ec);
  185. }
  186. /// Get the remote endpoint.
  187. endpoint_type remote_endpoint(const implementation_type& impl,
  188. asio::error_code& ec) const
  189. {
  190. return service_impl_.remote_endpoint(impl, ec);
  191. }
  192. /// Disable sends or receives on the socket.
  193. asio::error_code shutdown(implementation_type& impl,
  194. socket_base::shutdown_type what, asio::error_code& ec)
  195. {
  196. return service_impl_.shutdown(impl, what, ec);
  197. }
  198. /// Send the given data to the peer.
  199. template <typename ConstBufferSequence>
  200. std::size_t send(implementation_type& impl,
  201. const ConstBufferSequence& buffers,
  202. socket_base::message_flags flags, asio::error_code& ec)
  203. {
  204. return service_impl_.send(impl, buffers, flags, ec);
  205. }
  206. /// Start an asynchronous send.
  207. template <typename ConstBufferSequence, typename WriteHandler>
  208. void async_send(implementation_type& impl, const ConstBufferSequence& buffers,
  209. socket_base::message_flags flags, WriteHandler handler)
  210. {
  211. service_impl_.async_send(impl, buffers, flags, handler);
  212. }
  213. /// Send raw data to the specified endpoint.
  214. template <typename ConstBufferSequence>
  215. std::size_t send_to(implementation_type& impl,
  216. const ConstBufferSequence& buffers, const endpoint_type& destination,
  217. socket_base::message_flags flags, asio::error_code& ec)
  218. {
  219. return service_impl_.send_to(impl, buffers, destination, flags, ec);
  220. }
  221. /// Start an asynchronous send.
  222. template <typename ConstBufferSequence, typename WriteHandler>
  223. void async_send_to(implementation_type& impl,
  224. const ConstBufferSequence& buffers, const endpoint_type& destination,
  225. socket_base::message_flags flags, WriteHandler handler)
  226. {
  227. service_impl_.async_send_to(impl, buffers, destination, flags, handler);
  228. }
  229. /// Receive some data from the peer.
  230. template <typename MutableBufferSequence>
  231. std::size_t receive(implementation_type& impl,
  232. const MutableBufferSequence& buffers,
  233. socket_base::message_flags flags, asio::error_code& ec)
  234. {
  235. return service_impl_.receive(impl, buffers, flags, ec);
  236. }
  237. /// Start an asynchronous receive.
  238. template <typename MutableBufferSequence, typename ReadHandler>
  239. void async_receive(implementation_type& impl,
  240. const MutableBufferSequence& buffers,
  241. socket_base::message_flags flags, ReadHandler handler)
  242. {
  243. service_impl_.async_receive(impl, buffers, flags, handler);
  244. }
  245. /// Receive raw data with the endpoint of the sender.
  246. template <typename MutableBufferSequence>
  247. std::size_t receive_from(implementation_type& impl,
  248. const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
  249. socket_base::message_flags flags, asio::error_code& ec)
  250. {
  251. return service_impl_.receive_from(impl, buffers, sender_endpoint, flags,
  252. ec);
  253. }
  254. /// Start an asynchronous receive that will get the endpoint of the sender.
  255. template <typename MutableBufferSequence, typename ReadHandler>
  256. void async_receive_from(implementation_type& impl,
  257. const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
  258. socket_base::message_flags flags, ReadHandler handler)
  259. {
  260. service_impl_.async_receive_from(impl, buffers, sender_endpoint, flags,
  261. handler);
  262. }
  263. private:
  264. // The platform-specific implementation.
  265. service_impl_type service_impl_;
  266. };
  267. } // namespace asio
  268. #include "asio/detail/pop_options.hpp"
  269. #endif // ASIO_RAW_SOCKET_SERVICE_HPP