PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/include/asio/socket_base.hpp

https://github.com/afrancois/Cinder
C++ Header | 559 lines | 158 code | 35 blank | 366 comment | 1 complexity | 9587d1eec3d246c5310238a3f26f0c0e MD5 | raw file
  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 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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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(my_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 for putting received out-of-band data inline.
  409. /**
  410. * Implements the SOL_SOCKET/SO_OOBINLINE socket option.
  411. *
  412. * @par Examples
  413. * Setting the option:
  414. * @code
  415. * asio::ip::tcp::socket socket(my_context);
  416. * ...
  417. * asio::socket_base::out_of_band_inline option(true);
  418. * socket.set_option(option);
  419. * @endcode
  420. *
  421. * @par
  422. * Getting the current option value:
  423. * @code
  424. * asio::ip::tcp::socket socket(my_context);
  425. * ...
  426. * asio::socket_base::out_of_band_inline option;
  427. * socket.get_option(option);
  428. * bool value = option.value();
  429. * @endcode
  430. *
  431. * @par Concepts:
  432. * Socket_Option, Boolean_Socket_Option.
  433. */
  434. #if defined(GENERATING_DOCUMENTATION)
  435. typedef implementation_defined out_of_band_inline;
  436. #else
  437. typedef asio::detail::socket_option::boolean<
  438. ASIO_OS_DEF(SOL_SOCKET), ASIO_OS_DEF(SO_OOBINLINE)>
  439. out_of_band_inline;
  440. #endif
  441. /// Socket option to report aborted connections on accept.
  442. /**
  443. * Implements a custom socket option that determines whether or not an accept
  444. * operation is permitted to fail with asio::error::connection_aborted.
  445. * By default the option is false.
  446. *
  447. * @par Examples
  448. * Setting the option:
  449. * @code
  450. * asio::ip::tcp::acceptor acceptor(my_context);
  451. * ...
  452. * asio::socket_base::enable_connection_aborted option(true);
  453. * acceptor.set_option(option);
  454. * @endcode
  455. *
  456. * @par
  457. * Getting the current option value:
  458. * @code
  459. * asio::ip::tcp::acceptor acceptor(my_context);
  460. * ...
  461. * asio::socket_base::enable_connection_aborted option;
  462. * acceptor.get_option(option);
  463. * bool is_set = option.value();
  464. * @endcode
  465. *
  466. * @par Concepts:
  467. * Socket_Option, Boolean_Socket_Option.
  468. */
  469. #if defined(GENERATING_DOCUMENTATION)
  470. typedef implementation_defined enable_connection_aborted;
  471. #else
  472. typedef asio::detail::socket_option::boolean<
  473. asio::detail::custom_socket_option_level,
  474. asio::detail::enable_connection_aborted_option>
  475. enable_connection_aborted;
  476. #endif
  477. /// IO control command to get the amount of data that can be read without
  478. /// blocking.
  479. /**
  480. * Implements the FIONREAD IO control command.
  481. *
  482. * @par Example
  483. * @code
  484. * asio::ip::tcp::socket socket(my_context);
  485. * ...
  486. * asio::socket_base::bytes_readable command(true);
  487. * socket.io_control(command);
  488. * std::size_t bytes_readable = command.get();
  489. * @endcode
  490. *
  491. * @par Concepts:
  492. * IO_Control_Command, Size_IO_Control_Command.
  493. */
  494. #if defined(GENERATING_DOCUMENTATION)
  495. typedef implementation_defined bytes_readable;
  496. #else
  497. typedef asio::detail::io_control::bytes_readable bytes_readable;
  498. #endif
  499. /// The maximum length of the queue of pending incoming connections.
  500. #if defined(GENERATING_DOCUMENTATION)
  501. static const int max_listen_connections = implementation_defined;
  502. #else
  503. ASIO_STATIC_CONSTANT(int, max_listen_connections
  504. = ASIO_OS_DEF(SOMAXCONN));
  505. #endif
  506. #if !defined(ASIO_NO_DEPRECATED)
  507. /// (Deprecated: Use max_listen_connections.) The maximum length of the queue
  508. /// of pending incoming connections.
  509. #if defined(GENERATING_DOCUMENTATION)
  510. static const int max_connections = implementation_defined;
  511. #else
  512. ASIO_STATIC_CONSTANT(int, max_connections
  513. = ASIO_OS_DEF(SOMAXCONN));
  514. #endif
  515. #endif // !defined(ASIO_NO_DEPRECATED)
  516. protected:
  517. /// Protected destructor to prevent deletion through this type.
  518. ~socket_base()
  519. {
  520. }
  521. };
  522. } // namespace asio
  523. #include "asio/detail/pop_options.hpp"
  524. #endif // ASIO_SOCKET_BASE_HPP