basic_socket.hpp 49 KB

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