commit 727b9d6f7480e3701c75b1c6f2a1420107373d4e
parent 8c0b135b85265185738e9066dc1f29b0c3f83364
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date: Sun, 1 Aug 2021 13:31:09 +0200
Improve the RPC api
Diffstat:
3 files changed, 47 insertions(+), 11 deletions(-)
diff --git a/examples/io/messages.hh b/examples/io/messages.hh
@@ -20,6 +20,7 @@ namespace clap::messages {
kSetSizeRequest,
kSetSizeResponse,
kShowRequest,
+ kShowResponse,
kHideRequest,
kCloseRequest,
@@ -69,6 +70,10 @@ namespace clap::messages {
static const constexpr Type type = kShowRequest;
};
+ struct ShowResponse final {
+ static const constexpr Type type = kShowResponse;
+ };
+
struct HideRequest final {
static const constexpr Type type = kHideRequest;
};
diff --git a/examples/io/remote-channel.hh b/examples/io/remote-channel.hh
@@ -32,6 +32,19 @@ namespace clap {
}
template <typename T>
+ void get(T &obj) const noexcept {
+ constexpr const auto sz = sizeof(T);
+
+ if (size != sz)
+ std::terminate();
+
+ if (type != T::type)
+ std::terminate();
+
+ std::memcpy(&obj, data, sizeof(obj));
+ }
+
+ template <typename T>
const T &get() const noexcept {
return *reinterpret_cast<const T *>(data);
}
@@ -58,8 +71,17 @@ namespace clap {
uint32_t computeNextCookie() noexcept;
- bool sendMessageAsync(const Message &msg);
- bool sendMessageSync(const Message &msg, const MessageHandler &handler);
+ template <typename Request>
+ bool sendMessageAsync(const Request &request) {
+ return sendMessageAsync(RemoteChannel::Message(request, computeNextCookie()));
+ }
+
+ template <typename Request, typename Response>
+ bool sendMessageSync(const Request &request, Response &response) {
+ sendMessageSync(RemoteChannel::Message(request, computeNextCookie()),
+ [&response](const RemoteChannel::Message &m) { m.get(response); });
+ return true;
+ }
void close();
@@ -82,6 +104,9 @@ namespace clap {
void modifyFd(clap_fd_flags flags);
+ bool sendMessageAsync(const Message &msg);
+ bool sendMessageSync(const Message &msg, const MessageHandler &handler);
+
const bool cookieHalf_;
uint32_t nextCookie_ = 0;
diff --git a/examples/plugins/remote-gui.cc b/examples/plugins/remote-gui.cc
@@ -51,20 +51,26 @@ namespace clap {
}
bool RemoteGui::size(uint32_t *width, uint32_t *height) noexcept {
- channel_->sendMessageSync(
- RemoteChannel::Message(messages::SizeRequest{}, channel_->computeNextCookie()),
- [width, height](const RemoteChannel::Message &m) {
- auto &response = m.get<messages::SizeResponse>();
- *width = response.width;
- *height = response.height;
- });
+ messages::SizeRequest request;
+ messages::SizeResponse response;
+ if (!channel_->sendMessageSync(request, response))
+ return false;
+
+ *width = response.width;
+ *height = response.height;
return true;
}
void RemoteGui::setScale(double scale) noexcept {
- channel_->sendMessageAsync(
- RemoteChannel::Message(messages::SetScaleRequest{scale}, channel_->computeNextCookie()));
+ channel_->sendMessageAsync(messages::SetScaleRequest{scale});
+ }
+
+ bool RemoteGui::show() noexcept {
+ messages::ShowRequest request;
+ messages::ShowResponse response;
+
+ return channel_->sendMessageSync(request, response);
}
} // namespace clap
\ No newline at end of file