buffer_sequence_adapter.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. //
  2. // detail/buffer_sequence_adapter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_BUFFER_SEQUENCE_ADAPTER_HPP
  11. #define BOOST_ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP
  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. #include <boost/asio/buffer.hpp>
  17. #include <boost/asio/detail/array_fwd.hpp>
  18. #include <boost/asio/detail/socket_types.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. class buffer_sequence_adapter_base
  24. {
  25. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  26. public:
  27. // The maximum number of buffers to support in a single operation.
  28. enum { max_buffers = 1 };
  29. protected:
  30. typedef Windows::Storage::Streams::IBuffer^ native_buffer_type;
  31. BOOST_ASIO_DECL static void init_native_buffer(
  32. native_buffer_type& buf,
  33. const boost::asio::mutable_buffer& buffer);
  34. BOOST_ASIO_DECL static void init_native_buffer(
  35. native_buffer_type& buf,
  36. const boost::asio::const_buffer& buffer);
  37. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  38. public:
  39. // The maximum number of buffers to support in a single operation.
  40. enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len };
  41. protected:
  42. typedef WSABUF native_buffer_type;
  43. static void init_native_buffer(WSABUF& buf,
  44. const boost::asio::mutable_buffer& buffer)
  45. {
  46. buf.buf = static_cast<char*>(buffer.data());
  47. buf.len = static_cast<ULONG>(buffer.size());
  48. }
  49. static void init_native_buffer(WSABUF& buf,
  50. const boost::asio::const_buffer& buffer)
  51. {
  52. buf.buf = const_cast<char*>(static_cast<const char*>(buffer.data()));
  53. buf.len = static_cast<ULONG>(buffer.size());
  54. }
  55. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  56. public:
  57. // The maximum number of buffers to support in a single operation.
  58. enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len };
  59. protected:
  60. typedef iovec native_buffer_type;
  61. static void init_iov_base(void*& base, void* addr)
  62. {
  63. base = addr;
  64. }
  65. template <typename T>
  66. static void init_iov_base(T& base, void* addr)
  67. {
  68. base = static_cast<T>(addr);
  69. }
  70. static void init_native_buffer(iovec& iov,
  71. const boost::asio::mutable_buffer& buffer)
  72. {
  73. init_iov_base(iov.iov_base, buffer.data());
  74. iov.iov_len = buffer.size();
  75. }
  76. static void init_native_buffer(iovec& iov,
  77. const boost::asio::const_buffer& buffer)
  78. {
  79. init_iov_base(iov.iov_base, const_cast<void*>(buffer.data()));
  80. iov.iov_len = buffer.size();
  81. }
  82. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  83. };
  84. // Helper class to translate buffers into the native buffer representation.
  85. template <typename Buffer, typename Buffers>
  86. class buffer_sequence_adapter
  87. : buffer_sequence_adapter_base
  88. {
  89. public:
  90. enum { is_single_buffer = false };
  91. explicit buffer_sequence_adapter(const Buffers& buffer_sequence)
  92. : count_(0), total_buffer_size_(0)
  93. {
  94. buffer_sequence_adapter::init(
  95. boost::asio::buffer_sequence_begin(buffer_sequence),
  96. boost::asio::buffer_sequence_end(buffer_sequence));
  97. }
  98. native_buffer_type* buffers()
  99. {
  100. return buffers_;
  101. }
  102. std::size_t count() const
  103. {
  104. return count_;
  105. }
  106. std::size_t total_size() const
  107. {
  108. return total_buffer_size_;
  109. }
  110. bool all_empty() const
  111. {
  112. return total_buffer_size_ == 0;
  113. }
  114. static bool all_empty(const Buffers& buffer_sequence)
  115. {
  116. return buffer_sequence_adapter::all_empty(
  117. boost::asio::buffer_sequence_begin(buffer_sequence),
  118. boost::asio::buffer_sequence_end(buffer_sequence));
  119. }
  120. static void validate(const Buffers& buffer_sequence)
  121. {
  122. buffer_sequence_adapter::validate(
  123. boost::asio::buffer_sequence_begin(buffer_sequence),
  124. boost::asio::buffer_sequence_end(buffer_sequence));
  125. }
  126. static Buffer first(const Buffers& buffer_sequence)
  127. {
  128. return buffer_sequence_adapter::first(
  129. boost::asio::buffer_sequence_begin(buffer_sequence),
  130. boost::asio::buffer_sequence_end(buffer_sequence));
  131. }
  132. enum { linearisation_storage_size = 8192 };
  133. static Buffer linearise(const Buffers& buffer_sequence,
  134. const boost::asio::mutable_buffer& storage)
  135. {
  136. return buffer_sequence_adapter::linearise(
  137. boost::asio::buffer_sequence_begin(buffer_sequence),
  138. boost::asio::buffer_sequence_end(buffer_sequence), storage);
  139. }
  140. private:
  141. template <typename Iterator>
  142. void init(Iterator begin, Iterator end)
  143. {
  144. Iterator iter = begin;
  145. for (; iter != end && count_ < max_buffers; ++iter, ++count_)
  146. {
  147. Buffer buffer(*iter);
  148. init_native_buffer(buffers_[count_], buffer);
  149. total_buffer_size_ += buffer.size();
  150. }
  151. }
  152. template <typename Iterator>
  153. static bool all_empty(Iterator begin, Iterator end)
  154. {
  155. Iterator iter = begin;
  156. std::size_t i = 0;
  157. for (; iter != end && i < max_buffers; ++iter, ++i)
  158. if (Buffer(*iter).size() > 0)
  159. return false;
  160. return true;
  161. }
  162. template <typename Iterator>
  163. static void validate(Iterator begin, Iterator end)
  164. {
  165. Iterator iter = begin;
  166. for (; iter != end; ++iter)
  167. {
  168. Buffer buffer(*iter);
  169. buffer.data();
  170. }
  171. }
  172. template <typename Iterator>
  173. static Buffer first(Iterator begin, Iterator end)
  174. {
  175. Iterator iter = begin;
  176. for (; iter != end; ++iter)
  177. {
  178. Buffer buffer(*iter);
  179. if (buffer.size() != 0)
  180. return buffer;
  181. }
  182. return Buffer();
  183. }
  184. template <typename Iterator>
  185. static Buffer linearise(Iterator begin, Iterator end,
  186. const boost::asio::mutable_buffer& storage)
  187. {
  188. boost::asio::mutable_buffer unused_storage = storage;
  189. Iterator iter = begin;
  190. while (iter != end && unused_storage.size() != 0)
  191. {
  192. Buffer buffer(*iter);
  193. ++iter;
  194. if (buffer.size() == 0)
  195. continue;
  196. if (unused_storage.size() == storage.size())
  197. {
  198. if (iter == end)
  199. return buffer;
  200. if (buffer.size() >= unused_storage.size())
  201. return buffer;
  202. }
  203. unused_storage += boost::asio::buffer_copy(unused_storage, buffer);
  204. }
  205. return Buffer(storage.data(), storage.size() - unused_storage.size());
  206. }
  207. native_buffer_type buffers_[max_buffers];
  208. std::size_t count_;
  209. std::size_t total_buffer_size_;
  210. };
  211. template <typename Buffer>
  212. class buffer_sequence_adapter<Buffer, boost::asio::mutable_buffer>
  213. : buffer_sequence_adapter_base
  214. {
  215. public:
  216. enum { is_single_buffer = true };
  217. explicit buffer_sequence_adapter(
  218. const boost::asio::mutable_buffer& buffer_sequence)
  219. {
  220. init_native_buffer(buffer_, Buffer(buffer_sequence));
  221. total_buffer_size_ = buffer_sequence.size();
  222. }
  223. native_buffer_type* buffers()
  224. {
  225. return &buffer_;
  226. }
  227. std::size_t count() const
  228. {
  229. return 1;
  230. }
  231. std::size_t total_size() const
  232. {
  233. return total_buffer_size_;
  234. }
  235. bool all_empty() const
  236. {
  237. return total_buffer_size_ == 0;
  238. }
  239. static bool all_empty(const boost::asio::mutable_buffer& buffer_sequence)
  240. {
  241. return buffer_sequence.size() == 0;
  242. }
  243. static void validate(const boost::asio::mutable_buffer& buffer_sequence)
  244. {
  245. buffer_sequence.data();
  246. }
  247. static Buffer first(const boost::asio::mutable_buffer& buffer_sequence)
  248. {
  249. return Buffer(buffer_sequence);
  250. }
  251. enum { linearisation_storage_size = 1 };
  252. static Buffer linearise(const boost::asio::mutable_buffer& buffer_sequence,
  253. const Buffer&)
  254. {
  255. return Buffer(buffer_sequence);
  256. }
  257. private:
  258. native_buffer_type buffer_;
  259. std::size_t total_buffer_size_;
  260. };
  261. template <typename Buffer>
  262. class buffer_sequence_adapter<Buffer, boost::asio::const_buffer>
  263. : buffer_sequence_adapter_base
  264. {
  265. public:
  266. enum { is_single_buffer = true };
  267. explicit buffer_sequence_adapter(
  268. const boost::asio::const_buffer& buffer_sequence)
  269. {
  270. init_native_buffer(buffer_, Buffer(buffer_sequence));
  271. total_buffer_size_ = buffer_sequence.size();
  272. }
  273. native_buffer_type* buffers()
  274. {
  275. return &buffer_;
  276. }
  277. std::size_t count() const
  278. {
  279. return 1;
  280. }
  281. std::size_t total_size() const
  282. {
  283. return total_buffer_size_;
  284. }
  285. bool all_empty() const
  286. {
  287. return total_buffer_size_ == 0;
  288. }
  289. static bool all_empty(const boost::asio::const_buffer& buffer_sequence)
  290. {
  291. return buffer_sequence.size() == 0;
  292. }
  293. static void validate(const boost::asio::const_buffer& buffer_sequence)
  294. {
  295. buffer_sequence.data();
  296. }
  297. static Buffer first(const boost::asio::const_buffer& buffer_sequence)
  298. {
  299. return Buffer(buffer_sequence);
  300. }
  301. enum { linearisation_storage_size = 1 };
  302. static Buffer linearise(const boost::asio::const_buffer& buffer_sequence,
  303. const Buffer&)
  304. {
  305. return Buffer(buffer_sequence);
  306. }
  307. private:
  308. native_buffer_type buffer_;
  309. std::size_t total_buffer_size_;
  310. };
  311. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  312. template <typename Buffer>
  313. class buffer_sequence_adapter<Buffer, boost::asio::mutable_buffers_1>
  314. : buffer_sequence_adapter_base
  315. {
  316. public:
  317. enum { is_single_buffer = true };
  318. explicit buffer_sequence_adapter(
  319. const boost::asio::mutable_buffers_1& buffer_sequence)
  320. {
  321. init_native_buffer(buffer_, Buffer(buffer_sequence));
  322. total_buffer_size_ = buffer_sequence.size();
  323. }
  324. native_buffer_type* buffers()
  325. {
  326. return &buffer_;
  327. }
  328. std::size_t count() const
  329. {
  330. return 1;
  331. }
  332. std::size_t total_size() const
  333. {
  334. return total_buffer_size_;
  335. }
  336. bool all_empty() const
  337. {
  338. return total_buffer_size_ == 0;
  339. }
  340. static bool all_empty(const boost::asio::mutable_buffers_1& buffer_sequence)
  341. {
  342. return buffer_sequence.size() == 0;
  343. }
  344. static void validate(const boost::asio::mutable_buffers_1& buffer_sequence)
  345. {
  346. buffer_sequence.data();
  347. }
  348. static Buffer first(const boost::asio::mutable_buffers_1& buffer_sequence)
  349. {
  350. return Buffer(buffer_sequence);
  351. }
  352. enum { linearisation_storage_size = 1 };
  353. static Buffer linearise(const boost::asio::mutable_buffers_1& buffer_sequence,
  354. const Buffer&)
  355. {
  356. return Buffer(buffer_sequence);
  357. }
  358. private:
  359. native_buffer_type buffer_;
  360. std::size_t total_buffer_size_;
  361. };
  362. template <typename Buffer>
  363. class buffer_sequence_adapter<Buffer, boost::asio::const_buffers_1>
  364. : buffer_sequence_adapter_base
  365. {
  366. public:
  367. enum { is_single_buffer = true };
  368. explicit buffer_sequence_adapter(
  369. const boost::asio::const_buffers_1& buffer_sequence)
  370. {
  371. init_native_buffer(buffer_, Buffer(buffer_sequence));
  372. total_buffer_size_ = buffer_sequence.size();
  373. }
  374. native_buffer_type* buffers()
  375. {
  376. return &buffer_;
  377. }
  378. std::size_t count() const
  379. {
  380. return 1;
  381. }
  382. std::size_t total_size() const
  383. {
  384. return total_buffer_size_;
  385. }
  386. bool all_empty() const
  387. {
  388. return total_buffer_size_ == 0;
  389. }
  390. static bool all_empty(const boost::asio::const_buffers_1& buffer_sequence)
  391. {
  392. return buffer_sequence.size() == 0;
  393. }
  394. static void validate(const boost::asio::const_buffers_1& buffer_sequence)
  395. {
  396. buffer_sequence.data();
  397. }
  398. static Buffer first(const boost::asio::const_buffers_1& buffer_sequence)
  399. {
  400. return Buffer(buffer_sequence);
  401. }
  402. enum { linearisation_storage_size = 1 };
  403. static Buffer linearise(const boost::asio::const_buffers_1& buffer_sequence,
  404. const Buffer&)
  405. {
  406. return Buffer(buffer_sequence);
  407. }
  408. private:
  409. native_buffer_type buffer_;
  410. std::size_t total_buffer_size_;
  411. };
  412. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  413. template <typename Buffer, typename Elem>
  414. class buffer_sequence_adapter<Buffer, boost::array<Elem, 2> >
  415. : buffer_sequence_adapter_base
  416. {
  417. public:
  418. enum { is_single_buffer = false };
  419. explicit buffer_sequence_adapter(
  420. const boost::array<Elem, 2>& buffer_sequence)
  421. {
  422. init_native_buffer(buffers_[0], Buffer(buffer_sequence[0]));
  423. init_native_buffer(buffers_[1], Buffer(buffer_sequence[1]));
  424. total_buffer_size_ = buffer_sequence[0].size() + buffer_sequence[1].size();
  425. }
  426. native_buffer_type* buffers()
  427. {
  428. return buffers_;
  429. }
  430. std::size_t count() const
  431. {
  432. return 2;
  433. }
  434. std::size_t total_size() const
  435. {
  436. return total_buffer_size_;
  437. }
  438. bool all_empty() const
  439. {
  440. return total_buffer_size_ == 0;
  441. }
  442. static bool all_empty(const boost::array<Elem, 2>& buffer_sequence)
  443. {
  444. return buffer_sequence[0].size() == 0 && buffer_sequence[1].size() == 0;
  445. }
  446. static void validate(const boost::array<Elem, 2>& buffer_sequence)
  447. {
  448. buffer_sequence[0].data();
  449. buffer_sequence[1].data();
  450. }
  451. static Buffer first(const boost::array<Elem, 2>& buffer_sequence)
  452. {
  453. return Buffer(buffer_sequence[0].size() != 0
  454. ? buffer_sequence[0] : buffer_sequence[1]);
  455. }
  456. enum { linearisation_storage_size = 8192 };
  457. static Buffer linearise(const boost::array<Elem, 2>& buffer_sequence,
  458. const boost::asio::mutable_buffer& storage)
  459. {
  460. if (buffer_sequence[0].size() == 0)
  461. return Buffer(buffer_sequence[1]);
  462. if (buffer_sequence[1].size() == 0)
  463. return Buffer(buffer_sequence[0]);
  464. return Buffer(storage.data(),
  465. boost::asio::buffer_copy(storage, buffer_sequence));
  466. }
  467. private:
  468. native_buffer_type buffers_[2];
  469. std::size_t total_buffer_size_;
  470. };
  471. #if defined(BOOST_ASIO_HAS_STD_ARRAY)
  472. template <typename Buffer, typename Elem>
  473. class buffer_sequence_adapter<Buffer, std::array<Elem, 2> >
  474. : buffer_sequence_adapter_base
  475. {
  476. public:
  477. enum { is_single_buffer = false };
  478. explicit buffer_sequence_adapter(
  479. const std::array<Elem, 2>& buffer_sequence)
  480. {
  481. init_native_buffer(buffers_[0], Buffer(buffer_sequence[0]));
  482. init_native_buffer(buffers_[1], Buffer(buffer_sequence[1]));
  483. total_buffer_size_ = buffer_sequence[0].size() + buffer_sequence[1].size();
  484. }
  485. native_buffer_type* buffers()
  486. {
  487. return buffers_;
  488. }
  489. std::size_t count() const
  490. {
  491. return 2;
  492. }
  493. std::size_t total_size() const
  494. {
  495. return total_buffer_size_;
  496. }
  497. bool all_empty() const
  498. {
  499. return total_buffer_size_ == 0;
  500. }
  501. static bool all_empty(const std::array<Elem, 2>& buffer_sequence)
  502. {
  503. return buffer_sequence[0].size() == 0 && buffer_sequence[1].size() == 0;
  504. }
  505. static void validate(const std::array<Elem, 2>& buffer_sequence)
  506. {
  507. buffer_sequence[0].data();
  508. buffer_sequence[1].data();
  509. }
  510. static Buffer first(const std::array<Elem, 2>& buffer_sequence)
  511. {
  512. return Buffer(buffer_sequence[0].size() != 0
  513. ? buffer_sequence[0] : buffer_sequence[1]);
  514. }
  515. enum { linearisation_storage_size = 8192 };
  516. static Buffer linearise(const std::array<Elem, 2>& buffer_sequence,
  517. const boost::asio::mutable_buffer& storage)
  518. {
  519. if (buffer_sequence[0].size() == 0)
  520. return Buffer(buffer_sequence[1]);
  521. if (buffer_sequence[1].size() == 0)
  522. return Buffer(buffer_sequence[0]);
  523. return Buffer(storage.data(),
  524. boost::asio::buffer_copy(storage, buffer_sequence));
  525. }
  526. private:
  527. native_buffer_type buffers_[2];
  528. std::size_t total_buffer_size_;
  529. };
  530. #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
  531. } // namespace detail
  532. } // namespace asio
  533. } // namespace boost
  534. #include <boost/asio/detail/pop_options.hpp>
  535. #if defined(BOOST_ASIO_HEADER_ONLY)
  536. # include <boost/asio/detail/impl/buffer_sequence_adapter.ipp>
  537. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  538. #endif // BOOST_ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP