You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include <ipc/IpcConfig.h>
|
|
#include <rpc/RpcValue.h>
|
|
|
|
#include <string>
|
|
|
|
// Кодек, который знает, как упаковать/распаковать RPC-запросы/ответы
|
|
// в/IpcMessage. Живёт в IPC-слое, но опирается на типы RPC-ядра
|
|
// (RpcValue/RpcArgs).
|
|
namespace IpcCodec {
|
|
|
|
// Запрос: имя метода + вектор аргументов.
|
|
inline IpcMessage encodeRequest(const std::string& method,
|
|
const RpcArgs& args) {
|
|
IpcMessage msg;
|
|
|
|
// имя метода
|
|
msg.add(method);
|
|
|
|
// аргументы (PoC: только int)
|
|
for (const auto& a : args) {
|
|
msg.add(a.asInt());
|
|
}
|
|
|
|
return msg;
|
|
}
|
|
|
|
inline void decodeRequest(const IpcMessage& msg,
|
|
std::string& method,
|
|
RpcArgs& args) {
|
|
IpcMessage copy = msg;
|
|
|
|
// имя метода
|
|
method = copy.get<std::string>();
|
|
|
|
// аргументы (PoC: только int, читаем до конца сообщения)
|
|
args.clear();
|
|
while (!copy.empty()) {
|
|
int v = copy.get<int>();
|
|
args.emplace_back(RpcValue::fromInt(v));
|
|
}
|
|
}
|
|
|
|
// Ответ: одно RpcValue (PoC: считаем, что это int).
|
|
inline IpcMessage encodeResponse(const RpcValue& result) {
|
|
IpcMessage msg;
|
|
msg.add(result.asInt()); // PoC: только int
|
|
return msg;
|
|
}
|
|
|
|
inline RpcValue decodeResponse(const IpcMessage& msg) {
|
|
IpcMessage copy = msg;
|
|
int v = copy.get<int>();
|
|
return RpcValue::fromInt(v);
|
|
}
|
|
|
|
} // namespace IpcCodec
|
|
|
|
|
|
|