winrt_ssocket_service_base.ipp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. //
  2. // detail/impl/winrt_ssocket_service_base.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. BOOST_ASIO_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. if (impl.socket_)
  125. {
  126. delete impl.socket_;
  127. impl.socket_ = nullptr;
  128. }
  129. ec = boost::system::error_code();
  130. return ec;
  131. }
  132. winrt_ssocket_service_base::native_handle_type
  133. winrt_ssocket_service_base::release(
  134. winrt_ssocket_service_base::base_implementation_type& impl,
  135. boost::system::error_code& ec)
  136. {
  137. if (!is_open(impl))
  138. return nullptr;
  139. cancel(impl, ec);
  140. if (ec)
  141. return nullptr;
  142. native_handle_type tmp = impl.socket_;
  143. impl.socket_ = nullptr;
  144. return tmp;
  145. }
  146. std::size_t winrt_ssocket_service_base::do_get_endpoint(
  147. const base_implementation_type& impl, bool local,
  148. void* addr, std::size_t addr_len, boost::system::error_code& ec) const
  149. {
  150. if (!is_open(impl))
  151. {
  152. ec = boost::asio::error::bad_descriptor;
  153. return addr_len;
  154. }
  155. try
  156. {
  157. std::string addr_string = winrt_utils::string(local
  158. ? impl.socket_->Information->LocalAddress->CanonicalName
  159. : impl.socket_->Information->RemoteAddress->CanonicalName);
  160. unsigned short port = winrt_utils::integer(local
  161. ? impl.socket_->Information->LocalPort
  162. : impl.socket_->Information->RemotePort);
  163. unsigned long scope = 0;
  164. switch (reinterpret_cast<const socket_addr_type*>(addr)->sa_family)
  165. {
  166. case BOOST_ASIO_OS_DEF(AF_INET):
  167. if (addr_len < sizeof(sockaddr_in4_type))
  168. {
  169. ec = boost::asio::error::invalid_argument;
  170. return addr_len;
  171. }
  172. else
  173. {
  174. socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET), addr_string.c_str(),
  175. &reinterpret_cast<sockaddr_in4_type*>(addr)->sin_addr, &scope, ec);
  176. reinterpret_cast<sockaddr_in4_type*>(addr)->sin_port
  177. = socket_ops::host_to_network_short(port);
  178. ec = boost::system::error_code();
  179. return sizeof(sockaddr_in4_type);
  180. }
  181. case BOOST_ASIO_OS_DEF(AF_INET6):
  182. if (addr_len < sizeof(sockaddr_in6_type))
  183. {
  184. ec = boost::asio::error::invalid_argument;
  185. return addr_len;
  186. }
  187. else
  188. {
  189. socket_ops::inet_pton(BOOST_ASIO_OS_DEF(AF_INET6), addr_string.c_str(),
  190. &reinterpret_cast<sockaddr_in6_type*>(addr)->sin6_addr, &scope, ec);
  191. reinterpret_cast<sockaddr_in6_type*>(addr)->sin6_port
  192. = socket_ops::host_to_network_short(port);
  193. ec = boost::system::error_code();
  194. return sizeof(sockaddr_in6_type);
  195. }
  196. default:
  197. ec = boost::asio::error::address_family_not_supported;
  198. return addr_len;
  199. }
  200. }
  201. catch (Platform::Exception^ e)
  202. {
  203. ec = boost::system::error_code(e->HResult,
  204. boost::system::system_category());
  205. return addr_len;
  206. }
  207. }
  208. boost::system::error_code winrt_ssocket_service_base::do_set_option(
  209. winrt_ssocket_service_base::base_implementation_type& impl,
  210. int level, int optname, const void* optval,
  211. std::size_t optlen, boost::system::error_code& ec)
  212. {
  213. if (!is_open(impl))
  214. {
  215. ec = boost::asio::error::bad_descriptor;
  216. return ec;
  217. }
  218. try
  219. {
  220. if (level == BOOST_ASIO_OS_DEF(SOL_SOCKET)
  221. && optname == BOOST_ASIO_OS_DEF(SO_KEEPALIVE))
  222. {
  223. if (optlen == sizeof(int))
  224. {
  225. int value = 0;
  226. std::memcpy(&value, optval, optlen);
  227. impl.socket_->Control->KeepAlive = !!value;
  228. ec = boost::system::error_code();
  229. }
  230. else
  231. {
  232. ec = boost::asio::error::invalid_argument;
  233. }
  234. }
  235. else if (level == BOOST_ASIO_OS_DEF(IPPROTO_TCP)
  236. && optname == BOOST_ASIO_OS_DEF(TCP_NODELAY))
  237. {
  238. if (optlen == sizeof(int))
  239. {
  240. int value = 0;
  241. std::memcpy(&value, optval, optlen);
  242. impl.socket_->Control->NoDelay = !!value;
  243. ec = boost::system::error_code();
  244. }
  245. else
  246. {
  247. ec = boost::asio::error::invalid_argument;
  248. }
  249. }
  250. else
  251. {
  252. ec = boost::asio::error::invalid_argument;
  253. }
  254. }
  255. catch (Platform::Exception^ e)
  256. {
  257. ec = boost::system::error_code(e->HResult,
  258. boost::system::system_category());
  259. }
  260. return ec;
  261. }
  262. void winrt_ssocket_service_base::do_get_option(
  263. const winrt_ssocket_service_base::base_implementation_type& impl,
  264. int level, int optname, void* optval,
  265. std::size_t* optlen, boost::system::error_code& ec) const
  266. {
  267. if (!is_open(impl))
  268. {
  269. ec = boost::asio::error::bad_descriptor;
  270. return;
  271. }
  272. try
  273. {
  274. if (level == BOOST_ASIO_OS_DEF(SOL_SOCKET)
  275. && optname == BOOST_ASIO_OS_DEF(SO_KEEPALIVE))
  276. {
  277. if (*optlen >= sizeof(int))
  278. {
  279. int value = impl.socket_->Control->KeepAlive ? 1 : 0;
  280. std::memcpy(optval, &value, sizeof(int));
  281. *optlen = sizeof(int);
  282. ec = boost::system::error_code();
  283. }
  284. else
  285. {
  286. ec = boost::asio::error::invalid_argument;
  287. }
  288. }
  289. else if (level == BOOST_ASIO_OS_DEF(IPPROTO_TCP)
  290. && optname == BOOST_ASIO_OS_DEF(TCP_NODELAY))
  291. {
  292. if (*optlen >= sizeof(int))
  293. {
  294. int value = impl.socket_->Control->NoDelay ? 1 : 0;
  295. std::memcpy(optval, &value, sizeof(int));
  296. *optlen = sizeof(int);
  297. ec = boost::system::error_code();
  298. }
  299. else
  300. {
  301. ec = boost::asio::error::invalid_argument;
  302. }
  303. }
  304. else
  305. {
  306. ec = boost::asio::error::invalid_argument;
  307. }
  308. }
  309. catch (Platform::Exception^ e)
  310. {
  311. ec = boost::system::error_code(e->HResult,
  312. boost::system::system_category());
  313. }
  314. }
  315. boost::system::error_code winrt_ssocket_service_base::do_connect(
  316. winrt_ssocket_service_base::base_implementation_type& impl,
  317. const void* addr, boost::system::error_code& ec)
  318. {
  319. if (!is_open(impl))
  320. {
  321. ec = boost::asio::error::bad_descriptor;
  322. return ec;
  323. }
  324. char addr_string[max_addr_v6_str_len];
  325. unsigned short port;
  326. switch (reinterpret_cast<const socket_addr_type*>(addr)->sa_family)
  327. {
  328. case BOOST_ASIO_OS_DEF(AF_INET):
  329. socket_ops::inet_ntop(BOOST_ASIO_OS_DEF(AF_INET),
  330. &reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_addr,
  331. addr_string, sizeof(addr_string), 0, ec);
  332. port = socket_ops::network_to_host_short(
  333. reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_port);
  334. break;
  335. case BOOST_ASIO_OS_DEF(AF_INET6):
  336. socket_ops::inet_ntop(BOOST_ASIO_OS_DEF(AF_INET6),
  337. &reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_addr,
  338. addr_string, sizeof(addr_string), 0, ec);
  339. port = socket_ops::network_to_host_short(
  340. reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_port);
  341. break;
  342. default:
  343. ec = boost::asio::error::address_family_not_supported;
  344. return ec;
  345. }
  346. if (!ec) try
  347. {
  348. async_manager_.sync(impl.socket_->ConnectAsync(
  349. ref new Windows::Networking::HostName(
  350. winrt_utils::string(addr_string)),
  351. winrt_utils::string(port)), ec);
  352. }
  353. catch (Platform::Exception^ e)
  354. {
  355. ec = boost::system::error_code(e->HResult,
  356. boost::system::system_category());
  357. }
  358. return ec;
  359. }
  360. void winrt_ssocket_service_base::start_connect_op(
  361. winrt_ssocket_service_base::base_implementation_type& impl,
  362. const void* addr, winrt_async_op<void>* op, bool is_continuation)
  363. {
  364. if (!is_open(impl))
  365. {
  366. op->ec_ = boost::asio::error::bad_descriptor;
  367. scheduler_.post_immediate_completion(op, is_continuation);
  368. return;
  369. }
  370. char addr_string[max_addr_v6_str_len];
  371. unsigned short port = 0;
  372. switch (reinterpret_cast<const socket_addr_type*>(addr)->sa_family)
  373. {
  374. case BOOST_ASIO_OS_DEF(AF_INET):
  375. socket_ops::inet_ntop(BOOST_ASIO_OS_DEF(AF_INET),
  376. &reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_addr,
  377. addr_string, sizeof(addr_string), 0, op->ec_);
  378. port = socket_ops::network_to_host_short(
  379. reinterpret_cast<const sockaddr_in4_type*>(addr)->sin_port);
  380. break;
  381. case BOOST_ASIO_OS_DEF(AF_INET6):
  382. socket_ops::inet_ntop(BOOST_ASIO_OS_DEF(AF_INET6),
  383. &reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_addr,
  384. addr_string, sizeof(addr_string), 0, op->ec_);
  385. port = socket_ops::network_to_host_short(
  386. reinterpret_cast<const sockaddr_in6_type*>(addr)->sin6_port);
  387. break;
  388. default:
  389. op->ec_ = boost::asio::error::address_family_not_supported;
  390. break;
  391. }
  392. if (op->ec_)
  393. {
  394. scheduler_.post_immediate_completion(op, is_continuation);
  395. return;
  396. }
  397. try
  398. {
  399. async_manager_.async(impl.socket_->ConnectAsync(
  400. ref new Windows::Networking::HostName(
  401. winrt_utils::string(addr_string)),
  402. winrt_utils::string(port)), op);
  403. }
  404. catch (Platform::Exception^ e)
  405. {
  406. op->ec_ = boost::system::error_code(
  407. e->HResult, boost::system::system_category());
  408. scheduler_.post_immediate_completion(op, is_continuation);
  409. }
  410. }
  411. std::size_t winrt_ssocket_service_base::do_send(
  412. winrt_ssocket_service_base::base_implementation_type& impl,
  413. const boost::asio::const_buffer& data,
  414. socket_base::message_flags flags, boost::system::error_code& ec)
  415. {
  416. if (flags)
  417. {
  418. ec = boost::asio::error::operation_not_supported;
  419. return 0;
  420. }
  421. if (!is_open(impl))
  422. {
  423. ec = boost::asio::error::bad_descriptor;
  424. return 0;
  425. }
  426. try
  427. {
  428. buffer_sequence_adapter<boost::asio::const_buffer,
  429. boost::asio::const_buffer> bufs(boost::asio::buffer(data));
  430. if (bufs.all_empty())
  431. {
  432. ec = boost::system::error_code();
  433. return 0;
  434. }
  435. return async_manager_.sync(
  436. impl.socket_->OutputStream->WriteAsync(bufs.buffers()[0]), ec);
  437. }
  438. catch (Platform::Exception^ e)
  439. {
  440. ec = boost::system::error_code(e->HResult,
  441. boost::system::system_category());
  442. return 0;
  443. }
  444. }
  445. void winrt_ssocket_service_base::start_send_op(
  446. winrt_ssocket_service_base::base_implementation_type& impl,
  447. const boost::asio::const_buffer& data, socket_base::message_flags flags,
  448. winrt_async_op<unsigned int>* op, bool is_continuation)
  449. {
  450. if (flags)
  451. {
  452. op->ec_ = boost::asio::error::operation_not_supported;
  453. scheduler_.post_immediate_completion(op, is_continuation);
  454. return;
  455. }
  456. if (!is_open(impl))
  457. {
  458. op->ec_ = boost::asio::error::bad_descriptor;
  459. scheduler_.post_immediate_completion(op, is_continuation);
  460. return;
  461. }
  462. try
  463. {
  464. buffer_sequence_adapter<boost::asio::const_buffer,
  465. boost::asio::const_buffer> bufs(boost::asio::buffer(data));
  466. if (bufs.all_empty())
  467. {
  468. scheduler_.post_immediate_completion(op, is_continuation);
  469. return;
  470. }
  471. async_manager_.async(
  472. impl.socket_->OutputStream->WriteAsync(bufs.buffers()[0]), op);
  473. }
  474. catch (Platform::Exception^ e)
  475. {
  476. op->ec_ = boost::system::error_code(e->HResult,
  477. boost::system::system_category());
  478. scheduler_.post_immediate_completion(op, is_continuation);
  479. }
  480. }
  481. std::size_t winrt_ssocket_service_base::do_receive(
  482. winrt_ssocket_service_base::base_implementation_type& impl,
  483. const boost::asio::mutable_buffer& data,
  484. socket_base::message_flags flags, boost::system::error_code& ec)
  485. {
  486. if (flags)
  487. {
  488. ec = boost::asio::error::operation_not_supported;
  489. return 0;
  490. }
  491. if (!is_open(impl))
  492. {
  493. ec = boost::asio::error::bad_descriptor;
  494. return 0;
  495. }
  496. try
  497. {
  498. buffer_sequence_adapter<boost::asio::mutable_buffer,
  499. boost::asio::mutable_buffer> bufs(boost::asio::buffer(data));
  500. if (bufs.all_empty())
  501. {
  502. ec = boost::system::error_code();
  503. return 0;
  504. }
  505. async_manager_.sync(
  506. impl.socket_->InputStream->ReadAsync(
  507. bufs.buffers()[0], bufs.buffers()[0]->Capacity,
  508. Windows::Storage::Streams::InputStreamOptions::Partial), ec);
  509. std::size_t bytes_transferred = bufs.buffers()[0]->Length;
  510. if (bytes_transferred == 0 && !ec)
  511. {
  512. ec = boost::asio::error::eof;
  513. }
  514. return bytes_transferred;
  515. }
  516. catch (Platform::Exception^ e)
  517. {
  518. ec = boost::system::error_code(e->HResult,
  519. boost::system::system_category());
  520. return 0;
  521. }
  522. }
  523. void winrt_ssocket_service_base::start_receive_op(
  524. winrt_ssocket_service_base::base_implementation_type& impl,
  525. const boost::asio::mutable_buffer& data, socket_base::message_flags flags,
  526. winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
  527. bool is_continuation)
  528. {
  529. if (flags)
  530. {
  531. op->ec_ = boost::asio::error::operation_not_supported;
  532. scheduler_.post_immediate_completion(op, is_continuation);
  533. return;
  534. }
  535. if (!is_open(impl))
  536. {
  537. op->ec_ = boost::asio::error::bad_descriptor;
  538. scheduler_.post_immediate_completion(op, is_continuation);
  539. return;
  540. }
  541. try
  542. {
  543. buffer_sequence_adapter<boost::asio::mutable_buffer,
  544. boost::asio::mutable_buffer> bufs(boost::asio::buffer(data));
  545. if (bufs.all_empty())
  546. {
  547. scheduler_.post_immediate_completion(op, is_continuation);
  548. return;
  549. }
  550. async_manager_.async(
  551. impl.socket_->InputStream->ReadAsync(
  552. bufs.buffers()[0], bufs.buffers()[0]->Capacity,
  553. Windows::Storage::Streams::InputStreamOptions::Partial), op);
  554. }
  555. catch (Platform::Exception^ e)
  556. {
  557. op->ec_ = boost::system::error_code(e->HResult,
  558. boost::system::system_category());
  559. scheduler_.post_immediate_completion(op, is_continuation);
  560. }
  561. }
  562. } // namespace detail
  563. } // namespace asio
  564. } // namespace boost
  565. #include <boost/asio/detail/pop_options.hpp>
  566. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  567. #endif // BOOST_ASIO_DETAIL_IMPL_WINRT_SSOCKET_SERVICE_BASE_IPP