基于zmq RPC简单C++实现

simple-rpc

依赖

zmq

编译

mkdir build
cd build
cmake ..
make<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span>

复制

基本类型的序列化和反序列化

#include "serializer.h"
#include &lt;iostream&gt;

//自定义类型需要重载操作符&gt;&gt;和&lt;&lt;
struct foo {
    int n;
    std::string str;
    foo() {}
    foo(int a, std::string s) 
        : n(a), str(s) {}
    friend Serializer&amp; operator&gt;&gt;(Serializer&amp; in, foo&amp; f)
    {
        in &gt;&gt; f.n &gt;&gt; f.str;
        return in;
    }
    friend Serializer&amp; operator&lt;&lt;(Serializer&amp; out, foo&amp; f)
    {
        out &lt;&lt; f.n &lt;&lt; f.str;
        return out;
    }
};

// 测试原生类型的序列化和反序列化
void test1()
{
    int a = 100;
    std::string c = "hello";
    Serializer sr;
    sr &lt;&lt; a;  //序列化
    sr &lt;&lt; c;

    int b = 0;
    std::string d;
    sr &gt;&gt; b; //反序列化
    sr &gt;&gt; d;
    std::cout &lt;&lt; "b=" &lt;&lt; b &lt;&lt; ", d=" &lt;&lt; d &lt;&lt; std::endl;
}

//测试自定义类型的序列化和反序列化
void test2()
{
    foo a(23, "evenleo");
    Serializer sr;
    sr &lt;&lt; a;

    foo b;
    sr &gt;&gt; b;
    std::cout &lt;&lt; "b.n=" &lt;&lt; b.n &lt;&lt; ", b.str=" &lt;&lt; b.str &lt;&lt; std::endl;
}

int main(int argc, char** argv)
{
    test1();
    test2();
    return 0;
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span>

复制

输出结果:

b=100, d=hello
b.n=23, b.str=evenleo<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span>

复制

RPC

rpc_server

#include &lt;string&gt;
#include &lt;iostream&gt;
#include "rpc.h"

// 类成员函数
struct foo {
int add(int a, int b) { return a + b; }
};

// 普通函数
std::string Strcat(std::string s1, int n) 
{
    return s1 + std::to_string(n);
}

int main()
{
rpc server;
server.as_server(5555);
foo f;
server.regist("add", &amp;foo::add, &amp;f);  //注册类成员函数
server.regist("Strcat", Strcat);      //注册普通函数
server.run();
return 0;
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span>

复制

rpc_client

#include &lt;string&gt;
#include &lt;iostream&gt;
#include "rpc.h"

int main()
{
rpc client;
client.as_client("127.0.0.1", 5555);
std::string str = client.call&lt;std::string&gt;("Strcat", "even", 24).value();
std::cout &lt;&lt; "str=" &lt;&lt; str &lt;&lt; std::endl;
int result = client.call&lt;int&gt;("add", 3, 4).value();
std::cout &lt;&lt; "result=" &lt;&lt; result &lt;&lt; std::endl;
return 0;
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span>

复制

需要启动rpc_server,然后启动rpc_client,请求Strcat和add返回结果:

str=even24
result=7

原文链接 https://cloud.tencent.com/developer/article/1699189

posted @ 2024-02-19 11:02  MasonLee  阅读(27)  评论(0编辑  收藏  举报