Giter Club home page Giter Club logo

rpc's Introduction

rpc

A header only and out of the box tcp rpc framework written in C++.

Introduction

Protocol

Protocol is a template specified on compile time.
Server,Client and Session are all based on it.

using SizeType = unsigned short;
using CmdType = unsigned short;
using Protocol = rpc::Protocol<SizeType, CmdType>;

notice: SizeType should be unsigned.If not,there will be a assert on debug mode and the program will be abort.

Message

All message communicates on this framework should be wrapped by rpc::Protocol::MessageWrapper.
Otherwise the compile will be failed.

template <typename Protocol::Cmd cmd, typename MessageType,
                  class MessageSizeGetter = DefaultMessageSizeGetter<MessageType, typename Protocol::Size>> //optional

Wrap your class with Cmd,Struct and SizeGetter(optional)

struct Foo1
{
    int i;
    double d;
};

// Use sizeof() to calculate wrapped struct by default if you do not specify SizeGetter.
using MsgFoo1 = Protocol::MessageWrapper<0x01, Foo1>;

struct Foo2
{
    int i;
    unsigned short textSize;
    char text[120];

    struct SizeGetter
    {
        unsigned short operator()(const Foo2 &foo) const
        {
            return sizeof(foo.i) + sizeof(foo.textSize) + foo.textSize;
        }
    };
};

// If the size of wrapped struct is variable,you could specify a SizeGetter to calculate the message.
using MsgFoo2 = Protocol::MessageWrapper<0x02, Foo2, Foo2::SizeGetter>;

Server

Based on asio,rpc::Server has a nice speed.

const char *const IP = "127.0.0.1";
unsigned short port = 8890;

// specify listen address.
Server server(IP, port);

// could only bind message callback by struct wrapped by rpc::Protocol::MessageWrapper,
// that's why you need to specfify message cmd when using rpc::Protocol::MessageWrapper.
server.Bind<MsgFoo1>(
    [](const MsgFoo1 &req, Session &session) -> void
    {
        std::cout << req->i << std::endl;
        std::cout << req->d << std::endl;

        // every session have a buffer which is large to max protocol size.
        // also it is concurrent safe.
        MsgFoo2 &ret2 = session.WriteBuffer();
        ret2->i = 3;
        static const char v[] = "Hello";
        strcpy(ret2->text, v);
        ret2->textSize = strlen(v);

        // only class with Bytes() convert could be sent.
        // all classes/struct wrapped by MessageWrapper has converter by default.
        session.Write(ret2);
    });
    // this will block subsequent code.
    server.Start();

Client

const char *const IP = "127.0.0.1";
unsigned short port = 8890;

// construct with server address.
Client client(IP, port);

// could only bind message callback by struct wrapped by rpc::Protocol::MessageWrapper,
// that's why you need to specfify message cmd when using rpc::Protocol::MessageWrapper.
client.Bind<MsgFoo2>([](const MsgFoo2 &req, Session &session) -> void 
{
    std::string_view sv(req->text, req->textSize);
    std::cout << sv << std::endl; 
});

// this will also block.So we use thread here.
std::thread([&]() -> void{ client.Start(); }).detach();

// WriteBuffer could be cast to any type wrapped by rpc::Protocol::MessageWrapper.
MsgFoo1 &msg = client.WriteBuffer();
msg->i = 1;
msg->d = 2;

// only class with Bytes() convert could be sent.
// all classes/struct wrapped by MessageWrapper has converter by default.
client.Send(msg);

Full Usage

#include "include/rpc/server.h"
#include "include/rpc/session.h"
#include "include/rpc/client.h"
#include <iostream>
#include <string>

// All relevant classes is implemented with rpc::Protocol.
using Protocol = rpc::Protocol<unsigned short, unsigned short>;
using Server = rpc::Server<Protocol>;
using Client = rpc::Client<Protocol>;
using Session = rpc::Session<Protocol>;

struct Foo1
{
    int i;
    double d;
};

struct Foo2
{
    int i;
    unsigned short textSize;
    char text[120];

    struct SizeGetter
    {
        unsigned short operator()(const Foo2 &foo) const
        {
            return sizeof(foo.i) + sizeof(foo.textSize) + foo.textSize;
        }
    };
};

// wrap your own class with Protocol::MessageWrapper.

// Use sizeof() to calculate wrapped struct by default.
using MsgFoo1 = Protocol::MessageWrapper<0x01, Foo1>;

// If the size of wrapped struct is variable,you could specify a SizeGetter to calculate the message.
using MsgFoo2 = Protocol::MessageWrapper<0x02, Foo2, Foo2::SizeGetter>;

int main(int argc, char **argv)
{
    const char *const IP = "127.0.0.1";
    unsigned short port = 8890;

    Server server(IP, port);
    server.SetErrorHandler([](const asio::error_code &ec) -> void
                           { std::cout << ec << " " << ec.message() << std::endl; });
    server.Bind<MsgFoo1>(
        [](const MsgFoo1 &req, Session &session) -> void
        {
            std::cout << req->i << std::endl;
            std::cout << req->d << std::endl;

            // every session have a buffer which is large to max protocol size.
            // also it is concurrent safe.
            MsgFoo2 &ret2 = session.WriteBuffer();
            ret2->i = 3;
            static const char v[] = "Hello";
            strcpy(ret2->text, v);
            ret2->textSize = strlen(v);

            // only class with Bytes() convert could be sent.
            // all classes/struct wrapped by MessageWrapper has converter by default.
            session.Write(ret2);
        });
    std::thread([&]() -> void
                { server.Start(); })
        .detach();

    std::this_thread::sleep_for(std::chrono::seconds(1));
    Client client(IP, port);
    client.SetErrorHandler([](const asio::error_code &ec) -> void
                           { std::cout << ec << " " << ec.message() << std::endl; });
    client.Bind<MsgFoo2>([](const MsgFoo2 &req, Session &session) -> void
    {
            std::string_view sv(req->text, req->textSize);
            std::cout << sv << std::endl; 
    });

    std::thread([&]() -> void{ client.Start(); }).detach();
    MsgFoo1 &msg = client.WriteBuffer();
    msg->i = 1;
    msg->d = 2;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    client.Send(msg);
    std::this_thread::sleep_for(std::chrono::seconds(100));
}

rpc's People

Contributors

alpsmonaco avatar

Stargazers

 avatar

Watchers

 avatar

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.