Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NetworkByteStreamSerializable.hpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/platforms/asio/AsioWrapper.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. #endif
  26. #include <chrono>
  27. #include <cstdint>
  28. #include <type_traits>
  29. #include <utility>
  30. #include <vector>
  31. #if defined(LINK_PLATFORM_WINDOWS)
  32. #include <ws2tcpip.h>
  33. #include <winsock2.h>
  34. #include <windows.h>
  35. #endif
  36. namespace ableton
  37. {
  38. namespace discovery
  39. {
  40. // Concept: NetworkByteStreamSerializable
  41. //
  42. // A type that can be encoded to a stream of bytes and decoded from a
  43. // stream of bytes in network byte order. The following type is for
  44. // documentation purposes only.
  45. struct NetworkByteStreamSerializable
  46. {
  47. friend std::uint32_t sizeInByteStream(const NetworkByteStreamSerializable&);
  48. // The byte stream pointed to by 'out' must have sufficient space to
  49. // hold this object, as defined by sizeInByteStream.
  50. template <typename It>
  51. friend It toNetworkByteStream(const NetworkByteStreamSerializable&, It out);
  52. };
  53. // Deserialization aspect of the concept. Outside of the demonstration
  54. // type above because clients must specify the type
  55. // explicitly. Default implementation just defers to a class static
  56. // method on T. For types that can't provide such a method, specialize
  57. // this template.
  58. template <typename T>
  59. struct Deserialize
  60. {
  61. // Throws std::runtime_exception if parsing the type from the given
  62. // byte range fails. Returns a pair of the correctly parsed value
  63. // and an iterator to the next byte to parse.
  64. template <typename It>
  65. static std::pair<T, It> fromNetworkByteStream(It begin, It end)
  66. {
  67. return T::fromNetworkByteStream(std::move(begin), std::move(end));
  68. }
  69. };
  70. // Default size implementation. Works for primitive types.
  71. template <typename T,
  72. typename std::enable_if<std::is_fundamental<T>::value>::type* = nullptr>
  73. std::uint32_t sizeInByteStream(T)
  74. {
  75. return sizeof(T);
  76. }
  77. namespace detail
  78. {
  79. // utilities for implementing concept for primitive types
  80. template <typename T, typename It>
  81. It copyToByteStream(T t, It out)
  82. {
  83. using namespace std;
  84. return copy_n(
  85. reinterpret_cast<typename iterator_traits<It>::pointer>(&t), sizeof(t), out);
  86. }
  87. template <typename T, typename It>
  88. std::pair<T, It> copyFromByteStream(It begin, const It end)
  89. {
  90. using namespace std;
  91. using ItDiff = typename iterator_traits<It>::difference_type;
  92. if (distance(begin, end) < static_cast<ItDiff>(sizeof(T)))
  93. {
  94. throw range_error("Parsing type from byte stream failed");
  95. }
  96. else
  97. {
  98. T t;
  99. const auto n = sizeof(t);
  100. copy_n(begin, n, reinterpret_cast<uint8_t*>(&t));
  101. return make_pair(t, begin + n);
  102. }
  103. }
  104. } // namespace detail
  105. // Model the concept for unsigned integral types
  106. // uint8_t
  107. template <typename It>
  108. It toNetworkByteStream(const uint8_t byte, It out)
  109. {
  110. return detail::copyToByteStream(byte, std::move(out));
  111. }
  112. template <>
  113. struct Deserialize<uint8_t>
  114. {
  115. template <typename It>
  116. static std::pair<uint8_t, It> fromNetworkByteStream(It begin, It end)
  117. {
  118. return detail::copyFromByteStream<uint8_t>(std::move(begin), std::move(end));
  119. }
  120. };
  121. // uint16_t
  122. template <typename It>
  123. It toNetworkByteStream(uint16_t s, It out)
  124. {
  125. return detail::copyToByteStream(htons(s), std::move(out));
  126. }
  127. template <>
  128. struct Deserialize<uint16_t>
  129. {
  130. template <typename It>
  131. static std::pair<uint16_t, It> fromNetworkByteStream(It begin, It end)
  132. {
  133. auto result = detail::copyFromByteStream<uint16_t>(std::move(begin), std::move(end));
  134. result.first = ntohs(result.first);
  135. return result;
  136. }
  137. };
  138. // uint32_t
  139. template <typename It>
  140. It toNetworkByteStream(uint32_t l, It out)
  141. {
  142. return detail::copyToByteStream(htonl(l), std::move(out));
  143. }
  144. template <>
  145. struct Deserialize<uint32_t>
  146. {
  147. template <typename It>
  148. static std::pair<uint32_t, It> fromNetworkByteStream(It begin, It end)
  149. {
  150. auto result = detail::copyFromByteStream<uint32_t>(std::move(begin), std::move(end));
  151. result.first = ntohl(result.first);
  152. return result;
  153. }
  154. };
  155. // int32_t in terms of uint32_t
  156. template <typename It>
  157. It toNetworkByteStream(int32_t l, It out)
  158. {
  159. return toNetworkByteStream(reinterpret_cast<const uint32_t&>(l), std::move(out));
  160. }
  161. template <>
  162. struct Deserialize<int32_t>
  163. {
  164. template <typename It>
  165. static std::pair<int32_t, It> fromNetworkByteStream(It begin, It end)
  166. {
  167. auto result =
  168. Deserialize<uint32_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  169. return std::make_pair(reinterpret_cast<const int32_t&>(result.first), result.second);
  170. }
  171. };
  172. // uint64_t
  173. template <typename It>
  174. It toNetworkByteStream(uint64_t ll, It out)
  175. {
  176. return detail::copyToByteStream(htonll(ll), std::move(out));
  177. }
  178. template <>
  179. struct Deserialize<uint64_t>
  180. {
  181. template <typename It>
  182. static std::pair<uint64_t, It> fromNetworkByteStream(It begin, It end)
  183. {
  184. auto result = detail::copyFromByteStream<uint64_t>(std::move(begin), std::move(end));
  185. result.first = ntohll(result.first);
  186. return result;
  187. }
  188. };
  189. // int64_t in terms of uint64_t
  190. template <typename It>
  191. It toNetworkByteStream(int64_t ll, It out)
  192. {
  193. return toNetworkByteStream(reinterpret_cast<const uint64_t&>(ll), std::move(out));
  194. }
  195. template <>
  196. struct Deserialize<int64_t>
  197. {
  198. template <typename It>
  199. static std::pair<int64_t, It> fromNetworkByteStream(It begin, It end)
  200. {
  201. auto result =
  202. Deserialize<uint64_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  203. return std::make_pair(reinterpret_cast<const int64_t&>(result.first), result.second);
  204. }
  205. };
  206. // bool
  207. inline std::uint32_t sizeInByteStream(bool)
  208. {
  209. return sizeof(uint8_t);
  210. }
  211. template <typename It>
  212. It toNetworkByteStream(bool bl, It out)
  213. {
  214. return toNetworkByteStream(static_cast<uint8_t>(bl), std::move(out));
  215. }
  216. template <>
  217. struct Deserialize<bool>
  218. {
  219. template <typename It>
  220. static std::pair<bool, It> fromNetworkByteStream(It begin, It end)
  221. {
  222. auto result =
  223. Deserialize<uint8_t>::fromNetworkByteStream(std::move(begin), std::move(end));
  224. return std::make_pair(result.first != 0, result.second);
  225. }
  226. };
  227. // std::chrono::microseconds
  228. inline std::uint32_t sizeInByteStream(const std::chrono::microseconds micros)
  229. {
  230. return sizeInByteStream(micros.count());
  231. }
  232. template <typename It>
  233. It toNetworkByteStream(const std::chrono::microseconds micros, It out)
  234. {
  235. static_assert(sizeof(int64_t) == sizeof(std::chrono::microseconds::rep),
  236. "The size of microseconds::rep must matche the size of int64_t.");
  237. return toNetworkByteStream(static_cast<int64_t>(micros.count()), std::move(out));
  238. }
  239. template <>
  240. struct Deserialize<std::chrono::microseconds>
  241. {
  242. template <typename It>
  243. static std::pair<std::chrono::microseconds, It> fromNetworkByteStream(It begin, It end)
  244. {
  245. using namespace std;
  246. auto result = Deserialize<int64_t>::fromNetworkByteStream(move(begin), move(end));
  247. return make_pair(chrono::microseconds{result.first}, result.second);
  248. }
  249. };
  250. namespace detail
  251. {
  252. // Generic serialize/deserialize utilities for containers
  253. template <typename Container>
  254. std::uint32_t containerSizeInByteStream(const Container& container)
  255. {
  256. std::uint32_t totalSize = 0;
  257. for (const auto& val : container)
  258. {
  259. totalSize += sizeInByteStream(val);
  260. }
  261. return totalSize;
  262. }
  263. template <typename Container, typename It>
  264. It containerToNetworkByteStream(const Container& container, It out)
  265. {
  266. for (const auto& val : container)
  267. {
  268. out = toNetworkByteStream(val, out);
  269. }
  270. return out;
  271. }
  272. template <typename T, typename BytesIt, typename InsertIt>
  273. BytesIt deserializeContainer(BytesIt bytesBegin,
  274. const BytesIt bytesEnd,
  275. InsertIt contBegin,
  276. const std::uint32_t maxElements)
  277. {
  278. using namespace std;
  279. std::uint32_t numElements = 0;
  280. while (bytesBegin < bytesEnd && numElements < maxElements)
  281. {
  282. T newVal;
  283. tie(newVal, bytesBegin) = Deserialize<T>::fromNetworkByteStream(bytesBegin, bytesEnd);
  284. *contBegin++ = newVal;
  285. ++numElements;
  286. }
  287. return bytesBegin;
  288. }
  289. } // namespace detail
  290. // Need specific overloads for each container type, but use above
  291. // utilities for common implementation
  292. // array
  293. template <typename T, std::size_t Size>
  294. std::uint32_t sizeInByteStream(const std::array<T, Size>& arr)
  295. {
  296. return detail::containerSizeInByteStream(arr);
  297. }
  298. template <typename T, std::size_t Size, typename It>
  299. It toNetworkByteStream(const std::array<T, Size>& arr, It out)
  300. {
  301. return detail::containerToNetworkByteStream(arr, std::move(out));
  302. }
  303. template <typename T, std::size_t Size>
  304. struct Deserialize<std::array<T, Size>>
  305. {
  306. template <typename It>
  307. static std::pair<std::array<T, Size>, It> fromNetworkByteStream(It begin, It end)
  308. {
  309. using namespace std;
  310. array<T, Size> result{};
  311. auto resultIt =
  312. detail::deserializeContainer<T>(move(begin), move(end), move(result.begin()), Size);
  313. return make_pair(move(result), move(resultIt));
  314. }
  315. };
  316. // vector
  317. template <typename T, typename Alloc>
  318. std::uint32_t sizeInByteStream(const std::vector<T, Alloc>& vec)
  319. {
  320. return sizeof(uint32_t) + detail::containerSizeInByteStream(vec);
  321. }
  322. template <typename T, typename Alloc, typename It>
  323. It toNetworkByteStream(const std::vector<T, Alloc>& vec, It out)
  324. {
  325. out = toNetworkByteStream(static_cast<uint32_t>(vec.size()), out);
  326. return detail::containerToNetworkByteStream(vec, std::move(out));
  327. }
  328. template <typename T, typename Alloc>
  329. struct Deserialize<std::vector<T, Alloc>>
  330. {
  331. template <typename It>
  332. static std::pair<std::vector<T, Alloc>, It> fromNetworkByteStream(
  333. It bytesBegin, It bytesEnd)
  334. {
  335. using namespace std;
  336. auto result_size =
  337. Deserialize<uint32_t>::fromNetworkByteStream(move(bytesBegin), bytesEnd);
  338. vector<T, Alloc> result;
  339. auto resultIt = detail::deserializeContainer<T>(
  340. move(result_size.second), move(bytesEnd), back_inserter(result), result_size.first);
  341. return make_pair(move(result), move(resultIt));
  342. }
  343. };
  344. // 2-tuple
  345. template <typename X, typename Y>
  346. std::uint32_t sizeInByteStream(const std::tuple<X, Y>& tup)
  347. {
  348. return sizeInByteStream(std::get<0>(tup)) + sizeInByteStream(std::get<1>(tup));
  349. }
  350. template <typename X, typename Y, typename It>
  351. It toNetworkByteStream(const std::tuple<X, Y>& tup, It out)
  352. {
  353. return toNetworkByteStream(
  354. std::get<1>(tup), toNetworkByteStream(std::get<0>(tup), std::move(out)));
  355. }
  356. template <typename X, typename Y>
  357. struct Deserialize<std::tuple<X, Y>>
  358. {
  359. template <typename It>
  360. static std::pair<std::tuple<X, Y>, It> fromNetworkByteStream(It begin, It end)
  361. {
  362. using namespace std;
  363. auto xres = Deserialize<X>::fromNetworkByteStream(begin, end);
  364. auto yres = Deserialize<Y>::fromNetworkByteStream(xres.second, end);
  365. return make_pair(make_tuple(move(xres.first), move(yres.first)), move(yres.second));
  366. }
  367. };
  368. // 3-tuple
  369. template <typename X, typename Y, typename Z>
  370. std::uint32_t sizeInByteStream(const std::tuple<X, Y, Z>& tup)
  371. {
  372. return sizeInByteStream(std::get<0>(tup)) + sizeInByteStream(std::get<1>(tup))
  373. + sizeInByteStream(std::get<2>(tup));
  374. }
  375. template <typename X, typename Y, typename Z, typename It>
  376. It toNetworkByteStream(const std::tuple<X, Y, Z>& tup, It out)
  377. {
  378. return toNetworkByteStream(
  379. std::get<2>(tup), toNetworkByteStream(std::get<1>(tup),
  380. toNetworkByteStream(std::get<0>(tup), std::move(out))));
  381. }
  382. template <typename X, typename Y, typename Z>
  383. struct Deserialize<std::tuple<X, Y, Z>>
  384. {
  385. template <typename It>
  386. static std::pair<std::tuple<X, Y, Z>, It> fromNetworkByteStream(It begin, It end)
  387. {
  388. using namespace std;
  389. auto xres = Deserialize<X>::fromNetworkByteStream(begin, end);
  390. auto yres = Deserialize<Y>::fromNetworkByteStream(xres.second, end);
  391. auto zres = Deserialize<Z>::fromNetworkByteStream(yres.second, end);
  392. return make_pair(make_tuple(move(xres.first), move(yres.first), move(zres.first)),
  393. move(zres.second));
  394. }
  395. };
  396. } // namespace discovery
  397. } // namespace ableton