basic_socket.hpp 32 KB

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