refactor IPC-dependency out of Proxy
This commit is contained in:
@@ -1,27 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <ipc/IpcChannel.h>
|
||||
#include <rpc/RpcValue.h>
|
||||
|
||||
class ProxyMarshaller {
|
||||
public:
|
||||
explicit ProxyMarshaller(IpcChannel& ch) : channel(ch) {}
|
||||
|
||||
template<typename Ret, typename... Args>
|
||||
Ret call(const std::string& method, const Args&... args) {
|
||||
// Базовый type-erased вызов: принимает вектор RpcValue и возвращает RpcValue.
|
||||
RpcValue call(const std::string& method, const RpcArgs& args) {
|
||||
IpcMessage msg;
|
||||
|
||||
// имя метода
|
||||
msg.add(method);
|
||||
|
||||
// аргументы
|
||||
(msg.add(args), ...);
|
||||
// аргументы (PoC: только int)
|
||||
for (const auto& a : args) {
|
||||
msg.add(a.asInt());
|
||||
}
|
||||
|
||||
// отправить
|
||||
channel.send(msg);
|
||||
|
||||
// получить ответ
|
||||
IpcMessage resp = channel.receive();
|
||||
return resp.template get<Ret>();
|
||||
return RpcValue::fromInt(resp.get<int>());
|
||||
}
|
||||
|
||||
// Удобный шаблонный хелпер для сгенерированных прокси.
|
||||
template<typename Ret, typename... Args>
|
||||
Ret callTyped(const std::string& method, const Args&... args) {
|
||||
RpcArgs packed;
|
||||
packed.reserve(sizeof...(Args));
|
||||
(packed.emplace_back(RpcValue::fromInt(args)), ...); // PoC: только int
|
||||
|
||||
RpcValue r = call(method, packed);
|
||||
// PoC: Ret == int
|
||||
return static_cast<Ret>(r.asInt());
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
+20
-28
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <ipc/IpcMessage.h>
|
||||
#include <rpc/RpcValue.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
@@ -14,50 +14,42 @@ public:
|
||||
const std::string& name,
|
||||
Ret (Obj::*method)(Args...)) {
|
||||
handlers[name] =
|
||||
[instance, method](const IpcMessage& req) -> IpcMessage {
|
||||
IpcMessage msg = req;
|
||||
[instance, method](const RpcArgs& args) -> RpcValue {
|
||||
auto tuple = readArgs<Args...>(args);
|
||||
Ret result = std::apply(
|
||||
method, std::tuple_cat(std::make_tuple(instance), tuple));
|
||||
|
||||
// пропустить имя метода
|
||||
(void)msg.template get<std::string>();
|
||||
|
||||
// читать аргументы и вызвать метод
|
||||
return callMethod<Ret, Obj, Args...>(instance, method, msg);
|
||||
// PoC: считаем, что Ret == int.
|
||||
return RpcValue::fromInt(result);
|
||||
};
|
||||
}
|
||||
|
||||
IpcMessage dispatch(const IpcMessage& request) {
|
||||
IpcMessage tmp = request;
|
||||
const std::string method = tmp.get<std::string>();
|
||||
|
||||
RpcValue dispatch(const std::string& method, const RpcArgs& args) const {
|
||||
auto it = handlers.find(method);
|
||||
if (it == handlers.end()) {
|
||||
return IpcMessage{};
|
||||
// PoC: в случае ошибки возвращаем 0.
|
||||
return RpcValue::fromInt(0);
|
||||
}
|
||||
|
||||
return it->second(request);
|
||||
return it->second(args);
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename Ret, typename Obj, typename... Args>
|
||||
static IpcMessage callMethod(Obj* obj,
|
||||
Ret (Obj::*method)(Args...),
|
||||
IpcMessage& msg) {
|
||||
auto tuple = readArgs<Args...>(msg);
|
||||
Ret result =
|
||||
std::apply(method, std::tuple_cat(std::make_tuple(obj), tuple));
|
||||
|
||||
IpcMessage out;
|
||||
out.add(result);
|
||||
return out;
|
||||
template<typename... Args, std::size_t... I>
|
||||
static std::tuple<Args...>
|
||||
readArgsImpl(const RpcArgs& args, std::index_sequence<I...>) {
|
||||
return std::tuple<Args...>{
|
||||
static_cast<Args>(args[I].asInt())...}; // PoC: только int
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
static std::tuple<Args...> readArgs(IpcMessage& msg) {
|
||||
return std::tuple<Args...>{msg.template get<Args>()...};
|
||||
static std::tuple<Args...> readArgs(const RpcArgs& args) {
|
||||
return readArgsImpl<Args...>(
|
||||
args, std::make_index_sequence<sizeof...(Args)>{});
|
||||
}
|
||||
|
||||
std::unordered_map<std::string,
|
||||
std::function<IpcMessage(const IpcMessage&)>>
|
||||
std::function<RpcValue(const RpcArgs&)>>
|
||||
handlers;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
// Простейшее type-erased значение для RPC.
|
||||
// PoC: поддерживаем только int, но интерфейс позволяет в будущем
|
||||
// добавить другие типы (string, bool, и т.д.).
|
||||
|
||||
enum class RpcType {
|
||||
Int,
|
||||
};
|
||||
|
||||
class RpcValue {
|
||||
public:
|
||||
RpcValue() : type_(RpcType::Int), i_(0) {}
|
||||
|
||||
static RpcValue fromInt(int v) {
|
||||
RpcValue r;
|
||||
r.type_ = RpcType::Int;
|
||||
r.i_ = v;
|
||||
return r;
|
||||
}
|
||||
|
||||
RpcType type() const { return type_; }
|
||||
|
||||
int asInt() const {
|
||||
// PoC: единственный поддерживаемый тип.
|
||||
return i_;
|
||||
}
|
||||
|
||||
private:
|
||||
RpcType type_;
|
||||
int i_;
|
||||
};
|
||||
|
||||
using RpcArgs = std::vector<RpcValue>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user