Coder Social home page Coder Social logo

javeme / brpc Goto Github PK

View Code? Open in Web Editor NEW
5.0 5.0 2.0 21.25 MB

a c++ rpc lib

C 28.17% C++ 55.25% Batchfile 0.08% HTML 4.68% JavaScript 2.96% Perl 0.14% Makefile 0.07% Python 0.67% Java 1.87% Vim Script 1.93% Shell 0.01% Objective-C 4.17%

brpc's Introduction

brpc lib

a rpc lib for c++


examples:

example 1. hello service:

  • 1.add class MyRpcService as a service provider:
class MyRpcService : public RpcService
{
public:
	MyRpcService(cstring name) : RpcService(name)
	{
		//register functions to provide service
		as(MyRpcService::hello);
		as(MyRpcService::sum);
	}
	
public:
	std::string hello(const char* who)
	{
		std::string str = "hello, ";
		str += who;
		return str;
	}
	
	double sum(int a, double b)
	{
		return a + b;
	}
};
  • 2.add class MyRpcApi as client stub:
class MyRpcApi : public RpcApi
{
public:
	MyRpcApi(cstring url, cstring name, cstring password) : 
		checker(name, password),
		dispatcher("callback"),
		client(url, &dispatcher, &checker, "text/json", 1000*10),
		RpcApi("serviceName4Test")
	{
		ObjectList loginArgs;
		loginArgs.addValue(name);
		loginArgs.addValue(password);
		init(&client, loginArgs);
	}

	virtual ~MyRpcApi()
	{
		try{
			logout();
		}catch (Exception& e) {
			ErrorHandler::handle(e);
		}
	}

public:
	std::string hello(const char* who)
	{
		ObjectList args;
		args.addValue(who);
		return call<std::string>("hello", args);
	}

	double sum(int a, double b)
	{
		ObjectList args;
		args.addValue(a);
		args.addValue(b);
		return call<double>("sum", args);
	}
};
  • 3.start server:
void mian()
{
	MyRpcService dispatcher("serviceName4Test");
	DefaultAuthChecker checker("test", "123456");

	RpcServer server("http://0.0.0.0", &checker, "text/json");

	printf("starting server...\n");
	server.start(&dispatcher);
	server.wait();
}
  • 4.start client:
void mian()
{
	cstring url = "http://127.0.0.1";
	cstring name = "test", password = "123456";
	MyRpcApi myApi(url, name, password);

	std::string result = myApi.hello("brpc");
	printf("hello() returned: %s\n", result.c_str());

	double f = myApi.sum(2, 3.14);
	printf("sum() returned: %lf\n", f);
}

example 2. test brpc with bat:

  • 1.run server:

    execute bin/brpc-server.bat

  • 2.run client:

    execute bin/brpc-client*.bat

    or

    execute brpc.exe with parameters, such asbrpc.exe client nova.sum 3 3.14


    *all the client bats for test:*
      brpc-client echo.bat
      brpc-client echo - test n .bat
      brpc-client help.bat
      brpc-client ping.bat
      brpc-client sum.bat
      brpc-client play.bat
      brpc-client print.bat
      brpc-client exec output.bat
      brpc-client exec redirect
      brpc-client exec telnet.bat
      brpc-client exec telnet.bat
      brpc-client sub-sub-service-print.bat
      brpc-api-test.bat
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.