用C++的高级模版特性实现一个不需要IDL的RPC

本文介绍了一个高性能的远程过程调用(RPC)框架实现细节。该框架支持多种RPC接口定义及其实现,包括数值处理和字符串操作等。通过具体示例展示了如何在服务器端实现这些RPC接口,以及客户端如何调用这些服务。

项目地址:http://code.google.com/p/febird

 

目前已经全部完成,并且取得了非常好的效果 。

使用该RPC的简短代码:

//////////////////////////////////////////////////////////////////////////

// sample usage...

// test.h

namespace febird { namespace rpc {

//////////////////////////////////////////////////////////////////////////

// sample usage...

// test.h

 

class SampleRPC_Interface1 : public remote_object

{

public:

    typedef std::vector<var_uint32_t> vint_vec;

 

    BEGIN_RPC_REGISTER_MF(SampleRPC_Interface1)

        RPC_REGISTER_MF(get_val)

        RPC_REGISTER_MF(get_len)

        RPC_REGISTER_MF(squareVec)

        RPC_REGISTER_MF(multiVec)

    END_RPC_REGISTER_MF()

   

RPC_DECLARE_MF ( get_val , ( rpc_in < int > x ))

 

    RPC_DECLARE_MF(get_len, (const std::string& x))

    RPC_DECLARE_MF(squareVec, (vint_vec& x))

    RPC_DECLARE_MF(multiVec, (vint_vec& z, vint_vec& x, vint_vec& y))

 

 

#ifdef RPC_CLIENT_SIDE

    void printVec(const vint_vec& vec);

#endif

};

 

class SampleRPC_Interface2 : public remote_object

{

public:

    BEGIN_RPC_REGISTER_MF(SampleRPC_Interface2)

        RPC_REGISTER_MF(get_val)

        RPC_REGISTER_MF(get_len)

    END_RPC_REGISTER_MF()

   

RPC_DECLARE_MF ( get_val , ( rpc_in < int > x ))

 

    RPC_DECLARE_MF(get_len, (const std::string& x))

};

 

} } // namespace febird::rpc

 

 

// server.cpp

 

// test_rpc_server.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <tlib/rpc/server.h>

#include <tlib/io/SocketStream.h>

#include <iostream>

#include "../test.h"

 

namespace febird { namespace rpc {

 

rpc_return_t SampleRPC_Interface1::get_val(rpc_in<int> x)

{

    std::cout << "SampleRPC_Interface1::get_val(rpc_in<int> x=" << x.get() << ")\n";

    return x.get();

}

rpc_return_t SampleRPC_Interface1::get_len(const std::string& x)

{

    std::cout << "SampleRPC_Interface1::get_len(const std::string& x=\"" << x << "\")\n";

    return x.size();

}

rpc_return_t SampleRPC_Interface1::squareVec(vint_vec& x)

{

    for (vint_vec::iterator i = x.begin(); i != x.end(); ++i)

    {

        (*i).t *= (*i).t;

    }

    return x.size();

}

rpc_return_t SampleRPC_Interface1::multiVec(vint_vec& z, vint_vec& x, vint_vec& y)

{

    z.clear();

    for (int i = 0; i < x.size(); ++i)

    {

        z.push_back(var_uint32_t(x[i].t * y[i].t));

    }

    return 123456;

}

 

rpc_return_t SampleRPC_Interface2::get_val(rpc_in<int> x)

{

    std::cout << BOOST_CURRENT_FUNCTION << "x=" << x.get() << "\n";

    return x.get();

}

rpc_return_t SampleRPC_Interface2::get_len(const std::string& x)

{

    std::cout << BOOST_CURRENT_FUNCTION << "x=" << x << "\n";

    return x.size();

}

 

} } // namespace febird::rpc

 

    using namespace febird;

    using namespace febird::rpc;

    typedef SocketStream stream_t;

    typedef PortableDataInput <BufferedInputStream>   input_t;

    typedef PortableDataOutput<BufferedOutputStream> output_t;

 

int main(int argc, char** argv[])

{

 

#ifdef _WIN32

    WSADATA information;

    WSAStartup(MAKEWORD(2, 2), &information);

#endif

 

    rpc_server<input_t, output_t, SocketConnection> server;

    server.listen("0.0.0.0:8001");

 

    // register rpc implementation class...

 

项目地址:http://code.google.com/p/febird

 

介绍RCP的实现原理 目录 1. 前言 2 2. 基本概念 3 2.1. IDL 3 2.2. 代理(Proxy) 3 2.3. 存根(Stub) 4 3. 三要素 4 3.1. 网络通讯 4 3.2. 消息编解码 5 3.3. IDL编译器 5 4. flex和bison 5 4.1. 准备概念 5 4.1.1. 正则表达式(regex/regexp) 6 4.1.2. 符号∈ 6 4.1.3. 终结符/非终结符/产生式 6 4.1.4. 记号(Token) 6 4.1.5. 形式文法 7 4.1.6. 上下文无关文法(CFG) 7 4.1.7. BNF 8 4.1.8. 推导 8 4.1.9. 语法树 8 4.1.10. LL(k) 9 4.1.11. LR(k) 9 4.1.12. LALR(k) 9 4.1.13. GLR 9 4.1.14. 移进/归约 9 4.2. flex和bison文件格式 9 4.2.1. 定义部分 10 4.2.2. 规则部分 10 4.2.3. 用户子例程部分 10 4.3. flex基础 10 4.3.1. flex文件格式 11 4.3.2. 选项 11 4.3.3. 名字定义 11 4.3.4. 词法规则 12 4.3.5. 匹配规则 12 4.3.6. %option 13 4.3.7. 全局变量yytext 13 4.3.8. 全局变量yyval 13 4.3.9. 全局变量yyleng 13 4.3.10. 全局函数yylex 13 4.3.11. 全局函数yywrap 13 4.4. bison基础 14 4.4.1. bison文件格式 14 4.4.2. %union 14 4.4.3. %token 15 4.4.4. 全局函数yyerror() 15 4.4.5. 全局函数yyparse() 15 4.5. 例1:单词计数 15 4.5.1. 目的 15 4.5.2. flex词法文件wc.l 16 4.5.3. Makefile 16 4.6. 例2:表达式 17 4.6.1. 目的 17 4.6.2. flex词法exp.l 17 4.6.3. bison语法exp.y 17 4.6.4. Makefile 19 4.6.5. 代码集成 19 4.7. 例3:函数 20 4.7.1. 目的 20 4.7.2. func.h 20 4.7.3. func.c 21 4.7.4. IDL代码func.idl 22 4.7.5. flex词法func.l 22 4.7.6. bison语法func.y 24 4.7.7. Makefile 27 5. 进阶 27 5.1. 客户端函数实现 27 5.2. 服务端函数实现 28 5.2.1. Stub部分实现 28 5.2.2. 用户部分实现 29 6. 参考资料 29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值