The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

98 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::midi_ci::detail
  19. {
  20. /*
  21. Breaks up a large property exchange message into chunks of the requested size.
  22. Note that the header *must* fit inside the first block, so you must ensure
  23. that the header is small enough to fit inside the requested chunk size.
  24. */
  25. class PropertyDataMessageChunker
  26. {
  27. auto tie() const { return std::tie (storage, body, source, dest, chunkSize, messageKind, requestId); }
  28. public:
  29. /* Constructs a chunker instance.
  30. @param storageIn backing storage where each chunk will be written
  31. @param chunkSizeIn the maximum size of each chunk
  32. @param messageKindIn the subID2 byte identifying the type of message in each chunk
  33. @param requestIdIn the id that should be included in all messages that are part of the same property exchange transaction
  34. @param headerIn the header bytes of the message. This is always JSON encoded as 7-bit ASCII text, see the MIDI-CI spec for full details
  35. @param sourceIn the MUID of the device sending the chunked messages
  36. @param destIn the MUID of the recipient of the chunked messages
  37. @param bodyIn a stream that can supply the data payload for this chunk sequence. All payload bytes *must* be 7-bit (MSB not set).
  38. */
  39. PropertyDataMessageChunker (std::vector<std::byte>& storageIn,
  40. int chunkSizeIn,
  41. const std::byte messageKindIn,
  42. const std::byte requestIdIn,
  43. Span<const std::byte> headerIn,
  44. MUID sourceIn,
  45. MUID destIn,
  46. InputStream& bodyIn);
  47. /* Returns true if this chunker hasn't finished producing chunks. */
  48. explicit operator bool() const { return *this != PropertyDataMessageChunker(); }
  49. /* Allowing foreach usage. */
  50. auto begin() const { return *this; }
  51. /* Allow foreach usage. */
  52. auto end() const { return PropertyDataMessageChunker{}; }
  53. /* Writes the bytes of the next chunk, if any, into the storage buffer. */
  54. PropertyDataMessageChunker& operator++() noexcept;
  55. /* Checks whether the state of this chunker matches the state of another chunker, enabling foreach usage. */
  56. bool operator== (const PropertyDataMessageChunker& other) const noexcept { return tie() == other.tie(); }
  57. bool operator!= (const PropertyDataMessageChunker& other) const noexcept { return tie() != other.tie(); }
  58. /* Returns a span over the valid bytes in the output buffer. */
  59. Span<const std::byte> operator*() const noexcept;
  60. private:
  61. PropertyDataMessageChunker() = default;
  62. Span<const std::byte> getHeaderForBlock() const;
  63. int getRoomForBody() const;
  64. bool hasRoomForBody() const;
  65. void populateStorage() const;
  66. Span<const std::byte> header;
  67. std::vector<std::byte>* storage{};
  68. InputStream* body{};
  69. MUID source = MUID::makeUnchecked (0), dest = MUID::makeUnchecked (0);
  70. int chunkSize{};
  71. uint16_t thisChunk { 0x01 };
  72. std::byte messageKind{};
  73. std::byte requestId{};
  74. };
  75. } // namespace juce::midi_ci::detail