1. 程式人生 > >C++ RPC(遠端過程呼叫)

C++ RPC(遠端過程呼叫)

目的

最近由於摩爾定律已經不太適用,隨著大資料、計算量不斷增加,導致單機處理能力不能滿足需求,所以需要分散式計算,這就需要RPC(遠端過程呼叫),下面簡單介紹一下這個demo,來自於GitHub上的一個專案

client程式碼

#include <string>
#include <iostream>
#include <ctime>
#include "buttonrpc.hpp"

#ifdef _WIN32
#include <Windows.h>  // use sleep
#else
 #include <unistd.h>
#endif


#define buttont_assert(exp) { \
	if (!(exp)) {\
		std::cout << "ERROR: "; \
		std::cout << "function: " << __FUNCTION__  << ", line: " <<  __LINE__ << std::endl; \
		system("pause"); \
	}\
}\


struct PersonInfo
{
	int age;
	std::string name;
	float height;

	// must implement
	friend Serializer& operator >> (Serializer& in, PersonInfo& d) {
		in >> d.age >> d.name >> d.height;
		return in;
	}
	friend Serializer& operator << (Serializer& out, PersonInfo d) {
		out << d.age << d.name << d.height;
		return out;
	}
};

int main()
{
	buttonrpc client;
	client.as_client("127.0.0.1", 5555);
	client.set_timeout(2000);

	int callcnt = 0;
	while (1){
		std::cout << "current call count: " << ++callcnt << std::endl;

		client.call<void>("foo_1");

		client.call<void>("foo_2", 10);

		int foo3r = client.call<int>("foo_3", 10).val();
		buttont_assert(foo3r == 100);

		int foo4r = client.call<int>("foo_4", 10, "buttonrpc", 100, (float)10.8).val();
		buttont_assert(foo4r == 1000);

		PersonInfo  dd = { 10, "buttonrpc", 170 };
		dd = client.call<PersonInfo>("foo_5", dd, 120).val();
		buttont_assert(dd.age == 20);
		buttont_assert(dd.name == "buttonrpc is good");
		buttont_assert(dd.height == 180);

		int foo6r = client.call<int>("foo_6", 10, "buttonrpc", 100).val();
		buttont_assert(foo6r == 1000);

		buttonrpc::value_t<void> xx = client.call<void>("foo_7", 666);
		buttont_assert(!xx.valid());
#ifdef _WIN32
		Sleep(1000);
#else
        sleep(1);
#endif
	}

	return 0;
}

server程式碼

#include <string>
#include <iostream>
#include "buttonrpc.hpp"


#define buttont_assert(exp) { \
	if (!(exp)) {\
		std::cout << "ERROR: "; \
		std::cout << "function: " << __FUNCTION__  << ", line: " <<  __LINE__ << std::endl; \
		system("pause"); \
	}\
}\


// 測試例子
void foo_1() {
	std::cout << "foo_1()" << std::endl;
}

void foo_2(int arg1) {
	buttont_assert(arg1 == 10);
	std::cout << "foo_2(int arg1)" << std::endl;
}

int foo_3(int arg1) {
	buttont_assert(arg1 == 10);
	std::cout << "foo_3(int arg1)" << std::endl;
	return arg1 * arg1;
}

int foo_4(int arg1, std::string arg2, int arg3, float arg4) {
	buttont_assert(arg1 == 10);
	buttont_assert(arg2 == "buttonrpc");
	buttont_assert(arg3 == 100);
	buttont_assert((arg4 > 10.0) && (arg4 < 11.0));

	std::cout << "foo_4(int arg1, std::string arg2, int arg3, float arg4)" << std::endl;
	return arg1 * arg3;
}

class ClassMem
{
public:
	int bar(int arg1, std::string arg2, int arg3) {
		buttont_assert(arg1 == 10);
		buttont_assert(arg2 == "buttonrpc");
		buttont_assert(arg3 == 100);

		std::cout << "bar(int arg1, std::string arg2, int arg3)" << std::endl;

		return arg1 * arg3;
	}
};

struct PersonInfo
{
	int age;
	std::string name;
	float height;

	// must implement
	friend Serializer& operator >> (Serializer& in, PersonInfo& d) {
		in >> d.age >> d.name >> d.height;
		return in;
	}
	friend Serializer& operator << (Serializer& out, PersonInfo d) {
		out << d.age << d.name << d.height;
		return out;
	}
};

PersonInfo foo_5(PersonInfo d,  int weigth)
{
	buttont_assert(d.age == 10);
	buttont_assert(d.name == "buttonrpc");
	buttont_assert(d.height == 170);

	PersonInfo ret;
	ret.age = d.age + 10;
	ret.name = d.name + " is good";
	ret.height = d.height + 10;

	std::cout << "foo_5(PersonInfo d,  int weigth)" << std::endl;

	return ret;
}

int main()
{
	buttonrpc server;
	server.as_server(5555);

	server.bind("foo_1", foo_1);
	server.bind("foo_2", foo_2);
	server.bind("foo_3", std::function<int(int)>(foo_3));
	server.bind("foo_4", foo_4);
	server.bind("foo_5", foo_5);

	ClassMem s;
	server.bind("foo_6", &ClassMem::bar, &s);

	std::cout << "run rpc server on: " << 5555 << std::endl;
	server.run();

	return 0;
}

下載原始碼後,適用vs2015編譯就可以使用了。