Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Uring based Asynchronous Rpc in Modern C++♦️

License

NotificationsYou must be signed in to change notification settings

deepgrace/urpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Uring based Asynchronous Rpc

Overview

An implementation of theping / pong service.

protobuf message definition:

syntax="proto3";packagepb;messagerequest{optionalbytescommand=1;}messageresponse{optionalbytesresults=1;}serviceservice{rpcexecute(request)returns (response);}optioncc_generic_services=true;

client side implementation:

#include<thread>#include<iostream>#include<urpc.hpp>#include<ping.pb.h>namespacenet= unp;namespacegp= google::protobuf;structtask{    urpc::controller controller;    pb::request request;    pb::response response;    urpc::Closure* done;};classclient{public:client(net::io_uring_context& ioc,const std::string& host,const std::string& port) : ioc(ioc), host(host), port(port)    {        channel =newurpc::channel(ioc);        service =newpb::service::Stub(channel, pb::service::STUB_OWNS_CHANNEL);    }voidping()    {auto t = std::make_shared<task>();auto& controller = t->controller;        controller.host(host);        controller.port(port);        controller.timeout(80);        t->request.set_command("ping");        t->done =gp::NewCallback(this, &client::done, t);        service->execute(&t->controller, &t->request, &t->response, t->done);    }voiddone(std::shared_ptr<task> t)    {auto& controller = t->controller;if (controller.Failed())            std::cerr <<"ErrorCode:" << controller.ErrorCode() <<" ErrorText:" << controller.ErrorText() << std::endl;        std::cout << t->response.DebugString();    }~client()    {delete service;    }private:    net::io_uring_context& ioc;    urpc::channel* channel;    pb::service* service;    std::string host;    std::string port;};intmain(int argc,char* argv[]){if (argc !=3)    {        std::cout <<"Usage:" << argv[0] <<" <host> <port>" << std::endl;return1;    }    std::stringhost(argv[1]);    std::stringport(argv[2]);    net::io_uring_context ioc;    net::inplace_stop_source source;    clientc(ioc, host, port);    std::threadt([&]{ ioc.run(source.get_token()); });constexprsize_t size =100;char buff[size];while (fgets(buff, size, stdin))           c.ping();    t.join();return0;}

server side implementation:

#include<iostream>#include<urpc.hpp>#include<ping.pb.h>namespacenet= unp;namespacegp= google::protobuf;voiddone(){    std::cout <<"got called" << std::endl;}classservice :publicpb::service{public:service()    {    }voidexecute(gp::RpcController* controller,const pb::request* request, pb::response* response, gp::Closure* done)    {        std::cout << request->DebugString();if (request->command() !="ping")                    controller->SetFailed("unknown command");else            response->set_results("pong");        done->Run();    }~service()    {    }};intmain(int argc,char* argv[]){if (argc !=3)    {        std::cout <<"Usage:" << argv[0] <<" <host> <port>" << std::endl;return1;    }    std::stringhost(argv[1]);    std::stringport(argv[2]);    net::io_uring_context ioc;    net::inplace_stop_source source;    service s;    urpc::serverserver(ioc, host, port);        server.register_service(&s,gp::NewPermanentCallback(&done));    server.run();    ioc.run(source.get_token());return0;}

Introduction

urpc is aRemote Procedure Call (RPC) library, which is header-only, extensible and modern C++ oriented.
It's built on top off theunp and protobuf, it's based on theProactor design pattern with performance in mind.
urpc enables you to do network programming with tcp protocol in a straightforward, asynchronous and OOP manner.

urpc provides the following features:

  • timeout The upper limit of the total time for the call to time out between a RPC request and response
  • callback The callable to be invoked immediately after a RPC request or response has been accepted and processed
  • controller A way to manipulate settings specific to the RPC implementation and to find out about RPC-level errors

Prerequsites

unp
protobuf

Compiler requirements

The library relies on a C++20 compiler and standard library

More specifically, urpc requires a compiler/standard library supporting the following C++20 features (non-exhaustively):

  • concepts
  • lambda templates
  • All the C++20 type traits from the <type_traits> header

Building

urpc is header-only. To use it just add the necessary#include line to your source files, like this:

#include<urpc.hpp>

git clone https://github.com/deepgrace/unp.git and place it with urpc under the same directory.
To build the example with cmake,cd to the root of the project and setup the build directory:

mkdir buildcd buildcmake ..

Make and install the executables:

make -j4make install

The executables are now located at thebin directory of the root of the project.
The example can also be built with the scriptbuild.sh, just run it, the executables will be put at the/tmp directory.

Full example

Please seeexample.

License

urpc is licensed asBoost Software License 1.0.

About

Uring based Asynchronous Rpc in Modern C++♦️

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp