Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

515 lines
14KB

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