socket_base.hpp 14 KB

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