winrt_ssocket_service_base.ipp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. //
  2. // detail/impl/winrt_ssocket_service_base.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_IMPL_WINRT_SSOCKET_SERVICE_BASE_IPP
  11. #define ASIO_DETAIL_IMPL_WINRT_SSOCKET_SERVICE_BASE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_WINDOWS_RUNTIME)
  17. #include <cstring>
  18. #include "asio/detail/winrt_ssocket_service_base.hpp"
  19. #include "asio/detail/winrt_async_op.hpp"
  20. #include "asio/detail/winrt_utils.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. winrt_ssocket_service_base::winrt_ssocket_service_base(
  25. execution_context& context)
  26. : scheduler_(use_service<scheduler_impl>(context)),
  27. async_manager_(use_service<winrt_async_manager>(context)),
  28. mutex_(),
  29. impl_list_(0)
  30. {
  31. }
  32. void winrt_ssocket_service_base::base_shutdown()
  33. {
  34. // Close all implementations, causing all operations to complete.
  35. asio::detail::mutex::scoped_lock lock(mutex_);
  36. base_implementation_type* impl = impl_list_;
  37. while (impl)
  38. {
  39. asio::error_code ignored_ec;
  40. close(*impl, ignored_ec);
  41. impl = impl->next_;
  42. }
  43. }
  44. void winrt_ssocket_service_base::construct(
  45. winrt_ssocket_service_base::base_implementation_type& impl)
  46. {
  47. // Insert implementation into linked list of all implementations.
  48. asio::detail::mutex::scoped_lock lock(mutex_);
  49. impl.next_ = impl_list_;
  50. impl.prev_ = 0;
  51. if (impl_list_)
  52. impl_list_->prev_ = &impl;
  53. impl_list_ = &impl;
  54. }
  55. void winrt_ssocket_service_base::base_move_construct(
  56. winrt_ssocket_service_base::base_implementation_type& impl,
  57. winrt_ssocket_service_base::base_implementation_type& other_impl)
  58. noexcept
  59. {
  60. impl.socket_ = other_impl.socket_;
  61. other_impl.socket_ = nullptr;
  62. // Insert implementation into linked list of all implementations.
  63. asio::detail::mutex::scoped_lock lock(mutex_);
  64. impl.next_ = impl_list_;
  65. impl.prev_ = 0;
  66. if (impl_list_)
  67. impl_list_->prev_ = &impl;
  68. impl_list_ = &impl;
  69. }
  70. void winrt_ssocket_service_base::base_move_assign(
  71. winrt_ssocket_service_base::base_implementation_type& impl,
  72. winrt_ssocket_service_base& other_service,
  73. winrt_ssocket_service_base::base_implementation_type& other_impl)
  74. {
  75. asio::error_code ignored_ec;
  76. close(impl, ignored_ec);
  77. if (this != &other_service)
  78. {
  79. // Remove implementation from linked list of all implementations.
  80. asio::detail::mutex::scoped_lock lock(mutex_);
  81. if (impl_list_ == &impl)
  82. impl_list_ = impl.next_;
  83. if (impl.prev_)
  84. impl.prev_->next_ = impl.next_;
  85. if (impl.next_)
  86. impl.next_->prev_= impl.prev_;
  87. impl.next_ = 0;
  88. impl.prev_ = 0;
  89. }
  90. impl.socket_ = other_impl.socket_;
  91. other_impl.socket_ = nullptr;
  92. if (this != &other_service)
  93. {
  94. // Insert implementation into linked list of all implementations.
  95. asio::detail::mutex::scoped_lock lock(other_service.mutex_);
  96. impl.next_ = other_service.impl_list_;
  97. impl.prev_ = 0;
  98. if (other_service.impl_list_)
  99. other_service.impl_list_->prev_ = &impl;
  100. other_service.impl_list_ = &impl;
  101. }
  102. }
  103. void winrt_ssocket_service_base::destroy(
  104. winrt_ssocket_service_base::base_implementation_type& impl)
  105. {
  106. asio::error_code ignored_ec;
  107. close(impl, ignored_ec);
  108. // Remove implementation from linked list of all implementations.
  109. asio::detail::mutex::scoped_lock lock(mutex_);
  110. if (impl_list_ == &impl)
  111. impl_list_ = impl.next_;
  112. if (impl.prev_)
  113. impl.prev_->next_ = impl.next_;
  114. if (impl.next_)
  115. impl.next_->prev_= impl.prev_;
  116. impl.next_ = 0;
  117. impl.prev_ = 0;
  118. }
  119. asio::error_code winrt_ssocket_service_base::close(
  120. winrt_ssocket_service_base::base_implementation_type& impl,
  121. asio::error_code& ec)
  122. {
  123. delete impl.socket_;
  124. impl.socket_ = nullptr;
  125. ec = asio::error_code();
  126. return ec;
  127. }
  128. winrt_ssocket_service_base::native_handle_type
  129. winrt_ssocket_service_base::release(
  130. winrt_ssocket_service_base::base_implementation_type& impl,
  131. asio::error_code& ec)
  132. {
  133. if (!is_open(impl))
  134. return nullptr;
  135. cancel(impl, ec);
  136. if (ec)
  137. return nullptr;
  138. native_handle_type tmp = impl.socket_;
  139. impl.socket_ = nullptr;
  140. return tmp;
  141. }
  142. std::size_t winrt_ssocket_service_base::do_get_endpoint(
  143. const base_implementation_type& impl, bool local,
  144. void* addr, std::size_t addr_len, asio::error_code& ec) const
  145. {
  146. if (!is_open(impl))
  147. {
  148. ec = asio::error::bad_descriptor;
  149. return addr_len;
  150. }
  151. try
  152. {
  153. std::string addr_string = winrt_utils::string(local
  154. ? impl.socket_->Information->LocalAddress->CanonicalName
  155. : impl.socket_->Information->RemoteAddress->CanonicalName);
  156. unsigned short port = winrt_utils::integer(local
  157. ? impl.socket_->Information->LocalPort
  158. : impl.socket_->Information->RemotePort);
  159. unsigned long scope = 0;
  160. switch (static_cast<const socket_addr_type*>(addr)->sa_family)
  161. {
  162. case ASIO_OS_DEF(AF_INET):
  163. if (addr_len < sizeof(sockaddr_in4_type))
  164. {
  165. ec = asio::error::invalid_argument;
  166. return addr_len;
  167. }
  168. else
  169. {
  170. socket_ops::inet_pton(ASIO_OS_DEF(AF_INET), addr_string.c_str(),
  171. &reinterpret_cast<sockaddr_in4_type*>(addr)->sin_addr, &scope, ec);
  172. reinterpret_cast<sockaddr_in4_type*>(addr)->sin_port
  173. = socket_ops::host_to_network_short(port);
  174. ec = asio::error_code();
  175. return sizeof(sockaddr_in4_type);
  176. }
  177. case ASIO_OS_DEF(AF_INET6):
  178. if (addr_len < sizeof(sockaddr_in6_type))
  179. {
  180. ec = asio::error::invalid_argument;
  181. return addr_len;
  182. }
  183. else
  184. {
  185. socket_ops::inet_pton(ASIO_OS_DEF(AF_INET6), addr_string.c_str(),
  186. &reinterpret_cast<sockaddr_in6_type*>(addr)->sin6_addr, &scope, ec);
  187. reinterpret_cast<sockaddr_in6_type*>(addr)->sin6_port
  188. = socket_ops::host_to_network_short(port);
  189. ec = asio::error_code();
  190. return sizeof(sockaddr_in6_type);
  191. }
  192. default:
  193. ec = asio::error::address_family_not_supported;
  194. return addr_len;
  195. }
  196. }
  197. catch (Platform::Exception^ e)
  198. {
  199. ec = asio::error_code(e->HResult,
  200. asio::system_category());
  201. return addr_len;
  202. }
  203. }
  204. asio::error_code winrt_ssocket_service_base::do_set_option(
  205. winrt_ssocket_service_base::base_implementation_type& impl,
  206. int level, int optname, const void* optval,
  207. std::size_t optlen, asio::error_code& ec)
  208. {
  209. if (!is_open(impl))
  210. {
  211. ec = asio::error::bad_descriptor;
  212. return ec;
  213. }
  214. try
  215. {
  216. if (level == ASIO_OS_DEF(SOL_SOCKET)
  217. && optname == ASIO_OS_DEF(SO_KEEPALIVE))
  218. {
  219. if (optlen == sizeof(int))
  220. {
  221. int value = 0;
  222. std::memcpy(&value, optval, optlen);
  223. impl.socket_->Control->KeepAlive = !!value;
  224. ec = asio::error_code();
  225. }
  226. else
  227. {
  228. ec = asio::error::invalid_argument;
  229. }
  230. }
  231. else if (level == ASIO_OS_DEF(IPPROTO_TCP)
  232. && optname == ASIO_OS_DEF(TCP_NODELAY))
  233. {
  234. if (optlen == sizeof(int))
  235. {
  236. int value = 0;
  237. std::memcpy(&value, optval, optlen);
  238. impl.socket_->Control->NoDelay = !!value;
  239. ec = asio::error_code();
  240. }
  241. else
  242. {
  243. ec = asio::error::invalid_argument;
  244. }
  245. }
  246. else
  247. {
  248. ec = asio::error::invalid_argument;
  249. }
  250. }
  251. catch (Platform::Exception^ e)
  252. {
  253. ec = asio::error_code(e->HResult,
  254. asio::system_category());
  255. }
  256. return ec;
  257. }
  258. void winrt_ssocket_service_base::do_get_option(
  259. const winrt_ssocket_service_base::base_implementation_type& impl,
  260. int level, int optname, void* optval,
  261. std::size_t* optlen, asio::error_code& ec) const
  262. {
  263. if (!is_open(impl))
  264. {
  265. ec = asio::error::bad_descriptor;
  266. return;
  267. }
  268. try
  269. {
  270. if (level == ASIO_OS_DEF(SOL_SOCKET)
  271. && optname == ASIO_OS_DEF(SO_KEEPALIVE))
  272. {
  273. if (*optlen >= sizeof(int))
  274. {
  275. int value = impl.socket_->Control->KeepAlive ? 1 : 0;
  276. std::memcpy(optval, &value, sizeof(int));
  277. *optlen = sizeof(int);
  278. ec = asio::error_code();
  279. }
  280. else
  281. {
  282. ec = asio::error::invalid_argument;
  283. }
  284. }
  285. else if (level == ASIO_OS_DEF(IPPROTO_TCP)
  286. && optname == ASIO_OS_DEF(TCP_NODELAY))
  287. {
  288. if (*optlen >= sizeof(int))
  289. {
  290. int value = impl.socket_->Control->NoDelay ? 1 : 0;
  291. std::memcpy(optval, &value, sizeof(int));
  292. *optlen = sizeof(int);
  293. ec = asio::error_code();
  294. }
  295. else
  296. {
  297. ec = asio::error::invalid_argument;
  298. }
  299. }
  300. else
  301. {
  302. ec = asio::error::invalid_argument;
  303. }
  304. }
  305. catch (Platform::Exception^ e)
  306. {
  307. ec = asio::error_code(e->HResult,
  308. asio::system_category());
  309. }
  310. }
  311. asio::error_code winrt_ssocket_service_base::do_connect(
  312. winrt_ssocket_service_base::base_implementation_type& impl,
  313. const void* addr, asio::error_code& ec)
  314. {
  315. if (!is_open(impl))
  316. {
  317. ec = asio::error::bad_descriptor;
  318. return ec;
  319. }
  320. char addr_string[max_addr_v6_str_len];
  321. unsigned short port;
  322. switch (static_cast<const socket_addr_type*>(addr)->sa_family)
  323. {
  324. case ASIO_OS_DEF(AF_INET):
  325. socket_ops::inet_ntop(ASIO_OS_DEF(AF_INET),
  326. &reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_addr,
  327. addr_string, sizeof(addr_string), 0, ec);
  328. port = socket_ops::network_to_host_short(
  329. reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_port);
  330. break;
  331. case ASIO_OS_DEF(AF_INET6):
  332. socket_ops::inet_ntop(ASIO_OS_DEF(AF_INET6),
  333. &reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_addr,
  334. addr_string, sizeof(addr_string), 0, ec);
  335. port = socket_ops::network_to_host_short(
  336. reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_port);
  337. break;
  338. default:
  339. ec = asio::error::address_family_not_supported;
  340. return ec;
  341. }
  342. if (!ec) try
  343. {
  344. async_manager_.sync(impl.socket_->ConnectAsync(
  345. ref new Windows::Networking::HostName(
  346. winrt_utils::string(addr_string)),
  347. winrt_utils::string(port)), ec);
  348. }
  349. catch (Platform::Exception^ e)
  350. {
  351. ec = asio::error_code(e->HResult,
  352. asio::system_category());
  353. }
  354. return ec;
  355. }
  356. void winrt_ssocket_service_base::start_connect_op(
  357. winrt_ssocket_service_base::base_implementation_type& impl,
  358. const void* addr, winrt_async_op<void>* op, bool is_continuation)
  359. {
  360. if (!is_open(impl))
  361. {
  362. op->ec_ = asio::error::bad_descriptor;
  363. scheduler_.post_immediate_completion(op, is_continuation);
  364. return;
  365. }
  366. char addr_string[max_addr_v6_str_len];
  367. unsigned short port = 0;
  368. switch (static_cast<const socket_addr_type*>(addr)->sa_family)
  369. {
  370. case ASIO_OS_DEF(AF_INET):
  371. socket_ops::inet_ntop(ASIO_OS_DEF(AF_INET),
  372. &reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_addr,
  373. addr_string, sizeof(addr_string), 0, op->ec_);
  374. port = socket_ops::network_to_host_short(
  375. reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_port);
  376. break;
  377. case ASIO_OS_DEF(AF_INET6):
  378. socket_ops::inet_ntop(ASIO_OS_DEF(AF_INET6),
  379. &reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_addr,
  380. addr_string, sizeof(addr_string), 0, op->ec_);
  381. port = socket_ops::network_to_host_short(
  382. reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_port);
  383. break;
  384. default:
  385. op->ec_ = asio::error::address_family_not_supported;
  386. break;
  387. }
  388. if (op->ec_)
  389. {
  390. scheduler_.post_immediate_completion(op, is_continuation);
  391. return;
  392. }
  393. try
  394. {
  395. async_manager_.async(impl.socket_->ConnectAsync(
  396. ref new Windows::Networking::HostName(
  397. winrt_utils::string(addr_string)),
  398. winrt_utils::string(port)), op);
  399. }
  400. catch (Platform::Exception^ e)
  401. {
  402. op->ec_ = asio::error_code(
  403. e->HResult, asio::system_category());
  404. scheduler_.post_immediate_completion(op, is_continuation);
  405. }
  406. }
  407. std::size_t winrt_ssocket_service_base::do_send(
  408. winrt_ssocket_service_base::base_implementation_type& impl,
  409. const asio::const_buffer& data,
  410. socket_base::message_flags flags, asio::error_code& ec)
  411. {
  412. if (flags)
  413. {
  414. ec = asio::error::operation_not_supported;
  415. return 0;
  416. }
  417. if (!is_open(impl))
  418. {
  419. ec = asio::error::bad_descriptor;
  420. return 0;
  421. }
  422. try
  423. {
  424. buffer_sequence_adapter<asio::const_buffer,
  425. asio::const_buffer> bufs(asio::buffer(data));
  426. if (bufs.all_empty())
  427. {
  428. ec = asio::error_code();
  429. return 0;
  430. }
  431. return async_manager_.sync(
  432. impl.socket_->OutputStream->WriteAsync(bufs.buffers()[0]), ec);
  433. }
  434. catch (Platform::Exception^ e)
  435. {
  436. ec = asio::error_code(e->HResult,
  437. asio::system_category());
  438. return 0;
  439. }
  440. }
  441. void winrt_ssocket_service_base::start_send_op(
  442. winrt_ssocket_service_base::base_implementation_type& impl,
  443. const asio::const_buffer& data, socket_base::message_flags flags,
  444. winrt_async_op<unsigned int>* op, bool is_continuation)
  445. {
  446. if (flags)
  447. {
  448. op->ec_ = asio::error::operation_not_supported;
  449. scheduler_.post_immediate_completion(op, is_continuation);
  450. return;
  451. }
  452. if (!is_open(impl))
  453. {
  454. op->ec_ = asio::error::bad_descriptor;
  455. scheduler_.post_immediate_completion(op, is_continuation);
  456. return;
  457. }
  458. try
  459. {
  460. buffer_sequence_adapter<asio::const_buffer,
  461. asio::const_buffer> bufs(asio::buffer(data));
  462. if (bufs.all_empty())
  463. {
  464. scheduler_.post_immediate_completion(op, is_continuation);
  465. return;
  466. }
  467. async_manager_.async(
  468. impl.socket_->OutputStream->WriteAsync(bufs.buffers()[0]), op);
  469. }
  470. catch (Platform::Exception^ e)
  471. {
  472. op->ec_ = asio::error_code(e->HResult,
  473. asio::system_category());
  474. scheduler_.post_immediate_completion(op, is_continuation);
  475. }
  476. }
  477. std::size_t winrt_ssocket_service_base::do_receive(
  478. winrt_ssocket_service_base::base_implementation_type& impl,
  479. const asio::mutable_buffer& data,
  480. socket_base::message_flags flags, asio::error_code& ec)
  481. {
  482. if (flags)
  483. {
  484. ec = asio::error::operation_not_supported;
  485. return 0;
  486. }
  487. if (!is_open(impl))
  488. {
  489. ec = asio::error::bad_descriptor;
  490. return 0;
  491. }
  492. try
  493. {
  494. buffer_sequence_adapter<asio::mutable_buffer,
  495. asio::mutable_buffer> bufs(asio::buffer(data));
  496. if (bufs.all_empty())
  497. {
  498. ec = asio::error_code();
  499. return 0;
  500. }
  501. async_manager_.sync(
  502. impl.socket_->InputStream->ReadAsync(
  503. bufs.buffers()[0], bufs.buffers()[0]->Capacity,
  504. Windows::Storage::Streams::InputStreamOptions::Partial), ec);
  505. std::size_t bytes_transferred = bufs.buffers()[0]->Length;
  506. if (bytes_transferred == 0 && !ec)
  507. {
  508. ec = asio::error::eof;
  509. }
  510. return bytes_transferred;
  511. }
  512. catch (Platform::Exception^ e)
  513. {
  514. ec = asio::error_code(e->HResult,
  515. asio::system_category());
  516. return 0;
  517. }
  518. }
  519. void winrt_ssocket_service_base::start_receive_op(
  520. winrt_ssocket_service_base::base_implementation_type& impl,
  521. const asio::mutable_buffer& data, socket_base::message_flags flags,
  522. winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
  523. bool is_continuation)
  524. {
  525. if (flags)
  526. {
  527. op->ec_ = asio::error::operation_not_supported;
  528. scheduler_.post_immediate_completion(op, is_continuation);
  529. return;
  530. }
  531. if (!is_open(impl))
  532. {
  533. op->ec_ = asio::error::bad_descriptor;
  534. scheduler_.post_immediate_completion(op, is_continuation);
  535. return;
  536. }
  537. try
  538. {
  539. buffer_sequence_adapter<asio::mutable_buffer,
  540. asio::mutable_buffer> bufs(asio::buffer(data));
  541. if (bufs.all_empty())
  542. {
  543. scheduler_.post_immediate_completion(op, is_continuation);
  544. return;
  545. }
  546. async_manager_.async(
  547. impl.socket_->InputStream->ReadAsync(
  548. bufs.buffers()[0], bufs.buffers()[0]->Capacity,
  549. Windows::Storage::Streams::InputStreamOptions::Partial), op);
  550. }
  551. catch (Platform::Exception^ e)
  552. {
  553. op->ec_ = asio::error_code(e->HResult,
  554. asio::system_category());
  555. scheduler_.post_immediate_completion(op, is_continuation);
  556. }
  557. }
  558. } // namespace detail
  559. } // namespace asio
  560. #include "asio/detail/pop_options.hpp"
  561. #endif // defined(ASIO_WINDOWS_RUNTIME)
  562. #endif // ASIO_DETAIL_IMPL_WINRT_SSOCKET_SERVICE_BASE_IPP