winrt_ssocket_service_base.ipp 17 KB

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