basic_socket.hpp 34 KB

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