PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/raw_socket_service.hpp

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