1 Star 0 Fork 6

CloudGuan / rpc-frontend

forked from Plato / rpc-frontend 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

IDL转换工具

将IDL文件转换为通用JSON中间格式,方便使用任何支持JSON访问的语言用来生成RPC框架代码

使用步骤

  1. 定义IDL文件
  2. 导出为JSON文件

IDL文件规则

#import "test.idl"

// I'm a single line comment
/*
 * I'm a multi-line comment
 */

struct Data {
    i32 field1
    string field2
    seq<string> field3
    set<string> field4
    dict<i64,string> field5
    bool field6
    float field7
    double field8    
}

service Service {
    void method1(Data,string)
    string method2(i8,set<string>,ui64)        
}

如上所示IDL文件支持struct和service, struct是纯数据结构,不支持定义方法,service为接口定义,接口内可以包含若干方法,struct内可以嵌套定义struct,例如:

strutct A {
    struct B {
        ...
    }    
}

所有定义的struct都为全局作用域,不论是否定义在任何一个struct内部,service内部也可以定义struct,struct内部不能定义service,struct支持多种数据类型,包含基础数据类型和容器,所有数据类型如下:

数据类型 描述
i8 8位有符号整数
i16 16位有符号整数
i32 32位有符号整数
i64 64位有符号整数
ui8 8位无符号整数
ui16 16位无符号整数
ui32 32位无符号整数
ui64 64位无符号整数
string 字符串
bool 布尔
float 单精度浮点数
double 双精度浮点数
dict 字典
set 集合
seq 序列(数组)

当有多个IDL文件时,可以将struct和service组织在不同的文件内,将要使用的文件通过#import来导入到入口文件,被导入的文件都会被工具在运行时合并到一起并输出。当工具检测到错误时会给出错误现场.

服务类型

  1. 单件 同一时刻工厂只会创建一个实例
  2. 多实例 同时可以有多个实例存在
  3. 可重入 单个实例但可以被同时调用
  4. 静态,动态 服务器是否可以被动态加载
// 做多可以生成16个实例
service Service multiple=16 {
}

// 单件,只能有一个实例
service Service single {
}

// 单件,只能有一个实例,但可以被并发调用
service Service reentrant {
}

// 单件,只能有一个实例,但可以被并发调用,同时是动态加载
service dynamic Service reentrant {
}

方法类型

  1. 双工 代理调用后会等待返回(不论方法是否有返回值)
  2. 单工 代理调用后不会等待返回,也不关心调用成功或者失败,相当于通知
service Service multiple=16 {
    // 单工
    oneway void method1(Data,string)
}

方法超时和重试

  1. 双工服务方法默认有5秒的超时时间,默认不重试
  2. 超时时间和重试次数是可以配置的
service Service multiple=16 {
    // 单工
    void method1(Data,string) timeout=2000 retry=3
}

以上的配置为超时2秒,最多重试3次

生成JSON文件

frontend是一个命令行工具,运行help可以得到使用方法

输入图片说明

运行命令后可以得到对应类型的JSON文件,后续开发这可以使用这个JSON文件来生成自己的RPC框架代码,生成的构成已经进行对IDL做了语法检查和错误校验。

输入图片说明

可以通过 -i 或者--include 添加 你自己的import文件搜索路径 例子如下

./frontend -f test1.idl -t cpp -i ../example

上面的例子所生成的JSON文件如下:

./frontend -f test1.idl -t cpp
{
	"serviceNames" : 
	[
		"Service"
	],
	"services" : 
	[
		{
			"methods" : 
			[
				{
					"arguments" : 
					[
						{
							"isStruct" : true,
							"type" : "Data"
						},
						{
							"type" : "std::string"
						}
					],
					"name" : "method1",
					"retType" : 
					{
						"type" : "void"
					}
				},
				{
					"arguments" : 
					[
						{
							"type" : "std::int8_t"
						},
						{
							"key" : 
							{
								"type" : "std::string"
							},
							"type" : "std::unordered_set"
						},
						{
							"type" : "std::uint64_t"
						}
					],
					"name" : "method2",
					"retType" : 
					{
						"type" : "std::string"
					}
				}
			],
			"name" : "Service"
		}
	],
	"structNames" : 
	[
		"Data"
	],
	"structs" : 
	[
		{
			"fields" : 
			[
				{
					"name" : "field1",
					"type" : "std::int32_t"
				},
				{
					"name" : "field2",
					"type" : "std::string"
				},
				{
					"key" : 
					{
						"type" : "std::string"
					},
					"name" : "field3",
					"type" : "std::vector"
				},
				{
					"key" : 
					{
						"type" : "std::string"
					},
					"name" : "field4",
					"type" : "std::unordered_set"
				},
				{
					"key" : 
					{
						"type" : "std::int64_t"
					},
					"name" : "field5",
					"type" : "std::unordered_map",
					"value" : 
					{
						"type" : "std::string"
					}
				},
				{
					"name" : "field6",
					"type" : "bool"
				},
				{
					"name" : "field7",
					"type" : "float"
				},
				{
					"name" : "field8",
					"type" : "double"
				}
			],
			"name" : "Data"
		}
	]
}
MIT License Copyright (c) 2019 dennis-kk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

IDL文件生成前端 展开 收起
C++
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/CloudGuan/rpc-frontend.git
git@gitee.com:CloudGuan/rpc-frontend.git
CloudGuan
rpc-frontend
rpc-frontend
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891