Compare commits
2 Commits
40170d553d
...
f81ac5842c
| Author | SHA1 | Date | |
|---|---|---|---|
| f81ac5842c | |||
| 6cc64fa6a3 |
@@ -30,8 +30,7 @@ project/
|
||||
│ │ │ └── ProxyMarshaller.h
|
||||
│ │ └── rpc
|
||||
│ │ ├── rpc_export.h
|
||||
│ │ ├── RpcInvoker.h
|
||||
│ │ └── RpcSerializer.h
|
||||
│ │ └── RpcInvoker.h
|
||||
│ ├── MyService.cpp
|
||||
│ ├── MyService.h
|
||||
│ └── server.cpp
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
class RpcSerializer {
|
||||
public:
|
||||
template<typename T>
|
||||
static void write(std::ostringstream& out, const T& v) {
|
||||
out << v << ' ';
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T read(std::istringstream& in) {
|
||||
T v;
|
||||
in >> v;
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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>;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
{% for m in cls.methods %}
|
||||
{{ m.return_type }} {{ cls.name }}Proxy::{{ m.name }}({% for a in m.args %}{{ a.type }} {{ a.name }}{% if not loop.last %}, {% endif %}{% endfor %}) {
|
||||
return impl.call<{{ m.return_type }}>("{{ cls.name }}.{{ m.name }}"{% for a in m.args %}, {{ a.name }}{% endfor %});
|
||||
return impl.callTyped<{{ m.return_type }}>("{{ cls.name }}.{{ m.name }}"{% for a in m.args %}, {{ a.name }}{% endfor %});
|
||||
}
|
||||
{% endfor %}
|
||||
|
||||
|
||||
@@ -10,6 +10,23 @@
|
||||
}
|
||||
|
||||
IpcMessage {{ cls.name }}Skeleton::dispatch(const IpcMessage& req) {
|
||||
return invoker.dispatch(req);
|
||||
// Перепаковываем IpcMessage в RpcArgs и вызываем type-erased инвокер.
|
||||
IpcMessage msg = req;
|
||||
|
||||
// имя метода
|
||||
std::string method = msg.get<std::string>();
|
||||
|
||||
// аргументы (PoC: только int, читаем все до конца сообщения)
|
||||
RpcArgs args;
|
||||
while (!msg.empty()) {
|
||||
int v = msg.get<int>();
|
||||
args.emplace_back(RpcValue::fromInt(v));
|
||||
}
|
||||
|
||||
RpcValue result = invoker.dispatch(method, args);
|
||||
|
||||
IpcMessage resp;
|
||||
resp.add(result.asInt()); // PoC: только int
|
||||
return resp;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user