网站首页 > 文章精选 正文
常见的rpc框架有protobuf、thrift。
不过abelkhan没有采用这些开源的rpc框架,而是选择自己开发了一套新的rpc框架juggle(主要是为了享受重复发明轮子的乐趣)。
juggle采用一套dsl语言描述通信协议,然后使用codegen生成对应c++或c#的代码。dsl语言的语法如下:
module test{ void test_func(string argv1, int argv2); }
其中module在juggle中表示一组协议,在codegen的过程中,会以module为单位生成c++(c#)文件。
在juggle中函数返回值只能是void,因为juggle中的函数是远程调用(也就是说,当调用juggle的一个函数时,会发送一条消息到连接的目标进程),不支持立刻返回返回值。
juggle支持string、int、bool、float、array和table 6种数据类型。
juggle中的类型,在c++和c#中的对应类型如下:
bool -> in cpp bool (in c# Boolean) int -> in cpp int64_t (in c# Int64) float -> in cpp double (in c# Double) string -> in cpp std::string (in c# String) array -> in cpp std::vector<boost::any> (in c# ArrayList) table -> in cpp std::unordered_map<std::string, boost::any> (in c# Hashtable)
编写juggle的dsl脚本之后,进入juggle目录之后,执行gencpp即可生成c++代码(gencsharp生成c#代码)。
调用gencpp需要传入的2个参数,第一个为dsl脚本所在目录,第二个为生成代码所在的文件夹。
生成代码如下:
/*this caller file is codegen by juggle for c++*/ #include <sstream> #include <tuple> #include <string> #include <Icaller.h> #include <Ichannel.h> #include <boost/any.hpp> #include <boost/make_shared.hpp> namespace caller { class test : public juggle::Icaller { public: test(boost::shared_ptr<juggle::Ichannel> _ch) : Icaller(_ch) { module_name = "test"; } ~test{ } void test_func(std::string argv0,int64_t argv1){ auto v = boost::make_shared<std::vector<boost::any> >; v->push_back("test"); v->push_back("test_func"); v->push_back(boost::make_shared<std::vector<boost::any> >); (boost::any_cast<boost::shared_ptr<std::vector<boost::any> > >((*v)[2]))->push_back(argv0); (boost::any_cast<boost::shared_ptr<std::vector<boost::any> > >((*v)[2]))->push_back(argv1); ch->push(v); } }; }
/*this module file is codegen by juggle for c++*/ #include <Imodule.h> #include <boost/shared_ptr.hpp> #include <boost/signals2.hpp> #include <string> namespace module { class test : public juggle::Imodule { public: test{ module_name = "test"; protcolcall_set.insert(std::make_pair("test_func", boost::bind(&test::test_func, this, _1))); } ~test{ } boost::signals2::signal<void(std::string, int64_t) > sigtest_funchandle; void test_func(boost::shared_ptr<std::vector<boost::any> > _event){ sigtest_funchandle( boost::any_cast<std::string>((*_event)[0]), boost::any_cast<int64_t>((*_event)[1])); } }; }
caller文件夹中的是供发起远程调用端调用的代码,module是响应远程调用所需代码。
将生成的代码以include的方式引用头文件即可使用。
- 上一篇: C++设计模式:用代码演绎武侠世界的绝世神功
- 下一篇: C++11+ 泛型编程(模板)
猜你喜欢
- 2025-08-02 C++开发者都应该使用的十个C++11特性(上)
- 2025-08-02 如何实现自己的C++ unique_ptr?
- 2025-08-02 刚学会C++的小白用这个开源框架,做个 RPC 服务要多久?
- 2025-08-02 C++11+ 泛型编程(模板)
- 2025-08-02 C++设计模式:用代码演绎武侠世界的绝世神功
- 2025-08-02 视频分析与对象跟踪-扩展模块的单目标和多目标跟踪
- 2025-08-02 ROS2开发实践:ROS核心(节点、话题、服务、DDS通信协议等)
- 2025-08-02 C++语言程序员编程必收藏的20个经典实战案例(附完整源码)
- 2025-08-02 C# 控制电脑睡眠,休眠,关机以及唤醒
- 2025-08-02 别再人云亦云,C++到底难在哪?
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)
- mysql数据库面试题 (57)
- fmt.println (52)