base wo autocode
commit
64a3e637d5
@ -0,0 +1,2 @@
|
||||
*.swp
|
||||
build/
|
||||
@ -0,0 +1,5 @@
|
||||
#include "MyService.h"
|
||||
|
||||
int MyService::add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
class [[annotate("export")]] MyService {
|
||||
public:
|
||||
[[annotate("export")]]
|
||||
int add(int a, int b);
|
||||
};
|
||||
@ -0,0 +1,22 @@
|
||||
#include "MyService.proxy.h"
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sstream>
|
||||
|
||||
MyServiceProxy::MyServiceProxy(const char* pipeIn, const char* pipeOut) {
|
||||
fdIn = open(pipeIn, O_WRONLY);
|
||||
fdOut = open(pipeOut, O_RDONLY);
|
||||
}
|
||||
|
||||
int MyServiceProxy::add(int a, int b) {
|
||||
std::ostringstream out;
|
||||
out << "add " << a << " " << b << "\n";
|
||||
|
||||
write(fdIn, out.str().c_str(), out.str().size());
|
||||
|
||||
char buf[128];
|
||||
int n = read(fdOut, buf, sizeof(buf)-1);
|
||||
buf[n] = 0;
|
||||
|
||||
return std::atoi(buf);
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class MyServiceProxy {
|
||||
public:
|
||||
MyServiceProxy(const char* pipeIn, const char* pipeOut);
|
||||
|
||||
int add(int a, int b);
|
||||
|
||||
private:
|
||||
int fdIn; // клиент пишет сюда → сервер читает
|
||||
int fdOut; // сервер пишет сюда → клиент читает
|
||||
};
|
||||
@ -0,0 +1,19 @@
|
||||
#include "MyService.skeleton.h"
|
||||
#include <sstream>
|
||||
|
||||
MyServiceSkeleton::MyServiceSkeleton(MyService& o) : obj(o) {}
|
||||
|
||||
std::string MyServiceSkeleton::handleRequest(const std::string& req) {
|
||||
std::istringstream in(req);
|
||||
std::string method;
|
||||
in >> method;
|
||||
|
||||
if (method == "add") {
|
||||
int a, b;
|
||||
in >> a >> b;
|
||||
int r = obj.add(a, b);
|
||||
return std::to_string(r);
|
||||
}
|
||||
|
||||
return "ERR";
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "MyService.h"
|
||||
#include <string>
|
||||
|
||||
class MyServiceSkeleton {
|
||||
public:
|
||||
MyServiceSkeleton(MyService& obj);
|
||||
|
||||
// обработка одной строки запроса и возврат ответа
|
||||
std::string handleRequest(const std::string& req);
|
||||
|
||||
private:
|
||||
MyService& obj;
|
||||
};
|
||||
@ -0,0 +1,9 @@
|
||||
#include "MyService.proxy.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
MyServiceProxy proxy("/tmp/rpc_in", "/tmp/rpc_out");
|
||||
|
||||
int r = proxy.add(7, 8);
|
||||
std::cout << "RESULT: " << r << std::endl;
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
#include "MyService.h"
|
||||
#include "MyService.skeleton.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue