同步操作将从 Plato/rpc-frontend 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
将IDL文件转换为通用JSON中间格式,方便使用任何支持JSON访问的语言用来生成RPC框架代码
idl文件的命名采全小写不准带下划线。
IDL文件内部一共包含三个部分,分别是:enum , struct 和 service
当有多个IDL文件时,可以将struct和service组织在不同的文件内,将要使用的文件通过#import来导入到入口文件。
// 语法 在idl文件的最上方,类似c++的头文件的使用方式
#import "shop.idl"
#import "item.idl"
enum 枚举 0 已经被框架占用 用户定义的默认从1开始
enum ErrorCode{
SUCCESS,
INVALIDSCENE,
INVALIDPOSITION,
}
struct是纯数据结构,不支持定义方法,允许struct内部定义struct。
目前struct 内支持的数据类型包含以下15种:
数据类型 | 描述 |
---|---|
i8 | 8位有符号整数 |
i16 | 16位有符号整数 |
i32 | 32位有符号整数 |
i64 | 64位有符号整数 |
ui8 | 8位无符号整数 |
ui16 | 16位无符号整数 |
ui32 | 32位无符号整数 |
ui64 | 64位无符号整数 |
string | 字符串 |
bool | 布尔 |
float | 单精度浮点数 |
double | 双精度浮点数 |
dict | 字典 |
set | 集合 |
seq | 序列(数组) |
struct内部变量的命名规范:
// example
struct Item{
ui64 item_uid
string item_name
ui32 item_count
}
struct A {
struct B {
...
}
}
所有定义的struct都为全局作用域,不论是否定义在任何一个struct内部,还是service内部。struct不要定义在service内部。
service为接口定义,接口内可以包含若干方法。
目前修饰service的关键字一共有以下四种:
// 做多可以生成16个实例
service BuyItem multiple=16 {
}
// 单件,只能有一个实例
service ShellItem single {
}
// 单件,只能有一个实例,但可以被并发调用
service Shop reentrant {
}
// 单件,只能有一个实例,但可以被并发调用,同时是动态加载
service dynamic Mall reentrant {
}
// lua的动态服务
service dynamic Scene generic {
[script_type:lua]
}
service Shop multiple=16 {
oneway void SyncItem(Item)
}
service Shop multiple=16 {
void Buy(Data,string) timeout=2000 retry=3
}
以上的配置为超时2秒,最多重试3次
service内部变量的命名规范:
service Shop multiple=16 {
void Buy(item) timeout=2000 retry=3
oneway void SyncItem(Item)
}
服务可以添加标记,标记用于区分生成代码的语言。用法[key:value1, value2,...,valuen] (value为语言名称,例如: cpp , csharp , lua) 目前key只支持两个关键字: script_type 和 no_service。
service dynamic Scene generic {
// 需要生成lua的脚本
[script_type:lua]
}
service dynamic Scene multiple=18 {
// 不会生成csharp
[no_service:csharp]
}
idl文件支持单行注释和多行注释。
// 这是个单行注释
/*
* 这是个多行注释
*/
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,{
"enumNames" :
[
"ErrorCode"
],
"enums" :
[
{
"fields" :
[
{
"name" : "SUCCESS",
"value" : 1
},
{
"name" : "FAILD",
"value" : 2
}
],
"name" : "ErrorCode"
}
],
"idlname" : "test1",
"serviceNames" :
[
"Service",
"ServiceDynamic",
"MyService",
"Lua"
],
"services" :
[
{
"loadType" : "static",
"methods" :
[
{
"arguments" :
[
{
"IdlType" : "struct",
"index" : 1,
"isStruct" : true,
"type" : "Data"
},
{
"IdlType" : "string",
"index" : 2,
"type" : "std::string"
}
],
"index" : 1,
"is_stream" : false,
"name" : "method1",
"noexcept" : false,
"oneway" : true,
"retType" :
{
"IdlType" : "void",
"type" : "void"
}
},
{
"arguments" :
[
{
"IdlType" : "i8",
"index" : 1,
"type" : "std::int8_t"
},
{
"IdlType" : "set",
"index" : 2,
"key" :
{
"IdlType" : "string",
"type" : "std::string"
},
"type" : "std::unordered_set"
},
{
"IdlType" : "ui64",
"index" : 3,
"type" : "std::uint64_t"
}
],
"index" : 2,
"is_stream" : false,
"name" : "method2",
"noexcept" : false,
"retType" :
{
"IdlType" : "string",
"type" : "std::string"
},
"timeout" : 5000
}
],
"name" : "Service",
"type" : "single",
"uuid" : "9046620621498282749"
},
{
"loadType" : "dynamic",
"methods" :
[
{
"arguments" :
[
{
"IdlType" : "i8",
"index" : 1,
"type" : "std::int8_t"
},
{
"IdlType" : "set",
"index" : 2,
"key" :
{
"IdlType" : "string",
"type" : "std::string"
},
"type" : "std::unordered_set"
},
{
"IdlType" : "ui64",
"index" : 3,
"type" : "std::uint64_t"
}
],
"index" : 1,
"is_stream" : false,
"name" : "method1",
"noexcept" : false,
"retType" :
{
"IdlType" : "string",
"type" : "std::string"
},
"retry" : 3,
"timeout" : 2000
}
],
"name" : "ServiceDynamic",
"type" : "reentrant",
"uuid" : "9046620621990182746"
},
{
"loadType" : "dynamic",
"maxInst" : 16,
"methods" :
[
{
"arguments" :
[
{
"IdlType" : "void",
"index" : 1,
"type" : "void"
}
],
"index" : 1,
"is_stream" : false,
"name" : "method14",
"noexcept" : false,
"retType" :
{
"IdlType" : "struct",
"isStruct" : true,
"type" : "Dummy"
},
"timeout" : 5000
},
{
"arguments" :
[
{
"IdlType" : "void",
"index" : 1,
"type" : "void"
}
],
"index" : 2,
"is_stream" : false,
"name" : "method1",
"noexcept" : false,
"retType" :
{
"IdlType" : "seq",
"key" :
{
"IdlType" : "struct",
"isStruct" : true,
"type" : "Data"
},
"type" : "std::vector"
},
"timeout" : 5000
},
{
"arguments" :
[
{
"IdlType" : "void",
"index" : 1,
"type" : "void"
}
],
"index" : 3,
"is_stream" : false,
"name" : "method2",
"noexcept" : false,
"retType" :
{
"IdlType" : "dict",
"key" :
{
"IdlType" : "ui32",
"type" : "std::uint32_t"
},
"type" : "std::unordered_map",
"value" :
{
"IdlType" : "struct",
"isStruct" : true,
"type" : "Data"
}
},
"timeout" : 5000
}
],
"name" : "MyService",
"notations" :
[
{
"no_service" :
[
"csharp"
]
}
],
"type" : "multiple",
"uuid" : "9046620621385673117"
},
{
"loadType" : "dynamic",
"methods" :
[
{
"arguments" :
[
{
"IdlType" : "string",
"index" : 1,
"type" : "std::string"
},
{
"IdlType" : "string",
"index" : 2,
"type" : "std::string"
}
],
"index" : 1,
"is_stream" : false,
"name" : "login",
"noexcept" : false,
"retType" :
{
"IdlType" : "ui64",
"type" : "std::uint64_t"
},
"timeout" : 5000
},
{
"arguments" :
[
{
"IdlType" : "ui64",
"index" : 1,
"type" : "std::uint64_t"
}
],
"index" : 2,
"is_stream" : false,
"name" : "logout",
"noexcept" : false,
"retType" :
{
"IdlType" : "void",
"type" : "void"
},
"timeout" : 5000
},
{
"arguments" :
[
{
"IdlType" : "void",
"index" : 1,
"type" : "void"
}
],
"index" : 3,
"is_stream" : false,
"name" : "oneway_method",
"noexcept" : false,
"oneway" : true,
"retType" :
{
"IdlType" : "void",
"type" : "void"
}
}
],
"name" : "Lua",
"notations" :
[
{
"script_type" :
[
"lua"
]
}
],
"type" : "generic",
"uuid" : "9046620620658582252"
}
],
"structNames" :
[
"Dummy",
"Data"
],
"structs" :
[
{
"fields" :
[
{
"IdlType" : "dict",
"index" : 1,
"key" :
{
"IdlType" : "i64",
"type" : "std::int64_t"
},
"name" : "field1",
"type" : "std::unordered_map",
"value" :
{
"IdlType" : "string",
"type" : "std::string"
}
},
{
"IdlType" : "enum",
"index" : 2,
"isEnum" : true,
"name" : "error",
"type" : "ErrorCode"
}
],
"name" : "Dummy"
},
{
"fields" :
[
{
"IdlType" : "dict",
"index" : 1,
"key" :
{
"IdlType" : "i64",
"type" : "std::int64_t"
},
"name" : "field1",
"type" : "std::unordered_map",
"value" :
{
"IdlType" : "string",
"type" : "std::string"
}
},
{
"IdlType" : "struct",
"index" : 2,
"isStruct" : true,
"name" : "field3",
"type" : "Dummy"
},
{
"IdlType" : "seq",
"index" : 3,
"key" :
{
"IdlType" : "struct",
"isStruct" : true,
"type" : "Dummy"
},
"name" : "field4",
"type" : "std::vector"
},
{
"IdlType" : "dict",
"index" : 4,
"key" :
{
"IdlType" : "i64",
"type" : "std::int64_t"
},
"name" : "field6",
"type" : "std::unordered_map",
"value" :
{
"IdlType" : "struct",
"isStruct" : true,
"type" : "Dummy"
}
}
],
"name" : "Data"
}
]
}
"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"
}
]
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。