windows下编译和使用buttonrpc

1 下载 libzmq  GitHub - zeromq/libzmq: ZeroMQ core engine in C++, implements ZMTP/3.1

2 下载 cppzmq  https://github.com/zeromq/cppzmq

3 使用cmake 编译 libzmq 生成dll和lib

 

 使用时项目包含如下和来源

注意:zmq.hpp 编译会报错,将报错注释掉即可 (还没研究怎么不报错)

 //ZMQ_CPP11_DEPRECATED("from 4.7.0, use `set` taking option from zmq::sockopt")

rpcserver

// rpcdemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#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() {

}

void foo_2(int arg1) {
	buttont_assert(arg1 == 10);
}

int foo_3(int arg1) {
	buttont_assert(arg1 == 10);
	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));
	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);
		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;
	cout << "foo_5" << 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;
}

rpcclient

#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(10);
#else
		sleep(1);
#endif
	}

	return 0;
}

demo下载地址 :https://download.csdn.net/download/weixin_44602405/86953964

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值