febird.rpc echo 代码

本文介绍了一个使用 FeBird RPC 框架实现的简单客户端-服务器示例。该示例包括一个回显服务,服务器接收消息并返回带有前缀的相同消息。客户端连续发送输入直到文件结束。
// echo.h

class Echo : public GlobaleScope
{
public:
	BEGIN_RPC_ADD_MF(Echo)
		RPC_ADD_MF(echo)
	END_RPC_ADD_MF()

	// 3rd macro param is ';' means non-pure-virtual
	RPC_DECLARE_MF_EX(echo, (const std::string& msg, std::string* y), ;)
};
RPC_TYPEDEF_PTR(Echo);

 

// echo_server.cpp
#include <stdio.h>
#include <febird/rpc/server.h>
#include <febird/io/SocketStream.h>
#include <iostream>

using namespace std;
using namespace febird;
using namespace febird::rpc;

#include "../echo.h"

rpc_return_t Echo::echo(const std::string& msg, std::string* y)
{
	cout << "input: " << msg << endl;
	*y = "server: " + msg;
	return 0;
}

int main(int argc, char** argv[])
{

#ifdef _WIN32
	WSADATA information;
	WSAStartup(MAKEWORD(2, 2), &information);
#endif

	try {
		SocketAcceptor acceptor("0.0.0.0:8001");
		rpc_server<PortableDataInput, PortableDataOutput> server(&acceptor);

		// 加入命名的 Servant
		server.add_servant(
			new Echo,
			"echo",
			0 // 0 will not auto create GlobaleScope Object
			);

		server.start();
	}
	catch (const std::exception& exp)
	{
		printf("exception: what=%s\n", exp.what());
	}

#ifdef _WIN32
	WSACleanup();
#endif

	return 0;
}

 

// echo_client.cpp

#include <stdio.h>
#include <febird/rpc/client.h>
#include <febird/io/SocketStream.h>
#include <iostream>

using namespace std;
using namespace febird;
using namespace febird::rpc;

#include "../echo.h"

int main(int argc, char** argv[])
{
#ifdef _WIN32
	WSADATA information;
	WSAStartup(MAKEWORD(2, 2), &information);
#endif

	try {
		auto_ptr<SocketStream> cs(ConnectSocket("127.0.0.1:8001"));
		rpc_client<PortableDataInput, PortableDataOutput> client(cs.get());
		EchoPtr ec;
		client.create(ec, "echo");
		while (!cin.eof())
		{
			string msg, y;
			cin >> msg;
			ec->echo(msg, &y);
			cout << "msg:" << msg << endl;
			cout << "y__:" << y << endl;
		}
	}
	catch (const std::exception& exp)
	{
		printf("exception: what=%s\n", exp.what());
	}

#ifdef _WIN32
	WSACleanup();
#endif

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值