PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/sdk/include/boost/asio/basic_socket.hpp

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