PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/boost/asio/basic_socket.hpp

https://bitbucket.org/wetterj/fg-gc
C++ Header | 1062 lines | 280 code | 52 blank | 730 comment | 14 complexity | 762b9a3d01b999f86f7f7d510ad0e2af MD5 | raw file
  1. //
  2. // basic_socket.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 BOOST_ASIO_BASIC_SOCKET_HPP
  11. #define BOOST_ASIO_BASIC_SOCKET_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/config.hpp>
  16. #include <boost/asio/basic_io_object.hpp>
  17. #include <boost/asio/detail/throw_error.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/socket_base.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. /// Provides socket functionality.
  24. /**
  25. * The basic_socket class template provides functionality that is common to both
  26. * stream-oriented and datagram-oriented sockets.
  27. *
  28. * @par Thread Safety
  29. * @e Distinct @e objects: Safe.@n
  30. * @e Shared @e objects: Unsafe.
  31. */
  32. template <typename Protocol, typename SocketService>
  33. class basic_socket
  34. : public basic_io_object<SocketService>,
  35. public socket_base
  36. {
  37. public:
  38. /// The native representation of a socket.
  39. typedef typename SocketService::native_type native_type;
  40. /// The protocol type.
  41. typedef Protocol protocol_type;
  42. /// The endpoint type.
  43. typedef typename Protocol::endpoint endpoint_type;
  44. /// A basic_socket is always the lowest layer.
  45. typedef basic_socket<Protocol, SocketService> lowest_layer_type;
  46. /// Construct a basic_socket without opening it.
  47. /**
  48. * This constructor creates a socket without opening it.
  49. *
  50. * @param io_service The io_service object that the socket will use to
  51. * dispatch handlers for any asynchronous operations performed on the socket.
  52. */
  53. explicit basic_socket(boost::asio::io_service& io_service)
  54. : basic_io_object<SocketService>(io_service)
  55. {
  56. }
  57. /// Construct and open a basic_socket.
  58. /**
  59. * This constructor creates and opens a socket.
  60. *
  61. * @param io_service The io_service object that the socket will use to
  62. * dispatch handlers for any asynchronous operations performed on the socket.
  63. *
  64. * @param protocol An object specifying protocol parameters to be used.
  65. *
  66. * @throws boost::system::system_error Thrown on failure.
  67. */
  68. basic_socket(boost::asio::io_service& io_service,
  69. const protocol_type& protocol)
  70. : basic_io_object<SocketService>(io_service)
  71. {
  72. boost::system::error_code ec;
  73. this->service.open(this->implementation, protocol, ec);
  74. boost::asio::detail::throw_error(ec);
  75. }
  76. /// Construct a basic_socket, opening it and binding it to the given local
  77. /// endpoint.
  78. /**
  79. * This constructor creates a socket and automatically opens it bound to the
  80. * specified endpoint on the local machine. The protocol used is the protocol
  81. * associated with the given endpoint.
  82. *
  83. * @param io_service The io_service object that the socket will use to
  84. * dispatch handlers for any asynchronous operations performed on the socket.
  85. *
  86. * @param endpoint An endpoint on the local machine to which the socket will
  87. * be bound.
  88. *
  89. * @throws boost::system::system_error Thrown on failure.
  90. */
  91. basic_socket(boost::asio::io_service& io_service,
  92. const endpoint_type& endpoint)
  93. : basic_io_object<SocketService>(io_service)
  94. {
  95. boost::system::error_code ec;
  96. this->service.open(this->implementation, endpoint.protocol(), ec);
  97. boost::asio::detail::throw_error(ec);
  98. this->service.bind(this->implementation, endpoint, ec);
  99. boost::asio::detail::throw_error(ec);
  100. }
  101. /// Construct a basic_socket on an existing native socket.
  102. /**
  103. * This constructor creates a socket object to hold an existing native socket.
  104. *
  105. * @param io_service The io_service object that the socket will use to
  106. * dispatch handlers for any asynchronous operations performed on the socket.
  107. *
  108. * @param protocol An object specifying protocol parameters to be used.
  109. *
  110. * @param native_socket A native socket.
  111. *
  112. * @throws boost::system::system_error Thrown on failure.
  113. */
  114. basic_socket(boost::asio::io_service& io_service,
  115. const protocol_type& protocol, const native_type& native_socket)
  116. : basic_io_object<SocketService>(io_service)
  117. {
  118. boost::system::error_code ec;
  119. this->service.assign(this->implementation, protocol, native_socket, ec);
  120. boost::asio::detail::throw_error(ec);
  121. }
  122. /// Get a reference to the lowest layer.
  123. /**
  124. * This function returns a reference to the lowest layer in a stack of
  125. * layers. Since a basic_socket cannot contain any further layers, it simply
  126. * returns a reference to itself.
  127. *
  128. * @return A reference to the lowest layer in the stack of layers. Ownership
  129. * is not transferred to the caller.
  130. */
  131. lowest_layer_type& lowest_layer()
  132. {
  133. return *this;
  134. }
  135. /// Get a const reference to the lowest layer.
  136. /**
  137. * This function returns a const reference to the lowest layer in a stack of
  138. * layers. Since a basic_socket cannot contain any further layers, it simply
  139. * returns a reference to itself.
  140. *
  141. * @return A const reference to the lowest layer in the stack of layers.
  142. * Ownership is not transferred to the caller.
  143. */
  144. const lowest_layer_type& lowest_layer() const
  145. {
  146. return *this;
  147. }
  148. /// Open the socket using the specified protocol.
  149. /**
  150. * This function opens the socket so that it will use the specified protocol.
  151. *
  152. * @param protocol An object specifying protocol parameters to be used.
  153. *
  154. * @throws boost::system::system_error Thrown on failure.
  155. *
  156. * @par Example
  157. * @code
  158. * boost::asio::ip::tcp::socket socket(io_service);
  159. * socket.open(boost::asio::ip::tcp::v4());
  160. * @endcode
  161. */
  162. void open(const protocol_type& protocol = protocol_type())
  163. {
  164. boost::system::error_code ec;
  165. this->service.open(this->implementation, protocol, ec);
  166. boost::asio::detail::throw_error(ec);
  167. }
  168. /// Open the socket using the specified protocol.
  169. /**
  170. * This function opens the socket so that it will use the specified protocol.
  171. *
  172. * @param protocol An object specifying which protocol is to be used.
  173. *
  174. * @param ec Set to indicate what error occurred, if any.
  175. *
  176. * @par Example
  177. * @code
  178. * boost::asio::ip::tcp::socket socket(io_service);
  179. * boost::system::error_code ec;
  180. * socket.open(boost::asio::ip::tcp::v4(), ec);
  181. * if (ec)
  182. * {
  183. * // An error occurred.
  184. * }
  185. * @endcode
  186. */
  187. boost::system::error_code open(const protocol_type& protocol,
  188. boost::system::error_code& ec)
  189. {
  190. return this->service.open(this->implementation, protocol, ec);
  191. }
  192. /// Assign an existing native socket to the socket.
  193. /*
  194. * This function opens the socket to hold an existing native socket.
  195. *
  196. * @param protocol An object specifying which protocol is to be used.
  197. *
  198. * @param native_socket A native socket.
  199. *
  200. * @throws boost::system::system_error Thrown on failure.
  201. */
  202. void assign(const protocol_type& protocol, const native_type& native_socket)
  203. {
  204. boost::system::error_code ec;
  205. this->service.assign(this->implementation, protocol, native_socket, ec);
  206. boost::asio::detail::throw_error(ec);
  207. }
  208. /// Assign an existing native socket to the socket.
  209. /*
  210. * This function opens the socket to hold an existing native socket.
  211. *
  212. * @param protocol An object specifying which protocol is to be used.
  213. *
  214. * @param native_socket A native socket.
  215. *
  216. * @param ec Set to indicate what error occurred, if any.
  217. */
  218. boost::system::error_code assign(const protocol_type& protocol,
  219. const native_type& native_socket, boost::system::error_code& ec)
  220. {
  221. return this->service.assign(this->implementation,
  222. protocol, native_socket, ec);
  223. }
  224. /// Determine whether the socket is open.
  225. bool is_open() const
  226. {
  227. return this->service.is_open(this->implementation);
  228. }
  229. /// Close the socket.
  230. /**
  231. * This function is used to close the socket. Any asynchronous send, receive
  232. * or connect operations will be cancelled immediately, and will complete
  233. * with the boost::asio::error::operation_aborted error.
  234. *
  235. * @throws boost::system::system_error Thrown on failure.
  236. *
  237. * @note For portable behaviour with respect to graceful closure of a
  238. * connected socket, call shutdown() before closing the socket.
  239. */
  240. void close()
  241. {
  242. boost::system::error_code ec;
  243. this->service.close(this->implementation, ec);
  244. boost::asio::detail::throw_error(ec);
  245. }
  246. /// Close the socket.
  247. /**
  248. * This function is used to close the socket. Any asynchronous send, receive
  249. * or connect operations will be cancelled immediately, and will complete
  250. * with the boost::asio::error::operation_aborted error.
  251. *
  252. * @param ec Set to indicate what error occurred, if any.
  253. *
  254. * @par Example
  255. * @code
  256. * boost::asio::ip::tcp::socket socket(io_service);
  257. * ...
  258. * boost::system::error_code ec;
  259. * socket.close(ec);
  260. * if (ec)
  261. * {
  262. * // An error occurred.
  263. * }
  264. * @endcode
  265. *
  266. * @note For portable behaviour with respect to graceful closure of a
  267. * connected socket, call shutdown() before closing the socket.
  268. */
  269. boost::system::error_code close(boost::system::error_code& ec)
  270. {
  271. return this->service.close(this->implementation, ec);
  272. }
  273. /// Get the native socket representation.
  274. /**
  275. * This function may be used to obtain the underlying representation of the
  276. * socket. This is intended to allow access to native socket functionality
  277. * that is not otherwise provided.
  278. */
  279. native_type native()
  280. {
  281. return this->service.native(this->implementation);
  282. }
  283. /// Cancel all asynchronous operations associated with the socket.
  284. /**
  285. * This function causes all outstanding asynchronous connect, send and receive
  286. * operations to finish immediately, and the handlers for cancelled operations
  287. * will be passed the boost::asio::error::operation_aborted error.
  288. *
  289. * @throws boost::system::system_error Thrown on failure.
  290. *
  291. * @note Calls to cancel() will always fail with
  292. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  293. * Server 2003, and earlier versions of Windows, unless
  294. * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  295. * two issues that should be considered before enabling its use:
  296. *
  297. * @li It will only cancel asynchronous operations that were initiated in the
  298. * current thread.
  299. *
  300. * @li It can appear to complete without error, but the request to cancel the
  301. * unfinished operations may be silently ignored by the operating system.
  302. * Whether it works or not seems to depend on the drivers that are installed.
  303. *
  304. * For portable cancellation, consider using one of the following
  305. * alternatives:
  306. *
  307. * @li Disable asio's I/O completion port backend by defining
  308. * BOOST_ASIO_DISABLE_IOCP.
  309. *
  310. * @li Use the close() function to simultaneously cancel the outstanding
  311. * operations and close the socket.
  312. *
  313. * When running on Windows Vista, Windows Server 2008, and later, the
  314. * CancelIoEx function is always used. This function does not have the
  315. * problems described above.
  316. */
  317. #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
  318. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  319. && !defined(BOOST_ASIO_ENABLE_CANCELIO)
  320. __declspec(deprecated("By default, this function always fails with "
  321. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  322. "or earlier. Consult documentation for details."))
  323. #endif
  324. void cancel()
  325. {
  326. boost::system::error_code ec;
  327. this->service.cancel(this->implementation, ec);
  328. boost::asio::detail::throw_error(ec);
  329. }
  330. /// Cancel all asynchronous operations associated with the socket.
  331. /**
  332. * This function causes all outstanding asynchronous connect, send and receive
  333. * operations to finish immediately, and the handlers for cancelled operations
  334. * will be passed the boost::asio::error::operation_aborted error.
  335. *
  336. * @param ec Set to indicate what error occurred, if any.
  337. *
  338. * @note Calls to cancel() will always fail with
  339. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  340. * Server 2003, and earlier versions of Windows, unless
  341. * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  342. * two issues that should be considered before enabling its use:
  343. *
  344. * @li It will only cancel asynchronous operations that were initiated in the
  345. * current thread.
  346. *
  347. * @li It can appear to complete without error, but the request to cancel the
  348. * unfinished operations may be silently ignored by the operating system.
  349. * Whether it works or not seems to depend on the drivers that are installed.
  350. *
  351. * For portable cancellation, consider using one of the following
  352. * alternatives:
  353. *
  354. * @li Disable asio's I/O completion port backend by defining
  355. * BOOST_ASIO_DISABLE_IOCP.
  356. *
  357. * @li Use the close() function to simultaneously cancel the outstanding
  358. * operations and close the socket.
  359. *
  360. * When running on Windows Vista, Windows Server 2008, and later, the
  361. * CancelIoEx function is always used. This function does not have the
  362. * problems described above.
  363. */
  364. #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
  365. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  366. && !defined(BOOST_ASIO_ENABLE_CANCELIO)
  367. __declspec(deprecated("By default, this function always fails with "
  368. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  369. "or earlier. Consult documentation for details."))
  370. #endif
  371. boost::system::error_code cancel(boost::system::error_code& ec)
  372. {
  373. return this->service.cancel(this->implementation, ec);
  374. }
  375. /// Determine whether the socket is at the out-of-band data mark.
  376. /**
  377. * This function is used to check whether the socket input is currently
  378. * positioned at the out-of-band data mark.
  379. *
  380. * @return A bool indicating whether the socket is at the out-of-band data
  381. * mark.
  382. *
  383. * @throws boost::system::system_error Thrown on failure.
  384. */
  385. bool at_mark() const
  386. {
  387. boost::system::error_code ec;
  388. bool b = this->service.at_mark(this->implementation, ec);
  389. boost::asio::detail::throw_error(ec);
  390. return b;
  391. }
  392. /// Determine whether the socket is at the out-of-band data mark.
  393. /**
  394. * This function is used to check whether the socket input is currently
  395. * positioned at the out-of-band data mark.
  396. *
  397. * @param ec Set to indicate what error occurred, if any.
  398. *
  399. * @return A bool indicating whether the socket is at the out-of-band data
  400. * mark.
  401. */
  402. bool at_mark(boost::system::error_code& ec) const
  403. {
  404. return this->service.at_mark(this->implementation, ec);
  405. }
  406. /// Determine the number of bytes available for reading.
  407. /**
  408. * This function is used to determine the number of bytes that may be read
  409. * without blocking.
  410. *
  411. * @return The number of bytes that may be read without blocking, or 0 if an
  412. * error occurs.
  413. *
  414. * @throws boost::system::system_error Thrown on failure.
  415. */
  416. std::size_t available() const
  417. {
  418. boost::system::error_code ec;
  419. std::size_t s = this->service.available(this->implementation, ec);
  420. boost::asio::detail::throw_error(ec);
  421. return s;
  422. }
  423. /// Determine the number of bytes available for reading.
  424. /**
  425. * This function is used to determine the number of bytes that may be read
  426. * without blocking.
  427. *
  428. * @param ec Set to indicate what error occurred, if any.
  429. *
  430. * @return The number of bytes that may be read without blocking, or 0 if an
  431. * error occurs.
  432. */
  433. std::size_t available(boost::system::error_code& ec) const
  434. {
  435. return this->service.available(this->implementation, ec);
  436. }
  437. /// Bind the socket to the given local endpoint.
  438. /**
  439. * This function binds the socket to the specified endpoint on the local
  440. * machine.
  441. *
  442. * @param endpoint An endpoint on the local machine to which the socket will
  443. * be bound.
  444. *
  445. * @throws boost::system::system_error Thrown on failure.
  446. *
  447. * @par Example
  448. * @code
  449. * boost::asio::ip::tcp::socket socket(io_service);
  450. * socket.open(boost::asio::ip::tcp::v4());
  451. * socket.bind(boost::asio::ip::tcp::endpoint(
  452. * boost::asio::ip::tcp::v4(), 12345));
  453. * @endcode
  454. */
  455. void bind(const endpoint_type& endpoint)
  456. {
  457. boost::system::error_code ec;
  458. this->service.bind(this->implementation, endpoint, ec);
  459. boost::asio::detail::throw_error(ec);
  460. }
  461. /// Bind the socket to the given local endpoint.
  462. /**
  463. * This function binds the socket to the specified endpoint on the local
  464. * machine.
  465. *
  466. * @param endpoint An endpoint on the local machine to which the socket will
  467. * be bound.
  468. *
  469. * @param ec Set to indicate what error occurred, if any.
  470. *
  471. * @par Example
  472. * @code
  473. * boost::asio::ip::tcp::socket socket(io_service);
  474. * socket.open(boost::asio::ip::tcp::v4());
  475. * boost::system::error_code ec;
  476. * socket.bind(boost::asio::ip::tcp::endpoint(
  477. * boost::asio::ip::tcp::v4(), 12345), ec);
  478. * if (ec)
  479. * {
  480. * // An error occurred.
  481. * }
  482. * @endcode
  483. */
  484. boost::system::error_code bind(const endpoint_type& endpoint,
  485. boost::system::error_code& ec)
  486. {
  487. return this->service.bind(this->implementation, endpoint, ec);
  488. }
  489. /// Connect the socket to the specified endpoint.
  490. /**
  491. * This function is used to connect a socket to the specified remote endpoint.
  492. * The function call will block until the connection is successfully made or
  493. * an error occurs.
  494. *
  495. * The socket is automatically opened if it is not already open. If the
  496. * connect fails, and the socket was automatically opened, the socket is
  497. * not returned to the closed state.
  498. *
  499. * @param peer_endpoint The remote endpoint to which the socket will be
  500. * connected.
  501. *
  502. * @throws boost::system::system_error Thrown on failure.
  503. *
  504. * @par Example
  505. * @code
  506. * boost::asio::ip::tcp::socket socket(io_service);
  507. * boost::asio::ip::tcp::endpoint endpoint(
  508. * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  509. * socket.connect(endpoint);
  510. * @endcode
  511. */
  512. void connect(const endpoint_type& peer_endpoint)
  513. {
  514. boost::system::error_code ec;
  515. if (!is_open())
  516. {
  517. this->service.open(this->implementation, peer_endpoint.protocol(), ec);
  518. boost::asio::detail::throw_error(ec);
  519. }
  520. this->service.connect(this->implementation, peer_endpoint, ec);
  521. boost::asio::detail::throw_error(ec);
  522. }
  523. /// Connect the socket to the specified endpoint.
  524. /**
  525. * This function is used to connect a socket to the specified remote endpoint.
  526. * The function call will block until the connection is successfully made or
  527. * an error occurs.
  528. *
  529. * The socket is automatically opened if it is not already open. If the
  530. * connect fails, and the socket was automatically opened, the socket is
  531. * not returned to the closed state.
  532. *
  533. * @param peer_endpoint The remote endpoint to which the socket will be
  534. * connected.
  535. *
  536. * @param ec Set to indicate what error occurred, if any.
  537. *
  538. * @par Example
  539. * @code
  540. * boost::asio::ip::tcp::socket socket(io_service);
  541. * boost::asio::ip::tcp::endpoint endpoint(
  542. * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  543. * boost::system::error_code ec;
  544. * socket.connect(endpoint, ec);
  545. * if (ec)
  546. * {
  547. * // An error occurred.
  548. * }
  549. * @endcode
  550. */
  551. boost::system::error_code connect(const endpoint_type& peer_endpoint,
  552. boost::system::error_code& ec)
  553. {
  554. if (!is_open())
  555. {
  556. if (this->service.open(this->implementation,
  557. peer_endpoint.protocol(), ec))
  558. {
  559. return ec;
  560. }
  561. }
  562. return this->service.connect(this->implementation, peer_endpoint, ec);
  563. }
  564. /// Start an asynchronous connect.
  565. /**
  566. * This function is used to asynchronously connect a socket to the specified
  567. * remote endpoint. The function call always returns immediately.
  568. *
  569. * The socket is automatically opened if it is not already open. If the
  570. * connect fails, and the socket was automatically opened, the socket is
  571. * not returned to the closed state.
  572. *
  573. * @param peer_endpoint The remote endpoint to which the socket will be
  574. * connected. Copies will be made of the endpoint object as required.
  575. *
  576. * @param handler The handler to be called when the connection operation
  577. * completes. Copies will be made of the handler as required. The function
  578. * signature of the handler must be:
  579. * @code void handler(
  580. * const boost::system::error_code& error // Result of operation
  581. * ); @endcode
  582. * Regardless of whether the asynchronous operation completes immediately or
  583. * not, the handler will not be invoked from within this function. Invocation
  584. * of the handler will be performed in a manner equivalent to using
  585. * boost::asio::io_service::post().
  586. *
  587. * @par Example
  588. * @code
  589. * void connect_handler(const boost::system::error_code& error)
  590. * {
  591. * if (!error)
  592. * {
  593. * // Connect succeeded.
  594. * }
  595. * }
  596. *
  597. * ...
  598. *
  599. * boost::asio::ip::tcp::socket socket(io_service);
  600. * boost::asio::ip::tcp::endpoint endpoint(
  601. * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  602. * socket.async_connect(endpoint, connect_handler);
  603. * @endcode
  604. */
  605. template <typename ConnectHandler>
  606. void async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler)
  607. {
  608. if (!is_open())
  609. {
  610. boost::system::error_code ec;
  611. if (this->service.open(this->implementation,
  612. peer_endpoint.protocol(), ec))
  613. {
  614. this->get_io_service().post(
  615. boost::asio::detail::bind_handler(handler, ec));
  616. return;
  617. }
  618. }
  619. this->service.async_connect(this->implementation, peer_endpoint, handler);
  620. }
  621. /// Set an option on the socket.
  622. /**
  623. * This function is used to set an option on the socket.
  624. *
  625. * @param option The new option value to be set on the socket.
  626. *
  627. * @throws boost::system::system_error Thrown on failure.
  628. *
  629. * @sa SettableSocketOption @n
  630. * boost::asio::socket_base::broadcast @n
  631. * boost::asio::socket_base::do_not_route @n
  632. * boost::asio::socket_base::keep_alive @n
  633. * boost::asio::socket_base::linger @n
  634. * boost::asio::socket_base::receive_buffer_size @n
  635. * boost::asio::socket_base::receive_low_watermark @n
  636. * boost::asio::socket_base::reuse_address @n
  637. * boost::asio::socket_base::send_buffer_size @n
  638. * boost::asio::socket_base::send_low_watermark @n
  639. * boost::asio::ip::multicast::join_group @n
  640. * boost::asio::ip::multicast::leave_group @n
  641. * boost::asio::ip::multicast::enable_loopback @n
  642. * boost::asio::ip::multicast::outbound_interface @n
  643. * boost::asio::ip::multicast::hops @n
  644. * boost::asio::ip::tcp::no_delay
  645. *
  646. * @par Example
  647. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  648. * @code
  649. * boost::asio::ip::tcp::socket socket(io_service);
  650. * ...
  651. * boost::asio::ip::tcp::no_delay option(true);
  652. * socket.set_option(option);
  653. * @endcode
  654. */
  655. template <typename SettableSocketOption>
  656. void set_option(const SettableSocketOption& option)
  657. {
  658. boost::system::error_code ec;
  659. this->service.set_option(this->implementation, option, ec);
  660. boost::asio::detail::throw_error(ec);
  661. }
  662. /// Set an option on the socket.
  663. /**
  664. * This function is used to set an option on the socket.
  665. *
  666. * @param option The new option value to be set on the socket.
  667. *
  668. * @param ec Set to indicate what error occurred, if any.
  669. *
  670. * @sa SettableSocketOption @n
  671. * boost::asio::socket_base::broadcast @n
  672. * boost::asio::socket_base::do_not_route @n
  673. * boost::asio::socket_base::keep_alive @n
  674. * boost::asio::socket_base::linger @n
  675. * boost::asio::socket_base::receive_buffer_size @n
  676. * boost::asio::socket_base::receive_low_watermark @n
  677. * boost::asio::socket_base::reuse_address @n
  678. * boost::asio::socket_base::send_buffer_size @n
  679. * boost::asio::socket_base::send_low_watermark @n
  680. * boost::asio::ip::multicast::join_group @n
  681. * boost::asio::ip::multicast::leave_group @n
  682. * boost::asio::ip::multicast::enable_loopback @n
  683. * boost::asio::ip::multicast::outbound_interface @n
  684. * boost::asio::ip::multicast::hops @n
  685. * boost::asio::ip::tcp::no_delay
  686. *
  687. * @par Example
  688. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  689. * @code
  690. * boost::asio::ip::tcp::socket socket(io_service);
  691. * ...
  692. * boost::asio::ip::tcp::no_delay option(true);
  693. * boost::system::error_code ec;
  694. * socket.set_option(option, ec);
  695. * if (ec)
  696. * {
  697. * // An error occurred.
  698. * }
  699. * @endcode
  700. */
  701. template <typename SettableSocketOption>
  702. boost::system::error_code set_option(const SettableSocketOption& option,
  703. boost::system::error_code& ec)
  704. {
  705. return this->service.set_option(this->implementation, option, ec);
  706. }
  707. /// Get an option from the socket.
  708. /**
  709. * This function is used to get the current value of an option on the socket.
  710. *
  711. * @param option The option value to be obtained from the socket.
  712. *
  713. * @throws boost::system::system_error Thrown on failure.
  714. *
  715. * @sa GettableSocketOption @n
  716. * boost::asio::socket_base::broadcast @n
  717. * boost::asio::socket_base::do_not_route @n
  718. * boost::asio::socket_base::keep_alive @n
  719. * boost::asio::socket_base::linger @n
  720. * boost::asio::socket_base::receive_buffer_size @n
  721. * boost::asio::socket_base::receive_low_watermark @n
  722. * boost::asio::socket_base::reuse_address @n
  723. * boost::asio::socket_base::send_buffer_size @n
  724. * boost::asio::socket_base::send_low_watermark @n
  725. * boost::asio::ip::multicast::join_group @n
  726. * boost::asio::ip::multicast::leave_group @n
  727. * boost::asio::ip::multicast::enable_loopback @n
  728. * boost::asio::ip::multicast::outbound_interface @n
  729. * boost::asio::ip::multicast::hops @n
  730. * boost::asio::ip::tcp::no_delay
  731. *
  732. * @par Example
  733. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  734. * @code
  735. * boost::asio::ip::tcp::socket socket(io_service);
  736. * ...
  737. * boost::asio::ip::tcp::socket::keep_alive option;
  738. * socket.get_option(option);
  739. * bool is_set = option.get();
  740. * @endcode
  741. */
  742. template <typename GettableSocketOption>
  743. void get_option(GettableSocketOption& option) const
  744. {
  745. boost::system::error_code ec;
  746. this->service.get_option(this->implementation, option, ec);
  747. boost::asio::detail::throw_error(ec);
  748. }
  749. /// Get an option from the socket.
  750. /**
  751. * This function is used to get the current value of an option on the socket.
  752. *
  753. * @param option The option value to be obtained from the socket.
  754. *
  755. * @param ec Set to indicate what error occurred, if any.
  756. *
  757. * @sa GettableSocketOption @n
  758. * boost::asio::socket_base::broadcast @n
  759. * boost::asio::socket_base::do_not_route @n
  760. * boost::asio::socket_base::keep_alive @n
  761. * boost::asio::socket_base::linger @n
  762. * boost::asio::socket_base::receive_buffer_size @n
  763. * boost::asio::socket_base::receive_low_watermark @n
  764. * boost::asio::socket_base::reuse_address @n
  765. * boost::asio::socket_base::send_buffer_size @n
  766. * boost::asio::socket_base::send_low_watermark @n
  767. * boost::asio::ip::multicast::join_group @n
  768. * boost::asio::ip::multicast::leave_group @n
  769. * boost::asio::ip::multicast::enable_loopback @n
  770. * boost::asio::ip::multicast::outbound_interface @n
  771. * boost::asio::ip::multicast::hops @n
  772. * boost::asio::ip::tcp::no_delay
  773. *
  774. * @par Example
  775. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  776. * @code
  777. * boost::asio::ip::tcp::socket socket(io_service);
  778. * ...
  779. * boost::asio::ip::tcp::socket::keep_alive option;
  780. * boost::system::error_code ec;
  781. * socket.get_option(option, ec);
  782. * if (ec)
  783. * {
  784. * // An error occurred.
  785. * }
  786. * bool is_set = option.get();
  787. * @endcode
  788. */
  789. template <typename GettableSocketOption>
  790. boost::system::error_code get_option(GettableSocketOption& option,
  791. boost::system::error_code& ec) const
  792. {
  793. return this->service.get_option(this->implementation, option, ec);
  794. }
  795. /// Perform an IO control command on the socket.
  796. /**
  797. * This function is used to execute an IO control command on the socket.
  798. *
  799. * @param command The IO control command to be performed on the socket.
  800. *
  801. * @throws boost::system::system_error Thrown on failure.
  802. *
  803. * @sa IoControlCommand @n
  804. * boost::asio::socket_base::bytes_readable @n
  805. * boost::asio::socket_base::non_blocking_io
  806. *
  807. * @par Example
  808. * Getting the number of bytes ready to read:
  809. * @code
  810. * boost::asio::ip::tcp::socket socket(io_service);
  811. * ...
  812. * boost::asio::ip::tcp::socket::bytes_readable command;
  813. * socket.io_control(command);
  814. * std::size_t bytes_readable = command.get();
  815. * @endcode
  816. */
  817. template <typename IoControlCommand>
  818. void io_control(IoControlCommand& command)
  819. {
  820. boost::system::error_code ec;
  821. this->service.io_control(this->implementation, command, ec);
  822. boost::asio::detail::throw_error(ec);
  823. }
  824. /// Perform an IO control command on the socket.
  825. /**
  826. * This function is used to execute an IO control command on the socket.
  827. *
  828. * @param command The IO control command to be performed on the socket.
  829. *
  830. * @param ec Set to indicate what error occurred, if any.
  831. *
  832. * @sa IoControlCommand @n
  833. * boost::asio::socket_base::bytes_readable @n
  834. * boost::asio::socket_base::non_blocking_io
  835. *
  836. * @par Example
  837. * Getting the number of bytes ready to read:
  838. * @code
  839. * boost::asio::ip::tcp::socket socket(io_service);
  840. * ...
  841. * boost::asio::ip::tcp::socket::bytes_readable command;
  842. * boost::system::error_code ec;
  843. * socket.io_control(command, ec);
  844. * if (ec)
  845. * {
  846. * // An error occurred.
  847. * }
  848. * std::size_t bytes_readable = command.get();
  849. * @endcode
  850. */
  851. template <typename IoControlCommand>
  852. boost::system::error_code io_control(IoControlCommand& command,
  853. boost::system::error_code& ec)
  854. {
  855. return this->service.io_control(this->implementation, command, ec);
  856. }
  857. /// Get the local endpoint of the socket.
  858. /**
  859. * This function is used to obtain the locally bound endpoint of the socket.
  860. *
  861. * @returns An object that represents the local endpoint of the socket.
  862. *
  863. * @throws boost::system::system_error Thrown on failure.
  864. *
  865. * @par Example
  866. * @code
  867. * boost::asio::ip::tcp::socket socket(io_service);
  868. * ...
  869. * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
  870. * @endcode
  871. */
  872. endpoint_type local_endpoint() const
  873. {
  874. boost::system::error_code ec;
  875. endpoint_type ep = this->service.local_endpoint(this->implementation, ec);
  876. boost::asio::detail::throw_error(ec);
  877. return ep;
  878. }
  879. /// Get the local endpoint of the socket.
  880. /**
  881. * This function is used to obtain the locally bound endpoint of the socket.
  882. *
  883. * @param ec Set to indicate what error occurred, if any.
  884. *
  885. * @returns An object that represents the local endpoint of the socket.
  886. * Returns a default-constructed endpoint object if an error occurred.
  887. *
  888. * @par Example
  889. * @code
  890. * boost::asio::ip::tcp::socket socket(io_service);
  891. * ...
  892. * boost::system::error_code ec;
  893. * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
  894. * if (ec)
  895. * {
  896. * // An error occurred.
  897. * }
  898. * @endcode
  899. */
  900. endpoint_type local_endpoint(boost::system::error_code& ec) const
  901. {
  902. return this->service.local_endpoint(this->implementation, ec);
  903. }
  904. /// Get the remote endpoint of the socket.
  905. /**
  906. * This function is used to obtain the remote endpoint of the socket.
  907. *
  908. * @returns An object that represents the remote endpoint of the socket.
  909. *
  910. * @throws boost::system::system_error Thrown on failure.
  911. *
  912. * @par Example
  913. * @code
  914. * boost::asio::ip::tcp::socket socket(io_service);
  915. * ...
  916. * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
  917. * @endcode
  918. */
  919. endpoint_type remote_endpoint() const
  920. {
  921. boost::system::error_code ec;
  922. endpoint_type ep = this->service.remote_endpoint(this->implementation, ec);
  923. boost::asio::detail::throw_error(ec);
  924. return ep;
  925. }
  926. /// Get the remote endpoint of the socket.
  927. /**
  928. * This function is used to obtain the remote endpoint of the socket.
  929. *
  930. * @param ec Set to indicate what error occurred, if any.
  931. *
  932. * @returns An object that represents the remote endpoint of the socket.
  933. * Returns a default-constructed endpoint object if an error occurred.
  934. *
  935. * @par Example
  936. * @code
  937. * boost::asio::ip::tcp::socket socket(io_service);
  938. * ...
  939. * boost::system::error_code ec;
  940. * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
  941. * if (ec)
  942. * {
  943. * // An error occurred.
  944. * }
  945. * @endcode
  946. */
  947. endpoint_type remote_endpoint(boost::system::error_code& ec) const
  948. {
  949. return this->service.remote_endpoint(this->implementation, ec);
  950. }
  951. /// Disable sends or receives on the socket.
  952. /**
  953. * This function is used to disable send operations, receive operations, or
  954. * both.
  955. *
  956. * @param what Determines what types of operation will no longer be allowed.
  957. *
  958. * @throws boost::system::system_error Thrown on failure.
  959. *
  960. * @par Example
  961. * Shutting down the send side of the socket:
  962. * @code
  963. * boost::asio::ip::tcp::socket socket(io_service);
  964. * ...
  965. * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
  966. * @endcode
  967. */
  968. void shutdown(shutdown_type what)
  969. {
  970. boost::system::error_code ec;
  971. this->service.shutdown(this->implementation, what, ec);
  972. boost::asio::detail::throw_error(ec);
  973. }
  974. /// Disable sends or receives on the socket.
  975. /**
  976. * This function is used to disable send operations, receive operations, or
  977. * both.
  978. *
  979. * @param what Determines what types of operation will no longer be allowed.
  980. *
  981. * @param ec Set to indicate what error occurred, if any.
  982. *
  983. * @par Example
  984. * Shutting down the send side of the socket:
  985. * @code
  986. * boost::asio::ip::tcp::socket socket(io_service);
  987. * ...
  988. * boost::system::error_code ec;
  989. * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
  990. * if (ec)
  991. * {
  992. * // An error occurred.
  993. * }
  994. * @endcode
  995. */
  996. boost::system::error_code shutdown(shutdown_type what,
  997. boost::system::error_code& ec)
  998. {
  999. return this->service.shutdown(this->implementation, what, ec);
  1000. }
  1001. protected:
  1002. /// Protected destructor to prevent deletion through this type.
  1003. ~basic_socket()
  1004. {
  1005. }
  1006. };
  1007. } // namespace asio
  1008. } // namespace boost
  1009. #include <boost/asio/detail/pop_options.hpp>
  1010. #endif // BOOST_ASIO_BASIC_SOCKET_HPP