Git Product home page Git Product logo

asio_pbrpc's Introduction

Asio Protobuf RPC

A C++ RPC Library implementation with Protobuf and Boost Asio

Include Blocking Client, Async Client and Async Future Client

Features

Blocking Client

  • Use a SyncSend and SyncReceive, hang on asio write_some and read_some

Async Client

  • Use a AsyncSend and AsyncReceive, implemented by asio async_write_some and async_read_some

Async Future Client

  • Will get a future after calling asio async_write_some and async_read_some, wait until communication finished

RPC Server

  • An Async Server implemented by asio, support multiple services

  • A message include three parts: a message length, a method id and a protobuf message

Example

Client

  • create a RPC client
boost::asio::io_service ios;
Executor executor;
FutureRPCClient client(ios, executor);
  • connect to server, async is also supported
if (!client.SyncConnect("127.0.0.1", 6666)) {
  return -1;
}
  • start workers
std::thread t([&ios] {
  boost::asio::io_service::work work(ios);
  ios.run();
});
  • make a RPC call
OneService::Stub one_stub(&client);
EchoRequest echo_request;
echo_request.set_message("one echo from future client");
EchoResponse echo_response;
ClientRPCController rpc_controller;
one_stub.Echo(&rpc_controller, &echo_request, &echo_response, nullptr);
  • wait and check return state
client.Wait();
if (rpc_controller.Failed()) {
  return -1;
}
  • release
ios.stop();
t.join();

Server

  • extern google protobuf service
class OneServiceImpl : public asio_pbrpc::OneService {
 public:
  OneServiceImpl() = default;

  void Echo(::google::protobuf::RpcController* controller,
      const ::asio_pbrpc::EchoRequest* request,
      ::asio_pbrpc::EchoResponse* response,
      ::google::protobuf::Closure* done) override {
    std::cout << "one service received echo message: " << request->message() << std::endl;
    response->set_response(request->message());
    done->Run();
  }

  void Discard(::google::protobuf::RpcController* controller,
      const ::asio_pbrpc::DiscardRequest* request,
      ::asio_pbrpc::DiscardResponse* response,
      ::google::protobuf::Closure* done) override {
    std::cout << "one service received discard message: " << request->message() << std::endl;
    done->Run();
  }
};
  • create a RPC server
RPCServer server(6666);
  • register multiple services
server.RegisterService(std::make_shared<OneServiceImpl>());
server.RegisterService(std::make_shared<AnotherServiceImpl>());
  • start server
server.Start();
  • when finished, you can stop the server
server.Stop();

Dependings

  • cmake

  • boost

  • protobuf

================================ by Xiaojie Chen ([email protected])

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.