null_socket_service.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //
  2. // detail/null_socket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_DETAIL_NULL_SOCKET_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_NULL_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. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  17. #include <boost/asio/buffer.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/io_context.hpp>
  20. #include <boost/asio/socket_base.hpp>
  21. #include <boost/asio/detail/bind_handler.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. template <typename Protocol>
  27. class null_socket_service :
  28. public service_base<null_socket_service<Protocol> >
  29. {
  30. public:
  31. // The protocol type.
  32. typedef Protocol protocol_type;
  33. // The endpoint type.
  34. typedef typename Protocol::endpoint endpoint_type;
  35. // The native type of a socket.
  36. typedef int native_handle_type;
  37. // The implementation type of the socket.
  38. struct implementation_type
  39. {
  40. };
  41. // Constructor.
  42. null_socket_service(boost::asio::io_context& io_context)
  43. : service_base<null_socket_service<Protocol> >(io_context),
  44. io_context_(io_context)
  45. {
  46. }
  47. // Destroy all user-defined handler objects owned by the service.
  48. void shutdown()
  49. {
  50. }
  51. // Construct a new socket implementation.
  52. void construct(implementation_type&)
  53. {
  54. }
  55. // Move-construct a new socket implementation.
  56. void move_construct(implementation_type&, implementation_type&)
  57. {
  58. }
  59. // Move-assign from another socket implementation.
  60. void move_assign(implementation_type&,
  61. null_socket_service&, implementation_type&)
  62. {
  63. }
  64. // Move-construct a new socket implementation from another protocol type.
  65. template <typename Protocol1>
  66. void converting_move_construct(implementation_type&,
  67. null_socket_service<Protocol1>&,
  68. typename null_socket_service<Protocol1>::implementation_type&)
  69. {
  70. }
  71. // Destroy a socket implementation.
  72. void destroy(implementation_type&)
  73. {
  74. }
  75. // Open a new socket implementation.
  76. boost::system::error_code open(implementation_type&,
  77. const protocol_type&, boost::system::error_code& ec)
  78. {
  79. ec = boost::asio::error::operation_not_supported;
  80. return ec;
  81. }
  82. // Assign a native socket to a socket implementation.
  83. boost::system::error_code assign(implementation_type&, const protocol_type&,
  84. const native_handle_type&, boost::system::error_code& ec)
  85. {
  86. ec = boost::asio::error::operation_not_supported;
  87. return ec;
  88. }
  89. // Determine whether the socket is open.
  90. bool is_open(const implementation_type&) const
  91. {
  92. return false;
  93. }
  94. // Destroy a socket implementation.
  95. boost::system::error_code close(implementation_type&,
  96. boost::system::error_code& ec)
  97. {
  98. ec = boost::asio::error::operation_not_supported;
  99. return ec;
  100. }
  101. // Release ownership of the socket.
  102. native_handle_type release(implementation_type&,
  103. boost::system::error_code& ec)
  104. {
  105. ec = boost::asio::error::operation_not_supported;
  106. return 0;
  107. }
  108. // Get the native socket representation.
  109. native_handle_type native_handle(implementation_type&)
  110. {
  111. return 0;
  112. }
  113. // Cancel all operations associated with the socket.
  114. boost::system::error_code cancel(implementation_type&,
  115. boost::system::error_code& ec)
  116. {
  117. ec = boost::asio::error::operation_not_supported;
  118. return ec;
  119. }
  120. // Determine whether the socket is at the out-of-band data mark.
  121. bool at_mark(const implementation_type&,
  122. boost::system::error_code& ec) const
  123. {
  124. ec = boost::asio::error::operation_not_supported;
  125. return false;
  126. }
  127. // Determine the number of bytes available for reading.
  128. std::size_t available(const implementation_type&,
  129. boost::system::error_code& ec) const
  130. {
  131. ec = boost::asio::error::operation_not_supported;
  132. return 0;
  133. }
  134. // Place the socket into the state where it will listen for new connections.
  135. boost::system::error_code listen(implementation_type&,
  136. int, boost::system::error_code& ec)
  137. {
  138. ec = boost::asio::error::operation_not_supported;
  139. return ec;
  140. }
  141. // Perform an IO control command on the socket.
  142. template <typename IO_Control_Command>
  143. boost::system::error_code io_control(implementation_type&,
  144. IO_Control_Command&, boost::system::error_code& ec)
  145. {
  146. ec = boost::asio::error::operation_not_supported;
  147. return ec;
  148. }
  149. // Gets the non-blocking mode of the socket.
  150. bool non_blocking(const implementation_type&) const
  151. {
  152. return false;
  153. }
  154. // Sets the non-blocking mode of the socket.
  155. boost::system::error_code non_blocking(implementation_type&,
  156. bool, boost::system::error_code& ec)
  157. {
  158. ec = boost::asio::error::operation_not_supported;
  159. return ec;
  160. }
  161. // Gets the non-blocking mode of the native socket implementation.
  162. bool native_non_blocking(const implementation_type&) const
  163. {
  164. return false;
  165. }
  166. // Sets the non-blocking mode of the native socket implementation.
  167. boost::system::error_code native_non_blocking(implementation_type&,
  168. bool, boost::system::error_code& ec)
  169. {
  170. ec = boost::asio::error::operation_not_supported;
  171. return ec;
  172. }
  173. // Disable sends or receives on the socket.
  174. boost::system::error_code shutdown(implementation_type&,
  175. socket_base::shutdown_type, boost::system::error_code& ec)
  176. {
  177. ec = boost::asio::error::operation_not_supported;
  178. return ec;
  179. }
  180. // Bind the socket to the specified local endpoint.
  181. boost::system::error_code bind(implementation_type&,
  182. const endpoint_type&, boost::system::error_code& ec)
  183. {
  184. ec = boost::asio::error::operation_not_supported;
  185. return ec;
  186. }
  187. // Set a socket option.
  188. template <typename Option>
  189. boost::system::error_code set_option(implementation_type&,
  190. const Option&, boost::system::error_code& ec)
  191. {
  192. ec = boost::asio::error::operation_not_supported;
  193. return ec;
  194. }
  195. // Set a socket option.
  196. template <typename Option>
  197. boost::system::error_code get_option(const implementation_type&,
  198. Option&, boost::system::error_code& ec) const
  199. {
  200. ec = boost::asio::error::operation_not_supported;
  201. return ec;
  202. }
  203. // Get the local endpoint.
  204. endpoint_type local_endpoint(const implementation_type&,
  205. boost::system::error_code& ec) const
  206. {
  207. ec = boost::asio::error::operation_not_supported;
  208. return endpoint_type();
  209. }
  210. // Get the remote endpoint.
  211. endpoint_type remote_endpoint(const implementation_type&,
  212. boost::system::error_code& ec) const
  213. {
  214. ec = boost::asio::error::operation_not_supported;
  215. return endpoint_type();
  216. }
  217. // Send the given data to the peer.
  218. template <typename ConstBufferSequence>
  219. std::size_t send(implementation_type&, const ConstBufferSequence&,
  220. socket_base::message_flags, boost::system::error_code& ec)
  221. {
  222. ec = boost::asio::error::operation_not_supported;
  223. return 0;
  224. }
  225. // Wait until data can be sent without blocking.
  226. std::size_t send(implementation_type&, const null_buffers&,
  227. socket_base::message_flags, boost::system::error_code& ec)
  228. {
  229. ec = boost::asio::error::operation_not_supported;
  230. return 0;
  231. }
  232. // Start an asynchronous send. The data being sent must be valid for the
  233. // lifetime of the asynchronous operation.
  234. template <typename ConstBufferSequence, typename Handler>
  235. void async_send(implementation_type&, const ConstBufferSequence&,
  236. socket_base::message_flags, Handler& handler)
  237. {
  238. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  239. const std::size_t bytes_transferred = 0;
  240. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  241. }
  242. // Start an asynchronous wait until data can be sent without blocking.
  243. template <typename Handler>
  244. void async_send(implementation_type&, const null_buffers&,
  245. socket_base::message_flags, Handler& handler)
  246. {
  247. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  248. const std::size_t bytes_transferred = 0;
  249. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  250. }
  251. // Receive some data from the peer. Returns the number of bytes received.
  252. template <typename MutableBufferSequence>
  253. std::size_t receive(implementation_type&, const MutableBufferSequence&,
  254. socket_base::message_flags, boost::system::error_code& ec)
  255. {
  256. ec = boost::asio::error::operation_not_supported;
  257. return 0;
  258. }
  259. // Wait until data can be received without blocking.
  260. std::size_t receive(implementation_type&, const null_buffers&,
  261. socket_base::message_flags, boost::system::error_code& ec)
  262. {
  263. ec = boost::asio::error::operation_not_supported;
  264. return 0;
  265. }
  266. // Start an asynchronous receive. The buffer for the data being received
  267. // must be valid for the lifetime of the asynchronous operation.
  268. template <typename MutableBufferSequence, typename Handler>
  269. void async_receive(implementation_type&, const MutableBufferSequence&,
  270. socket_base::message_flags, Handler& handler)
  271. {
  272. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  273. const std::size_t bytes_transferred = 0;
  274. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  275. }
  276. // Wait until data can be received without blocking.
  277. template <typename Handler>
  278. void async_receive(implementation_type&, const null_buffers&,
  279. socket_base::message_flags, Handler& handler)
  280. {
  281. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  282. const std::size_t bytes_transferred = 0;
  283. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  284. }
  285. // Receive some data with associated flags. Returns the number of bytes
  286. // received.
  287. template <typename MutableBufferSequence>
  288. std::size_t receive_with_flags(implementation_type&,
  289. const MutableBufferSequence&, socket_base::message_flags,
  290. socket_base::message_flags&, boost::system::error_code& ec)
  291. {
  292. ec = boost::asio::error::operation_not_supported;
  293. return 0;
  294. }
  295. // Wait until data can be received without blocking.
  296. std::size_t receive_with_flags(implementation_type&,
  297. const null_buffers&, socket_base::message_flags,
  298. socket_base::message_flags&, boost::system::error_code& ec)
  299. {
  300. ec = boost::asio::error::operation_not_supported;
  301. return 0;
  302. }
  303. // Start an asynchronous receive. The buffer for the data being received
  304. // must be valid for the lifetime of the asynchronous operation.
  305. template <typename MutableBufferSequence, typename Handler>
  306. void async_receive_with_flags(implementation_type&,
  307. const MutableBufferSequence&, socket_base::message_flags,
  308. socket_base::message_flags&, Handler& handler)
  309. {
  310. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  311. const std::size_t bytes_transferred = 0;
  312. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  313. }
  314. // Wait until data can be received without blocking.
  315. template <typename Handler>
  316. void async_receive_with_flags(implementation_type&,
  317. const null_buffers&, socket_base::message_flags,
  318. socket_base::message_flags&, Handler& handler)
  319. {
  320. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  321. const std::size_t bytes_transferred = 0;
  322. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  323. }
  324. // Send a datagram to the specified endpoint. Returns the number of bytes
  325. // sent.
  326. template <typename ConstBufferSequence>
  327. std::size_t send_to(implementation_type&, const ConstBufferSequence&,
  328. const endpoint_type&, socket_base::message_flags,
  329. boost::system::error_code& ec)
  330. {
  331. ec = boost::asio::error::operation_not_supported;
  332. return 0;
  333. }
  334. // Wait until data can be sent without blocking.
  335. std::size_t send_to(implementation_type&, const null_buffers&,
  336. const endpoint_type&, socket_base::message_flags,
  337. boost::system::error_code& ec)
  338. {
  339. ec = boost::asio::error::operation_not_supported;
  340. return 0;
  341. }
  342. // Start an asynchronous send. The data being sent must be valid for the
  343. // lifetime of the asynchronous operation.
  344. template <typename ConstBufferSequence, typename Handler>
  345. void async_send_to(implementation_type&, const ConstBufferSequence&,
  346. const endpoint_type&, socket_base::message_flags,
  347. Handler& handler)
  348. {
  349. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  350. const std::size_t bytes_transferred = 0;
  351. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  352. }
  353. // Start an asynchronous wait until data can be sent without blocking.
  354. template <typename Handler>
  355. void async_send_to(implementation_type&, const null_buffers&,
  356. const endpoint_type&, socket_base::message_flags, Handler& handler)
  357. {
  358. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  359. const std::size_t bytes_transferred = 0;
  360. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  361. }
  362. // Receive a datagram with the endpoint of the sender. Returns the number of
  363. // bytes received.
  364. template <typename MutableBufferSequence>
  365. std::size_t receive_from(implementation_type&, const MutableBufferSequence&,
  366. endpoint_type&, socket_base::message_flags,
  367. boost::system::error_code& ec)
  368. {
  369. ec = boost::asio::error::operation_not_supported;
  370. return 0;
  371. }
  372. // Wait until data can be received without blocking.
  373. std::size_t receive_from(implementation_type&, const null_buffers&,
  374. endpoint_type&, socket_base::message_flags,
  375. boost::system::error_code& ec)
  376. {
  377. ec = boost::asio::error::operation_not_supported;
  378. return 0;
  379. }
  380. // Start an asynchronous receive. The buffer for the data being received and
  381. // the sender_endpoint object must both be valid for the lifetime of the
  382. // asynchronous operation.
  383. template <typename MutableBufferSequence, typename Handler>
  384. void async_receive_from(implementation_type&,
  385. const MutableBufferSequence&, endpoint_type&,
  386. socket_base::message_flags, Handler& handler)
  387. {
  388. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  389. const std::size_t bytes_transferred = 0;
  390. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  391. }
  392. // Wait until data can be received without blocking.
  393. template <typename Handler>
  394. void async_receive_from(implementation_type&,
  395. const null_buffers&, endpoint_type&,
  396. socket_base::message_flags, Handler& handler)
  397. {
  398. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  399. const std::size_t bytes_transferred = 0;
  400. io_context_.post(detail::bind_handler(handler, ec, bytes_transferred));
  401. }
  402. // Accept a new connection.
  403. template <typename Socket>
  404. boost::system::error_code accept(implementation_type&,
  405. Socket&, endpoint_type*, boost::system::error_code& ec)
  406. {
  407. ec = boost::asio::error::operation_not_supported;
  408. return ec;
  409. }
  410. // Start an asynchronous accept. The peer and peer_endpoint objects
  411. // must be valid until the accept's handler is invoked.
  412. template <typename Socket, typename Handler>
  413. void async_accept(implementation_type&, Socket&,
  414. endpoint_type*, Handler& handler)
  415. {
  416. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  417. io_context_.post(detail::bind_handler(handler, ec));
  418. }
  419. // Connect the socket to the specified endpoint.
  420. boost::system::error_code connect(implementation_type&,
  421. const endpoint_type&, boost::system::error_code& ec)
  422. {
  423. ec = boost::asio::error::operation_not_supported;
  424. return ec;
  425. }
  426. // Start an asynchronous connect.
  427. template <typename Handler>
  428. void async_connect(implementation_type&,
  429. const endpoint_type&, Handler& handler)
  430. {
  431. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  432. io_context_.post(detail::bind_handler(handler, ec));
  433. }
  434. private:
  435. boost::asio::io_context& io_context_;
  436. };
  437. } // namespace detail
  438. } // namespace asio
  439. } // namespace boost
  440. #include <boost/asio/detail/pop_options.hpp>
  441. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  442. #endif // BOOST_ASIO_DETAIL_NULL_SOCKET_SERVICE_HPP