basic_stream_socket.hpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. //
  2. // basic_stream_socket.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_STREAM_SOCKET_HPP
  11. #define ASIO_BASIC_STREAM_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 <cstddef>
  17. #include "asio/async_result.hpp"
  18. #include "asio/basic_socket.hpp"
  19. #include "asio/detail/handler_type_requirements.hpp"
  20. #include "asio/detail/non_const_lvalue.hpp"
  21. #include "asio/detail/throw_error.hpp"
  22. #include "asio/error.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. #if !defined(ASIO_BASIC_STREAM_SOCKET_FWD_DECL)
  26. #define ASIO_BASIC_STREAM_SOCKET_FWD_DECL
  27. // Forward declaration with defaulted arguments.
  28. template <typename Protocol, typename Executor = any_io_executor>
  29. class basic_stream_socket;
  30. #endif // !defined(ASIO_BASIC_STREAM_SOCKET_FWD_DECL)
  31. /// Provides stream-oriented socket functionality.
  32. /**
  33. * The basic_stream_socket class template provides asynchronous and blocking
  34. * stream-oriented socket functionality.
  35. *
  36. * @par Thread Safety
  37. * @e Distinct @e objects: Safe.@n
  38. * @e Shared @e objects: Unsafe.
  39. *
  40. * Synchronous @c send, @c receive, and @c connect operations are thread safe
  41. * with respect to each other, if the underlying operating system calls are
  42. * also thread safe. This means that it is permitted to perform concurrent
  43. * calls to these synchronous operations on a single socket object. Other
  44. * synchronous operations, such as @c open or @c close, are not thread safe.
  45. *
  46. * @par Concepts:
  47. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  48. */
  49. template <typename Protocol, typename Executor>
  50. class basic_stream_socket
  51. : public basic_socket<Protocol, Executor>
  52. {
  53. public:
  54. /// The type of the executor associated with the object.
  55. typedef Executor executor_type;
  56. /// Rebinds the socket type to another executor.
  57. template <typename Executor1>
  58. struct rebind_executor
  59. {
  60. /// The socket type when rebound to the specified executor.
  61. typedef basic_stream_socket<Protocol, Executor1> other;
  62. };
  63. /// The native representation of a socket.
  64. #if defined(GENERATING_DOCUMENTATION)
  65. typedef implementation_defined native_handle_type;
  66. #else
  67. typedef typename basic_socket<Protocol,
  68. Executor>::native_handle_type native_handle_type;
  69. #endif
  70. /// The protocol type.
  71. typedef Protocol protocol_type;
  72. /// The endpoint type.
  73. typedef typename Protocol::endpoint endpoint_type;
  74. /// Construct a basic_stream_socket without opening it.
  75. /**
  76. * This constructor creates a stream socket without opening it. The socket
  77. * needs to be opened and then connected or accepted before data can be sent
  78. * or received on it.
  79. *
  80. * @param ex The I/O executor that the socket will use, by default, to
  81. * dispatch handlers for any asynchronous operations performed on the socket.
  82. */
  83. explicit basic_stream_socket(const executor_type& ex)
  84. : basic_socket<Protocol, Executor>(ex)
  85. {
  86. }
  87. /// Construct a basic_stream_socket without opening it.
  88. /**
  89. * This constructor creates a stream socket without opening it. The socket
  90. * needs to be opened and then connected or accepted before data can be sent
  91. * or received on it.
  92. *
  93. * @param context An execution context which provides the I/O executor that
  94. * the socket will use, by default, to dispatch handlers for any asynchronous
  95. * operations performed on the socket.
  96. */
  97. template <typename ExecutionContext>
  98. explicit basic_stream_socket(ExecutionContext& context,
  99. typename constraint<
  100. is_convertible<ExecutionContext&, execution_context&>::value
  101. >::type = 0)
  102. : basic_socket<Protocol, Executor>(context)
  103. {
  104. }
  105. /// Construct and open a basic_stream_socket.
  106. /**
  107. * This constructor creates and opens a stream socket. The socket needs to be
  108. * connected or accepted before data can be sent or received on it.
  109. *
  110. * @param ex The I/O executor that the socket will use, by default, to
  111. * dispatch handlers for any asynchronous operations performed on the socket.
  112. *
  113. * @param protocol An object specifying protocol parameters to be used.
  114. *
  115. * @throws asio::system_error Thrown on failure.
  116. */
  117. basic_stream_socket(const executor_type& ex, const protocol_type& protocol)
  118. : basic_socket<Protocol, Executor>(ex, protocol)
  119. {
  120. }
  121. /// Construct and open a basic_stream_socket.
  122. /**
  123. * This constructor creates and opens a stream socket. The socket needs to be
  124. * connected or accepted before data can be sent or received on it.
  125. *
  126. * @param context An execution context which provides the I/O executor that
  127. * the socket will use, by default, to dispatch handlers for any asynchronous
  128. * operations performed on the socket.
  129. *
  130. * @param protocol An object specifying protocol parameters to be used.
  131. *
  132. * @throws asio::system_error Thrown on failure.
  133. */
  134. template <typename ExecutionContext>
  135. basic_stream_socket(ExecutionContext& context, const protocol_type& protocol,
  136. typename constraint<
  137. is_convertible<ExecutionContext&, execution_context&>::value,
  138. defaulted_constraint
  139. >::type = defaulted_constraint())
  140. : basic_socket<Protocol, Executor>(context, protocol)
  141. {
  142. }
  143. /// Construct a basic_stream_socket, opening it and binding it to the given
  144. /// local endpoint.
  145. /**
  146. * This constructor creates a stream socket and automatically opens it bound
  147. * to the specified endpoint on the local machine. The protocol used is the
  148. * protocol associated with the given endpoint.
  149. *
  150. * @param ex The I/O executor that the socket will use, by default, to
  151. * dispatch handlers for any asynchronous operations performed on the socket.
  152. *
  153. * @param endpoint An endpoint on the local machine to which the stream
  154. * socket will be bound.
  155. *
  156. * @throws asio::system_error Thrown on failure.
  157. */
  158. basic_stream_socket(const executor_type& ex, const endpoint_type& endpoint)
  159. : basic_socket<Protocol, Executor>(ex, endpoint)
  160. {
  161. }
  162. /// Construct a basic_stream_socket, opening it and binding it to the given
  163. /// local endpoint.
  164. /**
  165. * This constructor creates a stream socket and automatically opens it bound
  166. * to the specified endpoint on the local machine. The protocol used is the
  167. * protocol associated with the given endpoint.
  168. *
  169. * @param context An execution context which provides the I/O executor that
  170. * the socket will use, by default, to dispatch handlers for any asynchronous
  171. * operations performed on the socket.
  172. *
  173. * @param endpoint An endpoint on the local machine to which the stream
  174. * socket will be bound.
  175. *
  176. * @throws asio::system_error Thrown on failure.
  177. */
  178. template <typename ExecutionContext>
  179. basic_stream_socket(ExecutionContext& context, const endpoint_type& endpoint,
  180. typename constraint<
  181. is_convertible<ExecutionContext&, execution_context&>::value
  182. >::type = 0)
  183. : basic_socket<Protocol, Executor>(context, endpoint)
  184. {
  185. }
  186. /// Construct a basic_stream_socket on an existing native socket.
  187. /**
  188. * This constructor creates a stream socket object to hold an existing native
  189. * socket.
  190. *
  191. * @param ex The I/O executor that the socket will use, by default, to
  192. * dispatch handlers for any asynchronous operations performed on the socket.
  193. *
  194. * @param protocol An object specifying protocol parameters to be used.
  195. *
  196. * @param native_socket The new underlying socket implementation.
  197. *
  198. * @throws asio::system_error Thrown on failure.
  199. */
  200. basic_stream_socket(const executor_type& ex,
  201. const protocol_type& protocol, const native_handle_type& native_socket)
  202. : basic_socket<Protocol, Executor>(ex, protocol, native_socket)
  203. {
  204. }
  205. /// Construct a basic_stream_socket on an existing native socket.
  206. /**
  207. * This constructor creates a stream socket object to hold an existing native
  208. * socket.
  209. *
  210. * @param context An execution context which provides the I/O executor that
  211. * the socket will use, by default, to dispatch handlers for any asynchronous
  212. * operations performed on the socket.
  213. *
  214. * @param protocol An object specifying protocol parameters to be used.
  215. *
  216. * @param native_socket The new underlying socket implementation.
  217. *
  218. * @throws asio::system_error Thrown on failure.
  219. */
  220. template <typename ExecutionContext>
  221. basic_stream_socket(ExecutionContext& context,
  222. const protocol_type& protocol, const native_handle_type& native_socket,
  223. typename constraint<
  224. is_convertible<ExecutionContext&, execution_context&>::value
  225. >::type = 0)
  226. : basic_socket<Protocol, Executor>(context, protocol, native_socket)
  227. {
  228. }
  229. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  230. /// Move-construct a basic_stream_socket from another.
  231. /**
  232. * This constructor moves a stream socket from one object to another.
  233. *
  234. * @param other The other basic_stream_socket object from which the move
  235. * will occur.
  236. *
  237. * @note Following the move, the moved-from object is in the same state as if
  238. * constructed using the @c basic_stream_socket(const executor_type&)
  239. * constructor.
  240. */
  241. basic_stream_socket(basic_stream_socket&& other) ASIO_NOEXCEPT
  242. : basic_socket<Protocol, Executor>(std::move(other))
  243. {
  244. }
  245. /// Move-assign a basic_stream_socket from another.
  246. /**
  247. * This assignment operator moves a stream socket from one object to another.
  248. *
  249. * @param other The other basic_stream_socket object from which the move
  250. * will occur.
  251. *
  252. * @note Following the move, the moved-from object is in the same state as if
  253. * constructed using the @c basic_stream_socket(const executor_type&)
  254. * constructor.
  255. */
  256. basic_stream_socket& operator=(basic_stream_socket&& other)
  257. {
  258. basic_socket<Protocol, Executor>::operator=(std::move(other));
  259. return *this;
  260. }
  261. /// Move-construct a basic_stream_socket from a socket of another protocol
  262. /// type.
  263. /**
  264. * This constructor moves a stream socket from one object to another.
  265. *
  266. * @param other The other basic_stream_socket object from which the move
  267. * will occur.
  268. *
  269. * @note Following the move, the moved-from object is in the same state as if
  270. * constructed using the @c basic_stream_socket(const executor_type&)
  271. * constructor.
  272. */
  273. template <typename Protocol1, typename Executor1>
  274. basic_stream_socket(basic_stream_socket<Protocol1, Executor1>&& other,
  275. typename constraint<
  276. is_convertible<Protocol1, Protocol>::value
  277. && is_convertible<Executor1, Executor>::value
  278. >::type = 0)
  279. : basic_socket<Protocol, Executor>(std::move(other))
  280. {
  281. }
  282. /// Move-assign a basic_stream_socket from a socket of another protocol type.
  283. /**
  284. * This assignment operator moves a stream socket from one object to another.
  285. *
  286. * @param other The other basic_stream_socket object from which the move
  287. * will occur.
  288. *
  289. * @note Following the move, the moved-from object is in the same state as if
  290. * constructed using the @c basic_stream_socket(const executor_type&)
  291. * constructor.
  292. */
  293. template <typename Protocol1, typename Executor1>
  294. typename constraint<
  295. is_convertible<Protocol1, Protocol>::value
  296. && is_convertible<Executor1, Executor>::value,
  297. basic_stream_socket&
  298. >::type operator=(basic_stream_socket<Protocol1, Executor1>&& other)
  299. {
  300. basic_socket<Protocol, Executor>::operator=(std::move(other));
  301. return *this;
  302. }
  303. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  304. /// Destroys the socket.
  305. /**
  306. * This function destroys the socket, cancelling any outstanding asynchronous
  307. * operations associated with the socket as if by calling @c cancel.
  308. */
  309. ~basic_stream_socket()
  310. {
  311. }
  312. /// Send some data on the socket.
  313. /**
  314. * This function is used to send data on the stream socket. The function
  315. * call will block until one or more bytes of the data has been sent
  316. * successfully, or an until error occurs.
  317. *
  318. * @param buffers One or more data buffers to be sent on the socket.
  319. *
  320. * @returns The number of bytes sent.
  321. *
  322. * @throws asio::system_error Thrown on failure.
  323. *
  324. * @note The send operation may not transmit all of the data to the peer.
  325. * Consider using the @ref write function if you need to ensure that all data
  326. * is written before the blocking operation completes.
  327. *
  328. * @par Example
  329. * To send a single data buffer use the @ref buffer function as follows:
  330. * @code
  331. * socket.send(asio::buffer(data, size));
  332. * @endcode
  333. * See the @ref buffer documentation for information on sending multiple
  334. * buffers in one go, and how to use it with arrays, boost::array or
  335. * std::vector.
  336. */
  337. template <typename ConstBufferSequence>
  338. std::size_t send(const ConstBufferSequence& buffers)
  339. {
  340. asio::error_code ec;
  341. std::size_t s = this->impl_.get_service().send(
  342. this->impl_.get_implementation(), buffers, 0, ec);
  343. asio::detail::throw_error(ec, "send");
  344. return s;
  345. }
  346. /// Send some data on the socket.
  347. /**
  348. * This function is used to send data on the stream socket. The function
  349. * call will block until one or more bytes of the data has been sent
  350. * successfully, or an until error occurs.
  351. *
  352. * @param buffers One or more data buffers to be sent on the socket.
  353. *
  354. * @param flags Flags specifying how the send call is to be made.
  355. *
  356. * @returns The number of bytes sent.
  357. *
  358. * @throws asio::system_error Thrown on failure.
  359. *
  360. * @note The send operation may not transmit all of the data to the peer.
  361. * Consider using the @ref write function if you need to ensure that all data
  362. * is written before the blocking operation completes.
  363. *
  364. * @par Example
  365. * To send a single data buffer use the @ref buffer function as follows:
  366. * @code
  367. * socket.send(asio::buffer(data, size), 0);
  368. * @endcode
  369. * See the @ref buffer documentation for information on sending multiple
  370. * buffers in one go, and how to use it with arrays, boost::array or
  371. * std::vector.
  372. */
  373. template <typename ConstBufferSequence>
  374. std::size_t send(const ConstBufferSequence& buffers,
  375. socket_base::message_flags flags)
  376. {
  377. asio::error_code ec;
  378. std::size_t s = this->impl_.get_service().send(
  379. this->impl_.get_implementation(), buffers, flags, ec);
  380. asio::detail::throw_error(ec, "send");
  381. return s;
  382. }
  383. /// Send some data on the socket.
  384. /**
  385. * This function is used to send data on the stream socket. The function
  386. * call will block until one or more bytes of the data has been sent
  387. * successfully, or an until error occurs.
  388. *
  389. * @param buffers One or more data buffers to be sent on the socket.
  390. *
  391. * @param flags Flags specifying how the send call is to be made.
  392. *
  393. * @param ec Set to indicate what error occurred, if any.
  394. *
  395. * @returns The number of bytes sent. Returns 0 if an error occurred.
  396. *
  397. * @note The send operation may not transmit all of the data to the peer.
  398. * Consider using the @ref write function if you need to ensure that all data
  399. * is written before the blocking operation completes.
  400. */
  401. template <typename ConstBufferSequence>
  402. std::size_t send(const ConstBufferSequence& buffers,
  403. socket_base::message_flags flags, asio::error_code& ec)
  404. {
  405. return this->impl_.get_service().send(
  406. this->impl_.get_implementation(), buffers, flags, ec);
  407. }
  408. /// Start an asynchronous send.
  409. /**
  410. * This function is used to asynchronously send data on the stream socket.
  411. * It is an initiating function for an @ref asynchronous_operation, and always
  412. * returns immediately.
  413. *
  414. * @param buffers One or more data buffers to be sent on the socket. Although
  415. * the buffers object may be copied as necessary, ownership of the underlying
  416. * memory blocks is retained by the caller, which must guarantee that they
  417. * remain valid until the completion handler is called.
  418. *
  419. * @param token The @ref completion_token that will be used to produce a
  420. * completion handler, which will be called when the send completes.
  421. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  422. * @ref yield_context, or a function object with the correct completion
  423. * signature. The function signature of the completion handler must be:
  424. * @code void handler(
  425. * const asio::error_code& error, // Result of operation.
  426. * std::size_t bytes_transferred // Number of bytes sent.
  427. * ); @endcode
  428. * Regardless of whether the asynchronous operation completes immediately or
  429. * not, the completion handler will not be invoked from within this function.
  430. * On immediate completion, invocation of the handler will be performed in a
  431. * manner equivalent to using asio::post().
  432. *
  433. * @par Completion Signature
  434. * @code void(asio::error_code, std::size_t) @endcode
  435. *
  436. * @note The send operation may not transmit all of the data to the peer.
  437. * Consider using the @ref async_write function if you need to ensure that all
  438. * data is written before the asynchronous operation completes.
  439. *
  440. * @par Example
  441. * To send a single data buffer use the @ref buffer function as follows:
  442. * @code
  443. * socket.async_send(asio::buffer(data, size), handler);
  444. * @endcode
  445. * See the @ref buffer documentation for information on sending multiple
  446. * buffers in one go, and how to use it with arrays, boost::array or
  447. * std::vector.
  448. *
  449. * @par Per-Operation Cancellation
  450. * On POSIX or Windows operating systems, this asynchronous operation supports
  451. * cancellation for the following asio::cancellation_type values:
  452. *
  453. * @li @c cancellation_type::terminal
  454. *
  455. * @li @c cancellation_type::partial
  456. *
  457. * @li @c cancellation_type::total
  458. */
  459. template <typename ConstBufferSequence,
  460. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  461. std::size_t)) WriteToken
  462. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  463. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  464. void (asio::error_code, std::size_t))
  465. async_send(const ConstBufferSequence& buffers,
  466. ASIO_MOVE_ARG(WriteToken) token
  467. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  468. {
  469. return async_initiate<WriteToken,
  470. void (asio::error_code, std::size_t)>(
  471. initiate_async_send(this), token,
  472. buffers, socket_base::message_flags(0));
  473. }
  474. /// Start an asynchronous send.
  475. /**
  476. * This function is used to asynchronously send data on the stream socket.
  477. * It is an initiating function for an @ref asynchronous_operation, and always
  478. * returns immediately.
  479. *
  480. * @param buffers One or more data buffers to be sent on the socket. Although
  481. * the buffers object may be copied as necessary, ownership of the underlying
  482. * memory blocks is retained by the caller, which must guarantee that they
  483. * remain valid until the completion handler is called.
  484. *
  485. * @param flags Flags specifying how the send call is to be made.
  486. *
  487. * @param token The @ref completion_token that will be used to produce a
  488. * completion handler, which will be called when the send completes.
  489. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  490. * @ref yield_context, or a function object with the correct completion
  491. * signature. The function signature of the completion handler must be:
  492. * @code void handler(
  493. * const asio::error_code& error, // Result of operation.
  494. * std::size_t bytes_transferred // Number of bytes sent.
  495. * ); @endcode
  496. * Regardless of whether the asynchronous operation completes immediately or
  497. * not, the completion handler will not be invoked from within this function.
  498. * On immediate completion, invocation of the handler will be performed in a
  499. * manner equivalent to using asio::post().
  500. *
  501. * @par Completion Signature
  502. * @code void(asio::error_code, std::size_t) @endcode
  503. *
  504. * @note The send operation may not transmit all of the data to the peer.
  505. * Consider using the @ref async_write function if you need to ensure that all
  506. * data is written before the asynchronous operation completes.
  507. *
  508. * @par Example
  509. * To send a single data buffer use the @ref buffer function as follows:
  510. * @code
  511. * socket.async_send(asio::buffer(data, size), 0, handler);
  512. * @endcode
  513. * See the @ref buffer documentation for information on sending multiple
  514. * buffers in one go, and how to use it with arrays, boost::array or
  515. * std::vector.
  516. *
  517. * @par Per-Operation Cancellation
  518. * On POSIX or Windows operating systems, this asynchronous operation supports
  519. * cancellation for the following asio::cancellation_type values:
  520. *
  521. * @li @c cancellation_type::terminal
  522. *
  523. * @li @c cancellation_type::partial
  524. *
  525. * @li @c cancellation_type::total
  526. */
  527. template <typename ConstBufferSequence,
  528. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  529. std::size_t)) WriteToken
  530. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  531. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  532. void (asio::error_code, std::size_t))
  533. async_send(const ConstBufferSequence& buffers,
  534. socket_base::message_flags flags,
  535. ASIO_MOVE_ARG(WriteToken) token
  536. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  537. {
  538. return async_initiate<WriteToken,
  539. void (asio::error_code, std::size_t)>(
  540. initiate_async_send(this), token, buffers, flags);
  541. }
  542. /// Receive some data on the socket.
  543. /**
  544. * This function is used to receive data on the stream socket. The function
  545. * call will block until one or more bytes of data has been received
  546. * successfully, or until an error occurs.
  547. *
  548. * @param buffers One or more buffers into which the data will be received.
  549. *
  550. * @returns The number of bytes received.
  551. *
  552. * @throws asio::system_error Thrown on failure. An error code of
  553. * asio::error::eof indicates that the connection was closed by the
  554. * peer.
  555. *
  556. * @note The receive operation may not receive all of the requested number of
  557. * bytes. Consider using the @ref read function if you need to ensure that the
  558. * requested amount of data is read before the blocking operation completes.
  559. *
  560. * @par Example
  561. * To receive into a single data buffer use the @ref buffer function as
  562. * follows:
  563. * @code
  564. * socket.receive(asio::buffer(data, size));
  565. * @endcode
  566. * See the @ref buffer documentation for information on receiving into
  567. * multiple buffers in one go, and how to use it with arrays, boost::array or
  568. * std::vector.
  569. */
  570. template <typename MutableBufferSequence>
  571. std::size_t receive(const MutableBufferSequence& buffers)
  572. {
  573. asio::error_code ec;
  574. std::size_t s = this->impl_.get_service().receive(
  575. this->impl_.get_implementation(), buffers, 0, ec);
  576. asio::detail::throw_error(ec, "receive");
  577. return s;
  578. }
  579. /// Receive some data on the socket.
  580. /**
  581. * This function is used to receive data on the stream socket. The function
  582. * call will block until one or more bytes of data has been received
  583. * successfully, or until an error occurs.
  584. *
  585. * @param buffers One or more buffers into which the data will be received.
  586. *
  587. * @param flags Flags specifying how the receive call is to be made.
  588. *
  589. * @returns The number of bytes received.
  590. *
  591. * @throws asio::system_error Thrown on failure. An error code of
  592. * asio::error::eof indicates that the connection was closed by the
  593. * peer.
  594. *
  595. * @note The receive operation may not receive all of the requested number of
  596. * bytes. Consider using the @ref read function if you need to ensure that the
  597. * requested amount of data is read before the blocking operation completes.
  598. *
  599. * @par Example
  600. * To receive into a single data buffer use the @ref buffer function as
  601. * follows:
  602. * @code
  603. * socket.receive(asio::buffer(data, size), 0);
  604. * @endcode
  605. * See the @ref buffer documentation for information on receiving into
  606. * multiple buffers in one go, and how to use it with arrays, boost::array or
  607. * std::vector.
  608. */
  609. template <typename MutableBufferSequence>
  610. std::size_t receive(const MutableBufferSequence& buffers,
  611. socket_base::message_flags flags)
  612. {
  613. asio::error_code ec;
  614. std::size_t s = this->impl_.get_service().receive(
  615. this->impl_.get_implementation(), buffers, flags, ec);
  616. asio::detail::throw_error(ec, "receive");
  617. return s;
  618. }
  619. /// Receive some data on a connected socket.
  620. /**
  621. * This function is used to receive data on the stream socket. The function
  622. * call will block until one or more bytes of data has been received
  623. * successfully, or until an error occurs.
  624. *
  625. * @param buffers One or more buffers into which the data will be received.
  626. *
  627. * @param flags Flags specifying how the receive call is to be made.
  628. *
  629. * @param ec Set to indicate what error occurred, if any.
  630. *
  631. * @returns The number of bytes received. Returns 0 if an error occurred.
  632. *
  633. * @note The receive operation may not receive all of the requested number of
  634. * bytes. Consider using the @ref read function if you need to ensure that the
  635. * requested amount of data is read before the blocking operation completes.
  636. */
  637. template <typename MutableBufferSequence>
  638. std::size_t receive(const MutableBufferSequence& buffers,
  639. socket_base::message_flags flags, asio::error_code& ec)
  640. {
  641. return this->impl_.get_service().receive(
  642. this->impl_.get_implementation(), buffers, flags, ec);
  643. }
  644. /// Start an asynchronous receive.
  645. /**
  646. * This function is used to asynchronously receive data from the stream
  647. * socket. It is an initiating function for an @ref asynchronous_operation,
  648. * and always returns immediately.
  649. *
  650. * @param buffers One or more buffers into which the data will be received.
  651. * Although the buffers object may be copied as necessary, ownership of the
  652. * underlying memory blocks is retained by the caller, which must guarantee
  653. * that they remain valid until the completion handler is called.
  654. *
  655. * @param token The @ref completion_token that will be used to produce a
  656. * completion handler, which will be called when the receive completes.
  657. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  658. * @ref yield_context, or a function object with the correct completion
  659. * signature. The function signature of the completion handler must be:
  660. * @code void handler(
  661. * const asio::error_code& error, // Result of operation.
  662. * std::size_t bytes_transferred // Number of bytes received.
  663. * ); @endcode
  664. * Regardless of whether the asynchronous operation completes immediately or
  665. * not, the completion handler will not be invoked from within this function.
  666. * On immediate completion, invocation of the handler will be performed in a
  667. * manner equivalent to using asio::post().
  668. *
  669. * @par Completion Signature
  670. * @code void(asio::error_code, std::size_t) @endcode
  671. *
  672. * @note The receive operation may not receive all of the requested number of
  673. * bytes. Consider using the @ref async_read function if you need to ensure
  674. * that the requested amount of data is received before the asynchronous
  675. * operation completes.
  676. *
  677. * @par Example
  678. * To receive into a single data buffer use the @ref buffer function as
  679. * follows:
  680. * @code
  681. * socket.async_receive(asio::buffer(data, size), handler);
  682. * @endcode
  683. * See the @ref buffer documentation for information on receiving into
  684. * multiple buffers in one go, and how to use it with arrays, boost::array or
  685. * std::vector.
  686. *
  687. * @par Per-Operation Cancellation
  688. * On POSIX or Windows operating systems, this asynchronous operation supports
  689. * cancellation for the following asio::cancellation_type values:
  690. *
  691. * @li @c cancellation_type::terminal
  692. *
  693. * @li @c cancellation_type::partial
  694. *
  695. * @li @c cancellation_type::total
  696. */
  697. template <typename MutableBufferSequence,
  698. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  699. std::size_t)) ReadToken
  700. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  701. ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
  702. void (asio::error_code, std::size_t))
  703. async_receive(const MutableBufferSequence& buffers,
  704. ASIO_MOVE_ARG(ReadToken) token
  705. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  706. {
  707. return async_initiate<ReadToken,
  708. void (asio::error_code, std::size_t)>(
  709. initiate_async_receive(this), token,
  710. buffers, socket_base::message_flags(0));
  711. }
  712. /// Start an asynchronous receive.
  713. /**
  714. * This function is used to asynchronously receive data from the stream
  715. * socket. It is an initiating function for an @ref asynchronous_operation,
  716. * and always returns immediately.
  717. *
  718. * @param buffers One or more buffers into which the data will be received.
  719. * Although the buffers object may be copied as necessary, ownership of the
  720. * underlying memory blocks is retained by the caller, which must guarantee
  721. * that they remain valid until the completion handler is called.
  722. *
  723. * @param flags Flags specifying how the receive call is to be made.
  724. *
  725. * @param token The @ref completion_token that will be used to produce a
  726. * completion handler, which will be called when the receive completes.
  727. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  728. * @ref yield_context, or a function object with the correct completion
  729. * signature. The function signature of the completion handler must be:
  730. * @code void handler(
  731. * const asio::error_code& error, // Result of operation.
  732. * std::size_t bytes_transferred // Number of bytes received.
  733. * ); @endcode
  734. * Regardless of whether the asynchronous operation completes immediately or
  735. * not, the completion handler will not be invoked from within this function.
  736. * On immediate completion, invocation of the handler will be performed in a
  737. * manner equivalent to using asio::post().
  738. *
  739. * @par Completion Signature
  740. * @code void(asio::error_code, std::size_t) @endcode
  741. *
  742. * @note The receive operation may not receive all of the requested number of
  743. * bytes. Consider using the @ref async_read function if you need to ensure
  744. * that the requested amount of data is received before the asynchronous
  745. * operation completes.
  746. *
  747. * @par Example
  748. * To receive into a single data buffer use the @ref buffer function as
  749. * follows:
  750. * @code
  751. * socket.async_receive(asio::buffer(data, size), 0, handler);
  752. * @endcode
  753. * See the @ref buffer documentation for information on receiving into
  754. * multiple buffers in one go, and how to use it with arrays, boost::array or
  755. * std::vector.
  756. *
  757. * @par Per-Operation Cancellation
  758. * On POSIX or Windows operating systems, this asynchronous operation supports
  759. * cancellation for the following asio::cancellation_type values:
  760. *
  761. * @li @c cancellation_type::terminal
  762. *
  763. * @li @c cancellation_type::partial
  764. *
  765. * @li @c cancellation_type::total
  766. */
  767. template <typename MutableBufferSequence,
  768. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  769. std::size_t)) ReadToken
  770. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  771. ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
  772. void (asio::error_code, std::size_t))
  773. async_receive(const MutableBufferSequence& buffers,
  774. socket_base::message_flags flags,
  775. ASIO_MOVE_ARG(ReadToken) token
  776. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  777. {
  778. return async_initiate<ReadToken,
  779. void (asio::error_code, std::size_t)>(
  780. initiate_async_receive(this), token, buffers, flags);
  781. }
  782. /// Write some data to the socket.
  783. /**
  784. * This function is used to write data to the stream socket. The function call
  785. * will block until one or more bytes of the data has been written
  786. * successfully, or until an error occurs.
  787. *
  788. * @param buffers One or more data buffers to be written to the socket.
  789. *
  790. * @returns The number of bytes written.
  791. *
  792. * @throws asio::system_error Thrown on failure. An error code of
  793. * asio::error::eof indicates that the connection was closed by the
  794. * peer.
  795. *
  796. * @note The write_some operation may not transmit all of the data to the
  797. * peer. Consider using the @ref write function if you need to ensure that
  798. * all data is written before the blocking operation completes.
  799. *
  800. * @par Example
  801. * To write a single data buffer use the @ref buffer function as follows:
  802. * @code
  803. * socket.write_some(asio::buffer(data, size));
  804. * @endcode
  805. * See the @ref buffer documentation for information on writing multiple
  806. * buffers in one go, and how to use it with arrays, boost::array or
  807. * std::vector.
  808. */
  809. template <typename ConstBufferSequence>
  810. std::size_t write_some(const ConstBufferSequence& buffers)
  811. {
  812. asio::error_code ec;
  813. std::size_t s = this->impl_.get_service().send(
  814. this->impl_.get_implementation(), buffers, 0, ec);
  815. asio::detail::throw_error(ec, "write_some");
  816. return s;
  817. }
  818. /// Write some data to the socket.
  819. /**
  820. * This function is used to write data to the stream socket. The function call
  821. * will block until one or more bytes of the data has been written
  822. * successfully, or until an error occurs.
  823. *
  824. * @param buffers One or more data buffers to be written to the socket.
  825. *
  826. * @param ec Set to indicate what error occurred, if any.
  827. *
  828. * @returns The number of bytes written. Returns 0 if an error occurred.
  829. *
  830. * @note The write_some operation may not transmit all of the data to the
  831. * peer. Consider using the @ref write function if you need to ensure that
  832. * all data is written before the blocking operation completes.
  833. */
  834. template <typename ConstBufferSequence>
  835. std::size_t write_some(const ConstBufferSequence& buffers,
  836. asio::error_code& ec)
  837. {
  838. return this->impl_.get_service().send(
  839. this->impl_.get_implementation(), buffers, 0, ec);
  840. }
  841. /// Start an asynchronous write.
  842. /**
  843. * This function is used to asynchronously write data to the stream socket.
  844. * It is an initiating function for an @ref asynchronous_operation, and always
  845. * returns immediately.
  846. *
  847. * @param buffers One or more data buffers to be written to the socket.
  848. * Although the buffers object may be copied as necessary, ownership of the
  849. * underlying memory blocks is retained by the caller, which must guarantee
  850. * that they remain valid until the completion handler is called.
  851. *
  852. * @param token The @ref completion_token that will be used to produce a
  853. * completion handler, which will be called when the write completes.
  854. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  855. * @ref yield_context, or a function object with the correct completion
  856. * signature. The function signature of the completion handler must be:
  857. * @code void handler(
  858. * const asio::error_code& error, // Result of operation.
  859. * std::size_t bytes_transferred // Number of bytes written.
  860. * ); @endcode
  861. * Regardless of whether the asynchronous operation completes immediately or
  862. * not, the completion handler will not be invoked from within this function.
  863. * On immediate completion, invocation of the handler will be performed in a
  864. * manner equivalent to using asio::post().
  865. *
  866. * @par Completion Signature
  867. * @code void(asio::error_code, std::size_t) @endcode
  868. *
  869. * @note The write operation may not transmit all of the data to the peer.
  870. * Consider using the @ref async_write function if you need to ensure that all
  871. * data is written before the asynchronous operation completes.
  872. *
  873. * @par Example
  874. * To write a single data buffer use the @ref buffer function as follows:
  875. * @code
  876. * socket.async_write_some(asio::buffer(data, size), handler);
  877. * @endcode
  878. * See the @ref buffer documentation for information on writing multiple
  879. * buffers in one go, and how to use it with arrays, boost::array or
  880. * std::vector.
  881. *
  882. * @par Per-Operation Cancellation
  883. * On POSIX or Windows operating systems, this asynchronous operation supports
  884. * cancellation for the following asio::cancellation_type values:
  885. *
  886. * @li @c cancellation_type::terminal
  887. *
  888. * @li @c cancellation_type::partial
  889. *
  890. * @li @c cancellation_type::total
  891. */
  892. template <typename ConstBufferSequence,
  893. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  894. std::size_t)) WriteToken
  895. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  896. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  897. void (asio::error_code, std::size_t))
  898. async_write_some(const ConstBufferSequence& buffers,
  899. ASIO_MOVE_ARG(WriteToken) token
  900. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  901. {
  902. return async_initiate<WriteToken,
  903. void (asio::error_code, std::size_t)>(
  904. initiate_async_send(this), token,
  905. buffers, socket_base::message_flags(0));
  906. }
  907. /// Read some data from the socket.
  908. /**
  909. * This function is used to read data from the stream socket. The function
  910. * call will block until one or more bytes of data has been read successfully,
  911. * or until an error occurs.
  912. *
  913. * @param buffers One or more buffers into which the data will be read.
  914. *
  915. * @returns The number of bytes read.
  916. *
  917. * @throws asio::system_error Thrown on failure. An error code of
  918. * asio::error::eof indicates that the connection was closed by the
  919. * peer.
  920. *
  921. * @note The read_some operation may not read all of the requested number of
  922. * bytes. Consider using the @ref read function if you need to ensure that
  923. * the requested amount of data is read before the blocking operation
  924. * completes.
  925. *
  926. * @par Example
  927. * To read into a single data buffer use the @ref buffer function as follows:
  928. * @code
  929. * socket.read_some(asio::buffer(data, size));
  930. * @endcode
  931. * See the @ref buffer documentation for information on reading into multiple
  932. * buffers in one go, and how to use it with arrays, boost::array or
  933. * std::vector.
  934. */
  935. template <typename MutableBufferSequence>
  936. std::size_t read_some(const MutableBufferSequence& buffers)
  937. {
  938. asio::error_code ec;
  939. std::size_t s = this->impl_.get_service().receive(
  940. this->impl_.get_implementation(), buffers, 0, ec);
  941. asio::detail::throw_error(ec, "read_some");
  942. return s;
  943. }
  944. /// Read some data from the socket.
  945. /**
  946. * This function is used to read data from the stream socket. The function
  947. * call will block until one or more bytes of data has been read successfully,
  948. * or until an error occurs.
  949. *
  950. * @param buffers One or more buffers into which the data will be read.
  951. *
  952. * @param ec Set to indicate what error occurred, if any.
  953. *
  954. * @returns The number of bytes read. Returns 0 if an error occurred.
  955. *
  956. * @note The read_some operation may not read all of the requested number of
  957. * bytes. Consider using the @ref read function if you need to ensure that
  958. * the requested amount of data is read before the blocking operation
  959. * completes.
  960. */
  961. template <typename MutableBufferSequence>
  962. std::size_t read_some(const MutableBufferSequence& buffers,
  963. asio::error_code& ec)
  964. {
  965. return this->impl_.get_service().receive(
  966. this->impl_.get_implementation(), buffers, 0, ec);
  967. }
  968. /// Start an asynchronous read.
  969. /**
  970. * This function is used to asynchronously read data from the stream socket.
  971. * socket. It is an initiating function for an @ref asynchronous_operation,
  972. * and always returns immediately.
  973. *
  974. * @param buffers One or more buffers into which the data will be read.
  975. * Although the buffers object may be copied as necessary, ownership of the
  976. * underlying memory blocks is retained by the caller, which must guarantee
  977. * that they remain valid until the completion handler is called.
  978. *
  979. * @param token The @ref completion_token that will be used to produce a
  980. * completion handler, which will be called when the read completes.
  981. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  982. * @ref yield_context, or a function object with the correct completion
  983. * signature. The function signature of the completion handler must be:
  984. * @code void handler(
  985. * const asio::error_code& error, // Result of operation.
  986. * std::size_t bytes_transferred // Number of bytes read.
  987. * ); @endcode
  988. * Regardless of whether the asynchronous operation completes immediately or
  989. * not, the completion handler will not be invoked from within this function.
  990. * On immediate completion, invocation of the handler will be performed in a
  991. * manner equivalent to using asio::post().
  992. *
  993. * @par Completion Signature
  994. * @code void(asio::error_code, std::size_t) @endcode
  995. *
  996. * @note The read operation may not read all of the requested number of bytes.
  997. * Consider using the @ref async_read function if you need to ensure that the
  998. * requested amount of data is read before the asynchronous operation
  999. * completes.
  1000. *
  1001. * @par Example
  1002. * To read into a single data buffer use the @ref buffer function as follows:
  1003. * @code
  1004. * socket.async_read_some(asio::buffer(data, size), handler);
  1005. * @endcode
  1006. * See the @ref buffer documentation for information on reading into multiple
  1007. * buffers in one go, and how to use it with arrays, boost::array or
  1008. * std::vector.
  1009. *
  1010. * @par Per-Operation Cancellation
  1011. * On POSIX or Windows operating systems, this asynchronous operation supports
  1012. * cancellation for the following asio::cancellation_type values:
  1013. *
  1014. * @li @c cancellation_type::terminal
  1015. *
  1016. * @li @c cancellation_type::partial
  1017. *
  1018. * @li @c cancellation_type::total
  1019. */
  1020. template <typename MutableBufferSequence,
  1021. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1022. std::size_t)) ReadToken
  1023. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1024. ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
  1025. void (asio::error_code, std::size_t))
  1026. async_read_some(const MutableBufferSequence& buffers,
  1027. ASIO_MOVE_ARG(ReadToken) token
  1028. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1029. {
  1030. return async_initiate<ReadToken,
  1031. void (asio::error_code, std::size_t)>(
  1032. initiate_async_receive(this), token,
  1033. buffers, socket_base::message_flags(0));
  1034. }
  1035. private:
  1036. // Disallow copying and assignment.
  1037. basic_stream_socket(const basic_stream_socket&) ASIO_DELETED;
  1038. basic_stream_socket& operator=(const basic_stream_socket&) ASIO_DELETED;
  1039. class initiate_async_send
  1040. {
  1041. public:
  1042. typedef Executor executor_type;
  1043. explicit initiate_async_send(basic_stream_socket* self)
  1044. : self_(self)
  1045. {
  1046. }
  1047. executor_type get_executor() const ASIO_NOEXCEPT
  1048. {
  1049. return self_->get_executor();
  1050. }
  1051. template <typename WriteHandler, typename ConstBufferSequence>
  1052. void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
  1053. const ConstBufferSequence& buffers,
  1054. socket_base::message_flags flags) const
  1055. {
  1056. // If you get an error on the following line it means that your handler
  1057. // does not meet the documented type requirements for a WriteHandler.
  1058. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  1059. detail::non_const_lvalue<WriteHandler> handler2(handler);
  1060. self_->impl_.get_service().async_send(
  1061. self_->impl_.get_implementation(), buffers, flags,
  1062. handler2.value, self_->impl_.get_executor());
  1063. }
  1064. private:
  1065. basic_stream_socket* self_;
  1066. };
  1067. class initiate_async_receive
  1068. {
  1069. public:
  1070. typedef Executor executor_type;
  1071. explicit initiate_async_receive(basic_stream_socket* self)
  1072. : self_(self)
  1073. {
  1074. }
  1075. executor_type get_executor() const ASIO_NOEXCEPT
  1076. {
  1077. return self_->get_executor();
  1078. }
  1079. template <typename ReadHandler, typename MutableBufferSequence>
  1080. void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
  1081. const MutableBufferSequence& buffers,
  1082. socket_base::message_flags flags) const
  1083. {
  1084. // If you get an error on the following line it means that your handler
  1085. // does not meet the documented type requirements for a ReadHandler.
  1086. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  1087. detail::non_const_lvalue<ReadHandler> handler2(handler);
  1088. self_->impl_.get_service().async_receive(
  1089. self_->impl_.get_implementation(), buffers, flags,
  1090. handler2.value, self_->impl_.get_executor());
  1091. }
  1092. private:
  1093. basic_stream_socket* self_;
  1094. };
  1095. };
  1096. } // namespace asio
  1097. #include "asio/detail/pop_options.hpp"
  1098. #endif // ASIO_BASIC_STREAM_SOCKET_HPP