socket_base.hpp 13 KB

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