网站首页 > 编程文章 正文
常见的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的方式引用头文件即可使用。
猜你喜欢
- 2024-09-09 搞懂事件——C# 的event的机制深度理解
- 2024-09-09 C 容易犯错的新手问题有哪些?(c语言容易犯错误的地方)
- 2024-09-09 想要使用C#编程创建3D PDF转换器?Aspose.PDF必不可少
- 2024-09-09 .NET 大牛之路 019 | C#基础:理解装箱与拆箱
- 2024-09-09 在CSI.EXE中使用C#脚本绕过应用白名单(含缓解方案)
- 2024-09-09 在C#中向IC卡(通常指智能卡或集成电路卡)写入数据
- 2024-09-09 C#编程中如何使用线程(c# 线程编程)
- 2024-09-09 c# 串口连接xbee模块并且发送AT命令
- 2024-09-09 iOS开发生涯的初恋:详解Objective-C多项改进
- 2024-09-09 c# 串口连接xbee模块并且发送AT命令设置NI值
你 发表评论:
欢迎- 06-24一个老爸画了超级有爱的365幅画 | 父亲节献礼
- 06-24产品小白看魏则西事件——用产品思维审视百度推广
- 06-24某教程学习笔记(一):13、脚本木马原理
- 06-24十大常见web漏洞——命令执行漏洞
- 06-24初涉内网,提权那些事(内网渗透提权)
- 06-24黑客命令第16集:47种最常见的**网站方法2/2
- 06-24铭说 | 一句话木马的多种变形方式
- 06-24Java隐藏的10倍效率技巧!90%程序员不知道的魔法方法(附代码)
- 最近发表
- 标签列表
-
- spire.doc (70)
- instanceclient (62)
- solidworks (78)
- system.data.oracleclient (61)
- 按键小精灵源码提取 (66)
- pyqt5designer教程 (65)
- 联想刷bios工具 (66)
- c#源码 (64)
- graphics.h头文件 (62)
- mysqldump下载 (66)
- libmp3lame (60)
- maven3.3.9 (63)
- 二调符号库 (57)
- git.exe下载 (68)
- diskgenius_winpe (72)
- pythoncrc16 (57)
- solidworks宏文件下载 (59)
- qt帮助文档中文版 (73)
- satacontroller (66)
- hgcad (64)
- bootimg.exe (69)
- android-gif-drawable (62)
- axure9元件库免费下载 (57)
- libmysqlclient.so.18 (58)
- springbootdemo (64)
本文暂时没有评论,来添加一个吧(●'◡'●)