socket 74 KB

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