match_condition.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.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 __ASIO2_MATCH_CONDITION_HPP__
  11. #define __ASIO2_MATCH_CONDITION_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <cstdint>
  16. #include <string>
  17. #include <string_view>
  18. #include <utility>
  19. #include <asio2/external/asio.hpp>
  20. #include <asio2/base/detail/util.hpp>
  21. namespace asio2::detail
  22. {
  23. namespace
  24. {
  25. using iterator = asio::buffers_iterator<asio::streambuf::const_buffers_type>;
  26. using diff_type = typename iterator::difference_type;
  27. std::pair<iterator, bool> dgram_match_role(iterator begin, iterator end) noexcept
  28. {
  29. for (iterator p = begin; p < end;)
  30. {
  31. // If 0~253, current byte are the payload length.
  32. if (std::uint8_t(*p) < std::uint8_t(254))
  33. {
  34. std::uint8_t payload_size = static_cast<std::uint8_t>(*p);
  35. ++p;
  36. if (end - p < static_cast<diff_type>(payload_size))
  37. break;
  38. return std::pair(p + static_cast<diff_type>(payload_size), true);
  39. }
  40. // If 254, the following 2 bytes interpreted as a 16-bit unsigned integer
  41. // are the payload length.
  42. if (std::uint8_t(*p) == std::uint8_t(254))
  43. {
  44. ++p;
  45. if (end - p < 2)
  46. break;
  47. std::uint16_t payload_size = *(reinterpret_cast<const std::uint16_t*>(p.operator->()));
  48. // use little endian
  49. if (!is_little_endian())
  50. {
  51. swap_bytes<sizeof(std::uint16_t)>(reinterpret_cast<std::uint8_t*>(
  52. std::addressof(payload_size)));
  53. }
  54. // illegal data
  55. if (payload_size < static_cast<std::uint16_t>(254))
  56. return std::pair(begin, true);
  57. p += 2;
  58. if (end - p < static_cast<diff_type>(payload_size))
  59. break;
  60. return std::pair(p + static_cast<diff_type>(payload_size), true);
  61. }
  62. // If 255, the following 8 bytes interpreted as a 64-bit unsigned integer
  63. // (the most significant bit MUST be 0) are the payload length.
  64. if (std::uint8_t(*p) == 255)
  65. {
  66. ++p;
  67. if (end - p < 8)
  68. break;
  69. // the most significant bit MUST be 0
  70. std::int64_t payload_size = *(reinterpret_cast<const std::int64_t*>(p.operator->()));
  71. // use little endian
  72. if (!is_little_endian())
  73. {
  74. swap_bytes<sizeof(std::int64_t)>(reinterpret_cast<std::uint8_t*>(
  75. std::addressof(payload_size)));
  76. }
  77. // illegal data
  78. if (payload_size <= static_cast<std::int64_t>((std::numeric_limits<std::uint16_t>::max)()))
  79. return std::pair(begin, true);
  80. p += 8;
  81. if (end - p < static_cast<diff_type>(payload_size))
  82. break;
  83. return std::pair(p + static_cast<diff_type>(payload_size), true);
  84. }
  85. ASIO2_ASSERT(false);
  86. }
  87. return std::pair(begin, false);
  88. }
  89. /*
  90. std::pair<iterator, bool> json_format_match_role_impl(
  91. iterator begin, iterator end,
  92. typename iterator::value_type head, typename iterator::value_type tail) noexcept
  93. {
  94. for (iterator p = begin; p < end; ++p)
  95. {
  96. if (std::isspace(*p))
  97. continue;
  98. // illegal data
  99. if (*p != head)
  100. return std::pair(begin, true);
  101. for (std::int32_t depth = 0; p < end; ++p)
  102. {
  103. if (*p == head)
  104. {
  105. ++depth;
  106. }
  107. else if (*p == tail)
  108. {
  109. --depth;
  110. if (depth == 0)
  111. return std::pair(p + 1, true);
  112. }
  113. }
  114. }
  115. return std::pair(begin, false);
  116. }
  117. std::pair<iterator, bool> json_object_match_role(iterator begin, iterator end) noexcept
  118. {
  119. return json_format_match_role_impl(begin, end, '{', '}');
  120. }
  121. std::pair<iterator, bool> json_array_match_role(iterator begin, iterator end) noexcept
  122. {
  123. return json_format_match_role_impl(begin, end, '[', ']');
  124. }
  125. std::pair<iterator, bool> json_match_role(iterator begin, iterator end) noexcept
  126. {
  127. for (iterator p = begin; p < end; ++p)
  128. {
  129. if (std::isspace(*p))
  130. continue;
  131. // object
  132. if (*p == '{')
  133. return json_object_match_role(begin, end);
  134. // array
  135. if (*p == '[')
  136. return json_array_match_role(begin, end);
  137. // string
  138. if (*p == '\"')
  139. {
  140. for (++p; p < end; ++p)
  141. {
  142. if (*p == '\"')
  143. return std::pair(p + 1, true);
  144. }
  145. break;
  146. }
  147. // null
  148. if (*p == 'n')
  149. {
  150. if (end - p >= static_cast<diff_type>(4))
  151. {
  152. if (p[1] == 'u' && p[2] == 'l' && p[3] == 'l')
  153. return std::pair(p + 4, true);
  154. else
  155. return std::pair(begin, true);
  156. }
  157. break;
  158. }
  159. // integer
  160. // we can't know when the numeric string will end, so we can only return the entire string.
  161. if ((*p >= '0') && (*p <= '9'))
  162. return std::pair(end, true);
  163. // illegal data
  164. return std::pair(begin, true);
  165. }
  166. return std::pair(begin, false);
  167. }
  168. */
  169. }
  170. }
  171. namespace asio2::detail
  172. {
  173. //struct use_sync_t {};
  174. struct use_kcp_t {};
  175. struct use_dgram_t {};
  176. struct hook_buffer_t {};
  177. }
  178. namespace asio2::detail
  179. {
  180. /**
  181. * use condition_t to avoid having the same name as make_error_condition(condition c)
  182. */
  183. template<class T>
  184. class condition_t
  185. {
  186. public:
  187. using type = T;
  188. // must use explicit, Otherwise, there will be an error when there are the following
  189. // statements: condition_t<char> c1; auto c2 = c1;
  190. template<class C, std::enable_if_t<!std::is_base_of_v<condition_t, detail::remove_cvref_t<C>>, int> = 0>
  191. explicit condition_t(C c) noexcept : c_(std::move(c)) {}
  192. ~condition_t() noexcept = default;
  193. condition_t(condition_t&&) noexcept = default;
  194. condition_t(condition_t const&) noexcept = delete;
  195. condition_t& operator=(condition_t&&) noexcept = default;
  196. condition_t& operator=(condition_t const&) noexcept = delete;
  197. inline condition_t clone() noexcept { return condition_t{ this->c_ }; }
  198. inline T& operator()() noexcept { return this->c_; }
  199. inline T& lowest () noexcept { return this->c_; }
  200. protected:
  201. T c_;
  202. };
  203. // C++17 class template argument deduction guides
  204. template<class T>
  205. condition_t(T)->condition_t<std::remove_reference_t<T>>;
  206. template<>
  207. class condition_t<void>
  208. {
  209. public:
  210. using type = void;
  211. condition_t() noexcept = default;
  212. ~condition_t() noexcept = default;
  213. condition_t(condition_t&&) noexcept = default;
  214. condition_t(condition_t const&) noexcept = delete;
  215. condition_t& operator=(condition_t&&) noexcept = default;
  216. condition_t& operator=(condition_t const&) noexcept = delete;
  217. inline condition_t clone() noexcept { return condition_t{}; }
  218. inline void operator()() noexcept {}
  219. inline void lowest () noexcept {}
  220. };
  221. template<>
  222. class condition_t<use_dgram_t>
  223. {
  224. public:
  225. using type = use_dgram_t;
  226. explicit condition_t(use_dgram_t) noexcept {}
  227. ~condition_t() noexcept = default;
  228. condition_t(condition_t&&) noexcept = default;
  229. condition_t(condition_t const&) noexcept = delete;
  230. condition_t& operator=(condition_t&&) noexcept = default;
  231. condition_t& operator=(condition_t const&) noexcept = delete;
  232. inline condition_t clone() noexcept { return condition_t{ use_dgram_t{} }; }
  233. inline auto& operator()() noexcept { return dgram_match_role; }
  234. inline auto& lowest () noexcept { return dgram_match_role; }
  235. };
  236. template<>
  237. class condition_t<use_kcp_t>
  238. {
  239. public:
  240. using type = use_kcp_t;
  241. explicit condition_t(use_kcp_t) noexcept {}
  242. ~condition_t() noexcept = default;
  243. condition_t(condition_t&&) noexcept = default;
  244. condition_t(condition_t const&) noexcept = delete;
  245. condition_t& operator=(condition_t&&) noexcept = default;
  246. condition_t& operator=(condition_t const&) noexcept = delete;
  247. inline condition_t clone() noexcept { return condition_t{ use_kcp_t{} }; }
  248. inline asio::detail::transfer_at_least_t operator()() noexcept { return asio::transfer_at_least(1); }
  249. inline asio::detail::transfer_at_least_t lowest () noexcept { return asio::transfer_at_least(1); }
  250. };
  251. template<>
  252. class condition_t<hook_buffer_t>
  253. {
  254. public:
  255. using type = hook_buffer_t;
  256. explicit condition_t(hook_buffer_t) noexcept {}
  257. ~condition_t() noexcept = default;
  258. condition_t(condition_t&&) noexcept = default;
  259. condition_t(condition_t const&) noexcept = delete;
  260. condition_t& operator=(condition_t&&) noexcept = default;
  261. condition_t& operator=(condition_t const&) noexcept = delete;
  262. inline condition_t clone() noexcept { return condition_t{ hook_buffer_t{} }; }
  263. inline asio::detail::transfer_at_least_t operator()() noexcept { return asio::transfer_at_least(1); }
  264. inline asio::detail::transfer_at_least_t lowest () noexcept { return asio::transfer_at_least(1); }
  265. };
  266. }
  267. namespace asio2
  268. {
  269. //constexpr static detail::use_sync_t use_sync;
  270. // https://github.com/skywind3000/kcp
  271. constexpr static detail::use_kcp_t use_kcp;
  272. constexpr static detail::use_dgram_t use_dgram;
  273. constexpr static detail::hook_buffer_t hook_buffer;
  274. }
  275. #endif // !__ASIO2_MATCH_CONDITION_HPP__