clap

CLAP Audio Plugin API
Log | Files | Refs | README | LICENSE

commit e6b15db4f21b160d3f9c2a928471fad3a4f4d896
parent 336eb2fdc0df58be80fc7885a1049376fa249310
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date:   Fri, 30 Jul 2021 18:08:10 +0200

more work

Diffstat:
Mexamples/io/remote-channel.cc | 41+++++++++++++++++++++++++++++++++++++++--
Mexamples/io/remote-channel.hh | 16++++++++++------
Mexamples/plugins/remote-gui.cc | 5+++++
3 files changed, 54 insertions(+), 8 deletions(-)

diff --git a/examples/io/remote-channel.cc b/examples/io/remote-channel.cc @@ -26,8 +26,7 @@ namespace clap { } inputBuffer_.wrote(nbytes); - parseInput(); - inputBuffer_.rewind(); + processInput(); } void RemoteChannel::write(const void *_data, size_t size) { @@ -101,6 +100,34 @@ namespace clap { return cookie; } + void RemoteChannel::processInput() { + while (inputBuffer_.readAvail() > 12) { + const auto *data = inputBuffer_.readData(); + Message msg; + + std::memcpy(&msg.type, data, 4); + std::memcpy(&msg.cookie, data + 4, 4); + std::memcpy(&msg.size, data + 8, 4); + msg.data = data + 12; + + uint32_t totalSize = 12 + msg.size; + if (inputBuffer_.readAvail() < totalSize) + return; + + auto it = syncHandlers_.find(msg.cookie); + if (it != syncHandlers_.end()) { + it->second(msg); + syncHandlers_.erase(msg.cookie); + } else { + handler_(msg); + } + + inputBuffer_.read(totalSize); + } + + inputBuffer_.rewind(); + } + bool RemoteChannel::sendMessageAsync(const Message &msg) { write(&msg.type, sizeof(msg.type)); write(&msg.cookie, sizeof(msg.cookie)); @@ -111,6 +138,16 @@ namespace clap { } bool RemoteChannel::sendMessageSync(const Message &msg, const MessageHandler &handler) { + sendMessageAsync(msg); + + syncHandlers_.emplace(msg.cookie, handler); + + while (syncHandlers_.count(msg.cookie) > 0) + runOnce(); + + syncHandlers_.erase(msg.cookie); return true; } + + void RemoteChannel::runOnce() {} } // namespace clap \ No newline at end of file diff --git a/examples/io/remote-channel.hh b/examples/io/remote-channel.hh @@ -19,10 +19,12 @@ namespace clap { }; struct Message final { - uint32_t type; - uint32_t cookie; - uint32_t size; - const void *data; + uint32_t type = 0; + uint32_t cookie = 0; + uint32_t size = 0; + const void *data = nullptr; + + Message() = default; template <typename T> Message(const T &msg, uint32_t c) : cookie(c) { @@ -67,6 +69,8 @@ namespace clap { // Called when data can be written, non-blocking void onWrite(); + void runOnce(); + private: using ReadBuffer = Buffer<uint8_t, 128 * 1024>; using WriteBuffer = Buffer<uint8_t, 32 * 1024>; @@ -74,13 +78,13 @@ namespace clap { void write(const void *data, size_t size); WriteBuffer &nextWriteBuffer(); - void parseInput(); + void processInput(); const bool cookieHalf_; uint32_t nextCookie_ = 0; const MessageHandler &handler_; - std::unordered_map<uint32_t /* cookie */, MessageHandler &> syncHandlers_; + std::unordered_map<uint32_t /* cookie */, const MessageHandler &> syncHandlers_; EventControl &evControl_; clap_fd socket_; diff --git a/examples/plugins/remote-gui.cc b/examples/plugins/remote-gui.cc @@ -62,4 +62,9 @@ namespace clap { return true; } + void RemoteGui::setScale(double scale) noexcept { + channel_->sendMessageAsync( + RemoteChannel::Message(messages::SetScaleRequest{scale}, channel_->computeNextCookie())); + } + } // namespace clap \ No newline at end of file