base wo autocode
commit
ee2fd0ee2e
@ -0,0 +1,2 @@
|
|||||||
|
*.swp
|
||||||
|
build/
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
// MyService.cpp
|
||||||
|
#include "MyService.h"
|
||||||
|
|
||||||
|
int MyService::add(int a, int b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
// MyService.h
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class [[annotate("export")]] MyService {
|
||||||
|
public:
|
||||||
|
[[annotate("export")]]
|
||||||
|
int add(int a, int b);
|
||||||
|
};
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
// MyService.proxy.cpp
|
||||||
|
#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,14 @@
|
|||||||
|
// MyService.proxy.h
|
||||||
|
#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,20 @@
|
|||||||
|
// MyService.skeleton.cpp
|
||||||
|
#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,15 @@
|
|||||||
|
// MyService.skeleton.h
|
||||||
|
#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,10 @@
|
|||||||
|
// client.cpp
|
||||||
|
#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,32 @@
|
|||||||
|
// server.cpp
|
||||||
|
#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