PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/include/boost/asio/basic_socket.hpp

https://bitbucket.org/Leijing/lip-measurement
C++ Header | 1464 lines | 348 code | 63 blank | 1053 comment | 15 complexity | cd0ab984778bed4ec8e64c6e811995ce 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/handler_type_requirements.hpp>
  18. #include <boost/asio/detail/throw_error.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/socket_base.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /// Provides socket functionality.
  25. /**
  26. * The basic_socket class template provides functionality that is common to both
  27. * stream-oriented and datagram-oriented sockets.
  28. *
  29. * @par Thread Safety
  30. * @e Distinct @e objects: Safe.@n
  31. * @e Shared @e objects: Unsafe.
  32. */
  33. template <typename Protocol, typename SocketService>
  34. class basic_socket
  35. : public basic_io_object<SocketService>,
  36. public socket_base
  37. {
  38. public:
  39. /// (Deprecated: Use native_handle_type.) The native representation of a
  40. /// socket.
  41. typedef typename SocketService::native_handle_type native_type;
  42. /// The native representation of a socket.
  43. typedef typename SocketService::native_handle_type native_handle_type;
  44. /// The protocol type.
  45. typedef Protocol protocol_type;
  46. /// The endpoint type.
  47. typedef typename Protocol::endpoint endpoint_type;
  48. /// A basic_socket is always the lowest layer.
  49. typedef basic_socket<Protocol, SocketService> lowest_layer_type;
  50. /// Construct a basic_socket without opening it.
  51. /**
  52. * This constructor creates a socket without opening it.
  53. *
  54. * @param io_service The io_service object that the socket will use to
  55. * dispatch handlers for any asynchronous operations performed on the socket.
  56. */
  57. explicit basic_socket(boost::asio::io_service& io_service)
  58. : basic_io_object<SocketService>(io_service)
  59. {
  60. }
  61. /// Construct and open a basic_socket.
  62. /**
  63. * This constructor creates and opens a socket.
  64. *
  65. * @param io_service The io_service object that the socket will use to
  66. * dispatch handlers for any asynchronous operations performed on the socket.
  67. *
  68. * @param protocol An object specifying protocol parameters to be used.
  69. *
  70. * @throws boost::system::system_error Thrown on failure.
  71. */
  72. basic_socket(boost::asio::io_service& io_service,
  73. const protocol_type& protocol)
  74. : basic_io_object<SocketService>(io_service)
  75. {
  76. boost::system::error_code ec;
  77. this->get_service().open(this->get_implementation(), protocol, ec);
  78. boost::asio::detail::throw_error(ec, "open");
  79. }
  80. /// Construct a basic_socket, opening it and binding it to the given local
  81. /// endpoint.
  82. /**
  83. * This constructor creates a socket and automatically opens it bound to the
  84. * specified endpoint on the local machine. The protocol used is the protocol
  85. * associated with the given endpoint.
  86. *
  87. * @param io_service The io_service object that the socket will use to
  88. * dispatch handlers for any asynchronous operations performed on the socket.
  89. *
  90. * @param endpoint An endpoint on the local machine to which the socket will
  91. * be bound.
  92. *
  93. * @throws boost::system::system_error Thrown on failure.
  94. */
  95. basic_socket(boost::asio::io_service& io_service,
  96. const endpoint_type& endpoint)
  97. : basic_io_object<SocketService>(io_service)
  98. {
  99. boost::system::error_code ec;
  100. const protocol_type protocol = endpoint.protocol();
  101. this->get_service().open(this->get_implementation(), protocol, ec);
  102. boost::asio::detail::throw_error(ec, "open");
  103. this->get_service().bind(this->get_implementation(), endpoint, ec);
  104. boost::asio::detail::throw_error(ec, "bind");
  105. }
  106. /// Construct a basic_socket on an existing native socket.
  107. /**
  108. * This constructor creates a socket object to hold an existing native socket.
  109. *
  110. * @param io_service The io_service object that the socket will use to
  111. * dispatch handlers for any asynchronous operations performed on the socket.
  112. *
  113. * @param protocol An object specifying protocol parameters to be used.
  114. *
  115. * @param native_socket A native socket.
  116. *
  117. * @throws boost::system::system_error Thrown on failure.
  118. */
  119. basic_socket(boost::asio::io_service& io_service,
  120. const protocol_type& protocol, const native_handle_type& native_socket)
  121. : basic_io_object<SocketService>(io_service)
  122. {
  123. boost::system::error_code ec;
  124. this->get_service().assign(this->get_implementation(),
  125. protocol, native_socket, ec);
  126. boost::asio::detail::throw_error(ec, "assign");
  127. }
  128. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  129. /// Move-construct a basic_socket from another.
  130. /**
  131. * This constructor moves a socket from one object to another.
  132. *
  133. * @param other The other basic_socket object from which the move will
  134. * occur.
  135. *
  136. * @note Following the move, the moved-from object is in the same state as if
  137. * constructed using the @c basic_socket(io_service&) constructor.
  138. */
  139. basic_socket(basic_socket&& other)
  140. : basic_io_object<SocketService>(
  141. BOOST_ASIO_MOVE_CAST(basic_socket)(other))
  142. {
  143. }
  144. /// Move-assign a basic_socket from another.
  145. /**
  146. * This assignment operator moves a socket from one object to another.
  147. *
  148. * @param other The other basic_socket object from which the move will
  149. * occur.
  150. *
  151. * @note Following the move, the moved-from object is in the same state as if
  152. * constructed using the @c basic_socket(io_service&) constructor.
  153. */
  154. basic_socket& operator=(basic_socket&& other)
  155. {
  156. basic_io_object<SocketService>::operator=(
  157. BOOST_ASIO_MOVE_CAST(basic_socket)(other));
  158. return *this;
  159. }
  160. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  161. /// Get a reference to the lowest layer.
  162. /**
  163. * This function returns a reference to the lowest layer in a stack of
  164. * layers. Since a basic_socket cannot contain any further layers, it simply
  165. * returns a reference to itself.
  166. *
  167. * @return A reference to the lowest layer in the stack of layers. Ownership
  168. * is not transferred to the caller.
  169. */
  170. lowest_layer_type& lowest_layer()
  171. {
  172. return *this;
  173. }
  174. /// Get a const reference to the lowest layer.
  175. /**
  176. * This function returns a const reference to the lowest layer in a stack of
  177. * layers. Since a basic_socket cannot contain any further layers, it simply
  178. * returns a reference to itself.
  179. *
  180. * @return A const reference to the lowest layer in the stack of layers.
  181. * Ownership is not transferred to the caller.
  182. */
  183. const lowest_layer_type& lowest_layer() const
  184. {
  185. return *this;
  186. }
  187. /// Open the socket using the specified protocol.
  188. /**
  189. * This function opens the socket so that it will use the specified protocol.
  190. *
  191. * @param protocol An object specifying protocol parameters to be used.
  192. *
  193. * @throws boost::system::system_error Thrown on failure.
  194. *
  195. * @par Example
  196. * @code
  197. * boost::asio::ip::tcp::socket socket(io_service);
  198. * socket.open(boost::asio::ip::tcp::v4());
  199. * @endcode
  200. */
  201. void open(const protocol_type& protocol = protocol_type())
  202. {
  203. boost::system::error_code ec;
  204. this->get_service().open(this->get_implementation(), protocol, ec);
  205. boost::asio::detail::throw_error(ec, "open");
  206. }
  207. /// Open the socket using the specified protocol.
  208. /**
  209. * This function opens the socket so that it will use the specified protocol.
  210. *
  211. * @param protocol An object specifying which protocol is to be used.
  212. *
  213. * @param ec Set to indicate what error occurred, if any.
  214. *
  215. * @par Example
  216. * @code
  217. * boost::asio::ip::tcp::socket socket(io_service);
  218. * boost::system::error_code ec;
  219. * socket.open(boost::asio::ip::tcp::v4(), ec);
  220. * if (ec)
  221. * {
  222. * // An error occurred.
  223. * }
  224. * @endcode
  225. */
  226. boost::system::error_code open(const protocol_type& protocol,
  227. boost::system::error_code& ec)
  228. {
  229. return this->get_service().open(this->get_implementation(), protocol, ec);
  230. }
  231. /// Assign an existing native socket to the socket.
  232. /*
  233. * This function opens the socket to hold an existing native socket.
  234. *
  235. * @param protocol An object specifying which protocol is to be used.
  236. *
  237. * @param native_socket A native socket.
  238. *
  239. * @throws boost::system::system_error Thrown on failure.
  240. */
  241. void assign(const protocol_type& protocol,
  242. const native_handle_type& native_socket)
  243. {
  244. boost::system::error_code ec;
  245. this->get_service().assign(this->get_implementation(),
  246. protocol, native_socket, ec);
  247. boost::asio::detail::throw_error(ec, "assign");
  248. }
  249. /// Assign an existing native socket to the socket.
  250. /*
  251. * This function opens the socket to hold an existing native socket.
  252. *
  253. * @param protocol An object specifying which protocol is to be used.
  254. *
  255. * @param native_socket A native socket.
  256. *
  257. * @param ec Set to indicate what error occurred, if any.
  258. */
  259. boost::system::error_code assign(const protocol_type& protocol,
  260. const native_handle_type& native_socket, boost::system::error_code& ec)
  261. {
  262. return this->get_service().assign(this->get_implementation(),
  263. protocol, native_socket, ec);
  264. }
  265. /// Determine whether the socket is open.
  266. bool is_open() const
  267. {
  268. return this->get_service().is_open(this->get_implementation());
  269. }
  270. /// Close the socket.
  271. /**
  272. * This function is used to close the socket. Any asynchronous send, receive
  273. * or connect operations will be cancelled immediately, and will complete
  274. * with the boost::asio::error::operation_aborted error.
  275. *
  276. * @throws boost::system::system_error Thrown on failure. Note that, even if
  277. * the function indicates an error, the underlying descriptor is closed.
  278. *
  279. * @note For portable behaviour with respect to graceful closure of a
  280. * connected socket, call shutdown() before closing the socket.
  281. */
  282. void close()
  283. {
  284. boost::system::error_code ec;
  285. this->get_service().close(this->get_implementation(), ec);
  286. boost::asio::detail::throw_error(ec, "close");
  287. }
  288. /// Close the socket.
  289. /**
  290. * This function is used to close the socket. Any asynchronous send, receive
  291. * or connect operations will be cancelled immediately, and will complete
  292. * with the boost::asio::error::operation_aborted error.
  293. *
  294. * @param ec Set to indicate what error occurred, if any. Note that, even if
  295. * the function indicates an error, the underlying descriptor is closed.
  296. *
  297. * @par Example
  298. * @code
  299. * boost::asio::ip::tcp::socket socket(io_service);
  300. * ...
  301. * boost::system::error_code ec;
  302. * socket.close(ec);
  303. * if (ec)
  304. * {
  305. * // An error occurred.
  306. * }
  307. * @endcode
  308. *
  309. * @note For portable behaviour with respect to graceful closure of a
  310. * connected socket, call shutdown() before closing the socket.
  311. */
  312. boost::system::error_code close(boost::system::error_code& ec)
  313. {
  314. return this->get_service().close(this->get_implementation(), ec);
  315. }
  316. /// (Deprecated: Use native_handle().) Get the native socket representation.
  317. /**
  318. * This function may be used to obtain the underlying representation of the
  319. * socket. This is intended to allow access to native socket functionality
  320. * that is not otherwise provided.
  321. */
  322. native_type native()
  323. {
  324. return this->get_service().native_handle(this->get_implementation());
  325. }
  326. /// Get the native socket representation.
  327. /**
  328. * This function may be used to obtain the underlying representation of the
  329. * socket. This is intended to allow access to native socket functionality
  330. * that is not otherwise provided.
  331. */
  332. native_handle_type native_handle()
  333. {
  334. return this->get_service().native_handle(this->get_implementation());
  335. }
  336. /// Cancel all asynchronous operations associated with the socket.
  337. /**
  338. * This function causes all outstanding asynchronous connect, send and receive
  339. * operations to finish immediately, and the handlers for cancelled operations
  340. * will be passed the boost::asio::error::operation_aborted error.
  341. *
  342. * @throws boost::system::system_error Thrown on failure.
  343. *
  344. * @note Calls to cancel() will always fail with
  345. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  346. * Server 2003, and earlier versions of Windows, unless
  347. * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  348. * two issues that should be considered before enabling its use:
  349. *
  350. * @li It will only cancel asynchronous operations that were initiated in the
  351. * current thread.
  352. *
  353. * @li It can appear to complete without error, but the request to cancel the
  354. * unfinished operations may be silently ignored by the operating system.
  355. * Whether it works or not seems to depend on the drivers that are installed.
  356. *
  357. * For portable cancellation, consider using one of the following
  358. * alternatives:
  359. *
  360. * @li Disable asio's I/O completion port backend by defining
  361. * BOOST_ASIO_DISABLE_IOCP.
  362. *
  363. * @li Use the close() function to simultaneously cancel the outstanding
  364. * operations and close the socket.
  365. *
  366. * When running on Windows Vista, Windows Server 2008, and later, the
  367. * CancelIoEx function is always used. This function does not have the
  368. * problems described above.
  369. */
  370. #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
  371. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  372. && !defined(BOOST_ASIO_ENABLE_CANCELIO)
  373. __declspec(deprecated("By default, this function always fails with "
  374. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  375. "or earlier. Consult documentation for details."))
  376. #endif
  377. void cancel()
  378. {
  379. boost::system::error_code ec;
  380. this->get_service().cancel(this->get_implementation(), ec);
  381. boost::asio::detail::throw_error(ec, "cancel");
  382. }
  383. /// Cancel all asynchronous operations associated with the socket.
  384. /**
  385. * This function causes all outstanding asynchronous connect, send and receive
  386. * operations to finish immediately, and the handlers for cancelled operations
  387. * will be passed the boost::asio::error::operation_aborted error.
  388. *
  389. * @param ec Set to indicate what error occurred, if any.
  390. *
  391. * @note Calls to cancel() will always fail with
  392. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  393. * Server 2003, and earlier versions of Windows, unless
  394. * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  395. * two issues that should be considered before enabling its use:
  396. *
  397. * @li It will only cancel asynchronous operations that were initiated in the
  398. * current thread.
  399. *
  400. * @li It can appear to complete without error, but the request to cancel the
  401. * unfinished operations may be silently ignored by the operating system.
  402. * Whether it works or not seems to depend on the drivers that are installed.
  403. *
  404. * For portable cancellation, consider using one of the following
  405. * alternatives:
  406. *
  407. * @li Disable asio's I/O completion port backend by defining
  408. * BOOST_ASIO_DISABLE_IOCP.
  409. *
  410. * @li Use the close() function to simultaneously cancel the outstanding
  411. * operations and close the socket.
  412. *
  413. * When running on Windows Vista, Windows Server 2008, and later, the
  414. * CancelIoEx function is always used. This function does not have the
  415. * problems described above.
  416. */
  417. #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
  418. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  419. && !defined(BOOST_ASIO_ENABLE_CANCELIO)
  420. __declspec(deprecated("By default, this function always fails with "
  421. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  422. "or earlier. Consult documentation for details."))
  423. #endif
  424. boost::system::error_code cancel(boost::system::error_code& ec)
  425. {
  426. return this->get_service().cancel(this->get_implementation(), ec);
  427. }
  428. /// Determine whether the socket is at the out-of-band data mark.
  429. /**
  430. * This function is used to check whether the socket input is currently
  431. * positioned at the out-of-band data mark.
  432. *
  433. * @return A bool indicating whether the socket is at the out-of-band data
  434. * mark.
  435. *
  436. * @throws boost::system::system_error Thrown on failure.
  437. */
  438. bool at_mark() const
  439. {
  440. boost::system::error_code ec;
  441. bool b = this->get_service().at_mark(this->get_implementation(), ec);
  442. boost::asio::detail::throw_error(ec, "at_mark");
  443. return b;
  444. }
  445. /// Determine whether the socket is at the out-of-band data mark.
  446. /**
  447. * This function is used to check whether the socket input is currently
  448. * positioned at the out-of-band data mark.
  449. *
  450. * @param ec Set to indicate what error occurred, if any.
  451. *
  452. * @return A bool indicating whether the socket is at the out-of-band data
  453. * mark.
  454. */
  455. bool at_mark(boost::system::error_code& ec) const
  456. {
  457. return this->get_service().at_mark(this->get_implementation(), ec);
  458. }
  459. /// Determine the number of bytes available for reading.
  460. /**
  461. * This function is used to determine the number of bytes that may be read
  462. * without blocking.
  463. *
  464. * @return The number of bytes that may be read without blocking, or 0 if an
  465. * error occurs.
  466. *
  467. * @throws boost::system::system_error Thrown on failure.
  468. */
  469. std::size_t available() const
  470. {
  471. boost::system::error_code ec;
  472. std::size_t s = this->get_service().available(
  473. this->get_implementation(), ec);
  474. boost::asio::detail::throw_error(ec, "available");
  475. return s;
  476. }
  477. /// Determine the number of bytes available for reading.
  478. /**
  479. * This function is used to determine the number of bytes that may be read
  480. * without blocking.
  481. *
  482. * @param ec Set to indicate what error occurred, if any.
  483. *
  484. * @return The number of bytes that may be read without blocking, or 0 if an
  485. * error occurs.
  486. */
  487. std::size_t available(boost::system::error_code& ec) const
  488. {
  489. return this->get_service().available(this->get_implementation(), ec);
  490. }
  491. /// Bind the socket to the given local endpoint.
  492. /**
  493. * This function binds the socket to the specified endpoint on the local
  494. * machine.
  495. *
  496. * @param endpoint An endpoint on the local machine to which the socket will
  497. * be bound.
  498. *
  499. * @throws boost::system::system_error Thrown on failure.
  500. *
  501. * @par Example
  502. * @code
  503. * boost::asio::ip::tcp::socket socket(io_service);
  504. * socket.open(boost::asio::ip::tcp::v4());
  505. * socket.bind(boost::asio::ip::tcp::endpoint(
  506. * boost::asio::ip::tcp::v4(), 12345));
  507. * @endcode
  508. */
  509. void bind(const endpoint_type& endpoint)
  510. {
  511. boost::system::error_code ec;
  512. this->get_service().bind(this->get_implementation(), endpoint, ec);
  513. boost::asio::detail::throw_error(ec, "bind");
  514. }
  515. /// Bind the socket to the given local endpoint.
  516. /**
  517. * This function binds the socket to the specified endpoint on the local
  518. * machine.
  519. *
  520. * @param endpoint An endpoint on the local machine to which the socket will
  521. * be bound.
  522. *
  523. * @param ec Set to indicate what error occurred, if any.
  524. *
  525. * @par Example
  526. * @code
  527. * boost::asio::ip::tcp::socket socket(io_service);
  528. * socket.open(boost::asio::ip::tcp::v4());
  529. * boost::system::error_code ec;
  530. * socket.bind(boost::asio::ip::tcp::endpoint(
  531. * boost::asio::ip::tcp::v4(), 12345), ec);
  532. * if (ec)
  533. * {
  534. * // An error occurred.
  535. * }
  536. * @endcode
  537. */
  538. boost::system::error_code bind(const endpoint_type& endpoint,
  539. boost::system::error_code& ec)
  540. {
  541. return this->get_service().bind(this->get_implementation(), endpoint, ec);
  542. }
  543. /// Connect the socket to the specified endpoint.
  544. /**
  545. * This function is used to connect a socket to the specified remote endpoint.
  546. * The function call will block until the connection is successfully made or
  547. * an error occurs.
  548. *
  549. * The socket is automatically opened if it is not already open. If the
  550. * connect fails, and the socket was automatically opened, the socket is
  551. * not returned to the closed state.
  552. *
  553. * @param peer_endpoint The remote endpoint to which the socket will be
  554. * connected.
  555. *
  556. * @throws boost::system::system_error Thrown on failure.
  557. *
  558. * @par Example
  559. * @code
  560. * boost::asio::ip::tcp::socket socket(io_service);
  561. * boost::asio::ip::tcp::endpoint endpoint(
  562. * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  563. * socket.connect(endpoint);
  564. * @endcode
  565. */
  566. void connect(const endpoint_type& peer_endpoint)
  567. {
  568. boost::system::error_code ec;
  569. if (!is_open())
  570. {
  571. this->get_service().open(this->get_implementation(),
  572. peer_endpoint.protocol(), ec);
  573. boost::asio::detail::throw_error(ec, "connect");
  574. }
  575. this->get_service().connect(this->get_implementation(), peer_endpoint, ec);
  576. boost::asio::detail::throw_error(ec, "connect");
  577. }
  578. /// Connect the socket to the specified endpoint.
  579. /**
  580. * This function is used to connect a socket to the specified remote endpoint.
  581. * The function call will block until the connection is successfully made or
  582. * an error occurs.
  583. *
  584. * The socket is automatically opened if it is not already open. If the
  585. * connect fails, and the socket was automatically opened, the socket is
  586. * not returned to the closed state.
  587. *
  588. * @param peer_endpoint The remote endpoint to which the socket will be
  589. * connected.
  590. *
  591. * @param ec Set to indicate what error occurred, if any.
  592. *
  593. * @par Example
  594. * @code
  595. * boost::asio::ip::tcp::socket socket(io_service);
  596. * boost::asio::ip::tcp::endpoint endpoint(
  597. * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  598. * boost::system::error_code ec;
  599. * socket.connect(endpoint, ec);
  600. * if (ec)
  601. * {
  602. * // An error occurred.
  603. * }
  604. * @endcode
  605. */
  606. boost::system::error_code connect(const endpoint_type& peer_endpoint,
  607. boost::system::error_code& ec)
  608. {
  609. if (!is_open())
  610. {
  611. if (this->get_service().open(this->get_implementation(),
  612. peer_endpoint.protocol(), ec))
  613. {
  614. return ec;
  615. }
  616. }
  617. return this->get_service().connect(
  618. this->get_implementation(), peer_endpoint, ec);
  619. }
  620. /// Start an asynchronous connect.
  621. /**
  622. * This function is used to asynchronously connect a socket to the specified
  623. * remote endpoint. The function call always returns immediately.
  624. *
  625. * The socket is automatically opened if it is not already open. If the
  626. * connect fails, and the socket was automatically opened, the socket is
  627. * not returned to the closed state.
  628. *
  629. * @param peer_endpoint The remote endpoint to which the socket will be
  630. * connected. Copies will be made of the endpoint object as required.
  631. *
  632. * @param handler The handler to be called when the connection operation
  633. * completes. Copies will be made of the handler as required. The function
  634. * signature of the handler must be:
  635. * @code void handler(
  636. * const boost::system::error_code& error // Result of operation
  637. * ); @endcode
  638. * Regardless of whether the asynchronous operation completes immediately or
  639. * not, the handler will not be invoked from within this function. Invocation
  640. * of the handler will be performed in a manner equivalent to using
  641. * boost::asio::io_service::post().
  642. *
  643. * @par Example
  644. * @code
  645. * void connect_handler(const boost::system::error_code& error)
  646. * {
  647. * if (!error)
  648. * {
  649. * // Connect succeeded.
  650. * }
  651. * }
  652. *
  653. * ...
  654. *
  655. * boost::asio::ip::tcp::socket socket(io_service);
  656. * boost::asio::ip::tcp::endpoint endpoint(
  657. * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  658. * socket.async_connect(endpoint, connect_handler);
  659. * @endcode
  660. */
  661. template <typename ConnectHandler>
  662. void async_connect(const endpoint_type& peer_endpoint,
  663. BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
  664. {
  665. // If you get an error on the following line it means that your handler does
  666. // not meet the documented type requirements for a ConnectHandler.
  667. BOOST_ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
  668. if (!is_open())
  669. {
  670. boost::system::error_code ec;
  671. const protocol_type protocol = peer_endpoint.protocol();
  672. if (this->get_service().open(this->get_implementation(), protocol, ec))
  673. {
  674. this->get_io_service().post(
  675. boost::asio::detail::bind_handler(
  676. BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler), ec));
  677. return;
  678. }
  679. }
  680. this->get_service().async_connect(this->get_implementation(),
  681. peer_endpoint, BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
  682. }
  683. /// Set an option on the socket.
  684. /**
  685. * This function is used to set an option on the socket.
  686. *
  687. * @param option The new option value to be set on the socket.
  688. *
  689. * @throws boost::system::system_error Thrown on failure.
  690. *
  691. * @sa SettableSocketOption @n
  692. * boost::asio::socket_base::broadcast @n
  693. * boost::asio::socket_base::do_not_route @n
  694. * boost::asio::socket_base::keep_alive @n
  695. * boost::asio::socket_base::linger @n
  696. * boost::asio::socket_base::receive_buffer_size @n
  697. * boost::asio::socket_base::receive_low_watermark @n
  698. * boost::asio::socket_base::reuse_address @n
  699. * boost::asio::socket_base::send_buffer_size @n
  700. * boost::asio::socket_base::send_low_watermark @n
  701. * boost::asio::ip::multicast::join_group @n
  702. * boost::asio::ip::multicast::leave_group @n
  703. * boost::asio::ip::multicast::enable_loopback @n
  704. * boost::asio::ip::multicast::outbound_interface @n
  705. * boost::asio::ip::multicast::hops @n
  706. * boost::asio::ip::tcp::no_delay
  707. *
  708. * @par Example
  709. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  710. * @code
  711. * boost::asio::ip::tcp::socket socket(io_service);
  712. * ...
  713. * boost::asio::ip::tcp::no_delay option(true);
  714. * socket.set_option(option);
  715. * @endcode
  716. */
  717. template <typename SettableSocketOption>
  718. void set_option(const SettableSocketOption& option)
  719. {
  720. boost::system::error_code ec;
  721. this->get_service().set_option(this->get_implementation(), option, ec);
  722. boost::asio::detail::throw_error(ec, "set_option");
  723. }
  724. /// Set an option on the socket.
  725. /**
  726. * This function is used to set an option on the socket.
  727. *
  728. * @param option The new option value to be set on the socket.
  729. *
  730. * @param ec Set to indicate what error occurred, if any.
  731. *
  732. * @sa SettableSocketOption @n
  733. * boost::asio::socket_base::broadcast @n
  734. * boost::asio::socket_base::do_not_route @n
  735. * boost::asio::socket_base::keep_alive @n
  736. * boost::asio::socket_base::linger @n
  737. * boost::asio::socket_base::receive_buffer_size @n
  738. * boost::asio::socket_base::receive_low_watermark @n
  739. * boost::asio::socket_base::reuse_address @n
  740. * boost::asio::socket_base::send_buffer_size @n
  741. * boost::asio::socket_base::send_low_watermark @n
  742. * boost::asio::ip::multicast::join_group @n
  743. * boost::asio::ip::multicast::leave_group @n
  744. * boost::asio::ip::multicast::enable_loopback @n
  745. * boost::asio::ip::multicast::outbound_interface @n
  746. * boost::asio::ip::multicast::hops @n
  747. * boost::asio::ip::tcp::no_delay
  748. *
  749. * @par Example
  750. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  751. * @code
  752. * boost::asio::ip::tcp::socket socket(io_service);
  753. * ...
  754. * boost::asio::ip::tcp::no_delay option(true);
  755. * boost::system::error_code ec;
  756. * socket.set_option(option, ec);
  757. * if (ec)
  758. * {
  759. * // An error occurred.
  760. * }
  761. * @endcode
  762. */
  763. template <typename SettableSocketOption>
  764. boost::system::error_code set_option(const SettableSocketOption& option,
  765. boost::system::error_code& ec)
  766. {
  767. return this->get_service().set_option(
  768. this->get_implementation(), option, ec);
  769. }
  770. /// Get an option from the socket.
  771. /**
  772. * This function is used to get the current value of an option on the socket.
  773. *
  774. * @param option The option value to be obtained from the socket.
  775. *
  776. * @throws boost::system::system_error Thrown on failure.
  777. *
  778. * @sa GettableSocketOption @n
  779. * boost::asio::socket_base::broadcast @n
  780. * boost::asio::socket_base::do_not_route @n
  781. * boost::asio::socket_base::keep_alive @n
  782. * boost::asio::socket_base::linger @n
  783. * boost::asio::socket_base::receive_buffer_size @n
  784. * boost::asio::socket_base::receive_low_watermark @n
  785. * boost::asio::socket_base::reuse_address @n
  786. * boost::asio::socket_base::send_buffer_size @n
  787. * boost::asio::socket_base::send_low_watermark @n
  788. * boost::asio::ip::multicast::join_group @n
  789. * boost::asio::ip::multicast::leave_group @n
  790. * boost::asio::ip::multicast::enable_loopback @n
  791. * boost::asio::ip::multicast::outbound_interface @n
  792. * boost::asio::ip::multicast::hops @n
  793. * boost::asio::ip::tcp::no_delay
  794. *
  795. * @par Example
  796. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  797. * @code
  798. * boost::asio::ip::tcp::socket socket(io_service);
  799. * ...
  800. * boost::asio::ip::tcp::socket::keep_alive option;
  801. * socket.get_option(option);
  802. * bool is_set = option.get();
  803. * @endcode
  804. */
  805. template <typename GettableSocketOption>
  806. void get_option(GettableSocketOption& option) const
  807. {
  808. boost::system::error_code ec;
  809. this->get_service().get_option(this->get_implementation(), option, ec);
  810. boost::asio::detail::throw_error(ec, "get_option");
  811. }
  812. /// Get an option from the socket.
  813. /**
  814. * This function is used to get the current value of an option on the socket.
  815. *
  816. * @param option The option value to be obtained from the socket.
  817. *
  818. * @param ec Set to indicate what error occurred, if any.
  819. *
  820. * @sa GettableSocketOption @n
  821. * boost::asio::socket_base::broadcast @n
  822. * boost::asio::socket_base::do_not_route @n
  823. * boost::asio::socket_base::keep_alive @n
  824. * boost::asio::socket_base::linger @n
  825. * boost::asio::socket_base::receive_buffer_size @n
  826. * boost::asio::socket_base::receive_low_watermark @n
  827. * boost::asio::socket_base::reuse_address @n
  828. * boost::asio::socket_base::send_buffer_size @n
  829. * boost::asio::socket_base::send_low_watermark @n
  830. * boost::asio::ip::multicast::join_group @n
  831. * boost::asio::ip::multicast::leave_group @n
  832. * boost::asio::ip::multicast::enable_loopback @n
  833. * boost::asio::ip::multicast::outbound_interface @n
  834. * boost::asio::ip::multicast::hops @n
  835. * boost::asio::ip::tcp::no_delay
  836. *
  837. * @par Example
  838. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  839. * @code
  840. * boost::asio::ip::tcp::socket socket(io_service);
  841. * ...
  842. * boost::asio::ip::tcp::socket::keep_alive option;
  843. * boost::system::error_code ec;
  844. * socket.get_option(option, ec);
  845. * if (ec)
  846. * {
  847. * // An error occurred.
  848. * }
  849. * bool is_set = option.get();
  850. * @endcode
  851. */
  852. template <typename GettableSocketOption>
  853. boost::system::error_code get_option(GettableSocketOption& option,
  854. boost::system::error_code& ec) const
  855. {
  856. return this->get_service().get_option(
  857. this->get_implementation(), option, ec);
  858. }
  859. /// Perform an IO control command on the socket.
  860. /**
  861. * This function is used to execute an IO control command on the socket.
  862. *
  863. * @param command The IO control command to be performed on the socket.
  864. *
  865. * @throws boost::system::system_error Thrown on failure.
  866. *
  867. * @sa IoControlCommand @n
  868. * boost::asio::socket_base::bytes_readable @n
  869. * boost::asio::socket_base::non_blocking_io
  870. *
  871. * @par Example
  872. * Getting the number of bytes ready to read:
  873. * @code
  874. * boost::asio::ip::tcp::socket socket(io_service);
  875. * ...
  876. * boost::asio::ip::tcp::socket::bytes_readable command;
  877. * socket.io_control(command);
  878. * std::size_t bytes_readable = command.get();
  879. * @endcode
  880. */
  881. template <typename IoControlCommand>
  882. void io_control(IoControlCommand& command)
  883. {
  884. boost::system::error_code ec;
  885. this->get_service().io_control(this->get_implementation(), command, ec);
  886. boost::asio::detail::throw_error(ec, "io_control");
  887. }
  888. /// Perform an IO control command on the socket.
  889. /**
  890. * This function is used to execute an IO control command on the socket.
  891. *
  892. * @param command The IO control command to be performed on the socket.
  893. *
  894. * @param ec Set to indicate what error occurred, if any.
  895. *
  896. * @sa IoControlCommand @n
  897. * boost::asio::socket_base::bytes_readable @n
  898. * boost::asio::socket_base::non_blocking_io
  899. *
  900. * @par Example
  901. * Getting the number of bytes ready to read:
  902. * @code
  903. * boost::asio::ip::tcp::socket socket(io_service);
  904. * ...
  905. * boost::asio::ip::tcp::socket::bytes_readable command;
  906. * boost::system::error_code ec;
  907. * socket.io_control(command, ec);
  908. * if (ec)
  909. * {
  910. * // An error occurred.
  911. * }
  912. * std::size_t bytes_readable = command.get();
  913. * @endcode
  914. */
  915. template <typename IoControlCommand>
  916. boost::system::error_code io_control(IoControlCommand& command,
  917. boost::system::error_code& ec)
  918. {
  919. return this->get_service().io_control(
  920. this->get_implementation(), command, ec);
  921. }
  922. /// Gets the non-blocking mode of the socket.
  923. /**
  924. * @returns @c true if the socket's synchronous operations will fail with
  925. * boost::asio::error::would_block if they are unable to perform the requested
  926. * operation immediately. If @c false, synchronous operations will block
  927. * until complete.
  928. *
  929. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  930. * operations. Asynchronous operations will never fail with the error
  931. * boost::asio::error::would_block.
  932. */
  933. bool non_blocking() const
  934. {
  935. return this->get_service().non_blocking(this->get_implementation());
  936. }
  937. /// Sets the non-blocking mode of the socket.
  938. /**
  939. * @param mode If @c true, the socket's synchronous operations will fail with
  940. * boost::asio::error::would_block if they are unable to perform the requested
  941. * operation immediately. If @c false, synchronous operations will block
  942. * until complete.
  943. *
  944. * @throws boost::system::system_error Thrown on failure.
  945. *
  946. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  947. * operations. Asynchronous operations will never fail with the error
  948. * boost::asio::error::would_block.
  949. */
  950. void non_blocking(bool mode)
  951. {
  952. boost::system::error_code ec;
  953. this->get_service().non_blocking(this->get_implementation(), mode, ec);
  954. boost::asio::detail::throw_error(ec, "non_blocking");
  955. }
  956. /// Sets the non-blocking mode of the socket.
  957. /**
  958. * @param mode If @c true, the socket's synchronous operations will fail with
  959. * boost::asio::error::would_block if they are unable to perform the requested
  960. * operation immediately. If @c false, synchronous operations will block
  961. * until complete.
  962. *
  963. * @param ec Set to indicate what error occurred, if any.
  964. *
  965. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  966. * operations. Asynchronous operations will never fail with the error
  967. * boost::asio::error::would_block.
  968. */
  969. boost::system::error_code non_blocking(
  970. bool mode, boost::system::error_code& ec)
  971. {
  972. return this->get_service().non_blocking(
  973. this->get_implementation(), mode, ec);
  974. }
  975. /// Gets the non-blocking mode of the native socket implementation.
  976. /**
  977. * This function is used to retrieve the non-blocking mode of the underlying
  978. * native socket. This mode has no effect on the behaviour of the socket
  979. * object's synchronous operations.
  980. *
  981. * @returns @c true if the underlying socket is in non-blocking mode and
  982. * direct system calls may fail with boost::asio::error::would_block (or the
  983. * equivalent system error).
  984. *
  985. * @note The current non-blocking mode is cached by the socket object.
  986. * Consequently, the return value may be incorrect if the non-blocking mode
  987. * was set directly on the native socket.
  988. *
  989. * @par Example
  990. * This function is intended to allow the encapsulation of arbitrary
  991. * non-blocking system calls as asynchronous operations, in a way that is
  992. * transparent to the user of the socket object. The following example
  993. * illustrates how Linux's @c sendfile system call might be encapsulated:
  994. * @code template <typename Handler>
  995. * struct sendfile_op
  996. * {
  997. * tcp::socket& sock_;
  998. * int fd_;
  999. * Handler handler_;
  1000. * off_t offset_;
  1001. * std::size_t total_bytes_transferred_;
  1002. *
  1003. * // Function call operator meeting WriteHandler requirements.
  1004. * // Used as the handler for the async_write_some operation.
  1005. * void operator()(boost::system::error_code ec, std::size_t)
  1006. * {
  1007. * // Put the underlying socket into non-blocking mode.
  1008. * if (!ec)
  1009. * if (!sock_.native_non_blocking())
  1010. * sock_.native_non_blocking(true, ec);
  1011. *
  1012. * if (!ec)
  1013. * {
  1014. * for (;;)
  1015. * {
  1016. * // Try the system call.
  1017. * errno = 0;
  1018. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1019. * ec = boost::system::error_code(n < 0 ? errno : 0,
  1020. * boost::asio::error::get_system_category());
  1021. * total_bytes_transferred_ += ec ? 0 : n;
  1022. *
  1023. * // Retry operation immediately if interrupted by signal.
  1024. * if (ec == boost::asio::error::interrupted)
  1025. * continue;
  1026. *
  1027. * // Check if we need to run the operation again.
  1028. * if (ec == boost::asio::error::would_block
  1029. * || ec == boost::asio::error::try_again)
  1030. * {
  1031. * // We have to wait for the socket to become ready again.
  1032. * sock_.async_write_some(boost::asio::null_buffers(), *this);
  1033. * return;
  1034. * }
  1035. *
  1036. * if (ec || n == 0)
  1037. * {
  1038. * // An error occurred, or we have reached the end of the file.
  1039. * // Either way we must exit the loop so we can call the handler.
  1040. * break;
  1041. * }
  1042. *
  1043. * // Loop around to try calling sendfile again.
  1044. * }
  1045. * }
  1046. *
  1047. * // Pass result back to user's handler.
  1048. * handler_(ec, total_bytes_transferred_);
  1049. * }
  1050. * };
  1051. *
  1052. * template <typename Handler>
  1053. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1054. * {
  1055. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1056. * sock.async_write_some(boost::asio::null_buffers(), op);
  1057. * } @endcode
  1058. */
  1059. bool native_non_blocking() const
  1060. {
  1061. return this->get_service().native_non_blocking(this->get_implementation());
  1062. }
  1063. /// Sets the non-blocking mode of the native socket implementation.
  1064. /**
  1065. * This function is used to modify the non-blocking mode of the underlying
  1066. * native socket. It has no effect on the behaviour of the socket object's
  1067. * synchronous operations.
  1068. *
  1069. * @param mode If @c true, the underlying socket is put into non-blocking
  1070. * mode and direct system calls may fail with boost::asio::error::would_block
  1071. * (or the equivalent system error).
  1072. *
  1073. * @throws boost::system::system_error Thrown on failure. If the @c mode is
  1074. * @c false, but the current value of @c non_blocking() is @c true, this
  1075. * function fails with boost::asio::error::invalid_argument, as the
  1076. * combination does not make sense.
  1077. *
  1078. * @par Example
  1079. * This function is intended to allow the encapsulation of arbitrary
  1080. * non-blocking system calls as asynchronous operations, in a way that is
  1081. * transparent to the user of the socket object. The following example
  1082. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1083. * @code template <typename Handler>
  1084. * struct sendfile_op
  1085. * {
  1086. * tcp::socket& sock_;
  1087. * int fd_;
  1088. * Handler handler_;
  1089. * off_t offset_;
  1090. * std::size_t total_bytes_transferred_;
  1091. *
  1092. * // Function call operator meeting WriteHandler requirements.
  1093. * // Used as the handler for the async_write_some operation.
  1094. * void operator()(boost::system::error_code ec, std::size_t)
  1095. * {
  1096. * // Put the underlying socket into non-blocking mode.
  1097. * if (!ec)
  1098. * if (!sock_.native_non_blocking())
  1099. * sock_.native_non_blocking(true, ec);
  1100. *
  1101. * if (!ec)
  1102. * {
  1103. * for (;;)
  1104. * {
  1105. * // Try the system call.
  1106. * errno = 0;
  1107. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1108. * ec = boost::system::error_code(n < 0 ? errno : 0,
  1109. * boost::asio::error::get_system_category());
  1110. * total_bytes_transferred_ += ec ? 0 : n;
  1111. *
  1112. * // Retry operation immediately if interrupted by signal.
  1113. * if (ec == boost::asio::error::interrupted)
  1114. * continue;
  1115. *
  1116. * // Check if we need to run the operation again.
  1117. * if (ec == boost::asio::error::would_block
  1118. * || ec == boost::asio::error::try_again)
  1119. * {
  1120. * // We have to wait for the socket to become ready again.
  1121. * sock_.async_write_some(boost::asio::null_buffers(), *this);
  1122. * return;
  1123. * }
  1124. *
  1125. * if (ec || n == 0)
  1126. * {
  1127. * // An error occurred, or we have reached the end of the file.
  1128. * // Either way we must exit the loop so we can call the handler.
  1129. * break;
  1130. * }
  1131. *
  1132. * // Loop around to try calling sendfile again.
  1133. * }
  1134. * }
  1135. *
  1136. * // Pass result back to user's handler.
  1137. * handler_(ec, total_bytes_transferred_);
  1138. * }
  1139. * };
  1140. *
  1141. * template <typename Handler>
  1142. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1143. * {
  1144. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1145. * sock.async_write_some(boost::asio::null_buffers(), op);
  1146. * } @endcode
  1147. */
  1148. void native_non_blocking(bool mode)
  1149. {
  1150. boost::system::error_code ec;
  1151. this->get_service().native_non_blocking(
  1152. this->get_implementation(), mode, ec);
  1153. boost::asio::detail::throw_error(ec, "native_non_blocking");
  1154. }
  1155. /// Sets the non-blocking mode of the native socket implementation.
  1156. /**
  1157. * This function is used to modify the non-blocking mode of the underlying
  1158. * native socket. It has no effect on the behaviour of the socket object's
  1159. * synchronous operations.
  1160. *
  1161. * @param mode If @c true, the underlying socket is put into non-blocking
  1162. * mode and direct system calls may fail with boost::asio::error::would_block
  1163. * (or the equivalent system error).
  1164. *
  1165. * @param ec Set to indicate what error occurred, if any. If the @c mode is
  1166. * @c false, but the current value of @c non_blocking() is @c true, this
  1167. * function fails with boost::asio::error::invalid_argument, as the
  1168. * combination does not make sense.
  1169. *
  1170. * @par Example
  1171. * This function is intended to allow the encapsulation of arbitrary
  1172. * non-blocking system calls as asynchronous operations, in a way that is
  1173. * transparent to the user of the socket object. The following example
  1174. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1175. * @code template <typename Handler>
  1176. * struct sendfile_op
  1177. * {
  1178. * tcp::socket& sock_;
  1179. * int fd_;
  1180. * Handler handler_;
  1181. * off_t offset_;
  1182. * std::size_t total_bytes_transferred_;
  1183. *
  1184. * // Function call operator meeting WriteHandler requirements.
  1185. * // Used as the handler for the async_write_some operation.
  1186. * void operator()(boost::system::error_code ec, std::size_t)
  1187. * {
  1188. * // Put the underlying socket into non-blocking mode.
  1189. * if (!ec)
  1190. * if (!sock_.native_non_blocking())
  1191. * sock_.native_non_blocking(true, ec);
  1192. *
  1193. * if (!ec)
  1194. * {
  1195. * for (;;)
  1196. * {
  1197. * // Try the system call.
  1198. * errno = 0;
  1199. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1200. * ec = boost::system::error_code(n < 0 ? errno : 0,
  1201. * boost::asio::error::get_system_category());
  1202. * total_bytes_transferred_ += ec ? 0 : n;
  1203. *
  1204. * // Retry operation immediately if interrupted by signal.
  1205. * if (ec == boost::asio::error::interrupted)
  1206. * continue;
  1207. *
  1208. * // Check if we need to run the operation again.
  1209. * if (ec == boost::asio::error::would_block
  1210. * || ec == boost::asio::error::try_again)
  1211. * {
  1212. * // We have to wait for the socket to become ready again.
  1213. * sock_.async_write_some(boost::asio::null_buffers(), *this);
  1214. * return;
  1215. * }
  1216. *
  1217. * if (ec || n == 0)
  1218. * {
  1219. * // An error occurred, or we have reached the end of the file.
  1220. * // Either way we must exit the loop so we can call the handler.
  1221. * break;
  1222. * }
  1223. *
  1224. * // Loop around to try calling sendfile again.
  1225. * }
  1226. * }
  1227. *
  1228. * // Pass result back to user's handler.
  1229. * handler_(ec, total_bytes_transferred_);
  1230. * }
  1231. * };
  1232. *
  1233. * template <typename Handler>
  1234. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1235. * {
  1236. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1237. * sock.async_write_some(boost::asio::null_buffers(), op);
  1238. * } @endcode
  1239. */
  1240. boost::system::error_code native_non_blocking(
  1241. bool mode, boost::system::error_code& ec)
  1242. {
  1243. return this->get_service().native_non_blocking(
  1244. this->get_implementation(), mode, ec);
  1245. }
  1246. /// Get the local endpoint of the socket.
  1247. /**
  1248. * This function is used to obtain the locally bound endpoint of the socket.
  1249. *
  1250. * @returns An object that represents the local endpoint of the socket.
  1251. *
  1252. * @throws boost::system::system_error Thrown on failure.
  1253. *
  1254. * @par Example
  1255. * @code
  1256. * boost::asio::ip::tcp::socket socket(io_service);
  1257. * ...
  1258. * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
  1259. * @endcode
  1260. */
  1261. endpoint_type local_endpoint() const
  1262. {
  1263. boost::system::error_code ec;
  1264. endpoint_type ep = this->get_service().local_endpoint(
  1265. this->get_implementation(), ec);
  1266. boost::asio::detail::throw_error(ec, "local_endpoint");
  1267. return ep;
  1268. }
  1269. /// Get the local endpoint of the socket.
  1270. /**
  1271. * This function is used to obtain the locally bound endpoint of the socket.
  1272. *
  1273. * @param ec Set to indicate what error occurred, if any.
  1274. *
  1275. * @returns An object that represents the local endpoint of the socket.
  1276. * Returns a default-constructed endpoint object if an error occurred.
  1277. *
  1278. * @par Example
  1279. * @code
  1280. * boost::asio::ip::tcp::socket socket(io_service);
  1281. * ...
  1282. * boost::system::error_code ec;
  1283. * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
  1284. * if (ec)
  1285. * {
  1286. * // An error occurred.
  1287. * }
  1288. * @endcode
  1289. */
  1290. endpoint_type local_endpoint(boost::system::error_code& ec) const
  1291. {
  1292. return this->get_service().local_endpoint(this->get_implementation(), ec);
  1293. }
  1294. /// Get the remote endpoint of the socket.
  1295. /**
  1296. * This function is used to obtain the remote endpoint of the socket.
  1297. *
  1298. * @returns An object that represents the remote endpoint of the socket.
  1299. *
  1300. * @throws boost::system::system_error Thrown on failure.
  1301. *
  1302. * @par Example
  1303. * @code
  1304. * boost::asio::ip::tcp::socket socket(io_service);
  1305. * ...
  1306. * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
  1307. * @endcode
  1308. */
  1309. endpoint_type remote_endpoint() const
  1310. {
  1311. boost::system::error_code ec;
  1312. endpoint_type ep = this->get_service().remote_endpoint(
  1313. this->get_implementation(), ec);
  1314. boost::asio::detail::throw_error(ec, "remote_endpoint");
  1315. return ep;
  1316. }
  1317. /// Get the remote endpoint of the socket.
  1318. /**
  1319. * This function is used to obtain the remote endpoint of the socket.
  1320. *
  1321. * @param ec Set to indicate what error occurred, if any.
  1322. *
  1323. * @returns An object that represents the remote endpoint of the socket.
  1324. * Returns a default-constructed endpoint object if an error occurred.
  1325. *
  1326. * @par Example
  1327. * @code
  1328. * boost::asio::ip::tcp::socket socket(io_service);
  1329. * ...
  1330. * boost::system::error_code ec;
  1331. * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
  1332. * if (ec)
  1333. * {
  1334. * // An error occurred.
  1335. * }
  1336. * @endcode
  1337. */
  1338. endpoint_type remote_endpoint(boost::system::error_code& ec) const
  1339. {
  1340. return this->get_service().remote_endpoint(this->get_implementation(), ec);
  1341. }
  1342. /// Disable sends or receives on the socket.
  1343. /**
  1344. * This function is used to disable send operations, receive operations, or
  1345. * both.
  1346. *
  1347. * @param what Determines what types of operation will no longer be allowed.
  1348. *
  1349. * @throws boost::system::system_error Thrown on failure.
  1350. *
  1351. * @par Example
  1352. * Shutting down the send side of the socket:
  1353. * @code
  1354. * boost::asio::ip::tcp::socket socket(io_service);
  1355. * ...
  1356. * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
  1357. * @endcode
  1358. */
  1359. void shutdown(shutdown_type what)
  1360. {
  1361. boost::system::error_code ec;
  1362. this->get_service().shutdown(this->get_implementation(), what, ec);
  1363. boost::asio::detail::throw_error(ec, "shutdown");
  1364. }
  1365. /// Disable sends or receives on the socket.
  1366. /**
  1367. * This function is used to disable send operations, receive operations, or
  1368. * both.
  1369. *
  1370. * @param what Determines what types of operation will no longer be allowed.
  1371. *
  1372. * @param ec Set to indicate what error occurred, if any.
  1373. *
  1374. * @par Example
  1375. * Shutting down the send side of the socket:
  1376. * @code
  1377. * boost::asio::ip::tcp::socket socket(io_service);
  1378. * ...
  1379. * boost::system::error_code ec;
  1380. * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
  1381. * if (ec)
  1382. * {
  1383. * // An error occurred.
  1384. * }
  1385. * @endcode
  1386. */
  1387. boost::system::error_code shutdown(shutdown_type what,
  1388. boost::system::error_code& ec)
  1389. {
  1390. return this->get_service().shutdown(this->get_implementation(), what, ec);
  1391. }
  1392. protected:
  1393. /// Protected destructor to prevent deletion through this type.
  1394. ~basic_socket()
  1395. {
  1396. }
  1397. };
  1398. } // namespace asio
  1399. } // namespace boost
  1400. #include <boost/asio/detail/pop_options.hpp>
  1401. #endif // BOOST_ASIO_BASIC_SOCKET_HPP