socket_base.hpp 14 KB

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