socket_base.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. //
  2. // socket_base.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_SOCKET_BASE_HPP
  11. #define BOOST_ASIO_SOCKET_BASE_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 <boost/asio/detail/io_control.hpp>
  17. #include <boost/asio/detail/socket_option.hpp>
  18. #include <boost/asio/detail/socket_types.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. /// The socket_base class is used as a base for the basic_stream_socket and
  23. /// basic_datagram_socket class templates so that we have a common place to
  24. /// define the shutdown_type and enum.
  25. class socket_base
  26. {
  27. public:
  28. /// Different ways a socket may be shutdown.
  29. enum shutdown_type
  30. {
  31. #if defined(GENERATING_DOCUMENTATION)
  32. /// Shutdown the receive side of the socket.
  33. shutdown_receive = implementation_defined,
  34. /// Shutdown the send side of the socket.
  35. shutdown_send = implementation_defined,
  36. /// Shutdown both send and receive on the socket.
  37. shutdown_both = implementation_defined
  38. #else
  39. shutdown_receive = BOOST_ASIO_OS_DEF(SHUT_RD),
  40. shutdown_send = BOOST_ASIO_OS_DEF(SHUT_WR),
  41. shutdown_both = BOOST_ASIO_OS_DEF(SHUT_RDWR)
  42. #endif
  43. };
  44. /// Bitmask type for flags that can be passed to send and receive operations.
  45. typedef int message_flags;
  46. #if defined(GENERATING_DOCUMENTATION)
  47. /// Peek at incoming data without removing it from the input queue.
  48. static const int message_peek = implementation_defined;
  49. /// Process out-of-band data.
  50. static const int message_out_of_band = implementation_defined;
  51. /// Specify that the data should not be subject to routing.
  52. static const int message_do_not_route = implementation_defined;
  53. /// Specifies that the data marks the end of a record.
  54. static const int message_end_of_record = implementation_defined;
  55. #else
  56. BOOST_ASIO_STATIC_CONSTANT(int,
  57. message_peek = BOOST_ASIO_OS_DEF(MSG_PEEK));
  58. BOOST_ASIO_STATIC_CONSTANT(int,
  59. message_out_of_band = BOOST_ASIO_OS_DEF(MSG_OOB));
  60. BOOST_ASIO_STATIC_CONSTANT(int,
  61. message_do_not_route = BOOST_ASIO_OS_DEF(MSG_DONTROUTE));
  62. BOOST_ASIO_STATIC_CONSTANT(int,
  63. message_end_of_record = BOOST_ASIO_OS_DEF(MSG_EOR));
  64. #endif
  65. /// Wait types.
  66. /**
  67. * For use with basic_socket::wait() and basic_socket::async_wait().
  68. */
  69. enum wait_type
  70. {
  71. /// Wait for a socket to become ready to read.
  72. wait_read,
  73. /// Wait for a socket to become ready to write.
  74. wait_write,
  75. /// Wait for a socket to have error conditions pending.
  76. wait_error
  77. };
  78. /// Socket option to permit sending of broadcast messages.
  79. /**
  80. * Implements the SOL_SOCKET/SO_BROADCAST socket option.
  81. *
  82. * @par Examples
  83. * Setting the option:
  84. * @code
  85. * boost::asio::ip::udp::socket socket(my_context);
  86. * ...
  87. * boost::asio::socket_base::broadcast option(true);
  88. * socket.set_option(option);
  89. * @endcode
  90. *
  91. * @par
  92. * Getting the current option value:
  93. * @code
  94. * boost::asio::ip::udp::socket socket(my_context);
  95. * ...
  96. * boost::asio::socket_base::broadcast option;
  97. * socket.get_option(option);
  98. * bool is_set = option.value();
  99. * @endcode
  100. *
  101. * @par Concepts:
  102. * Socket_Option, Boolean_Socket_Option.
  103. */
  104. #if defined(GENERATING_DOCUMENTATION)
  105. typedef implementation_defined broadcast;
  106. #else
  107. typedef boost::asio::detail::socket_option::boolean<
  108. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_BROADCAST)>
  109. broadcast;
  110. #endif
  111. /// Socket option to enable socket-level debugging.
  112. /**
  113. * Implements the SOL_SOCKET/SO_DEBUG socket option.
  114. *
  115. * @par Examples
  116. * Setting the option:
  117. * @code
  118. * boost::asio::ip::tcp::socket socket(my_context);
  119. * ...
  120. * boost::asio::socket_base::debug option(true);
  121. * socket.set_option(option);
  122. * @endcode
  123. *
  124. * @par
  125. * Getting the current option value:
  126. * @code
  127. * boost::asio::ip::tcp::socket socket(my_context);
  128. * ...
  129. * boost::asio::socket_base::debug option;
  130. * socket.get_option(option);
  131. * bool is_set = option.value();
  132. * @endcode
  133. *
  134. * @par Concepts:
  135. * Socket_Option, Boolean_Socket_Option.
  136. */
  137. #if defined(GENERATING_DOCUMENTATION)
  138. typedef implementation_defined debug;
  139. #else
  140. typedef boost::asio::detail::socket_option::boolean<
  141. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_DEBUG)> debug;
  142. #endif
  143. /// Socket option to prevent routing, use local interfaces only.
  144. /**
  145. * Implements the SOL_SOCKET/SO_DONTROUTE socket option.
  146. *
  147. * @par Examples
  148. * Setting the option:
  149. * @code
  150. * boost::asio::ip::udp::socket socket(my_context);
  151. * ...
  152. * boost::asio::socket_base::do_not_route option(true);
  153. * socket.set_option(option);
  154. * @endcode
  155. *
  156. * @par
  157. * Getting the current option value:
  158. * @code
  159. * boost::asio::ip::udp::socket socket(my_context);
  160. * ...
  161. * boost::asio::socket_base::do_not_route option;
  162. * socket.get_option(option);
  163. * bool is_set = option.value();
  164. * @endcode
  165. *
  166. * @par Concepts:
  167. * Socket_Option, Boolean_Socket_Option.
  168. */
  169. #if defined(GENERATING_DOCUMENTATION)
  170. typedef implementation_defined do_not_route;
  171. #else
  172. typedef boost::asio::detail::socket_option::boolean<
  173. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_DONTROUTE)>
  174. do_not_route;
  175. #endif
  176. /// Socket option to send keep-alives.
  177. /**
  178. * Implements the SOL_SOCKET/SO_KEEPALIVE socket option.
  179. *
  180. * @par Examples
  181. * Setting the option:
  182. * @code
  183. * boost::asio::ip::tcp::socket socket(my_context);
  184. * ...
  185. * boost::asio::socket_base::keep_alive option(true);
  186. * socket.set_option(option);
  187. * @endcode
  188. *
  189. * @par
  190. * Getting the current option value:
  191. * @code
  192. * boost::asio::ip::tcp::socket socket(my_context);
  193. * ...
  194. * boost::asio::socket_base::keep_alive option;
  195. * socket.get_option(option);
  196. * bool is_set = option.value();
  197. * @endcode
  198. *
  199. * @par Concepts:
  200. * Socket_Option, Boolean_Socket_Option.
  201. */
  202. #if defined(GENERATING_DOCUMENTATION)
  203. typedef implementation_defined keep_alive;
  204. #else
  205. typedef boost::asio::detail::socket_option::boolean<
  206. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_KEEPALIVE)> keep_alive;
  207. #endif
  208. /// Socket option for the send buffer size of a socket.
  209. /**
  210. * Implements the SOL_SOCKET/SO_SNDBUF socket option.
  211. *
  212. * @par Examples
  213. * Setting the option:
  214. * @code
  215. * boost::asio::ip::tcp::socket socket(my_context);
  216. * ...
  217. * boost::asio::socket_base::send_buffer_size option(8192);
  218. * socket.set_option(option);
  219. * @endcode
  220. *
  221. * @par
  222. * Getting the current option value:
  223. * @code
  224. * boost::asio::ip::tcp::socket socket(my_context);
  225. * ...
  226. * boost::asio::socket_base::send_buffer_size option;
  227. * socket.get_option(option);
  228. * int size = option.value();
  229. * @endcode
  230. *
  231. * @par Concepts:
  232. * Socket_Option, Integer_Socket_Option.
  233. */
  234. #if defined(GENERATING_DOCUMENTATION)
  235. typedef implementation_defined send_buffer_size;
  236. #else
  237. typedef boost::asio::detail::socket_option::integer<
  238. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_SNDBUF)>
  239. send_buffer_size;
  240. #endif
  241. /// Socket option for the send low watermark.
  242. /**
  243. * Implements the SOL_SOCKET/SO_SNDLOWAT socket option.
  244. *
  245. * @par Examples
  246. * Setting the option:
  247. * @code
  248. * boost::asio::ip::tcp::socket socket(my_context);
  249. * ...
  250. * boost::asio::socket_base::send_low_watermark option(1024);
  251. * socket.set_option(option);
  252. * @endcode
  253. *
  254. * @par
  255. * Getting the current option value:
  256. * @code
  257. * boost::asio::ip::tcp::socket socket(my_context);
  258. * ...
  259. * boost::asio::socket_base::send_low_watermark option;
  260. * socket.get_option(option);
  261. * int size = option.value();
  262. * @endcode
  263. *
  264. * @par Concepts:
  265. * Socket_Option, Integer_Socket_Option.
  266. */
  267. #if defined(GENERATING_DOCUMENTATION)
  268. typedef implementation_defined send_low_watermark;
  269. #else
  270. typedef boost::asio::detail::socket_option::integer<
  271. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_SNDLOWAT)>
  272. send_low_watermark;
  273. #endif
  274. /// Socket option for the receive buffer size of a socket.
  275. /**
  276. * Implements the SOL_SOCKET/SO_RCVBUF socket option.
  277. *
  278. * @par Examples
  279. * Setting the option:
  280. * @code
  281. * boost::asio::ip::tcp::socket socket(my_context);
  282. * ...
  283. * boost::asio::socket_base::receive_buffer_size option(8192);
  284. * socket.set_option(option);
  285. * @endcode
  286. *
  287. * @par
  288. * Getting the current option value:
  289. * @code
  290. * boost::asio::ip::tcp::socket socket(my_context);
  291. * ...
  292. * boost::asio::socket_base::receive_buffer_size option;
  293. * socket.get_option(option);
  294. * int size = option.value();
  295. * @endcode
  296. *
  297. * @par Concepts:
  298. * Socket_Option, Integer_Socket_Option.
  299. */
  300. #if defined(GENERATING_DOCUMENTATION)
  301. typedef implementation_defined receive_buffer_size;
  302. #else
  303. typedef boost::asio::detail::socket_option::integer<
  304. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_RCVBUF)>
  305. receive_buffer_size;
  306. #endif
  307. /// Socket option for the receive low watermark.
  308. /**
  309. * Implements the SOL_SOCKET/SO_RCVLOWAT socket option.
  310. *
  311. * @par Examples
  312. * Setting the option:
  313. * @code
  314. * boost::asio::ip::tcp::socket socket(my_context);
  315. * ...
  316. * boost::asio::socket_base::receive_low_watermark option(1024);
  317. * socket.set_option(option);
  318. * @endcode
  319. *
  320. * @par
  321. * Getting the current option value:
  322. * @code
  323. * boost::asio::ip::tcp::socket socket(my_context);
  324. * ...
  325. * boost::asio::socket_base::receive_low_watermark option;
  326. * socket.get_option(option);
  327. * int size = option.value();
  328. * @endcode
  329. *
  330. * @par Concepts:
  331. * Socket_Option, Integer_Socket_Option.
  332. */
  333. #if defined(GENERATING_DOCUMENTATION)
  334. typedef implementation_defined receive_low_watermark;
  335. #else
  336. typedef boost::asio::detail::socket_option::integer<
  337. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_RCVLOWAT)>
  338. receive_low_watermark;
  339. #endif
  340. /// Socket option to allow the socket to be bound to an address that is
  341. /// already in use.
  342. /**
  343. * Implements the SOL_SOCKET/SO_REUSEADDR socket option.
  344. *
  345. * @par Examples
  346. * Setting the option:
  347. * @code
  348. * boost::asio::ip::tcp::acceptor acceptor(my_context);
  349. * ...
  350. * boost::asio::socket_base::reuse_address option(true);
  351. * acceptor.set_option(option);
  352. * @endcode
  353. *
  354. * @par
  355. * Getting the current option value:
  356. * @code
  357. * boost::asio::ip::tcp::acceptor acceptor(my_context);
  358. * ...
  359. * boost::asio::socket_base::reuse_address option;
  360. * acceptor.get_option(option);
  361. * bool is_set = option.value();
  362. * @endcode
  363. *
  364. * @par Concepts:
  365. * Socket_Option, Boolean_Socket_Option.
  366. */
  367. #if defined(GENERATING_DOCUMENTATION)
  368. typedef implementation_defined reuse_address;
  369. #else
  370. typedef boost::asio::detail::socket_option::boolean<
  371. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_REUSEADDR)>
  372. reuse_address;
  373. #endif
  374. /// Socket option to specify whether the socket lingers on close if unsent
  375. /// data is present.
  376. /**
  377. * Implements the SOL_SOCKET/SO_LINGER socket option.
  378. *
  379. * @par Examples
  380. * Setting the option:
  381. * @code
  382. * boost::asio::ip::tcp::socket socket(my_context);
  383. * ...
  384. * boost::asio::socket_base::linger option(true, 30);
  385. * socket.set_option(option);
  386. * @endcode
  387. *
  388. * @par
  389. * Getting the current option value:
  390. * @code
  391. * boost::asio::ip::tcp::socket socket(my_context);
  392. * ...
  393. * boost::asio::socket_base::linger option;
  394. * socket.get_option(option);
  395. * bool is_set = option.enabled();
  396. * unsigned short timeout = option.timeout();
  397. * @endcode
  398. *
  399. * @par Concepts:
  400. * Socket_Option, Linger_Socket_Option.
  401. */
  402. #if defined(GENERATING_DOCUMENTATION)
  403. typedef implementation_defined linger;
  404. #else
  405. typedef boost::asio::detail::socket_option::linger<
  406. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_LINGER)>
  407. linger;
  408. #endif
  409. /// Socket option for putting received out-of-band data inline.
  410. /**
  411. * Implements the SOL_SOCKET/SO_OOBINLINE socket option.
  412. *
  413. * @par Examples
  414. * Setting the option:
  415. * @code
  416. * boost::asio::ip::tcp::socket socket(my_context);
  417. * ...
  418. * boost::asio::socket_base::out_of_band_inline option(true);
  419. * socket.set_option(option);
  420. * @endcode
  421. *
  422. * @par
  423. * Getting the current option value:
  424. * @code
  425. * boost::asio::ip::tcp::socket socket(my_context);
  426. * ...
  427. * boost::asio::socket_base::out_of_band_inline option;
  428. * socket.get_option(option);
  429. * bool value = option.value();
  430. * @endcode
  431. *
  432. * @par Concepts:
  433. * Socket_Option, Boolean_Socket_Option.
  434. */
  435. #if defined(GENERATING_DOCUMENTATION)
  436. typedef implementation_defined out_of_band_inline;
  437. #else
  438. typedef boost::asio::detail::socket_option::boolean<
  439. BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_OOBINLINE)>
  440. out_of_band_inline;
  441. #endif
  442. /// Socket option to report aborted connections on accept.
  443. /**
  444. * Implements a custom socket option that determines whether or not an accept
  445. * operation is permitted to fail with boost::asio::error::connection_aborted.
  446. * By default the option is false.
  447. *
  448. * @par Examples
  449. * Setting the option:
  450. * @code
  451. * boost::asio::ip::tcp::acceptor acceptor(my_context);
  452. * ...
  453. * boost::asio::socket_base::enable_connection_aborted option(true);
  454. * acceptor.set_option(option);
  455. * @endcode
  456. *
  457. * @par
  458. * Getting the current option value:
  459. * @code
  460. * boost::asio::ip::tcp::acceptor acceptor(my_context);
  461. * ...
  462. * boost::asio::socket_base::enable_connection_aborted option;
  463. * acceptor.get_option(option);
  464. * bool is_set = option.value();
  465. * @endcode
  466. *
  467. * @par Concepts:
  468. * Socket_Option, Boolean_Socket_Option.
  469. */
  470. #if defined(GENERATING_DOCUMENTATION)
  471. typedef implementation_defined enable_connection_aborted;
  472. #else
  473. typedef boost::asio::detail::socket_option::boolean<
  474. boost::asio::detail::custom_socket_option_level,
  475. boost::asio::detail::enable_connection_aborted_option>
  476. enable_connection_aborted;
  477. #endif
  478. /// IO control command to get the amount of data that can be read without
  479. /// blocking.
  480. /**
  481. * Implements the FIONREAD IO control command.
  482. *
  483. * @par Example
  484. * @code
  485. * boost::asio::ip::tcp::socket socket(my_context);
  486. * ...
  487. * boost::asio::socket_base::bytes_readable command(true);
  488. * socket.io_control(command);
  489. * std::size_t bytes_readable = command.get();
  490. * @endcode
  491. *
  492. * @par Concepts:
  493. * IO_Control_Command, Size_IO_Control_Command.
  494. */
  495. #if defined(GENERATING_DOCUMENTATION)
  496. typedef implementation_defined bytes_readable;
  497. #else
  498. typedef boost::asio::detail::io_control::bytes_readable bytes_readable;
  499. #endif
  500. /// The maximum length of the queue of pending incoming connections.
  501. #if defined(GENERATING_DOCUMENTATION)
  502. static const int max_listen_connections = implementation_defined;
  503. #else
  504. BOOST_ASIO_STATIC_CONSTANT(int, max_listen_connections
  505. = BOOST_ASIO_OS_DEF(SOMAXCONN));
  506. #endif
  507. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  508. /// (Deprecated: Use max_listen_connections.) The maximum length of the queue
  509. /// of pending incoming connections.
  510. #if defined(GENERATING_DOCUMENTATION)
  511. static const int max_connections = implementation_defined;
  512. #else
  513. BOOST_ASIO_STATIC_CONSTANT(int, max_connections
  514. = BOOST_ASIO_OS_DEF(SOMAXCONN));
  515. #endif
  516. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  517. protected:
  518. /// Protected destructor to prevent deletion through this type.
  519. ~socket_base()
  520. {
  521. }
  522. };
  523. } // namespace asio
  524. } // namespace boost
  525. #include <boost/asio/detail/pop_options.hpp>
  526. #endif // BOOST_ASIO_SOCKET_BASE_HPP