disaggregate templates

This commit is contained in:
Сергей Маринкевич
2025-12-02 16:02:19 +07:00
parent adbb2c374b
commit 7ce411b643
11 changed files with 289 additions and 69 deletions
+40 -1
View File
@@ -1,9 +1,48 @@
#include "MyService.proxy.h"
#include "rpc/RpcChannel.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <string>
class RpcChannelFifoClient : public RpcChannel {
public:
RpcChannelFifoClient(const char* inPipe, const char* outPipe) {
fdOut = open(inPipe, O_WRONLY);
fdIn = open(outPipe, O_RDONLY);
}
void send(const std::string& data) override {
::write(fdOut, data.c_str(), data.size());
::write(fdOut, "\n", 1);
}
std::string receive() override {
char buf[4096];
int n = ::read(fdIn, buf, sizeof(buf) - 1);
if (n <= 0) {
return {};
}
buf[n] = 0;
return std::string(buf);
}
private:
int fdIn{};
int fdOut{};
};
int main() {
MyServiceProxy proxy("/tmp/rpc_in", "/tmp/rpc_out");
mkfifo("/tmp/rpc_in", 0666);
mkfifo("/tmp/rpc_out", 0666);
RpcChannelFifoClient ch("/tmp/rpc_in", "/tmp/rpc_out");
MyServiceProxy proxy(ch);
int r = proxy.add(7, 8);
std::cout << "RESULT: " << r << std::endl;
}
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include <string>
#include <vector>
class RpcChannel {
public:
virtual ~RpcChannel() = default;
virtual void send(const std::string& data) = 0;
virtual std::string receive() = 0;
};
+35
View File
@@ -0,0 +1,35 @@
#include "RpcChannel.h"
#include <fcntl.h>
#include <unistd.h>
#include <string>
class RpcChannelFifo : public RpcChannel {
public:
RpcChannelFifo(const char* inPipe, const char* outPipe) {
fdOut = open(inPipe, O_WRONLY);
fdIn = open(outPipe, O_RDONLY);
}
void send(const std::string& data) override {
::write(fdOut, data.c_str(), data.size());
::write(fdOut, "\n", 1);
}
std::string receive() override {
char buf[4096];
int n = ::read(fdIn, buf, sizeof(buf) - 1);
if (n <= 0) {
return {};
}
buf[n] = 0;
return std::string(buf);
}
private:
int fdIn{};
int fdOut{};
};
+37
View File
@@ -0,0 +1,37 @@
#pragma once
#include "RpcChannel.h"
#include "RpcSerializer.h"
#include <sstream>
#include <string>
class RpcClient {
public:
explicit RpcClient(RpcChannel& ch) : channel(ch) {}
template<typename Ret, typename... Args>
Ret call(const std::string& method, const Args&... args) {
std::ostringstream out;
// имя метода
RpcSerializer::write(out, method);
// аргументы
(RpcSerializer::write(out, args), ...);
// отправить
channel.send(out.str());
// получить ответ
std::string resp = channel.receive();
std::istringstream in(resp);
return RpcSerializer::read<Ret>(in);
}
private:
RpcChannel& channel;
};
+67
View File
@@ -0,0 +1,67 @@
#pragma once
#include "RpcSerializer.h"
#include <functional>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
class RpcInvoker {
public:
template<typename Obj, typename Ret, typename... Args>
void registerMethod(Obj* instance,
const std::string& name,
Ret (Obj::*method)(Args...)) {
handlers[name] =
[instance, method](const std::string& req) -> std::string {
std::istringstream in(req);
// пропустить имя метода
std::string skip;
in >> skip;
// читать аргументы и вызвать метод
return callMethod<Ret, Obj, Args...>(instance, method, in);
};
}
std::string dispatch(const std::string& request) {
std::istringstream in(request);
std::string method;
in >> method;
auto it = handlers.find(method);
if (it == handlers.end()) {
return "ERR";
}
return it->second(request);
}
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);
Ret result =
std::apply(method, std::tuple_cat(std::make_tuple(obj), tuple));
std::ostringstream out;
RpcSerializer::write(out, result);
return out.str();
}
template<typename... Args>
static std::tuple<Args...> readArgs(std::istringstream& in) {
return std::tuple<Args...>{RpcSerializer::read<Args>(in)...};
}
std::unordered_map<std::string,
std::function<std::string(const std::string&)>>
handlers;
};
+21
View File
@@ -0,0 +1,21 @@
#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;
}
};
+39 -12
View File
@@ -1,31 +1,58 @@
#include "MyService.h"
#include "MyService.skeleton.h"
#include "rpc/RpcChannel.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <string>
class RpcChannelFifoServer : public RpcChannel {
public:
RpcChannelFifoServer(const char* inPipe, const char* outPipe) {
fdIn = open(inPipe, O_RDONLY);
fdOut = open(outPipe, O_WRONLY);
}
void send(const std::string& data) override {
::write(fdOut, data.c_str(), data.size());
::write(fdOut, "\n", 1);
}
std::string receive() override {
char buf[4096];
int n = ::read(fdIn, buf, sizeof(buf) - 1);
if (n <= 0) {
return {};
}
buf[n] = 0;
return std::string(buf);
}
private:
int fdIn{};
int fdOut{};
};
int main() {
mkfifo("/tmp/rpc_in", 0666);
mkfifo("/tmp/rpc_out", 0666);
int fdIn = open("/tmp/rpc_in", O_RDONLY);
int fdOut = open("/tmp/rpc_out", O_WRONLY);
RpcChannelFifoServer ch("/tmp/rpc_in", "/tmp/rpc_out");
MyService realObj;
MyServiceSkeleton skeleton(realObj);
char buf[256];
while (true) {
int n = read(fdIn, buf, sizeof(buf)-1);
if (n <= 0) break;
buf[n] = 0;
std::string req(buf);
std::string resp = skeleton.handleRequest(req);
write(fdOut, resp.c_str(), resp.size());
std::string req = ch.receive();
if (req.empty()) {
break;
}
std::string resp = skeleton.dispatch(req);
ch.send(resp);
}
}