basic_stream.hpp 49 KB

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