move marshaller to IPC component
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <ipc/IpcChannel.h>
|
||||
#include <ipc/IpcCodec.h>
|
||||
#include <rpc/RpcValue.h>
|
||||
|
||||
// Маршаллер, который знает, как превратить типизированный RPC-вызов
|
||||
// в IpcMessage и обратно. Живёт в IPC-слое и опирается на IpcChannel
|
||||
// и IpcCodec, но снаружи предъявляет только callTyped<T>(...).
|
||||
class IpcMarshaller {
|
||||
public:
|
||||
explicit IpcMarshaller(IpcChannel& ch)
|
||||
: channel(ch) {}
|
||||
|
||||
// Базовый type-erased вызов: принимает вектор RpcValue и возвращает RpcValue.
|
||||
RpcValue call(const std::string& method, const RpcArgs& args) {
|
||||
// упаковать запрос в IpcMessage
|
||||
IpcMessage msg = IpcCodec::encodeRequest(method, args);
|
||||
|
||||
// отправить
|
||||
channel.send(msg);
|
||||
|
||||
// получить ответ и распаковать
|
||||
IpcMessage resp = channel.receive();
|
||||
return IpcCodec::decodeResponse(resp);
|
||||
}
|
||||
|
||||
// Типизированный хелпер: контрактом является только наличие этого метода.
|
||||
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:
|
||||
IpcChannel& channel;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user