RCF简要概述

RCF是什么

要知道RCF(Remote Call Framework)是什么先要了解什么是RPC(Remote Procedure Call Protocol),RPC是一种远程过程调用协议,是一种通过网络从远程计算机请求服务,使得服务的调用就像调用本地方法一样,不需要了解底层网络技术的协议。RPC跨越了传输层和应用层,很容易开发分布式应用。而常用的RPC框架有几个分别是grpc,thrift,dubbo,和RCF。所以RCF就是一个远程调用框架,这是一个基于c++和IPC的框架,提供了一种在C ++程序中实现进程间通信的的方法,它基于强类型的客户端/服务器接口的概念。

RCF的优点

RCF是用100%标准C++编写的,既可移植又高效。并且RCF从2008年发布的第一个正式版本1.0,到现在,经历了多个版本变化,发展的用户包括”爱立信、惠普“等这样全球性的大公司,是功能比较强大的分布式通信框架,从稳定性上来说,RCF目前比较稳定,也比较成熟;基于RCF的通信层可以提供跨多种编译器、操作系统和平台的可移植性,以及从多种传输机制、线程模型和消息传递范例中进行选择的能力。

RCF运用实例

这是一个简单的回显服务器和客户端案例,可以通过向服务器向客服端公开接收字符串的函数,并返回相同的字符串。服务端代码为:

#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/TcpEndpoint.hpp>

RCF_BEGIN(I_Echo, "I_Echo")
RCF_METHOD_R1(std::string, echo, const std::string &)
RCF_END(I_Echo)

class Echo
{
public:
    std::string echo(const std::string &s)
    {
        return s;
    }
};

int main()
{
    Echo echo;
    RCF::RcfServer server(RCF::TcpEndpoint(50001));
    server.bind<I_Echo>(echo);
    server.startInThisThread();
    return 0;
}

客户端代码为:

#include <RCF/Idl.hpp>
#include <RCF/TcpEndpoint.hpp>

RCF_BEGIN(I_Echo, "I_Echo")
RCF_METHOD_R1(std::string, echo, const std::string &)
RCF_END(I_Echo)

int main()
{
    RcfClient<I_Echo> echoClient(RCF::TcpEndpoint("localhost", 50001));
    std::string s = echoClient.echo(RCF::Twoway, "what's up");
    return 0;
}

其中RCF_BEGIN()、RCF_METHOD_xx()和RCF_END()是宏,用于为远程调用生成client和server存根。
RCF_BEGIN()开始接口定义,并定义接口的编译时标识符和接口的运行时标识符。
RCF_METHOD_xx() —— RCF_METHOD_xx()宏定义远程方法。RCF_METHOD_V1()定义了一个远程方法,它接受一个参数(在本例中是const std::string &),并返回void类型。
RCF_METHOD_xx()宏是为接受最多15个参数的void和非void远程调用定义的。
在方法定义的时候xx一般为v/r+数字v代表无返回值,r代表有返回值,数字代表是第几个方法。
RCF_METHOD_xx()
RCF_END()结束接口定义。
此外使用RCF还可以通过UDP进行通讯只需要更改TcpEndpoint变为UdpEndpoint。
使用RCF还可以实现一个进程内线程之间的通信:

#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/UdpEndpoint.hpp>

#ifndef RCF_USE_BOOST_THREADS
#error Need to build with RCF_USE_BOOST_THREADS
#endif

RCF_BEGIN(I_Echo, "I_Echo")
    RCF_METHOD_R1(std::string, echo, const std::string &)
RCF_END(I_Echo)

class Echo
{
public:
    std::string echo(const std::string &s)
    {
        return s;
    }
};

int main()
{
    Echo echo;
    RCF::RcfServer server(RCF::UdpEndpoint(50001));
    server.bind<I_Echo>(echo);
    server.start();
    RcfClient<I_Echo> echoClient(RCF::UdpEndpoint("127.0.0.1", 50001));
    std::string s = echoClient.echo(RCF::Twoway, "what's up");
    server.stop(); // Would happen anyway as server object goes out of scope.

    return 0;
}

RCF还可以完成发布/订阅等分布式编程模式,一个进程起着发布者的作用,并将定期的信息包发送给一组订阅者。订阅者呼叫发布商,并请求订阅发布商发布的数据:

RCF_BEGIN(I_Notify, "I_Notify")
    RCF_METHOD_V1(void, func1, int)
    RCF_METHOD_V2(void, func2, std::string, std::string)
RCF_END(I_Notify)

{
    RCF::RcfServer publishingServer(endpoint);
    RCF::PublishingServicePtr publishingServicePtr(
        new RCF::PublishingService() );
    publishingServer.addService(publishingServicePtr);
    publishingServer.start();

    // Start accepting subscription requests for I_Notify.
    publishingServicePtr->beginPublish<I_Notify>();

    // Call func1() on all subscribers.
    publishingServicePtr->publish<I_Notify>().func1(1);

    // Call func2(std::string, std::string) on all subscribers.
    publishingServicePtr->publish<I_Notify>().func2("one", "two");

    // Stop publishing I_Notify and disconnect all subscribers.
    publishingServicePtr->endPublish<I_Notify>();

    publishingServer.stop();
}

{
    RCF::RcfServer subscribingServer(endpoint);

    RCF::SubscriptionServicePtr subscriptionServicePtr(
        new RCF::SubscriptionService() );

    subscribingServer.addService( subscriptionServicePtr );
    subscribingServer.start();

    Notify notify;
    subscriptionServicePtr->beginSubscribe<I_Notify>(
        notify,
        RCF::TcpEndpoint(ip, port));

    // notify.func1() and notify.func2() will
    // now be remotely called by the publisher.
    // ...

    subscriptionServicePtr->endSubscribe<I_Notify>(notify);
}

本文章参考博客如下:
https://blog.csdn.net/swartz_lubel/article/details/76423470?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-76423470-blog-127238930.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-76423470-blog-127238930.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=2
https://blog.csdn.net/qq_23599965/article/details/89383017?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-89383017-blog-76423470.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-89383017-blog-76423470.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=3
https://blog.csdn.net/liutudengv/article/details/103303486
https://blog.csdn.net/yinsizhilian/article/details/109824648

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值