Compare commits

..

2 Commits

Author SHA1 Message Date
Сергей Маринкевич f81ac5842c refactor IPC-dependency out of Proxy 2 weeks ago
Сергей Маринкевич 6cc64fa6a3 remove RpcSerializer 2 weeks ago

@ -30,8 +30,7 @@ project/
│   │   │   └── ProxyMarshaller.h │   │   │   └── ProxyMarshaller.h
│   │   └── rpc │   │   └── rpc
│   │   ├── rpc_export.h │   │   ├── rpc_export.h
│   │   ├── RpcInvoker.h │   │   └── RpcInvoker.h
│   │   └── RpcSerializer.h
│   ├── MyService.cpp │   ├── MyService.cpp
│   ├── MyService.h │   ├── MyService.h
│   └── server.cpp │   └── server.cpp

@ -1,27 +1,42 @@
#pragma once #pragma once
#include <ipc/IpcChannel.h> #include <ipc/IpcChannel.h>
#include <rpc/RpcValue.h>
class ProxyMarshaller { class ProxyMarshaller {
public: public:
explicit ProxyMarshaller(IpcChannel& ch) : channel(ch) {} explicit ProxyMarshaller(IpcChannel& ch) : channel(ch) {}
template<typename Ret, typename... Args> // Базовый type-erased вызов: принимает вектор RpcValue и возвращает RpcValue.
Ret call(const std::string& method, const Args&... args) { RpcValue call(const std::string& method, const RpcArgs& args) {
IpcMessage msg; IpcMessage msg;
// имя метода // имя метода
msg.add(method); msg.add(method);
// аргументы // аргументы (PoC: только int)
(msg.add(args), ...); for (const auto& a : args) {
msg.add(a.asInt());
}
// отправить // отправить
channel.send(msg); channel.send(msg);
// получить ответ // получить ответ
IpcMessage resp = channel.receive(); 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: private:

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <ipc/IpcMessage.h> #include <rpc/RpcValue.h>
#include <functional> #include <functional>
#include <string> #include <string>
@ -14,50 +14,42 @@ public:
const std::string& name, const std::string& name,
Ret (Obj::*method)(Args...)) { Ret (Obj::*method)(Args...)) {
handlers[name] = handlers[name] =
[instance, method](const IpcMessage& req) -> IpcMessage { [instance, method](const RpcArgs& args) -> RpcValue {
IpcMessage msg = req; auto tuple = readArgs<Args...>(args);
Ret result = std::apply(
method, std::tuple_cat(std::make_tuple(instance), tuple));
// пропустить имя метода // PoC: считаем, что Ret == int.
(void)msg.template get<std::string>(); return RpcValue::fromInt(result);
// читать аргументы и вызвать метод
return callMethod<Ret, Obj, Args...>(instance, method, msg);
}; };
} }
IpcMessage dispatch(const IpcMessage& request) { RpcValue dispatch(const std::string& method, const RpcArgs& args) const {
IpcMessage tmp = request;
const std::string method = tmp.get<std::string>();
auto it = handlers.find(method); auto it = handlers.find(method);
if (it == handlers.end()) { if (it == handlers.end()) {
return IpcMessage{}; // PoC: в случае ошибки возвращаем 0.
return RpcValue::fromInt(0);
} }
return it->second(request); return it->second(args);
} }
private: private:
template<typename Ret, typename Obj, typename... Args> template<typename... Args, std::size_t... I>
static IpcMessage callMethod(Obj* obj, static std::tuple<Args...>
Ret (Obj::*method)(Args...), readArgsImpl(const RpcArgs& args, std::index_sequence<I...>) {
IpcMessage& msg) { return std::tuple<Args...>{
auto tuple = readArgs<Args...>(msg); static_cast<Args>(args[I].asInt())...}; // PoC: только int
Ret result =
std::apply(method, std::tuple_cat(std::make_tuple(obj), tuple));
IpcMessage out;
out.add(result);
return out;
} }
template<typename... Args> template<typename... Args>
static std::tuple<Args...> readArgs(IpcMessage& msg) { static std::tuple<Args...> readArgs(const RpcArgs& args) {
return std::tuple<Args...>{msg.template get<Args>()...}; return readArgsImpl<Args...>(
args, std::make_index_sequence<sizeof...(Args)>{});
} }
std::unordered_map<std::string, std::unordered_map<std::string,
std::function<IpcMessage(const IpcMessage&)>> std::function<RpcValue(const RpcArgs&)>>
handlers; 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 %} {% 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 %}) { {{ 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 %} {% endfor %}

@ -10,6 +10,23 @@
} }
IpcMessage {{ cls.name }}Skeleton::dispatch(const IpcMessage& req) { 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;
} }

Loading…
Cancel
Save