socket 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. // <experimental/socket> -*- C++ -*-
  2. // Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/socket
  21. * This is a TS C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_EXPERIMENTAL_SOCKET
  24. #define _GLIBCXX_EXPERIMENTAL_SOCKET
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201402L
  27. #include <experimental/netfwd>
  28. #include <experimental/buffer>
  29. #include <experimental/io_context>
  30. #include <experimental/bits/net.h>
  31. #include <streambuf>
  32. #include <istream>
  33. #include <bits/unique_ptr.h>
  34. #if _GLIBCXX_HAVE_UNISTD_H
  35. # include <unistd.h>
  36. # ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  37. # include <sys/socket.h> // socket etc
  38. # endif
  39. # ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
  40. # include <sys/ioctl.h> // ioctl
  41. # endif
  42. # ifdef _GLIBCXX_HAVE_SYS_UIO_H
  43. # include <sys/uio.h> // iovec
  44. # endif
  45. # ifdef _GLIBCXX_HAVE_POLL_H
  46. # include <poll.h> // poll, pollfd, POLLIN, POLLOUT, POLLERR
  47. # endif
  48. # ifdef _GLIBCXX_HAVE_FCNTL_H
  49. # include <fcntl.h> // fcntl, F_GETFL, F_SETFL
  50. # endif
  51. #endif
  52. namespace std _GLIBCXX_VISIBILITY(default)
  53. {
  54. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  55. namespace experimental
  56. {
  57. namespace net
  58. {
  59. inline namespace v1
  60. {
  61. /**
  62. * @ingroup networking
  63. * @{
  64. */
  65. enum class socket_errc { // TODO decide values
  66. already_open = 3,
  67. not_found = 4
  68. };
  69. const error_category& socket_category() noexcept
  70. {
  71. struct __cat : error_category
  72. {
  73. const char* name() const noexcept { return "socket"; }
  74. std::string message(int __e) const
  75. {
  76. if (__e == (int)socket_errc::already_open)
  77. return "already open";
  78. else if (__e == (int)socket_errc::not_found)
  79. return "endpoint not found";
  80. return "socket error";
  81. }
  82. virtual void __message(int) { } // TODO dual ABI XXX
  83. };
  84. static __cat __c;
  85. return __c;
  86. }
  87. inline error_code
  88. make_error_code(socket_errc __e) noexcept
  89. { return error_code(static_cast<int>(__e), socket_category()); }
  90. inline error_condition
  91. make_error_condition(socket_errc __e) noexcept
  92. { return error_condition(static_cast<int>(__e), socket_category()); }
  93. template<typename _Tp, typename = __void_t<>>
  94. struct __is_endpoint_impl : false_type
  95. { };
  96. // Check Endpoint requirements.
  97. template<typename _Tp>
  98. auto
  99. __endpoint_reqs(const _Tp* __a = 0)
  100. -> enable_if_t<__and_<
  101. is_default_constructible<_Tp>,
  102. __is_value_constructible<_Tp>,
  103. is_same<decltype(__a->__protocol()), typename _Tp::protocol_type>
  104. >::value,
  105. __void_t< typename _Tp::protocol_type::endpoint >>;
  106. template<typename _Tp>
  107. struct __is_endpoint_impl<_Tp, decltype(__endpoint_reqs<_Tp>())>
  108. : true_type
  109. { };
  110. template<typename _Tp>
  111. struct __is_endpoint : __is_endpoint_impl<_Tp>
  112. { };
  113. // TODO Endpoint reqs for extensible implementations
  114. // TODO _Protocol reqs
  115. // TODO AcceptableProtocol reqs
  116. // TODO GettableSocket reqs
  117. // TODO SettableSocket reqs
  118. // TODO BooleanSocketOption reqs
  119. // TODO IntegerSocketOption reqs
  120. // TODO _IoControlCommand reqs
  121. // TODO _ConnectCondition reqs
  122. /** @brief Sockets
  123. * @{
  124. */
  125. class socket_base
  126. {
  127. public:
  128. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  129. struct broadcast : __sockopt_crtp<broadcast, bool>
  130. {
  131. using __sockopt_crtp::__sockopt_crtp;
  132. static const int _S_level = SOL_SOCKET;
  133. static const int _S_name = SO_BROADCAST;
  134. };
  135. struct debug : __sockopt_crtp<debug, bool>
  136. {
  137. using __sockopt_crtp::__sockopt_crtp;
  138. static const int _S_level = SOL_SOCKET;
  139. static const int _S_name = SO_DEBUG;
  140. };
  141. struct do_not_route : __sockopt_crtp<do_not_route, bool>
  142. {
  143. using __sockopt_crtp::__sockopt_crtp;
  144. static const int _S_level = SOL_SOCKET;
  145. static const int _S_name = SO_DONTROUTE;
  146. };
  147. struct keep_alive : __sockopt_crtp<keep_alive, bool>
  148. {
  149. using __sockopt_crtp::__sockopt_crtp;
  150. static const int _S_level = SOL_SOCKET;
  151. static const int _S_name = SO_KEEPALIVE;
  152. };
  153. struct linger : __sockopt_crtp<linger, ::linger>
  154. {
  155. using __sockopt_crtp::__sockopt_crtp;
  156. linger() noexcept = default;
  157. linger(bool __e, chrono::seconds __t) noexcept
  158. {
  159. enabled(__e);
  160. timeout(__t);
  161. }
  162. bool
  163. enabled() const noexcept
  164. { return _M_value.l_onoff != 0; }
  165. void
  166. enabled(bool __e) noexcept
  167. { _M_value.l_onoff = int(__e); }
  168. chrono::seconds
  169. timeout() const noexcept
  170. { return chrono::seconds(_M_value.l_linger); }
  171. void
  172. timeout(chrono::seconds __t) noexcept
  173. { _M_value.l_linger = __t.count(); }
  174. static const int _S_level = SOL_SOCKET;
  175. static const int _S_name = SO_LINGER;
  176. };
  177. struct out_of_band_inline : __sockopt_crtp<out_of_band_inline, bool>
  178. {
  179. using __sockopt_crtp::__sockopt_crtp;
  180. static const int _S_level = SOL_SOCKET;
  181. static const int _S_name = SO_OOBINLINE;
  182. };
  183. struct receive_buffer_size : __sockopt_crtp<receive_buffer_size>
  184. {
  185. using __sockopt_crtp::__sockopt_crtp;
  186. static const int _S_level = SOL_SOCKET;
  187. static const int _S_name = SO_RCVBUF;
  188. };
  189. struct receive_low_watermark : __sockopt_crtp<receive_low_watermark>
  190. {
  191. using __sockopt_crtp::__sockopt_crtp;
  192. static const int _S_level = SOL_SOCKET;
  193. static const int _S_name = SO_RCVLOWAT;
  194. };
  195. struct reuse_address : __sockopt_crtp<reuse_address, bool>
  196. {
  197. using __sockopt_crtp::__sockopt_crtp;
  198. static const int _S_level = SOL_SOCKET;
  199. static const int _S_name = SO_REUSEADDR;
  200. };
  201. struct send_buffer_size : __sockopt_crtp<send_buffer_size>
  202. {
  203. using __sockopt_crtp::__sockopt_crtp;
  204. static const int _S_level = SOL_SOCKET;
  205. static const int _S_name = SO_SNDBUF;
  206. };
  207. struct send_low_watermark : __sockopt_crtp<send_low_watermark>
  208. {
  209. using __sockopt_crtp::__sockopt_crtp;
  210. static const int _S_level = SOL_SOCKET;
  211. static const int _S_name = SO_SNDLOWAT;
  212. };
  213. enum shutdown_type : int
  214. {
  215. __shutdown_receive = SHUT_RD,
  216. __shutdown_send = SHUT_WR,
  217. __shutdown_both = SHUT_RDWR
  218. };
  219. static constexpr shutdown_type shutdown_receive = __shutdown_receive;
  220. static constexpr shutdown_type shutdown_send = __shutdown_send;
  221. static constexpr shutdown_type shutdown_both = __shutdown_both;
  222. #ifdef _GLIBCXX_HAVE_POLL_H
  223. enum wait_type : int
  224. {
  225. __wait_read = POLLIN,
  226. __wait_write = POLLOUT,
  227. __wait_error = POLLERR
  228. };
  229. static constexpr wait_type wait_read = __wait_read;
  230. static constexpr wait_type wait_write = __wait_write;
  231. static constexpr wait_type wait_error = __wait_error;
  232. #endif
  233. enum message_flags : int
  234. {
  235. __message_peek = MSG_PEEK,
  236. __message_oob = MSG_OOB,
  237. __message_dontroute = MSG_DONTROUTE
  238. };
  239. static constexpr message_flags message_peek = __message_peek;
  240. static constexpr message_flags message_out_of_band = __message_oob;
  241. static constexpr message_flags message_do_not_route = __message_dontroute;
  242. static const int max_listen_connections = SOMAXCONN;
  243. #endif
  244. protected:
  245. socket_base() = default;
  246. ~socket_base() = default;
  247. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  248. struct __msg_hdr : ::msghdr
  249. {
  250. #ifdef IOV_MAX
  251. using __iovec_array = array<::iovec, IOV_MAX>;
  252. #elif _GLIBCXX_HAVE_UNISTD_H
  253. struct __iovec_array
  254. {
  255. __iovec_array() : _M_ptr(new ::iovec[size()]) { }
  256. ::iovec& operator[](size_t __n) noexcept { return _M_ptr[__n]; }
  257. ::iovec* data() noexcept { return _M_ptr.get(); }
  258. static size_t size()
  259. {
  260. static const size_t __iov_max = ::sysconf(_SC_IOV_MAX);
  261. return __iov_max;
  262. }
  263. private:
  264. unique_ptr<::iovec[]> _M_ptr;
  265. };
  266. #else
  267. using __iovec_array = array<::iovec, 16>;
  268. #endif
  269. __iovec_array _M_iov;
  270. template<typename _BufferSequence>
  271. explicit
  272. __msg_hdr(const _BufferSequence& __buffers)
  273. : msghdr()
  274. {
  275. auto __buf = net::buffer_sequence_begin(__buffers);
  276. const auto __bufend = net::buffer_sequence_end(__buffers);
  277. size_t __len = 0;
  278. while (__buf != __bufend && __len != _M_iov.size())
  279. {
  280. _M_iov[__len].iov_base = (void*)__buf->data();
  281. _M_iov[__len].iov_len = __buf->size();
  282. ++__buf;
  283. ++__len;
  284. }
  285. this->msg_iovlen = __len;
  286. this->msg_iov = _M_iov.data();
  287. }
  288. template<typename _BufferSequence, typename _Endpoint>
  289. __msg_hdr(const _BufferSequence& __buffers, const _Endpoint& __ep)
  290. : __msg_hdr(__buffers)
  291. {
  292. this->msg_name = __ep.data();
  293. this->msg_namelen = __ep.size();
  294. }
  295. };
  296. #endif
  297. };
  298. constexpr socket_base::message_flags
  299. operator&(socket_base::message_flags __f1, socket_base::message_flags __f2)
  300. { return socket_base::message_flags( int(__f1) & int(__f2) ); }
  301. constexpr socket_base::message_flags
  302. operator|(socket_base::message_flags __f1, socket_base::message_flags __f2)
  303. { return socket_base::message_flags( int(__f1) | int(__f2) ); }
  304. constexpr socket_base::message_flags
  305. operator^(socket_base::message_flags __f1, socket_base::message_flags __f2)
  306. { return socket_base::message_flags( int(__f1) ^ int(__f2) ); }
  307. constexpr socket_base::message_flags
  308. operator~(socket_base::message_flags __f)
  309. { return socket_base::message_flags( ~int(__f) ); }
  310. inline socket_base::message_flags&
  311. operator&=(socket_base::message_flags& __f1, socket_base::message_flags __f2)
  312. { return __f1 = (__f1 & __f2); }
  313. inline socket_base::message_flags&
  314. operator|=(socket_base::message_flags& __f1, socket_base::message_flags __f2)
  315. { return __f1 = (__f1 | __f2); }
  316. inline socket_base::message_flags&
  317. operator^=(socket_base::message_flags& __f1, socket_base::message_flags __f2)
  318. { return __f1 = (__f1 ^ __f2); }
  319. #if _GLIBCXX_HAVE_UNISTD_H
  320. class __socket_impl
  321. {
  322. protected:
  323. using executor_type = io_context::executor_type;
  324. using native_handle_type = int;
  325. explicit
  326. __socket_impl(io_context& __ctx) : _M_ctx(std::addressof(__ctx)) { }
  327. __socket_impl(__socket_impl&& __rhs)
  328. : _M_ctx(__rhs._M_ctx),
  329. _M_sockfd(std::exchange(__rhs._M_sockfd, -1)),
  330. _M_bits(std::exchange(__rhs._M_bits, {}))
  331. { }
  332. __socket_impl&
  333. operator=(__socket_impl&& __rhs)
  334. {
  335. _M_ctx = __rhs._M_ctx;
  336. _M_sockfd = std::exchange(__rhs._M_sockfd, -1);
  337. _M_bits = std::exchange(__rhs._M_bits, {});
  338. return *this;
  339. }
  340. ~__socket_impl() = default;
  341. __socket_impl(const __socket_impl&) = delete;
  342. __socket_impl& operator=(const __socket_impl&) = delete;
  343. executor_type get_executor() noexcept { return _M_ctx->get_executor(); }
  344. native_handle_type native_handle() noexcept { return _M_sockfd; }
  345. bool is_open() const noexcept { return _M_sockfd != -1; }
  346. void
  347. close(error_code& __ec)
  348. {
  349. if (is_open())
  350. {
  351. cancel(__ec);
  352. if (!__ec)
  353. {
  354. if (::close(_M_sockfd) == -1)
  355. __ec.assign(errno, generic_category());
  356. else
  357. {
  358. get_executor().context()._M_remove_fd(_M_sockfd);
  359. _M_sockfd = -1;
  360. }
  361. }
  362. }
  363. }
  364. void cancel(error_code& __ec) { _M_ctx->cancel(_M_sockfd, __ec); }
  365. void
  366. non_blocking(bool __mode, error_code&)
  367. { _M_bits.non_blocking = __mode; }
  368. bool non_blocking() const { return _M_bits.non_blocking; }
  369. void
  370. native_non_blocking(bool __mode, error_code& __ec)
  371. {
  372. #ifdef _GLIBCXX_HAVE_FCNTL_H
  373. int __flags = ::fcntl(_M_sockfd, F_GETFL, 0);
  374. if (__flags >= 0)
  375. {
  376. if (__mode)
  377. __flags |= O_NONBLOCK;
  378. else
  379. __flags &= ~O_NONBLOCK;
  380. __flags = ::fcntl(_M_sockfd, F_SETFL, __flags);
  381. }
  382. if (__flags == -1)
  383. __ec.assign(errno, generic_category());
  384. else
  385. {
  386. __ec.clear();
  387. _M_bits.native_non_blocking = __mode;
  388. }
  389. #else
  390. __ec = std::make_error_code(std::errc::not_supported);
  391. #endif
  392. }
  393. bool
  394. native_non_blocking() const
  395. {
  396. #ifdef _GLIBCXX_HAVE_FCNTL_H
  397. if (_M_bits.native_non_blocking == -1)
  398. {
  399. const int __flags = ::fcntl(_M_sockfd, F_GETFL, 0);
  400. if (__flags == -1)
  401. return 0;
  402. _M_bits.native_non_blocking = __flags & O_NONBLOCK;
  403. }
  404. return _M_bits.native_non_blocking;
  405. #else
  406. return false;
  407. #endif
  408. }
  409. io_context* _M_ctx;
  410. int _M_sockfd{-1};
  411. struct {
  412. unsigned non_blocking : 1;
  413. mutable signed native_non_blocking : 2;
  414. unsigned enable_connection_aborted : 1;
  415. } _M_bits{};
  416. };
  417. template<typename _Protocol>
  418. class __basic_socket_impl : public __socket_impl
  419. {
  420. using __base = __socket_impl;
  421. protected:
  422. using protocol_type = _Protocol;
  423. using endpoint_type = typename protocol_type::endpoint;
  424. explicit
  425. __basic_socket_impl(io_context& __ctx) : __base(__ctx) { }
  426. __basic_socket_impl(__basic_socket_impl&&) = default;
  427. template<typename _OtherProtocol>
  428. __basic_socket_impl(__basic_socket_impl<_OtherProtocol>&& __rhs)
  429. : __base(std::move(__rhs)), _M_protocol(std::move(__rhs._M_protocol))
  430. { }
  431. __basic_socket_impl&
  432. operator=(__basic_socket_impl&& __rhs)
  433. {
  434. if (this == std::addressof(__rhs))
  435. return *this;
  436. _M_close();
  437. __base::operator=(std::move(__rhs));
  438. return *this;
  439. }
  440. ~__basic_socket_impl() { _M_close(); }
  441. __basic_socket_impl(const __basic_socket_impl&) = delete;
  442. __basic_socket_impl& operator=(const __basic_socket_impl&) = delete;
  443. void
  444. open(const protocol_type& __protocol, error_code& __ec)
  445. {
  446. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  447. if (is_open())
  448. __ec = socket_errc::already_open;
  449. else
  450. {
  451. _M_protocol = __protocol;
  452. _M_sockfd = ::socket(__protocol.family(), __protocol.type(),
  453. __protocol.protocol());
  454. if (is_open())
  455. {
  456. get_executor().context()._M_add_fd(_M_sockfd);
  457. __ec.clear();
  458. }
  459. else
  460. __ec.assign(errno, std::generic_category());
  461. }
  462. #else
  463. __ec = std::make_error_code(errc::operation_not_supported);
  464. #endif
  465. }
  466. void
  467. assign(const protocol_type& __protocol,
  468. const native_handle_type& __native_socket,
  469. error_code& __ec)
  470. {
  471. if (is_open())
  472. __ec = socket_errc::already_open;
  473. else
  474. {
  475. _M_protocol = __protocol;
  476. _M_bits.native_non_blocking = -1;
  477. _M_sockfd = __native_socket;
  478. if (is_open())
  479. {
  480. get_executor().context()._M_add_fd(_M_sockfd);
  481. __ec.clear();
  482. }
  483. else
  484. __ec.assign(errno, std::generic_category());
  485. }
  486. }
  487. template<typename _SettableSocketOption>
  488. void
  489. set_option(const _SettableSocketOption& __option, error_code& __ec)
  490. {
  491. int __result = ::setsockopt(_M_sockfd, __option.level(_M_protocol),
  492. __option.name(_M_protocol),
  493. __option.data(_M_protocol),
  494. __option.size(_M_protocol));
  495. if (__result == -1)
  496. __ec.assign(errno, generic_category());
  497. else
  498. __ec.clear();
  499. }
  500. template<typename _GettableSocketOption>
  501. void
  502. get_option(_GettableSocketOption& __option, error_code& __ec) const
  503. {
  504. int __result = ::getsockopt(_M_sockfd, __option.level(_M_protocol),
  505. __option.name(_M_protocol),
  506. __option.data(_M_protocol),
  507. __option.size(_M_protocol));
  508. if (__result == -1)
  509. __ec.assign(errno, generic_category());
  510. else
  511. __ec.clear();
  512. }
  513. template<typename _IoControlCommand>
  514. void
  515. io_control(_IoControlCommand& __command, error_code& __ec)
  516. {
  517. #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
  518. int __result = ::ioctl(_M_sockfd, __command.name(),
  519. __command.data());
  520. if (__result == -1)
  521. __ec.assign(errno, generic_category());
  522. else
  523. __ec.clear();
  524. #else
  525. __ec = std::make_error_code(std::errc::not_supported);
  526. #endif
  527. }
  528. endpoint_type
  529. local_endpoint(error_code& __ec) const
  530. {
  531. endpoint_type __endpoint;
  532. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  533. socklen_t __endpoint_len = __endpoint.capacity();
  534. if (::getsockname(_M_sockfd, (sockaddr*)__endpoint.data(),
  535. &__endpoint_len) == -1)
  536. {
  537. __ec.assign(errno, generic_category());
  538. return endpoint_type{};
  539. }
  540. __ec.clear();
  541. __endpoint.resize(__endpoint_len);
  542. #else
  543. __ec = std::make_error_code(errc::operation_not_supported);
  544. #endif
  545. return __endpoint;
  546. }
  547. void
  548. bind(const endpoint_type& __endpoint, error_code& __ec)
  549. {
  550. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  551. if (::bind(_M_sockfd, (sockaddr*)__endpoint.data(), __endpoint.size())
  552. == -1)
  553. __ec.assign(errno, generic_category());
  554. else
  555. __ec.clear();
  556. #else
  557. __ec = std::make_error_code(errc::operation_not_supported);
  558. #endif
  559. }
  560. _Protocol _M_protocol{ endpoint_type{}.protocol() };
  561. private:
  562. void
  563. _M_close()
  564. {
  565. if (is_open())
  566. {
  567. error_code __ec;
  568. cancel(__ec);
  569. set_option(socket_base::linger{false, chrono::seconds{}}, __ec);
  570. ::close(_M_sockfd);
  571. }
  572. }
  573. };
  574. template<typename _Protocol>
  575. class basic_socket
  576. : public socket_base, private __basic_socket_impl<_Protocol>
  577. {
  578. using __base = __basic_socket_impl<_Protocol>;
  579. public:
  580. // types:
  581. typedef io_context::executor_type executor_type;
  582. typedef int native_handle_type;
  583. typedef _Protocol protocol_type;
  584. typedef typename protocol_type::endpoint endpoint_type;
  585. // basic_socket operations:
  586. executor_type get_executor() noexcept { return __base::get_executor(); }
  587. native_handle_type
  588. native_handle() noexcept { return __base::native_handle(); }
  589. void
  590. open(const protocol_type& __protocol = protocol_type())
  591. { open(__protocol, __throw_on_error{"basic_socket::open"}); }
  592. void
  593. open(const protocol_type& __protocol, error_code& __ec)
  594. { __base::open(__protocol, __ec); }
  595. void
  596. assign(const protocol_type& __protocol,
  597. const native_handle_type& __native_socket)
  598. {
  599. assign(__protocol, __native_socket,
  600. __throw_on_error{"basic_socket::assign"});
  601. }
  602. void
  603. assign(const protocol_type& __protocol,
  604. const native_handle_type& __native_socket,
  605. error_code& __ec)
  606. { __base::assign(__protocol, __native_socket, __ec); }
  607. bool is_open() const noexcept { return __base::is_open(); }
  608. void close() { close(__throw_on_error{"basic_socket::close"}); }
  609. void close(error_code& __ec) { __base::close(); }
  610. void cancel() { cancel(__throw_on_error{"basic_socket::cancel"}); }
  611. void cancel(error_code& __ec) { __base::cancel(__ec); }
  612. template<typename _SettableSocketOption>
  613. void
  614. set_option(const _SettableSocketOption& __option)
  615. { set_option(__option, __throw_on_error{"basic_socket::set_option"}); }
  616. template<typename _SettableSocketOption>
  617. void
  618. set_option(const _SettableSocketOption& __option, error_code& __ec)
  619. { __base::set_option(__option, __ec); }
  620. template<typename _GettableSocketOption>
  621. void
  622. get_option(_GettableSocketOption& __option) const
  623. { get_option(__option, __throw_on_error{"basic_socket::get_option"}); }
  624. template<typename _GettableSocketOption>
  625. void
  626. get_option(_GettableSocketOption& __option, error_code& __ec) const
  627. { __base::get_option(__option, __ec); }
  628. template<typename _IoControlCommand>
  629. void
  630. io_control(_IoControlCommand& __command)
  631. {
  632. io_control(__command, __throw_on_error{"basic_socket::io_control"});
  633. }
  634. template<typename _IoControlCommand>
  635. void
  636. io_control(_IoControlCommand& __command, error_code& __ec)
  637. { __base::io_control(__command, __ec); }
  638. void
  639. non_blocking(bool __mode)
  640. { non_blocking(__mode, __throw_on_error{"basic_socket::non_blocking"}); }
  641. void
  642. non_blocking(bool __mode, error_code& __ec)
  643. { __base::non_blocking(__mode, __ec); }
  644. bool non_blocking() const { return __base::non_blocking(); }
  645. void
  646. native_non_blocking(bool __mode)
  647. {
  648. native_non_blocking(__mode, __throw_on_error{
  649. "basic_socket::native_non_blocking"});
  650. }
  651. void
  652. native_non_blocking(bool __mode, error_code& __ec)
  653. { __base::native_non_blocking(__mode, __ec); }
  654. bool
  655. native_non_blocking() const
  656. { return __base::native_non_blocking(); }
  657. bool at_mark() const
  658. { return at_mark(__throw_on_error{"basic_socket::at_mark"}); }
  659. bool
  660. at_mark(error_code& __ec) const
  661. {
  662. #ifdef _GLIBCXX_HAVE_SOCKATMARK
  663. const int __result = ::sockatmark(native_handle());
  664. if (__result == -1)
  665. {
  666. __ec.assign(errno, generic_category());
  667. return false;
  668. }
  669. __ec.clear();
  670. return (bool)__result;
  671. #else
  672. __ec = std::make_error_code(errc::operation_not_supported);
  673. return false;
  674. #endif
  675. }
  676. size_t
  677. available() const
  678. { return available(__throw_on_error{"basic_socket::available"}); }
  679. size_t
  680. available(error_code& __ec) const
  681. {
  682. if (!is_open())
  683. {
  684. __ec = std::make_error_code(errc::bad_file_descriptor);
  685. return 0;
  686. }
  687. #if defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined FIONREAD
  688. int __avail = 0;
  689. if (::ioctl(this->_M_sockfd, FIONREAD, &__avail) == -1)
  690. {
  691. __ec.assign(errno, generic_category());
  692. return 0;
  693. }
  694. __ec.clear();
  695. return __avail;
  696. #else
  697. return 0;
  698. #endif
  699. }
  700. void
  701. bind(const endpoint_type& __endpoint)
  702. { return bind(__endpoint, __throw_on_error{"basic_socket::bind"}); }
  703. void
  704. bind(const endpoint_type& __endpoint, error_code& __ec)
  705. { __base::bind(__endpoint, __ec); }
  706. void shutdown(shutdown_type __what)
  707. { return shutdown(__what, __throw_on_error{"basic_socket::shutdown"}); }
  708. void
  709. shutdown(shutdown_type __what, error_code& __ec)
  710. {
  711. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  712. if (::shutdown(native_handle(), static_cast<int>(__what)) == -1)
  713. __ec.assign(errno, generic_category());
  714. else
  715. __ec.clear();
  716. #else
  717. __ec = std::make_error_code(errc::operation_not_supported);
  718. #endif
  719. }
  720. endpoint_type
  721. local_endpoint() const
  722. {
  723. return local_endpoint(
  724. __throw_on_error{"basic_socket::local_endpoint"});
  725. }
  726. endpoint_type
  727. local_endpoint(error_code& __ec) const
  728. { return __base::local_endpoint(__ec); }
  729. endpoint_type
  730. remote_endpoint() const
  731. {
  732. return remote_endpoint(
  733. __throw_on_error{"basic_socket::remote_endpoint"});
  734. }
  735. endpoint_type
  736. remote_endpoint(error_code& __ec) const
  737. {
  738. endpoint_type __endpoint;
  739. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  740. socklen_t __endpoint_len = __endpoint.capacity();
  741. if (::getpeername(this->_M_sockfd, (sockaddr*)__endpoint.data(),
  742. &__endpoint_len)
  743. == -1)
  744. {
  745. __ec.assign(errno, generic_category());
  746. return endpoint_type{};
  747. }
  748. __ec.clear();
  749. __endpoint.resize(__endpoint_len);
  750. #else
  751. __ec = std::make_error_code(errc::operation_not_supported);
  752. #endif
  753. return __endpoint;
  754. }
  755. void
  756. connect(const endpoint_type& __endpoint)
  757. {
  758. return connect(__endpoint, __throw_on_error{"basic_socket::connect"});
  759. }
  760. void
  761. connect(const endpoint_type& __endpoint, error_code& __ec)
  762. {
  763. if (!is_open())
  764. {
  765. open(__endpoint.protocol(), __ec);
  766. if (__ec)
  767. return;
  768. }
  769. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  770. if (::connect(native_handle(), (const sockaddr*)__endpoint.data(),
  771. __endpoint.size()) == -1)
  772. __ec.assign(errno, generic_category());
  773. else
  774. __ec.clear();
  775. #else
  776. __ec = std::make_error_code(errc::operation_not_supported);
  777. #endif
  778. }
  779. template<typename _CompletionToken>
  780. __deduced_t<_CompletionToken, void(error_code)>
  781. async_connect(const endpoint_type& __endpoint,
  782. _CompletionToken&& __token)
  783. {
  784. async_completion<_CompletionToken, void(error_code)> __init{__token};
  785. if (!is_open())
  786. {
  787. error_code __ec;
  788. open(__endpoint.protocol(), __ec);
  789. if (__ec)
  790. {
  791. auto __ex = net::get_associated_executor(
  792. __init.completion_handler, get_executor());
  793. auto __a = get_associated_allocator(
  794. __init.completion_handler, std::allocator<void>());
  795. __ex.post(
  796. [__h=std::move(__init.completion_handler), __ec]
  797. () mutable
  798. { __h(__ec); }, __a);
  799. return __init.result.get();
  800. }
  801. }
  802. get_executor().context().async_wait( native_handle(),
  803. socket_base::wait_read,
  804. [__h = std::move(__init.completion_handler),
  805. __ep = std::move(__endpoint),
  806. __fd = native_handle()]
  807. (error_code __ec) mutable {
  808. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  809. if (!__ec && ::connect(__fd, (const sockaddr*)__ep.data(),
  810. __ep.size()) == -1)
  811. __ec.assign(errno, generic_category());
  812. #else
  813. __ec = std::make_error_code(errc::operation_not_supported);
  814. #endif
  815. __h(__ec);
  816. });
  817. return __init.result.get();
  818. }
  819. void
  820. wait(wait_type __w)
  821. { return wait(__w, __throw_on_error{"basic_socket::wait"}); }
  822. void
  823. wait(wait_type __w, error_code& __ec)
  824. {
  825. #ifdef _GLIBCXX_HAVE_POLL_H
  826. ::pollfd __fd;
  827. __fd.fd = native_handle();
  828. __fd.events = static_cast<int>(__w);
  829. int __res = ::poll(&__fd, 1, -1);
  830. if (__res == -1)
  831. __ec.assign(errno, generic_category());
  832. else
  833. __ec.clear();
  834. #else
  835. __ec = std::make_error_code(errc::operation_not_supported);
  836. #endif
  837. }
  838. template<typename _CompletionToken>
  839. __deduced_t<_CompletionToken, void(error_code)>
  840. async_wait(wait_type __w, _CompletionToken&& __token)
  841. {
  842. async_completion<_CompletionToken, void(error_code)> __init{__token};
  843. get_executor().context().async_wait( native_handle(),
  844. static_cast<int>(__w),
  845. [__h = std::move(__init.completion_handler)]
  846. (error_code __ec) mutable {
  847. __h(__ec);
  848. });
  849. return __init.result.get();
  850. }
  851. protected:
  852. // construct / copy / destroy:
  853. using __base::__base;
  854. explicit
  855. basic_socket(io_context& __ctx) : __base(__ctx) { }
  856. basic_socket(io_context& __ctx, const protocol_type& __protocol)
  857. : __base(__ctx)
  858. { open(__protocol); }
  859. basic_socket(io_context& __ctx, const endpoint_type& __endpoint)
  860. : basic_socket(std::addressof(__ctx), __endpoint.protocol())
  861. { bind(__endpoint); }
  862. basic_socket(io_context& __ctx, const protocol_type& __protocol,
  863. const native_handle_type& __native_socket)
  864. : __base(__ctx)
  865. { assign(__protocol, __native_socket); }
  866. basic_socket(const basic_socket&) = delete;
  867. basic_socket(basic_socket&& __rhs) = default;
  868. template<typename _OtherProtocol, typename _Requires
  869. = _Require<is_convertible<_OtherProtocol, _Protocol>>>
  870. basic_socket(basic_socket<_OtherProtocol>&& __rhs)
  871. : __base(std::move(__rhs)) { }
  872. ~basic_socket() = default;
  873. basic_socket& operator=(const basic_socket&) = delete;
  874. basic_socket& operator=(basic_socket&& __rhs) = default;
  875. template<typename _OtherProtocol>
  876. enable_if_t<is_convertible<_OtherProtocol, _Protocol>::value,
  877. basic_socket&>
  878. operator=(basic_socket<_OtherProtocol>&& __rhs)
  879. { return *this = basic_socket{std::move(__rhs)}; }
  880. };
  881. template<typename _Protocol>
  882. class basic_datagram_socket : public basic_socket<_Protocol>
  883. {
  884. using __base = basic_socket<_Protocol>;
  885. public:
  886. // types:
  887. typedef int native_handle_type;
  888. typedef _Protocol protocol_type;
  889. typedef typename protocol_type::endpoint endpoint_type;
  890. // construct / copy / destroy:
  891. explicit
  892. basic_datagram_socket(io_context& __ctx) : __base(__ctx) { }
  893. basic_datagram_socket(io_context& __ctx, const protocol_type& __protocol)
  894. : __base(__ctx, __protocol) { }
  895. basic_datagram_socket(io_context& __ctx, const endpoint_type& __endpoint)
  896. : __base(__ctx, __endpoint) { }
  897. basic_datagram_socket(io_context& __ctx, const protocol_type& __protocol,
  898. const native_handle_type& __native_socket)
  899. : __base(__ctx, __protocol, __native_socket) { }
  900. basic_datagram_socket(const basic_datagram_socket&) = delete;
  901. basic_datagram_socket(basic_datagram_socket&& __rhs) = default;
  902. template<typename _OtherProtocol, typename _Requires
  903. = _Require<is_convertible<_OtherProtocol, _Protocol>>>
  904. basic_datagram_socket(basic_datagram_socket<_OtherProtocol>&& __rhs)
  905. : __base(std::move(__rhs)) { }
  906. ~basic_datagram_socket() = default;
  907. basic_datagram_socket& operator=(const basic_datagram_socket&) = delete;
  908. basic_datagram_socket& operator=(basic_datagram_socket&& __rhs) = default;
  909. template<typename _OtherProtocol>
  910. enable_if_t<is_convertible<_OtherProtocol, _Protocol>::value,
  911. basic_datagram_socket&>
  912. operator=(basic_datagram_socket<_OtherProtocol>&& __rhs)
  913. {
  914. __base::operator=(std::move(__rhs));
  915. return *this;
  916. }
  917. // basic_datagram_socket operations:
  918. template<typename _MutableBufferSequence>
  919. size_t
  920. receive(const _MutableBufferSequence& __buffers)
  921. {
  922. return receive(__buffers, socket_base::message_flags(),
  923. __throw_on_error{"basic_datagram_socket::receive"});
  924. }
  925. template<typename _MutableBufferSequence>
  926. size_t
  927. receive(const _MutableBufferSequence& __buffers, error_code& __ec)
  928. { return receive(__buffers, socket_base::message_flags(), __ec); }
  929. template<typename _MutableBufferSequence>
  930. size_t
  931. receive(const _MutableBufferSequence& __buffers,
  932. socket_base::message_flags __flags)
  933. {
  934. return receive(__buffers, __flags,
  935. __throw_on_error{"basic_datagram_socket::receive"});
  936. }
  937. template<typename _MutableBufferSequence>
  938. size_t
  939. receive(const _MutableBufferSequence& __buffers,
  940. socket_base::message_flags __flags, error_code& __ec)
  941. {
  942. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  943. socket_base::__msg_hdr __msg(__buffers);
  944. ssize_t __result = ::recvmsg(this->native_handle(), &__msg,
  945. static_cast<int>(__flags));
  946. if (__result == -1)
  947. {
  948. __ec.assign(errno, generic_category());
  949. return 0;
  950. }
  951. __ec.clear();
  952. return __result;
  953. #else
  954. __ec = std::make_error_code(errc::operation_not_supported);
  955. return 0;
  956. #endif
  957. }
  958. template<typename _MutableBufferSequence, typename _CompletionToken>
  959. __deduced_t<_CompletionToken, void(error_code, size_t)>
  960. async_receive(const _MutableBufferSequence& __buffers,
  961. _CompletionToken&& __token)
  962. {
  963. return async_receive(__buffers, socket_base::message_flags(),
  964. std::forward<_CompletionToken>(__token));
  965. }
  966. template<typename _MutableBufferSequence, typename _CompletionToken>
  967. __deduced_t<_CompletionToken, void(error_code, size_t)>
  968. async_receive(const _MutableBufferSequence& __buffers,
  969. socket_base::message_flags __flags,
  970. _CompletionToken&& __token)
  971. {
  972. async_completion<_CompletionToken, void(error_code, size_t)>
  973. __init{__token};
  974. this->get_executor().context().async_wait(this->native_handle(),
  975. socket_base::wait_read,
  976. [__h = std::move(__init.completion_handler),
  977. &__buffers, __flags = static_cast<int>(__flags),
  978. __fd = this->native_handle()]
  979. (error_code __ec) mutable {
  980. if (__ec)
  981. {
  982. __h(__ec);
  983. return;
  984. }
  985. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  986. socket_base::__msg_hdr __msg(__buffers);
  987. ssize_t __result = ::recvmsg(__fd, &__msg, __flags);
  988. if (__result == -1)
  989. {
  990. __ec.assign(errno, generic_category());
  991. __result = 0;
  992. }
  993. else
  994. __ec.clear();
  995. __h(__ec, __result);
  996. #else
  997. __h(std::make_error_code(errc::operation_not_supported), 0);
  998. #endif
  999. });
  1000. return __init.result.get();
  1001. }
  1002. template<typename _MutableBufferSequence>
  1003. size_t
  1004. receive_from(const _MutableBufferSequence& __buffers,
  1005. endpoint_type& __sender)
  1006. {
  1007. return receive_from(__buffers, __sender,
  1008. socket_base::message_flags(),
  1009. __throw_on_error{
  1010. "basic_datagram_socket::receive_from"});
  1011. }
  1012. template<typename _MutableBufferSequence>
  1013. size_t
  1014. receive_from(const _MutableBufferSequence& __buffers,
  1015. endpoint_type& __sender, error_code& __ec)
  1016. {
  1017. return receive_from(__buffers, __sender,
  1018. socket_base::message_flags(), __ec);
  1019. }
  1020. template<typename _MutableBufferSequence>
  1021. size_t
  1022. receive_from(const _MutableBufferSequence& __buffers,
  1023. endpoint_type& __sender,
  1024. socket_base::message_flags __flags)
  1025. {
  1026. return receive_from(__buffers, __sender, __flags,
  1027. __throw_on_error{
  1028. "basic_datagram_socket::receive_from"});
  1029. }
  1030. template<typename _MutableBufferSequence>
  1031. size_t
  1032. receive_from(const _MutableBufferSequence& __buffers,
  1033. endpoint_type& __sender,
  1034. socket_base::message_flags __flags,
  1035. error_code& __ec)
  1036. {
  1037. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1038. socket_base::__msg_hdr __msg(__buffers, __sender);
  1039. ssize_t __result = ::recvmsg(this->native_handle(), &__msg,
  1040. static_cast<int>(__flags));
  1041. if (__result == -1)
  1042. {
  1043. __ec.assign(errno, generic_category());
  1044. return 0;
  1045. }
  1046. __ec.clear();
  1047. __sender.resize(__msg.msg_namelen);
  1048. return __result;
  1049. #else
  1050. __ec = std::make_error_code(errc::operation_not_supported);
  1051. return 0;
  1052. #endif
  1053. }
  1054. template<typename _MutableBufferSequence, typename _CompletionToken>
  1055. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1056. async_receive_from(const _MutableBufferSequence& __buffers,
  1057. endpoint_type& __sender,
  1058. _CompletionToken&& __token)
  1059. {
  1060. return async_receive_from(__buffers, __sender,
  1061. socket_base::message_flags(),
  1062. std::forward<_CompletionToken>(__token));
  1063. }
  1064. template<typename _MutableBufferSequence, typename _CompletionToken>
  1065. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1066. async_receive_from(const _MutableBufferSequence& __buffers,
  1067. endpoint_type& __sender,
  1068. socket_base::message_flags __flags,
  1069. _CompletionToken&& __token)
  1070. {
  1071. async_completion<_CompletionToken, void(error_code, size_t)>
  1072. __init{__token};
  1073. this->get_executor().context().async_wait( this->native_handle(),
  1074. socket_base::wait_read,
  1075. [__h = std::move(__init.completion_handler),
  1076. &__buffers, __flags = static_cast<int>(__flags),
  1077. __sender = std::move(__sender),
  1078. __fd = this->native_handle()]
  1079. (error_code __ec) mutable {
  1080. if (__ec)
  1081. {
  1082. __h(__ec);
  1083. return;
  1084. }
  1085. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1086. socket_base::__msg_hdr __msg(__buffers, __sender);
  1087. ssize_t __result = ::recvmsg(__fd, &__msg, __flags);
  1088. if (__result == -1)
  1089. {
  1090. __ec.assign(errno, generic_category());
  1091. __result = 0;
  1092. }
  1093. else
  1094. {
  1095. __ec.clear();
  1096. __sender.resize(__msg.msg_namelen);
  1097. }
  1098. __h(__ec, __result);
  1099. #else
  1100. __h(std::make_error_code(errc::operation_not_supported), 0);
  1101. #endif
  1102. });
  1103. return __init.result.get();
  1104. }
  1105. template<typename _ConstBufferSequence>
  1106. size_t
  1107. send(const _ConstBufferSequence& __buffers)
  1108. {
  1109. return send(__buffers, socket_base::message_flags(),
  1110. __throw_on_error{"basic_datagram_socket::send"});
  1111. }
  1112. template<typename _ConstBufferSequence>
  1113. size_t
  1114. send(const _ConstBufferSequence& __buffers, error_code& __ec)
  1115. { return send(__buffers, socket_base::message_flags(), __ec); }
  1116. template<typename _ConstBufferSequence>
  1117. size_t
  1118. send(const _ConstBufferSequence& __buffers,
  1119. socket_base::message_flags __flags)
  1120. {
  1121. return send(__buffers, __flags,
  1122. __throw_on_error{"basic_datagram_socket::send"});
  1123. }
  1124. template<typename _ConstBufferSequence>
  1125. size_t
  1126. send(const _ConstBufferSequence& __buffers,
  1127. socket_base::message_flags __flags, error_code& __ec)
  1128. {
  1129. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1130. socket_base::__msg_hdr __msg(__buffers);
  1131. ssize_t __result = ::sendmsg(this->native_handle(), &__msg,
  1132. static_cast<int>(__flags));
  1133. if (__result == -1)
  1134. {
  1135. __ec.assign(errno, generic_category());
  1136. return 0;
  1137. }
  1138. __ec.clear();
  1139. return __result;
  1140. #else
  1141. __ec = std::make_error_code(errc::operation_not_supported);
  1142. return 0;
  1143. #endif
  1144. }
  1145. template<typename _ConstBufferSequence, typename _CompletionToken>
  1146. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1147. async_send(const _ConstBufferSequence& __buffers,
  1148. _CompletionToken&& __token)
  1149. {
  1150. return async_send(__buffers, socket_base::message_flags(),
  1151. std::forward<_CompletionToken>(__token));
  1152. }
  1153. template<typename _ConstBufferSequence, typename _CompletionToken>
  1154. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1155. async_send(const _ConstBufferSequence& __buffers,
  1156. socket_base::message_flags __flags,
  1157. _CompletionToken&& __token)
  1158. {
  1159. async_completion<_CompletionToken, void(error_code, size_t)>
  1160. __init{__token};
  1161. this->get_executor().context().async_wait( this->native_handle(),
  1162. socket_base::wait_write,
  1163. [__h = std::move(__init.completion_handler),
  1164. &__buffers, __flags = static_cast<int>(__flags),
  1165. __fd = this->native_handle()]
  1166. (error_code __ec) mutable {
  1167. if (__ec)
  1168. {
  1169. __h(__ec);
  1170. return;
  1171. }
  1172. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1173. socket_base::__msg_hdr __msg(__buffers);
  1174. ssize_t __result = ::sendmsg(__fd, &__msg, __flags);
  1175. if (__result == -1)
  1176. {
  1177. __ec.assign(errno, generic_category());
  1178. __result = 0;
  1179. }
  1180. else
  1181. __ec.clear();
  1182. __h(__ec, __result);
  1183. #else
  1184. __h(std::make_error_code(errc::operation_not_supported), 0);
  1185. #endif
  1186. });
  1187. return __init.result.get();
  1188. }
  1189. template<typename _ConstBufferSequence>
  1190. size_t
  1191. send_to(const _ConstBufferSequence& __buffers,
  1192. const endpoint_type& __recipient)
  1193. {
  1194. return send_to(__buffers, __recipient,
  1195. socket_base::message_flags(),
  1196. __throw_on_error{"basic_datagram_socket::send_to"});
  1197. }
  1198. template<typename _ConstBufferSequence>
  1199. size_t
  1200. send_to(const _ConstBufferSequence& __buffers,
  1201. const endpoint_type& __recipient, error_code& __ec)
  1202. {
  1203. return send_to(__buffers, __recipient,
  1204. socket_base::message_flags(), __ec);
  1205. }
  1206. template<typename _ConstBufferSequence>
  1207. size_t
  1208. send_to(const _ConstBufferSequence& __buffers,
  1209. const endpoint_type& __recipient,
  1210. socket_base::message_flags __flags)
  1211. {
  1212. return send_to(__buffers, __recipient, __flags,
  1213. __throw_on_error{"basic_datagram_socket::send_to"});
  1214. }
  1215. template<typename _ConstBufferSequence>
  1216. size_t
  1217. send_to(const _ConstBufferSequence& __buffers,
  1218. const endpoint_type& __recipient,
  1219. socket_base::message_flags __flags, error_code& __ec)
  1220. {
  1221. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1222. socket_base::__msg_hdr __msg(__buffers, __recipient);
  1223. ssize_t __result = ::sendmsg(this->native_handle(), &__msg,
  1224. static_cast<int>(__flags));
  1225. if (__result == -1)
  1226. {
  1227. __ec.assign(errno, generic_category());
  1228. return 0;
  1229. }
  1230. __ec.clear();
  1231. __recipient.resize(__msg.msg_namelen);
  1232. return __result;
  1233. #else
  1234. __ec = std::make_error_code(errc::operation_not_supported);
  1235. return 0;
  1236. #endif
  1237. }
  1238. template<typename _ConstBufferSequence, typename _CompletionToken>
  1239. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1240. async_send_to(const _ConstBufferSequence& __buffers,
  1241. const endpoint_type& __recipient,
  1242. _CompletionToken&& __token)
  1243. {
  1244. return async_send_to(__buffers, __recipient,
  1245. socket_base::message_flags(),
  1246. std::forward<_CompletionToken>(__token));
  1247. }
  1248. template<typename _ConstBufferSequence, typename _CompletionToken>
  1249. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1250. async_send_to(const _ConstBufferSequence& __buffers,
  1251. const endpoint_type& __recipient,
  1252. socket_base::message_flags __flags,
  1253. _CompletionToken&& __token)
  1254. {
  1255. async_completion<_CompletionToken, void(error_code, size_t)>
  1256. __init{__token};
  1257. this->get_executor().context().async_wait( this->native_handle(),
  1258. socket_base::wait_write,
  1259. [__h = std::move(__init.completion_handler),
  1260. &__buffers, __flags = static_cast<int>(__flags),
  1261. __recipient = std::move(__recipient),
  1262. __fd = this->native_handle()]
  1263. (error_code __ec) mutable {
  1264. if (__ec)
  1265. {
  1266. __h(__ec);
  1267. return;
  1268. }
  1269. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1270. socket_base::__msg_hdr __msg(__buffers, __recipient);
  1271. ssize_t __result = ::sendmsg(__fd, &__msg, __flags);
  1272. if (__result == -1)
  1273. {
  1274. __ec.assign(errno, generic_category());
  1275. __result = 0;
  1276. }
  1277. else
  1278. {
  1279. __ec.clear();
  1280. __recipient.resize(__msg.msg_namelen);
  1281. }
  1282. __h(__ec, __result);
  1283. #else
  1284. __h(std::make_error_code(errc::operation_not_supported), 0);
  1285. #endif
  1286. });
  1287. return __init.result.get();
  1288. }
  1289. };
  1290. template<typename _Protocol>
  1291. class basic_stream_socket : public basic_socket<_Protocol>
  1292. {
  1293. using __base = basic_socket<_Protocol>;
  1294. public:
  1295. // types:
  1296. typedef int native_handle_type;
  1297. typedef _Protocol protocol_type;
  1298. typedef typename protocol_type::endpoint endpoint_type;
  1299. // construct / copy / destroy:
  1300. explicit
  1301. basic_stream_socket(io_context& __ctx) : __base(__ctx) { }
  1302. basic_stream_socket(io_context& __ctx, const protocol_type& __protocol)
  1303. : __base(__ctx, __protocol) { }
  1304. basic_stream_socket(io_context& __ctx, const endpoint_type& __endpoint)
  1305. : __base(__ctx, __endpoint) { }
  1306. basic_stream_socket(io_context& __ctx, const protocol_type& __protocol,
  1307. const native_handle_type& __native_socket)
  1308. : __base(__ctx, __protocol, __native_socket) { }
  1309. basic_stream_socket(const basic_stream_socket&) = delete;
  1310. basic_stream_socket(basic_stream_socket&& __rhs) = default;
  1311. template<typename _OtherProtocol, typename _Requires
  1312. = _Require<is_convertible<_OtherProtocol, _Protocol>>>
  1313. basic_stream_socket(basic_stream_socket<_OtherProtocol>&& __rhs)
  1314. : __base(std::move(__rhs)) { }
  1315. ~basic_stream_socket() = default;
  1316. basic_stream_socket& operator=(const basic_stream_socket&) = delete;
  1317. basic_stream_socket& operator=(basic_stream_socket&& __rhs) = default;
  1318. template<class _OtherProtocol>
  1319. enable_if_t<is_convertible<_OtherProtocol, _Protocol>::value,
  1320. basic_stream_socket&>
  1321. operator=(basic_stream_socket<_OtherProtocol>&& __rhs)
  1322. {
  1323. __base::operator=(std::move(__rhs));
  1324. return *this;
  1325. }
  1326. // basic_stream_socket operations:
  1327. template<class _MutableBufferSequence>
  1328. size_t
  1329. receive(const _MutableBufferSequence& __buffers)
  1330. {
  1331. return receive(__buffers, socket_base::message_flags(),
  1332. __throw_on_error{"basic_stream_socket::receive"});
  1333. }
  1334. template<class _MutableBufferSequence>
  1335. size_t
  1336. receive(const _MutableBufferSequence& __buffers, error_code& __ec)
  1337. { return receive(__buffers, socket_base::message_flags(), __ec); }
  1338. template<class _MutableBufferSequence>
  1339. size_t
  1340. receive(const _MutableBufferSequence& __buffers,
  1341. socket_base::message_flags __flags)
  1342. {
  1343. return receive(__buffers, __flags,
  1344. __throw_on_error{"basic_stream_socket::receive"});
  1345. }
  1346. template<class _MutableBufferSequence>
  1347. size_t
  1348. receive(const _MutableBufferSequence& __buffers,
  1349. socket_base::message_flags __flags, error_code& __ec)
  1350. {
  1351. if (__buffer_empty(__buffers))
  1352. {
  1353. __ec.clear();
  1354. return 0;
  1355. }
  1356. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1357. socket_base::__msg_hdr __msg(__buffers);
  1358. ssize_t __result = ::recvmsg(this->native_handle(), &__msg,
  1359. static_cast<int>(__flags));
  1360. if (__result >= 0)
  1361. {
  1362. __ec.clear();
  1363. return __result;
  1364. }
  1365. __ec.assign(errno, generic_category());
  1366. #else
  1367. __ec = std::make_error_code(errc::operation_not_supported);
  1368. #endif
  1369. return 0;
  1370. }
  1371. template<class _MutableBufferSequence, class _CompletionToken>
  1372. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1373. async_receive(const _MutableBufferSequence& __buffers,
  1374. _CompletionToken&& __token)
  1375. {
  1376. return async_receive(__buffers, socket_base::message_flags(),
  1377. std::forward<_CompletionToken>(__token));
  1378. }
  1379. template<class _MutableBufferSequence, class _CompletionToken>
  1380. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1381. async_receive(const _MutableBufferSequence& __buffers,
  1382. socket_base::message_flags __flags,
  1383. _CompletionToken&& __token)
  1384. {
  1385. async_completion<_CompletionToken, void(error_code, size_t)>
  1386. __init{__token};
  1387. if (__buffer_empty(__buffers))
  1388. {
  1389. auto __ex = net::get_associated_executor(
  1390. __init.completion_handler, this->get_executor());
  1391. auto __a = get_associated_allocator(
  1392. __init.completion_handler, std::allocator<void>());
  1393. __ex.post(
  1394. [__h=std::move(__init.completion_handler)] () mutable
  1395. { __h(error_code{}, 0); }, __a);
  1396. return __init.result.get();
  1397. }
  1398. this->get_executor().context().async_wait(this->native_handle(),
  1399. socket_base::wait_read,
  1400. [__h = std::move(__init.completion_handler),
  1401. &__buffers, __flags = static_cast<int>(__flags),
  1402. __fd = this->native_handle()]
  1403. (error_code __ec) mutable {
  1404. if (__ec)
  1405. {
  1406. __h(__ec);
  1407. return;
  1408. }
  1409. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1410. socket_base::__msg_hdr __msg(__buffers);
  1411. ssize_t __result = ::recvmsg(__fd, &__msg, __flags);
  1412. if (__result == -1)
  1413. {
  1414. __ec.assign(errno, generic_category());
  1415. __result = 0;
  1416. }
  1417. else
  1418. __ec.clear();
  1419. __h(__ec, __result);
  1420. #else
  1421. __h(std::make_error_code(errc::operation_not_supported), 0);
  1422. #endif
  1423. });
  1424. return __init.result.get();
  1425. }
  1426. template<class _ConstBufferSequence>
  1427. size_t
  1428. send(const _ConstBufferSequence& __buffers)
  1429. {
  1430. return send(__buffers, socket_base::message_flags(),
  1431. __throw_on_error{"basic_stream_socket::send"});
  1432. }
  1433. template<class _ConstBufferSequence>
  1434. size_t
  1435. send(const _ConstBufferSequence& __buffers, error_code& __ec)
  1436. { return send(__buffers, socket_base::message_flags(), __ec); }
  1437. template<class _ConstBufferSequence>
  1438. size_t
  1439. send(const _ConstBufferSequence& __buffers,
  1440. socket_base::message_flags __flags)
  1441. {
  1442. return send(__buffers, socket_base::message_flags(),
  1443. __throw_on_error{"basic_stream_socket::send"});
  1444. }
  1445. template<class _ConstBufferSequence>
  1446. size_t
  1447. send(const _ConstBufferSequence& __buffers,
  1448. socket_base::message_flags __flags, error_code& __ec)
  1449. {
  1450. if (__buffer_empty(__buffers))
  1451. {
  1452. __ec.clear();
  1453. return 0;
  1454. }
  1455. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1456. socket_base::__msg_hdr __msg(__buffers);
  1457. ssize_t __result = ::sendmsg(this->native_handle(), &__msg,
  1458. static_cast<int>(__flags));
  1459. if (__result >= 0)
  1460. {
  1461. __ec.clear();
  1462. return __result;
  1463. }
  1464. __ec.assign(errno, generic_category());
  1465. #else
  1466. __ec = std::make_error_code(errc::operation_not_supported);
  1467. #endif
  1468. return 0;
  1469. }
  1470. template<class _ConstBufferSequence, class _CompletionToken>
  1471. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1472. async_send(const _ConstBufferSequence& __buffers,
  1473. _CompletionToken&& __token)
  1474. {
  1475. return async_send(__buffers, socket_base::message_flags(),
  1476. std::forward<_CompletionToken>(__token));
  1477. }
  1478. template<class _ConstBufferSequence, class _CompletionToken>
  1479. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1480. async_send(const _ConstBufferSequence& __buffers,
  1481. socket_base::message_flags __flags,
  1482. _CompletionToken&& __token)
  1483. {
  1484. async_completion<_CompletionToken, void(error_code, size_t)>
  1485. __init{__token};
  1486. if (__buffer_empty(__buffers))
  1487. {
  1488. auto __ex = net::get_associated_executor(
  1489. __init.completion_handler, this->get_executor());
  1490. auto __a = get_associated_allocator(
  1491. __init.completion_handler, std::allocator<void>());
  1492. __ex.post(
  1493. [__h=std::move(__init.completion_handler)] () mutable
  1494. { __h(error_code{}, 0); }, __a);
  1495. return __init.result.get();
  1496. }
  1497. this->get_executor().context().async_wait(this->native_handle(),
  1498. socket_base::wait_write,
  1499. [__h = std::move(__init.completion_handler),
  1500. &__buffers, __flags = static_cast<int>(__flags),
  1501. __fd = this->native_handle()]
  1502. (error_code __ec) mutable {
  1503. if (__ec)
  1504. {
  1505. __h(__ec);
  1506. return;
  1507. }
  1508. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1509. socket_base::__msg_hdr __msg(__buffers);
  1510. ssize_t __result = ::sendmsg(__fd, &__msg, __flags);
  1511. if (__result == -1)
  1512. {
  1513. __ec.assign(errno, generic_category());
  1514. __result = 0;
  1515. }
  1516. else
  1517. __ec.clear();
  1518. __h(__ec, __result);
  1519. #else
  1520. __h(std::make_error_code(errc::operation_not_supported), 0);
  1521. #endif
  1522. });
  1523. return __init.result.get();
  1524. }
  1525. template<class _MutableBufferSequence>
  1526. size_t
  1527. read_some(const _MutableBufferSequence& __buffers)
  1528. {
  1529. return receive(__buffers,
  1530. __throw_on_error{"basic_stream_socket::read_some"});
  1531. }
  1532. template<class _MutableBufferSequence>
  1533. size_t
  1534. read_some(const _MutableBufferSequence& __buffers, error_code& __ec)
  1535. { return receive(__buffers, __ec); }
  1536. template<class _MutableBufferSequence, class _CompletionToken>
  1537. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1538. async_read_some(const _MutableBufferSequence& __buffers,
  1539. _CompletionToken&& __token)
  1540. {
  1541. return async_receive(__buffers,
  1542. std::forward<_CompletionToken>(__token));
  1543. }
  1544. template<class _ConstBufferSequence>
  1545. size_t
  1546. write_some(const _ConstBufferSequence& __buffers)
  1547. {
  1548. return send(__buffers,
  1549. __throw_on_error{"basic_stream_socket:write_some"});
  1550. }
  1551. template<class _ConstBufferSequence>
  1552. size_t
  1553. write_some(const _ConstBufferSequence& __buffers, error_code& __ec)
  1554. { return send(__buffers, __ec); }
  1555. template<class _ConstBufferSequence, class _CompletionToken>
  1556. __deduced_t<_CompletionToken, void(error_code, size_t)>
  1557. async_write_some(const _ConstBufferSequence& __buffers,
  1558. _CompletionToken&& __token)
  1559. {
  1560. return async_send(__buffers,
  1561. std::forward<_CompletionToken>(__token));
  1562. }
  1563. };
  1564. template<typename _AcceptableProtocol>
  1565. class basic_socket_acceptor
  1566. : public socket_base, private __basic_socket_impl<_AcceptableProtocol>
  1567. {
  1568. using __base = __basic_socket_impl<_AcceptableProtocol>;
  1569. public:
  1570. // types:
  1571. typedef io_context::executor_type executor_type;
  1572. typedef int native_handle_type;
  1573. typedef _AcceptableProtocol protocol_type;
  1574. typedef typename protocol_type::endpoint endpoint_type;
  1575. typedef typename protocol_type::socket socket_type;
  1576. // construct / copy / destroy:
  1577. explicit
  1578. basic_socket_acceptor(io_context& __ctx)
  1579. : __base(__ctx), _M_protocol(endpoint_type{}.protocol()) { }
  1580. basic_socket_acceptor(io_context& __ctx,
  1581. const protocol_type& __protocol)
  1582. : __base(__ctx), _M_protocol(__protocol)
  1583. { open(__protocol); }
  1584. basic_socket_acceptor(io_context& __ctx, const endpoint_type& __endpoint,
  1585. bool __reuse_addr = true)
  1586. : basic_socket_acceptor(__ctx, __endpoint.protocol())
  1587. {
  1588. if (__reuse_addr)
  1589. set_option(reuse_address(true));
  1590. bind(__endpoint);
  1591. listen();
  1592. }
  1593. basic_socket_acceptor(io_context& __ctx, const protocol_type& __protocol,
  1594. const native_handle_type& __native_acceptor)
  1595. : basic_socket_acceptor(__ctx, __protocol)
  1596. { assign(__protocol, __native_acceptor); }
  1597. basic_socket_acceptor(const basic_socket_acceptor&) = delete;
  1598. basic_socket_acceptor(basic_socket_acceptor&&) = default;
  1599. template<typename _OtherProtocol, typename _Requires
  1600. = _Require<is_convertible<_OtherProtocol, protocol_type>>>
  1601. basic_socket_acceptor(basic_socket_acceptor<_OtherProtocol>&& __rhs)
  1602. : __base(std::move(__rhs)) { }
  1603. ~basic_socket_acceptor() = default;
  1604. basic_socket_acceptor& operator=(const basic_socket_acceptor&) = delete;
  1605. basic_socket_acceptor& operator=(basic_socket_acceptor&&) = default;
  1606. template<class _OtherProtocol>
  1607. enable_if_t<is_convertible<_OtherProtocol, protocol_type>::value,
  1608. basic_socket_acceptor&>
  1609. operator=(basic_socket_acceptor<_OtherProtocol>&& __rhs)
  1610. {
  1611. __base::operator=(std::move(__rhs));
  1612. return *this;
  1613. }
  1614. // basic_socket_acceptor operations:
  1615. executor_type get_executor() noexcept { return __base::get_executor(); }
  1616. native_handle_type
  1617. native_handle() noexcept { return __base::native_handle(); }
  1618. void
  1619. open(const protocol_type& __protocol = protocol_type())
  1620. { open(__protocol, __throw_on_error{"basic_socket_acceptor::open"}); }
  1621. void
  1622. open(const protocol_type& __protocol, error_code& __ec)
  1623. { __base::open(__protocol, __ec); }
  1624. void
  1625. assign(const protocol_type& __protocol,
  1626. const native_handle_type& __native_acceptor)
  1627. {
  1628. assign(__protocol, __native_acceptor,
  1629. __throw_on_error{"basic_socket_acceptor::assign"});
  1630. }
  1631. void
  1632. assign(const protocol_type& __protocol,
  1633. const native_handle_type& __native_acceptor,
  1634. error_code& __ec)
  1635. { __base::assign(__protocol, __native_acceptor, __ec); }
  1636. bool
  1637. is_open() const noexcept { return __base::is_open(); }
  1638. void
  1639. close() { close(__throw_on_error{"basic_socket_acceptor::close"}); }
  1640. void
  1641. close(error_code& __ec) { __base::_close(__ec); }
  1642. void
  1643. cancel() { cancel(__throw_on_error{"basic_socket_acceptor::cancel"}); }
  1644. void
  1645. cancel(error_code& __ec) { __base::cancel(__ec); }
  1646. template<typename _SettableSocketOption>
  1647. void
  1648. set_option(const _SettableSocketOption& __option)
  1649. {
  1650. set_option(__option,
  1651. __throw_on_error{"basic_socket_acceptor::set_option"});
  1652. }
  1653. template<typename _SettableSocketOption>
  1654. void
  1655. set_option(const _SettableSocketOption& __option, error_code& __ec)
  1656. { __base::set_option(__option, __ec); }
  1657. template<typename _GettableSocketOption>
  1658. void
  1659. get_option(_GettableSocketOption& __option) const
  1660. {
  1661. get_option(__option,
  1662. __throw_on_error{"basic_socket_acceptor::get_option"});
  1663. }
  1664. template<typename _GettableSocketOption>
  1665. void
  1666. get_option(_GettableSocketOption& __option, error_code& __ec) const
  1667. { __base::get_option(__option, __ec); }
  1668. template<typename _IoControlCommand>
  1669. void
  1670. io_control(_IoControlCommand& __command)
  1671. {
  1672. io_control(__command,
  1673. __throw_on_error{"basic_socket_acceptor::io_control"});
  1674. }
  1675. template<typename _IoControlCommand>
  1676. void
  1677. io_control(_IoControlCommand& __command, error_code& __ec)
  1678. { __base::io_control(__command, __ec); }
  1679. void
  1680. non_blocking(bool __mode)
  1681. {
  1682. non_blocking(__mode,
  1683. __throw_on_error{"basic_socket_acceptor::non_blocking"});
  1684. }
  1685. void
  1686. non_blocking(bool __mode, error_code& __ec)
  1687. { __base::non_blocking(__mode, __ec); }
  1688. bool non_blocking() const { return __base::non_blocking(); }
  1689. void
  1690. native_non_blocking(bool __mode)
  1691. {
  1692. native_non_blocking(__mode, __throw_on_error{
  1693. "basic_socket_acceptor::native_non_blocking"});
  1694. }
  1695. void
  1696. native_non_blocking(bool __mode, error_code& __ec)
  1697. { __base::native_non_blocking(__mode, __ec); }
  1698. bool
  1699. native_non_blocking() const
  1700. { return __base::native_non_blocking(); }
  1701. void
  1702. bind(const endpoint_type& __endpoint)
  1703. {
  1704. return bind(__endpoint,
  1705. __throw_on_error{"basic_socket_acceptor::bind"});
  1706. }
  1707. void
  1708. bind(const endpoint_type& __endpoint, error_code& __ec)
  1709. { __base::bind(__endpoint, __ec); }
  1710. void
  1711. listen(int __backlog = max_listen_connections)
  1712. {
  1713. return listen(__backlog,
  1714. __throw_on_error{"basic_socket_acceptor::listen"});
  1715. }
  1716. void
  1717. listen(int __backlog, error_code& __ec)
  1718. {
  1719. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1720. if (::listen(native_handle(), __backlog) == -1)
  1721. __ec.assign(errno, generic_category());
  1722. else
  1723. __ec.clear();
  1724. #else
  1725. __ec = std::make_error_code(errc::operation_not_supported);
  1726. #endif
  1727. }
  1728. endpoint_type
  1729. local_endpoint() const
  1730. {
  1731. return local_endpoint(
  1732. __throw_on_error{"basic_socket_acceptor::local_endpoint"});
  1733. }
  1734. endpoint_type
  1735. local_endpoint(error_code& __ec) const
  1736. { return __base::local_endpoint(__ec); }
  1737. void
  1738. enable_connection_aborted(bool __mode)
  1739. { __base::_M_bits.enable_connection_aborted = __mode; }
  1740. bool
  1741. enable_connection_aborted() const
  1742. { return __base::_M_bits.enable_connection_aborted; }
  1743. socket_type
  1744. accept()
  1745. { return accept(__throw_on_error{"basic_socket_acceptor::accept"}); }
  1746. socket_type
  1747. accept(error_code& __ec)
  1748. { return accept(get_executor().context(), __ec); }
  1749. socket_type accept(io_context& __ctx)
  1750. {
  1751. return accept(__ctx,
  1752. __throw_on_error{"basic_socket_acceptor::accept"});
  1753. }
  1754. socket_type
  1755. accept(io_context& __ctx, error_code& __ec)
  1756. {
  1757. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1758. do
  1759. {
  1760. int __h = ::accept(native_handle(), nullptr, 0);
  1761. if (__h != -1)
  1762. {
  1763. __ec.clear();
  1764. return socket_type{__ctx, _M_protocol, __h};
  1765. }
  1766. } while (errno == ECONNABORTED && enable_connection_aborted());
  1767. __ec.assign(errno, generic_category());
  1768. #else
  1769. __ec = std::make_error_code(errc::operation_not_supported);
  1770. #endif
  1771. return socket_type{__ctx};
  1772. }
  1773. template<class _CompletionToken>
  1774. __deduced_t<_CompletionToken, void(error_code, socket_type)>
  1775. async_accept(_CompletionToken&& __token)
  1776. {
  1777. return async_accept(get_executor().context(),
  1778. std::forward<_CompletionToken>(__token));
  1779. }
  1780. template<class _CompletionToken>
  1781. __deduced_t<_CompletionToken, void(error_code, socket_type)>
  1782. async_accept(io_context& __ctx, _CompletionToken&& __token)
  1783. {
  1784. async_completion<_CompletionToken, void(error_code, socket_type)>
  1785. __init{__token};
  1786. __ctx.get_executor().context().async_wait(native_handle(),
  1787. socket_base::wait_read,
  1788. [__h = std::move(__init.completion_handler),
  1789. __connabort = enable_connection_aborted(),
  1790. __fd = native_handle(),
  1791. __protocol = _M_protocol,
  1792. &__ctx
  1793. ]
  1794. (error_code __ec) mutable {
  1795. if (__ec)
  1796. {
  1797. __h(__ec, socket_type(__ctx));
  1798. return;
  1799. }
  1800. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1801. do
  1802. {
  1803. int __newfd = ::accept(__fd, nullptr, 0);
  1804. if (__newfd != -1)
  1805. {
  1806. __ec.clear();
  1807. __h(__ec, socket_type{__ctx, __protocol, __newfd});
  1808. return;
  1809. }
  1810. } while (errno == ECONNABORTED && __connabort);
  1811. __ec.assign(errno, generic_category());
  1812. __h(__ec, socket_type(__ctx));
  1813. #else
  1814. __h(std::make_error_code(errc::operation_not_supported), 0);
  1815. #endif
  1816. });
  1817. return __init.result.get();
  1818. }
  1819. socket_type
  1820. accept(endpoint_type& __endpoint)
  1821. {
  1822. return accept(get_executor().context(), __endpoint,
  1823. __throw_on_error{"basic_socket_acceptor::accept"});
  1824. }
  1825. socket_type
  1826. accept(endpoint_type& __endpoint, error_code& __ec)
  1827. { return accept(get_executor().context(), __endpoint, __ec); }
  1828. socket_type
  1829. accept(io_context& __ctx, endpoint_type& __endpoint)
  1830. {
  1831. return accept(__ctx, __endpoint,
  1832. __throw_on_error{"basic_socket_acceptor::accept"});
  1833. }
  1834. socket_type
  1835. accept(io_context& __ctx, endpoint_type& __endpoint, error_code& __ec)
  1836. {
  1837. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1838. do
  1839. {
  1840. socklen_t __len = __endpoint.capacity();
  1841. int __h = ::accept(native_handle(), (sockaddr*)__endpoint.data(),
  1842. &__len);
  1843. if (__h != -1)
  1844. {
  1845. __endpoint.resize(__len);
  1846. return socket_type{__ctx, _M_protocol, __h};
  1847. }
  1848. } while (errno == ECONNABORTED && enable_connection_aborted());
  1849. __ec.assign(errno, generic_category());
  1850. #else
  1851. __ec = std::make_error_code(errc::operation_not_supported);
  1852. #endif
  1853. return socket_type{__ctx};
  1854. }
  1855. template<class _CompletionToken>
  1856. __deduced_t<_CompletionToken, void(error_code, socket_type)>
  1857. async_accept(endpoint_type& __endpoint,
  1858. _CompletionToken&& __token)
  1859. {
  1860. return async_accept(get_executor().context(), __endpoint,
  1861. std::forward<_CompletionToken>(__token));
  1862. }
  1863. template<class _CompletionToken>
  1864. __deduced_t<_CompletionToken, void(error_code, socket_type)>
  1865. async_accept(io_context& __ctx, endpoint_type& __endpoint,
  1866. _CompletionToken&& __token)
  1867. {
  1868. async_completion<_CompletionToken, void(error_code, socket_type)>
  1869. __init{__token};
  1870. __ctx.get_executor().context().async_wait(native_handle(),
  1871. socket_base::wait_read,
  1872. [__h = std::move(__init.completion_handler),
  1873. __ep = std::move(__endpoint),
  1874. __connabort = enable_connection_aborted(),
  1875. __fd = native_handle(),
  1876. &__ctx
  1877. ]
  1878. (error_code __ec) mutable {
  1879. if (__ec)
  1880. {
  1881. __h(__ec, socket_type(__ctx));
  1882. return;
  1883. }
  1884. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  1885. do
  1886. {
  1887. socklen_t __len = __ep.capacity();
  1888. int __newfd = ::accept(__fd, __ep.data, &__len);
  1889. if (__newfd != -1)
  1890. {
  1891. __ep.resize(__len);
  1892. auto __protocol = __ep.protocol();
  1893. __ec.clear();
  1894. __h(__ec, socket_type{__ctx, __protocol, __newfd});
  1895. return;
  1896. }
  1897. } while (errno == ECONNABORTED && __connabort);
  1898. __ec.assign(errno, generic_category());
  1899. #else
  1900. __ec = std::make_error_code(errc::operation_not_supported);
  1901. #endif
  1902. __h(__ec, socket_type(__ctx));
  1903. });
  1904. return __init.result.get();
  1905. }
  1906. void
  1907. wait(wait_type __w)
  1908. { wait(__w, __throw_on_error{"basic_socket_acceptor::wait"}); }
  1909. void
  1910. wait(wait_type __w, error_code& __ec)
  1911. {
  1912. #ifdef _GLIBCXX_HAVE_POLL_H
  1913. ::pollfd __fds;
  1914. __fds.fd = native_handle();
  1915. __fds.events = __w; // __w | POLLIN;
  1916. if (::poll(&__fds, 1, -1) == -1)
  1917. __ec.assign(errno, generic_category());
  1918. else
  1919. __ec.clear();
  1920. #else
  1921. __ec = std::make_error_code(errc::operation_not_supported);
  1922. #endif
  1923. }
  1924. template<class _CompletionToken>
  1925. __deduced_t<_CompletionToken, void(error_code)>
  1926. async_wait(wait_type __w, _CompletionToken&& __token)
  1927. {
  1928. async_completion<_CompletionToken, void(error_code)> __init{__token};
  1929. get_executor().context().async_wait( native_handle(),
  1930. static_cast<int>(__w),
  1931. [__h = std::move(__init.completion_handler)]
  1932. (error_code __ec) mutable {
  1933. __h(__ec);
  1934. });
  1935. return __init.result.get();
  1936. }
  1937. private:
  1938. protocol_type _M_protocol;
  1939. };
  1940. // @}
  1941. /** @brief Socket streams
  1942. * @{
  1943. */
  1944. template<typename _Protocol, typename _Clock, typename _WaitTraits>
  1945. class basic_socket_streambuf : public basic_streambuf<char>
  1946. {
  1947. public:
  1948. // types:
  1949. typedef _Protocol protocol_type;
  1950. typedef typename protocol_type::endpoint endpoint_type;
  1951. typedef _Clock clock_type;
  1952. typedef typename clock_type::time_point time_point;
  1953. typedef typename clock_type::duration duration;
  1954. typedef _WaitTraits wait_traits_type;
  1955. // construct / copy / destroy:
  1956. basic_socket_streambuf() : _M_socket(_S_ctx()) { }
  1957. explicit
  1958. basic_socket_streambuf(basic_stream_socket<protocol_type> __s)
  1959. : _M_socket(std::move(__s)) { }
  1960. basic_socket_streambuf(const basic_socket_streambuf&) = delete;
  1961. basic_socket_streambuf(basic_socket_streambuf&& __rhs); // TODO
  1962. virtual ~basic_socket_streambuf(); // TODO
  1963. basic_socket_streambuf& operator=(const basic_socket_streambuf&) = delete;
  1964. basic_socket_streambuf& operator=(basic_socket_streambuf&& __rhs); // TODO
  1965. // members:
  1966. basic_socket_streambuf* connect(const endpoint_type& __e); // TODO
  1967. template<typename... _Args>
  1968. basic_socket_streambuf* connect(_Args&&... ); // TODO
  1969. basic_socket_streambuf* close(); // TODO
  1970. basic_socket<protocol_type>& socket() { return _M_socket; }
  1971. error_code error() const { return _M_ec; }
  1972. time_point expiry() const { return _M_expiry; }
  1973. void
  1974. expires_at(const time_point& __t)
  1975. { _M_expiry = __t; }
  1976. void
  1977. expires_after(const duration& __d)
  1978. { expires_at(clock_type::now() + __d); }
  1979. protected:
  1980. // overridden virtual functions: // TODO
  1981. virtual int_type underflow() override;
  1982. virtual int_type pbackfail(int_type __c = traits_type::eof()) override;
  1983. virtual int_type overflow(int_type __c = traits_type::eof()) override;
  1984. virtual int sync() override;
  1985. virtual streambuf* setbuf(char_type* __s, streamsize __n) override;
  1986. private:
  1987. static io_context&
  1988. _S_ctx()
  1989. {
  1990. static io_context __ctx;
  1991. return __ctx;
  1992. }
  1993. basic_stream_socket<protocol_type> _M_socket;
  1994. error_code _M_ec;
  1995. time_point _M_expiry{ time_point::max() };
  1996. };
  1997. template<typename _Protocol, class _Clock, typename _WaitTraits>
  1998. class basic_socket_iostream : public basic_iostream<char>
  1999. {
  2000. using __streambuf_type
  2001. = basic_socket_streambuf<_Protocol, _Clock, _WaitTraits>;
  2002. public:
  2003. // types:
  2004. typedef _Protocol protocol_type;
  2005. typedef typename protocol_type::endpoint endpoint_type;
  2006. typedef _Clock clock_type;
  2007. typedef typename clock_type::time_point time_point;
  2008. typedef typename clock_type::duration duration;
  2009. typedef _WaitTraits wait_traits_type;
  2010. // construct / copy / destroy:
  2011. // TODO base-from-member ?
  2012. basic_socket_iostream() : basic_iostream(nullptr), _M_sb()
  2013. {
  2014. this->init(std::addressof(_M_sb));
  2015. this->setf(std::ios::unitbuf);
  2016. }
  2017. explicit
  2018. basic_socket_iostream(basic_stream_socket<protocol_type> __s)
  2019. : basic_iostream(nullptr), _M_sb(std::move(__s))
  2020. {
  2021. this->init(std::addressof(_M_sb));
  2022. this->setf(std::ios::unitbuf);
  2023. }
  2024. basic_socket_iostream(const basic_socket_iostream&) = delete;
  2025. basic_socket_iostream(basic_socket_iostream&& __rhs)
  2026. : basic_iostream(nullptr), _M_sb(std::move(__rhs._M_sb))
  2027. // XXX ??? ^^^^^^^
  2028. {
  2029. // XXX ??? this->init(std::addressof(_M_sb));
  2030. this->set_rbduf(std::addressof(_M_sb));
  2031. }
  2032. template<typename... _Args>
  2033. explicit
  2034. basic_socket_iostream(_Args&&... __args)
  2035. : basic_iostream(nullptr), _M_sb()
  2036. {
  2037. this->init(std::addressof(_M_sb));
  2038. this->setf(std::ios::unitbuf);
  2039. connect(forward<_Args>(__args)...);
  2040. }
  2041. basic_socket_iostream& operator=(const basic_socket_iostream&) = delete;
  2042. basic_socket_iostream& operator=(basic_socket_iostream&& __rhs); // TODO
  2043. // members:
  2044. template<typename... _Args>
  2045. void
  2046. connect(_Args&&... __args)
  2047. {
  2048. if (rdbuf()->connect(forward<_Args>(__args)...) == nullptr)
  2049. this->setstate(failbit);
  2050. }
  2051. void
  2052. close()
  2053. {
  2054. if (rdbuf()->close() == nullptr)
  2055. this->setstate(failbit);
  2056. }
  2057. basic_socket_streambuf<protocol_type, clock_type, wait_traits_type>*
  2058. rdbuf() const
  2059. { return const_cast<__streambuf_type*>(std::addressof(_M_sb)); }
  2060. basic_socket<protocol_type>& socket() { return rdbuf()->socket(); }
  2061. error_code error() const { return rdbuf()->error(); }
  2062. time_point expiry() const { return rdbuf()->expiry(); }
  2063. void expires_at(const time_point& __t) { rdbuf()->expires_at(__t); }
  2064. void expires_after(const duration& __d) { rdbuf()->expires_after(__d); }
  2065. private:
  2066. __streambuf_type _M_sb;
  2067. };
  2068. // @}
  2069. /** @brief synchronous connect operations
  2070. * @{
  2071. */
  2072. template<typename _Protocol, typename _EndpointSequence,
  2073. typename _ConnectCondition>
  2074. inline typename _Protocol::endpoint
  2075. connect(basic_socket<_Protocol>& __s,
  2076. const _EndpointSequence& __endpoints,
  2077. _ConnectCondition __c, error_code& __ec)
  2078. {
  2079. __ec.clear();
  2080. bool __found = false;
  2081. for (auto& __ep : __endpoints)
  2082. {
  2083. if (__c(__ec, __ep))
  2084. {
  2085. __found = true;
  2086. __s.close(__ec);
  2087. if (!__ec)
  2088. __s.open(__ep.protocol(), __ec);
  2089. if (!__ec)
  2090. __s.connect(__ep, __ec);
  2091. if (!__ec)
  2092. return __ep;
  2093. }
  2094. }
  2095. if (!__found)
  2096. __ec = socket_errc::not_found;
  2097. return typename _Protocol::endpoint{};
  2098. }
  2099. template<typename _Protocol, typename _InputIterator,
  2100. typename _ConnectCondition>
  2101. inline _InputIterator
  2102. connect(basic_socket<_Protocol>& __s,
  2103. _InputIterator __first, _InputIterator __last,
  2104. _ConnectCondition __c, error_code& __ec)
  2105. {
  2106. __ec.clear();
  2107. bool __found = false;
  2108. for (auto __i = __first; __i != __last; ++__i)
  2109. {
  2110. if (__c(__ec, *__i))
  2111. {
  2112. __found = true;
  2113. __s.close(__ec);
  2114. if (!__ec)
  2115. __s.open(typename _Protocol::endpoint(*__i).protocol(), __ec);
  2116. if (!__ec)
  2117. __s.connect(*__i, __ec);
  2118. if (!__ec)
  2119. return __i;
  2120. }
  2121. }
  2122. if (!__found)
  2123. __ec = socket_errc::not_found;
  2124. return __last;
  2125. }
  2126. template<typename _Protocol, typename _EndpointSequence,
  2127. typename _ConnectCondition>
  2128. inline typename _Protocol::endpoint
  2129. connect(basic_socket<_Protocol>& __s,
  2130. const _EndpointSequence& __endpoints,
  2131. _ConnectCondition __c)
  2132. {
  2133. return net::connect(__s, __endpoints, __c, __throw_on_error{"connect"});
  2134. }
  2135. template<typename _Protocol, typename _InputIterator,
  2136. typename _ConnectCondition>
  2137. inline _InputIterator
  2138. connect(basic_socket<_Protocol>& __s,
  2139. _InputIterator __first, _InputIterator __last,
  2140. _ConnectCondition __c)
  2141. {
  2142. return net::connect(__s, __first, __last, __c,
  2143. __throw_on_error{"connect"});
  2144. }
  2145. template<typename _Protocol, typename _EndpointSequence>
  2146. inline typename _Protocol::endpoint
  2147. connect(basic_socket<_Protocol>& __s,
  2148. const _EndpointSequence& __endpoints)
  2149. {
  2150. return net::connect(__s, __endpoints, [](auto, auto){ return true; },
  2151. __throw_on_error{"connect"});
  2152. }
  2153. template<typename _Protocol, typename _EndpointSequence>
  2154. inline typename _Protocol::endpoint
  2155. connect(basic_socket<_Protocol>& __s,
  2156. const _EndpointSequence& __endpoints,
  2157. error_code& __ec)
  2158. {
  2159. return net::connect(__s, __endpoints, [](auto, auto){ return true; },
  2160. __ec);
  2161. }
  2162. template<typename _Protocol, typename _InputIterator>
  2163. inline _InputIterator
  2164. connect(basic_socket<_Protocol>& __s,
  2165. _InputIterator __first, _InputIterator __last)
  2166. {
  2167. return net::connect(__s, __first, __last, [](auto, auto){ return true; },
  2168. __throw_on_error{"connect"});
  2169. }
  2170. template<typename _Protocol, typename _InputIterator>
  2171. inline _InputIterator
  2172. connect(basic_socket<_Protocol>& __s,
  2173. _InputIterator __first, _InputIterator __last,
  2174. error_code& __ec)
  2175. {
  2176. return net::connect(__s, __first, __last, [](auto, auto){ return true; },
  2177. __ec);
  2178. }
  2179. // @}
  2180. /** @brief asynchronous connect operations
  2181. * @{
  2182. */
  2183. template<typename _Protocol, typename _EndpointSequence,
  2184. typename _ConnectCondition, typename _CompletionToken>
  2185. inline
  2186. __deduced_t<_CompletionToken,
  2187. void(error_code, typename _Protocol::endpoint)>
  2188. async_connect(basic_socket<_Protocol>& __s,
  2189. const _EndpointSequence& __endpoints,
  2190. _ConnectCondition __c, _CompletionToken&& __token); // TODO
  2191. template<typename _Protocol, typename _EndpointSequence,
  2192. typename _CompletionToken>
  2193. inline
  2194. __deduced_t<_CompletionToken,
  2195. void(error_code, typename _Protocol::endpoint)>
  2196. async_connect(basic_socket<_Protocol>& __s,
  2197. const _EndpointSequence& __endpoints,
  2198. _CompletionToken&& __token)
  2199. {
  2200. return net::async_connect(__s, __endpoints,
  2201. [](auto, auto){ return true; },
  2202. forward<_CompletionToken>(__token));
  2203. }
  2204. template<typename _Protocol, typename _InputIterator,
  2205. typename _ConnectCondition, typename _CompletionToken>
  2206. inline
  2207. __deduced_t<_CompletionToken, void(error_code, _InputIterator)>
  2208. async_connect(basic_socket<_Protocol>& __s,
  2209. _InputIterator __first, _InputIterator __last,
  2210. _ConnectCondition __c, _CompletionToken&& __token); // TODO
  2211. template<typename _Protocol, typename _InputIterator,
  2212. typename _CompletionToken>
  2213. inline
  2214. __deduced_t<_CompletionToken, void(error_code, _InputIterator)>
  2215. async_connect(basic_socket<_Protocol>& __s,
  2216. _InputIterator __first, _InputIterator __last,
  2217. _CompletionToken&& __token)
  2218. {
  2219. return net::async_connect(__s, __first, __last,
  2220. [](auto, auto){ return true; },
  2221. forward<_CompletionToken>(__token));
  2222. }
  2223. // @}
  2224. #endif // _GLIBCXX_HAVE_UNISTD_H
  2225. // @}
  2226. } // namespace v1
  2227. } // namespace net
  2228. } // namespace experimental
  2229. template<>
  2230. struct is_error_code_enum<experimental::net::v1::socket_errc>
  2231. : public true_type {};
  2232. _GLIBCXX_END_NAMESPACE_VERSION
  2233. } // namespace std
  2234. #endif // C++14
  2235. #endif // _GLIBCXX_EXPERIMENTAL_SOCKET