febird.rpc echo 代码

// 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;
}

 

你可能感兴趣的:(echo)