|
|
|
|
@ -1,9 +1,8 @@
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "RpcSerializer.h"
|
|
|
|
|
#include "IpcMessage.h"
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
@ -16,21 +15,19 @@ public:
|
|
|
|
|
Ret (Obj::*method)(Args...)) {
|
|
|
|
|
handlers[name] =
|
|
|
|
|
[instance, method](const std::string& req) -> std::string {
|
|
|
|
|
std::istringstream in(req);
|
|
|
|
|
IpcMessageReader reader(req);
|
|
|
|
|
|
|
|
|
|
// пропустить имя метода
|
|
|
|
|
std::string skip;
|
|
|
|
|
in >> skip;
|
|
|
|
|
(void)reader.template read<std::string>();
|
|
|
|
|
|
|
|
|
|
// читать аргументы и вызвать метод
|
|
|
|
|
return callMethod<Ret, Obj, Args...>(instance, method, in);
|
|
|
|
|
return callMethod<Ret, Obj, Args...>(instance, method, reader);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string dispatch(const std::string& request) {
|
|
|
|
|
std::istringstream in(request);
|
|
|
|
|
std::string method;
|
|
|
|
|
in >> method;
|
|
|
|
|
IpcMessageReader reader(request);
|
|
|
|
|
const std::string method = reader.read<std::string>();
|
|
|
|
|
|
|
|
|
|
auto it = handlers.find(method);
|
|
|
|
|
if (it == handlers.end()) {
|
|
|
|
|
@ -44,19 +41,19 @@ private:
|
|
|
|
|
template<typename Ret, typename Obj, typename... Args>
|
|
|
|
|
static std::string callMethod(Obj* obj,
|
|
|
|
|
Ret (Obj::*method)(Args...),
|
|
|
|
|
std::istringstream& in) {
|
|
|
|
|
auto tuple = readArgs<Args...>(in);
|
|
|
|
|
IpcMessageReader& reader) {
|
|
|
|
|
auto tuple = readArgs<Args...>(reader);
|
|
|
|
|
Ret result =
|
|
|
|
|
std::apply(method, std::tuple_cat(std::make_tuple(obj), tuple));
|
|
|
|
|
|
|
|
|
|
std::ostringstream out;
|
|
|
|
|
RpcSerializer::write(out, result);
|
|
|
|
|
IpcMessageWriter out;
|
|
|
|
|
out.add(result);
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
|
static std::tuple<Args...> readArgs(std::istringstream& in) {
|
|
|
|
|
return std::tuple<Args...>{RpcSerializer::read<Args>(in)...};
|
|
|
|
|
static std::tuple<Args...> readArgs(IpcMessageReader& reader) {
|
|
|
|
|
return std::tuple<Args...>{reader.template read<Args>()...};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unordered_map<std::string,
|
|
|
|
|
|