basic_socket.hpp 32 KB

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