目的

最近由于摩尔定律已经不太适用,随着大数据、计算量不断增加,导致单机处理能力不能满足需求,所以需要分布式计算,这就需要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编译就可以使用了。

11-16 18:06