NetworkByteStreamSerializable.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/discovery/AsioTypes.hpp>
  21. #if defined(LINK_PLATFORM_MACOSX)
  22. #include <ableton/platforms/darwin/Darwin.hpp>
  23. #elif defined(LINK_PLATFORM_LINUX)
  24. #include <ableton/platforms/linux/Linux.hpp>
  25. #elif defined(LINK_PLATFORM_WINDOWS)
  26. #include <ableton/platforms/windows/Windows.hpp>
  27. #elif defined(ESP_PLATFORM)
  28. #include <ableton/platforms/esp32/Esp32.hpp>
  29. #endif
  30. #include <chrono>
  31. #include <cstdint>
  32. #include <iterator>
  33. #include <string>
  34. #include <type_traits>
  35. #include <utility>
  36. #include <vector>
  37. #if defined(LINK_PLATFORM_WINDOWS)
  38. #include <WS2tcpip.h>
  39. #include <WinSock2.h>
  40. #include <Windows.h>
  41. #endif
  42. namespace ableton
  43. {
  44. namespace discovery
  45. {
  46. // Concept: NetworkByteStreamSerializable
  47. //
  48. // A type that can be encoded to a stream of bytes and decoded from a
  49. // stream of bytes in network byte order. The following type is for
  50. // documentation purposes only.
  51. struct NetworkByteStreamSerializable
  52. {
  53. friend std::uint32_t sizeInByteStream(const NetworkByteStreamSerializable&);
  54. // The byte stream pointed to by 'out' must have sufficient space to
  55. // hold this object, as defined by sizeInByteStream.
  56. template <typename It>
  57. friend It toNetworkByteStream(const NetworkByteStreamSerializable&, It out);
  58. };
  59. // Deserialization aspect of the concept. Outside of the demonstration
  60. // type above because clients must specify the type
  61. // explicitly. Default implementation just defers to a class static
  62. // method on T. For types that can't provide such a method, specialize
  63. // this template.
  64. template <typename T>
  65. struct Deserialize
  66. {
  67. // Throws std::runtime_exception if parsing the type from the given
  68. // byte range fails. Returns a pair of the correctly parsed value
  69. // and an iterator to the next byte to parse.
  70. template <typename It>
  71. static std::pair<T, It> fromNetworkByteStream(It begin, It end)
  72. {
  73. return T::fromNetworkByteStream(std::move(begin), std::move(end));
  74. }
  75. };
  76. // Default size implementation. Works for primitive types.
  77. template <typename T,
  78. typename std::enable_if<std::is_fundamental<T>::value>::type* = nullptr>
  79. std::uint32_t sizeInByteStream(T)
  80. {
  81. return sizeof(T);
  82. }
  83. namespace detail
  84. {
  85. // utilities for implementing concept for primitive types
  86. template <typename T, typename It>
  87. It copyToByteStream(T t, It out)
  88. {
  89. using namespace std;
  90. return copy_n(
  91. reinterpret_cast<typename iterator_traits<It>::pointer>(&t), sizeof(t), out);
  92. }
  93. template <typename T, typename It>
  94. std::pair<T, It> copyFromByteStream(It begin, const It end)
  95. {
  96. using namespace std;
  97. using ItDiff = typename iterator_traits<It>::difference_type;
  98. if (distance(begin, end) < static_cast<ItDiff>(sizeof(T)))
  99. {
  100. throw range_error("Parsing type from byte stream failed");
  101. }
  102. else
  103. {
  104. T t;
  105. const auto n = sizeof(t);
  106. copy_n(begin, n, reinterpret_cast<uint8_t*>(&t));
  107. return make_pair(t, begin + n);
  108. }
  109. }
  110. } // namespace detail
  111. // Model the concept for unsigned integral types
  112. // uint8_t
  113. template <typename It>
  114. It toNetworkByteStream(const uint8_t byte, It out)
  115. {
  116. return detail::copyToByteStream(byte, std::move(out));
  117. }
  118. template <>
  119. struct Deserialize<uint8_t>
  120. {
  121. template <typename It>
  122. static std::pair<uint8_t, It> fromNetworkByteStream(It begin, It end)
  123. {
  124. return detail::copyFromByteStream<uint8_t>(std::move(begin), std::move(end));
  125. }
  126. };
  127. // uint16_t
  128. template <typename It>
  129. It toNetworkByteStream(uint16_t s, It out)
  130. {
  131. return detail::copyToByteStream(htons(s), std::move(out));
  132. }
  133. template <>
  134. struct Deserialize<uint16_t>
  135. {
  136. template <typename It>
  137. static std::pair<uint16_t, It> fromNetworkByteStream(It begin, It end)
  138. {
  139. auto result = detail::copyFromByteStream<uint16_t>(std::move(begin), std::move(end));
  140. result.first = ntohs(result.first);
  141. return result;
  142. }
  143. };
  144. // uint32_t
  145. template <typename It>
  146. It toNetworkByteStream(uint32_t l, It out)
  147. {
  148. return detail::copyToByteStream(htonl(l), std::move(out));
  149. }
  150. template <>
  151. struct Deserialize<uint32_t>
  152. {
  153. template <typename It>
  154. static std::pair<uint32_t, It> fromNetworkByteStream(It begin, It end)
  155. {
  156. auto result = detail::copyFromByteStream<uint32_t>(std::move(begin), std::move(end));
  157. result.first = ntohl(result.first);
  158. return result;
  159. }
  160. };
  161. // int32_t in terms of uint32_t
  162. template <typename It>
  163. It toNetworkByteStream(int32_t l, It out)
  164. {
  165. return toNetworkByteStream(reinterpret_cast<const uint32_t&>(l), std::move(out));
  166. }
  167. template <>
  168. struct Deserialize<int32_t>
  169. {
  170. template <typename It>
  171. static std::pair<int32_t, It> fromNetworkByteStream(It begin, It end)
  172. {
  173. auto result =
  174. Deserialize<uint32_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  175. return std::make_pair(reinterpret_cast<const int32_t&>(result.first), result.second);
  176. }
  177. };
  178. // uint64_t
  179. template <typename It>
  180. It toNetworkByteStream(uint64_t ll, It out)
  181. {
  182. return detail::copyToByteStream(htonll(ll), std::move(out));
  183. }
  184. template <>
  185. struct Deserialize<uint64_t>
  186. {
  187. template <typename It>
  188. static std::pair<uint64_t, It> fromNetworkByteStream(It begin, It end)
  189. {
  190. auto result = detail::copyFromByteStream<uint64_t>(std::move(begin), std::move(end));
  191. result.first = ntohll(result.first);
  192. return result;
  193. }
  194. };
  195. // int64_t in terms of uint64_t
  196. template <typename It>
  197. It toNetworkByteStream(int64_t ll, It out)
  198. {
  199. return toNetworkByteStream(reinterpret_cast<const uint64_t&>(ll), std::move(out));
  200. }
  201. template <>
  202. struct Deserialize<int64_t>
  203. {
  204. template <typename It>
  205. static std::pair<int64_t, It> fromNetworkByteStream(It begin, It end)
  206. {
  207. auto result =
  208. Deserialize<uint64_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  209. return std::make_pair(reinterpret_cast<const int64_t&>(result.first), result.second);
  210. }
  211. };
  212. // int16_t in terms of uint16_t
  213. template <typename It>
  214. It toNetworkByteStream(int16_t i, It out)
  215. {
  216. return toNetworkByteStream(reinterpret_cast<const uint16_t&>(i), std::move(out));
  217. }
  218. template <>
  219. struct Deserialize<int16_t>
  220. {
  221. template <typename It>
  222. static std::pair<int16_t, It> fromNetworkByteStream(It begin, It end)
  223. {
  224. auto result =
  225. Deserialize<uint16_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  226. return std::make_pair(reinterpret_cast<const int16_t&>(result.first), result.second);
  227. }
  228. };
  229. // bool
  230. inline std::uint32_t sizeInByteStream(bool)
  231. {
  232. return sizeof(uint8_t);
  233. }
  234. template <typename It>
  235. It toNetworkByteStream(bool bl, It out)
  236. {
  237. return toNetworkByteStream(static_cast<uint8_t>(bl), std::move(out));
  238. }
  239. template <>
  240. struct Deserialize<bool>
  241. {
  242. template <typename It>
  243. static std::pair<bool, It> fromNetworkByteStream(It begin, It end)
  244. {
  245. auto result =
  246. Deserialize<uint8_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  247. return std::make_pair(result.first != 0, result.second);
  248. }
  249. };
  250. // std::chrono::microseconds
  251. inline std::uint32_t sizeInByteStream(const std::chrono::microseconds micros)
  252. {
  253. return sizeInByteStream(micros.count());
  254. }
  255. template <typename It>
  256. It toNetworkByteStream(const std::chrono::microseconds micros, It out)
  257. {
  258. static_assert(sizeof(int64_t) == sizeof(std::chrono::microseconds::rep),
  259. "The size of microseconds::rep must match the size of int64_t.");
  260. return toNetworkByteStream(static_cast<int64_t>(micros.count()), std::move(out));
  261. }
  262. template <>
  263. struct Deserialize<std::chrono::microseconds>
  264. {
  265. template <typename It>
  266. static std::pair<std::chrono::microseconds, It> fromNetworkByteStream(It begin, It end)
  267. {
  268. using namespace std;
  269. auto result =
  270. Deserialize<int64_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  271. return make_pair(chrono::microseconds{result.first}, result.second);
  272. }
  273. };
  274. // string
  275. inline std::uint32_t sizeInByteStream(const std::string& str)
  276. {
  277. return sizeof(std::uint32_t) + static_cast<std::uint32_t>(str.size());
  278. }
  279. template <typename It>
  280. It toNetworkByteStream(const std::string& str, It out)
  281. {
  282. static_assert(sizeof(std::uint8_t) == sizeof(char),
  283. "The size of char must match the size of uint8_t.");
  284. out = toNetworkByteStream(static_cast<std::uint32_t>(str.size()), out);
  285. return std::copy(str.begin(), str.end(), out);
  286. }
  287. template <>
  288. struct Deserialize<std::string>
  289. {
  290. template <typename It>
  291. static std::pair<std::string, It> fromNetworkByteStream(It bytesBegin, It bytesEnd)
  292. {
  293. const auto result = Deserialize<uint32_t>::fromNetworkByteStream(
  294. std::move(bytesBegin), std::move(bytesEnd));
  295. const auto endIt = std::next(result.second, result.first);
  296. return std::make_pair(std::string(result.second, endIt), endIt);
  297. }
  298. };
  299. namespace detail
  300. {
  301. // Generic serialize/deserialize utilities for containers
  302. template <typename Container>
  303. std::uint32_t containerSizeInByteStream(const Container& container)
  304. {
  305. std::uint32_t totalSize = 0;
  306. for (const auto& val : container)
  307. {
  308. totalSize += sizeInByteStream(val);
  309. }
  310. return totalSize;
  311. }
  312. template <typename Container, typename It>
  313. It containerToNetworkByteStream(const Container& container, It out)
  314. {
  315. for (const auto& val : container)
  316. {
  317. out = toNetworkByteStream(val, out);
  318. }
  319. return out;
  320. }
  321. template <typename T, typename BytesIt, typename InsertIt>
  322. BytesIt deserializeContainer(BytesIt bytesBegin,
  323. const BytesIt bytesEnd,
  324. InsertIt contBegin,
  325. const std::uint32_t maxElements)
  326. {
  327. using namespace std;
  328. std::uint32_t numElements = 0;
  329. while (bytesBegin < bytesEnd && numElements < maxElements)
  330. {
  331. T newVal;
  332. tie(newVal, bytesBegin) = Deserialize<T>::fromNetworkByteStream(bytesBegin, bytesEnd);
  333. *contBegin++ = newVal;
  334. ++numElements;
  335. }
  336. return bytesBegin;
  337. }
  338. } // namespace detail
  339. // Need specific overloads for each container type, but use above
  340. // utilities for common implementation
  341. // array
  342. template <typename T, std::size_t Size>
  343. std::uint32_t sizeInByteStream(const std::array<T, Size>& arr)
  344. {
  345. return detail::containerSizeInByteStream(arr);
  346. }
  347. template <typename T, std::size_t Size, typename It>
  348. It toNetworkByteStream(const std::array<T, Size>& arr, It out)
  349. {
  350. return detail::containerToNetworkByteStream(arr, std::move(out));
  351. }
  352. template <typename T, std::size_t Size>
  353. struct Deserialize<std::array<T, Size>>
  354. {
  355. template <typename It>
  356. static std::pair<std::array<T, Size>, It> fromNetworkByteStream(It begin, It end)
  357. {
  358. using namespace std;
  359. array<T, Size> result{};
  360. auto resultIt = detail::deserializeContainer<T>(
  361. std::move(begin), std::move(end), std::move(result.begin()), Size);
  362. return make_pair(std::move(result), std::move(resultIt));
  363. }
  364. };
  365. // vector
  366. template <typename T, typename Alloc>
  367. std::uint32_t sizeInByteStream(const std::vector<T, Alloc>& vec)
  368. {
  369. return sizeof(uint32_t) + detail::containerSizeInByteStream(vec);
  370. }
  371. template <typename T, typename Alloc, typename It>
  372. It toNetworkByteStream(const std::vector<T, Alloc>& vec, It out)
  373. {
  374. out = toNetworkByteStream(static_cast<uint32_t>(vec.size()), out);
  375. return detail::containerToNetworkByteStream(vec, std::move(out));
  376. }
  377. template <typename T, typename Alloc>
  378. struct Deserialize<std::vector<T, Alloc>>
  379. {
  380. template <typename It>
  381. static std::pair<std::vector<T, Alloc>, It> fromNetworkByteStream(It bytesBegin,
  382. It bytesEnd)
  383. {
  384. using namespace std;
  385. auto result_size =
  386. Deserialize<uint32_t>::fromNetworkByteStream(std::move(bytesBegin), bytesEnd);
  387. vector<T, Alloc> result;
  388. auto resultIt = detail::deserializeContainer<T>(std::move(result_size.second),
  389. std::move(bytesEnd),
  390. back_inserter(result),
  391. result_size.first);
  392. return make_pair(std::move(result), std::move(resultIt));
  393. }
  394. };
  395. // 2-tuple
  396. template <typename X, typename Y>
  397. std::uint32_t sizeInByteStream(const std::tuple<X, Y>& tup)
  398. {
  399. return sizeInByteStream(std::get<0>(tup)) + sizeInByteStream(std::get<1>(tup));
  400. }
  401. template <typename X, typename Y, typename It>
  402. It toNetworkByteStream(const std::tuple<X, Y>& tup, It out)
  403. {
  404. return toNetworkByteStream(
  405. std::get<1>(tup), toNetworkByteStream(std::get<0>(tup), std::move(out)));
  406. }
  407. template <typename X, typename Y>
  408. struct Deserialize<std::tuple<X, Y>>
  409. {
  410. template <typename It>
  411. static std::pair<std::tuple<X, Y>, It> fromNetworkByteStream(It begin, It end)
  412. {
  413. using namespace std;
  414. auto xres = Deserialize<X>::fromNetworkByteStream(begin, end);
  415. auto yres = Deserialize<Y>::fromNetworkByteStream(xres.second, end);
  416. return make_pair(
  417. make_tuple(std::move(xres.first), std::move(yres.first)), std::move(yres.second));
  418. }
  419. };
  420. // 3-tuple
  421. template <typename X, typename Y, typename Z>
  422. std::uint32_t sizeInByteStream(const std::tuple<X, Y, Z>& tup)
  423. {
  424. return sizeInByteStream(std::get<0>(tup)) + sizeInByteStream(std::get<1>(tup))
  425. + sizeInByteStream(std::get<2>(tup));
  426. }
  427. template <typename X, typename Y, typename Z, typename It>
  428. It toNetworkByteStream(const std::tuple<X, Y, Z>& tup, It out)
  429. {
  430. return toNetworkByteStream(
  431. std::get<2>(tup),
  432. toNetworkByteStream(
  433. std::get<1>(tup), toNetworkByteStream(std::get<0>(tup), std::move(out))));
  434. }
  435. template <typename X, typename Y, typename Z>
  436. struct Deserialize<std::tuple<X, Y, Z>>
  437. {
  438. template <typename It>
  439. static std::pair<std::tuple<X, Y, Z>, It> fromNetworkByteStream(It begin, It end)
  440. {
  441. using namespace std;
  442. auto xres = Deserialize<X>::fromNetworkByteStream(begin, end);
  443. auto yres = Deserialize<Y>::fromNetworkByteStream(xres.second, end);
  444. auto zres = Deserialize<Z>::fromNetworkByteStream(yres.second, end);
  445. return make_pair(
  446. make_tuple(std::move(xres.first), std::move(yres.first), std::move(zres.first)),
  447. std::move(zres.second));
  448. }
  449. };
  450. } // namespace discovery
  451. } // namespace ableton