erlang自定义二进制协议
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

672 行
22 KiB

  1. -module(protoMsg).
  2. -export([encode/1, decode/1, encodeRec/1, decodeRec/2]).
  3. -define(min8, -128).
  4. -define(max8, 127).
  5. -define(min16, -32768).
  6. -define(max16, 32767).
  7. -define(min32, -2147483648).
  8. -define(max32, 2147483647).
  9. -define(min64, -9223372036854775808).
  10. -define(max64, 9223372036854775807).
  11. -define(minF32, -3.4E+38).
  12. -define(maxF32, 3.4E+38).
  13. -define(minF64, -1.797E-308).
  14. -define(maxF64, 1.797E+308).
  15. -define(int8(V), <<V:8>>).
  16. -define(uint8(V), <<V:8>>).
  17. -define(int16(V), <<V:16/big>>).
  18. -define(uint16(V), <<V:16/big>>).
  19. -define(int32(V), <<V:32/big>>).
  20. -define(uint32(V), <<V:32/big>>).
  21. -define(int64(V), <<V:64/big>>).
  22. -define(uint64(V), <<V:64/big>>).
  23. -define(integer(V), (integer(V))).
  24. -define(number(V), (number(V))).
  25. -define(string(V), (string(V))).
  26. -define(float(V), <<V:32/big-float>>).
  27. -define(double(V), <<V:64/big-float>>).
  28. -define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
  29. -define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)).
  30. -define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]).
  31. -define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]).
  32. -define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]).
  33. -define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]).
  34. -define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]).
  35. -define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]).
  36. -define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]).
  37. -define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]).
  38. -define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]).
  39. -define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]).
  40. -define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]).
  41. -define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]).
  42. -define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]).
  43. -define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]).
  44. -define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]).
  45. integer(V) ->
  46. if
  47. V >= ?min8 andalso V =< ?max8 ->
  48. <<8:8, <<V:8>>/binary>>;
  49. V >= ?min16 andalso V =< ?max16 ->
  50. <<16:8, <<V:16/big>>/binary>>;
  51. V >= ?min32 andalso V =< ?max32 ->
  52. <<32:8, <<V:32/big>>/binary>>;
  53. V >= ?min64 andalso V =< ?max64 ->
  54. <<64:8, <<V:64/big>>/binary>>;
  55. true ->
  56. throw(exceeded_the_integer)
  57. end.
  58. numInteger(V) ->
  59. if
  60. V >= ?min8 andalso V =< ?max8 ->
  61. <<8:8, <<V:8>>/binary>>;
  62. V >= ?min16 andalso V =< ?max16 ->
  63. <<16:8, <<V:16/big>>/binary>>;
  64. V >= ?min32 andalso V =< ?max32 ->
  65. <<32:8, <<V:32/big>>/binary>>;
  66. V >= ?min64 andalso V =< ?max64 ->
  67. <<64:8, <<V:64/big>>/binary>>;
  68. true ->
  69. throw(exceeded_the_integer)
  70. end.
  71. numFloat(V) ->
  72. if
  73. V >= ?minF32 andalso V =< ?maxF32 ->
  74. <<33:8, <<V:32/big-float>>/binary>>;
  75. V >= ?minF64 andalso V =< ?maxF64 ->
  76. <<65:8, <<V:64/big-float>>/binary>>;
  77. true ->
  78. throw(exceeded_the_float)
  79. end.
  80. number(V) ->
  81. if
  82. erlang:is_integer(V) == true ->
  83. numInteger(V);
  84. erlang:is_float(V) == true ->
  85. numFloat(V);
  86. true ->
  87. throw(is_not_number)
  88. end.
  89. string(Str) when is_binary(Str) ->
  90. [<<(byte_size(Str)):16/big>>, Str];
  91. string(Str) ->
  92. Str2 = unicode:characters_to_binary(Str, utf8),
  93. [<<(byte_size(Str2)):16/big>>, Str2].
  94. encode(Record) ->
  95. MsgBin = encodeRec(Record),
  96. MsgId = getMsgId(element(1, Record)),
  97. [<<MsgId:16/big>>, MsgBin].
  98. decode(Bin) ->
  99. <<MsgId:16/big, MsgBin/binary>> = Bin,
  100. SchList = getMsgSchema(MsgId),
  101. {<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]),
  102. list_to_tuple(ResultList).
  103. decodeRec(RecordName, Bin) ->
  104. SchList = getMsgSchema(RecordName),
  105. {LeftBin, Result} = decodeField(SchList, Bin, [RecordName]),
  106. {LeftBin, list_to_tuple(Result)}.
  107. decodeField([], LeftBin, Result) ->
  108. {LeftBin, lists:reverse(Result)};
  109. decodeField([Type | SchList], MsgBin, Result) ->
  110. case Type of
  111. int32 ->
  112. <<Int:32/big-signed, LeftBin/binary>> = MsgBin,
  113. decodeField(SchList, LeftBin, [Int | Result]);
  114. uint32 ->
  115. <<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
  116. decodeField(SchList, LeftBin, [Int | Result]);
  117. string ->
  118. <<Len:16/big, StrBin:Len/binary, LeftBin/binary>> = MsgBin,
  119. decodeField(SchList, LeftBin, [StrBin | Result]);
  120. int16 ->
  121. <<Int:16/big-signed, LeftBin/binary>> = MsgBin,
  122. decodeField(SchList, LeftBin, [Int | Result]);
  123. uint16 ->
  124. <<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
  125. decodeField(SchList, LeftBin, [Int | Result]);
  126. int8 ->
  127. <<Int:8/big-signed, LeftBin/binary>> = MsgBin,
  128. decodeField(SchList, LeftBin, [Int | Result]);
  129. uint8 ->
  130. <<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
  131. decodeField(SchList, LeftBin, [Int | Result]);
  132. int64 ->
  133. <<Int:64/big-signed, LeftBin/binary>> = MsgBin,
  134. decodeField(SchList, LeftBin, [Int | Result]);
  135. uint64 ->
  136. <<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
  137. decodeField(SchList, LeftBin, [Int | Result]);
  138. integer ->
  139. <<IntBits:8, Int:IntBits/big-signed, LeftBin/binary>> = MsgBin,
  140. decodeField(SchList, LeftBin, [Int | Result]);
  141. number ->
  142. <<NumBits:8, NumBin/binary>> = MsgBin,
  143. case NumBits of
  144. 33 ->
  145. <<Float:32/big-float, LeftBin/binary>> = NumBin,
  146. decodeField(SchList, LeftBin, [Float | Result]);
  147. 65 ->
  148. <<Float:64/big-float, LeftBin/binary>> = NumBin,
  149. decodeField(SchList, LeftBin, [Float | Result]);
  150. _ ->
  151. <<Int:NumBits/big-signed, LeftBin/binary>> = NumBin,
  152. decodeField(SchList, LeftBin, [Int | Result])
  153. end;
  154. bool ->
  155. <<Bool:8/big-unsigned, LeftBin/binary>> = MsgBin,
  156. case Bool =:= 1 of
  157. true ->
  158. decodeField(SchList, LeftBin, [true | Result]);
  159. _ ->
  160. decodeField(SchList, LeftBin, [false | Result])
  161. end;
  162. float ->
  163. <<Float:32/big-float, LeftBin/binary>> = MsgBin,
  164. decodeField(SchList, LeftBin, [Float | Result]);
  165. double ->
  166. <<Float:64/big-float, LeftBin/binary>> = MsgBin,
  167. decodeField(SchList, LeftBin, [Float | Result]);
  168. {list, int32} ->
  169. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  170. {LeftBin, RetList} = deInt32List(Len, LeftListBin, []),
  171. decodeField(SchList, LeftBin, [RetList | Result]);
  172. {list, uint32} ->
  173. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  174. {LeftBin, RetList} = deUint32List(Len, LeftListBin, []),
  175. decodeField(SchList, LeftBin, [RetList | Result]);
  176. {list, int16} ->
  177. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  178. {LeftBin, RetList} = deInt16List(Len, LeftListBin, []),
  179. decodeField(SchList, LeftBin, [RetList | Result]);
  180. {list, uint16} ->
  181. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  182. {LeftBin, RetList} = deUint16List(Len, LeftListBin, []),
  183. decodeField(SchList, LeftBin, [RetList | Result]);
  184. {list, int8} ->
  185. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  186. {LeftBin, RetList} = deInt8List(Len, LeftListBin, []),
  187. decodeField(SchList, LeftBin, [RetList | Result]);
  188. {list, uint8} ->
  189. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  190. {LeftBin, RetList} = deUint8List(Len, LeftListBin, []),
  191. decodeField(SchList, LeftBin, [RetList | Result]);
  192. {list, string} ->
  193. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  194. {LeftBin, RetList} = deStringList(Len, LeftListBin, []),
  195. decodeField(SchList, LeftBin, [RetList | Result]);
  196. {list, int64} ->
  197. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  198. {LeftBin, RetList} = deInt64List(Len, LeftListBin, []),
  199. decodeField(SchList, LeftBin, [RetList | Result]);
  200. {list, uint64} ->
  201. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  202. {LeftBin, RetList} = deUint64List(Len, LeftListBin, []),
  203. decodeField(SchList, LeftBin, [RetList | Result]);
  204. {list, integer} ->
  205. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  206. {LeftBin, RetList} = deIntegerList(Len, LeftListBin, []),
  207. decodeField(SchList, LeftBin, [RetList | Result]);
  208. {list, number} ->
  209. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  210. {LeftBin, RetList} = deNumberList(Len, LeftListBin, []),
  211. decodeField(SchList, LeftBin, [RetList | Result]);
  212. {list, bool} ->
  213. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  214. {LeftBin, RetList} = deBoolList(Len, LeftListBin, []),
  215. decodeField(SchList, LeftBin, [RetList | Result]);
  216. {list, float} ->
  217. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  218. {LeftBin, RetList} = deFloatList(Len, LeftListBin, []),
  219. decodeField(SchList, LeftBin, [RetList | Result]);
  220. {list, double} ->
  221. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  222. {LeftBin, RetList} = deDoubleList(Len, LeftListBin, []),
  223. decodeField(SchList, LeftBin, [RetList | Result]);
  224. {list, RecordName} ->
  225. <<Len:16/big, LeftListBin/binary>> = MsgBin,
  226. {LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []),
  227. decodeField(SchList, LeftBin, [RetList | Result]);
  228. RecordName ->
  229. <<IsUndef:8, LeftBin/binary>> = MsgBin,
  230. case IsUndef of
  231. 0 ->
  232. decodeField(SchList, LeftBin, [undefined | Result]);
  233. _ ->
  234. SubSchList = getMsgSchema(RecordName),
  235. {SubLeftBin, SubResultList} = decodeField(SubSchList, LeftBin, [RecordName]),
  236. decodeField(SchList, SubLeftBin, [list_to_tuple(SubResultList) | Result])
  237. end
  238. end.
  239. deBoolList(0, MsgBin, RetList) ->
  240. {MsgBin, lists:reverse(RetList)};
  241. deBoolList(N, MsgBin, RetList) ->
  242. <<Bool:8, LeftBin/binary>> = MsgBin,
  243. case Bool =:= 1 of
  244. true ->
  245. deBoolList(N - 1, LeftBin, [true | RetList]);
  246. _ ->
  247. deBoolList(N - 1, LeftBin, [false | RetList])
  248. end.
  249. deInt8List(0, MsgBin, RetList) ->
  250. {MsgBin, RetList};
  251. deInt8List(N, MsgBin, RetList) ->
  252. <<Int:8/big-signed, LeftBin/binary>> = MsgBin,
  253. deInt8List(N - 1, LeftBin, [Int | RetList]).
  254. deUint8List(0, MsgBin, RetList) ->
  255. {MsgBin, lists:reverse(RetList)};
  256. deUint8List(N, MsgBin, RetList) ->
  257. <<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
  258. deUint8List(N - 1, LeftBin, [Int | RetList]).
  259. deInt16List(0, MsgBin, RetList) ->
  260. {MsgBin, lists:reverse(RetList)};
  261. deInt16List(N, MsgBin, RetList) ->
  262. <<Int:16/big-signed, LeftBin/binary>> = MsgBin,
  263. deInt16List(N - 1, LeftBin, [Int | RetList]).
  264. deUint16List(0, MsgBin, RetList) ->
  265. {MsgBin, lists:reverse(RetList)};
  266. deUint16List(N, MsgBin, RetList) ->
  267. <<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
  268. deUint16List(N - 1, LeftBin, [Int | RetList]).
  269. deInt32List(0, MsgBin, RetList) ->
  270. {MsgBin, lists:reverse(RetList)};
  271. deInt32List(N, MsgBin, RetList) ->
  272. <<Int:32/big-signed, LeftBin/binary>> = MsgBin,
  273. deInt32List(N - 1, LeftBin, [Int | RetList]).
  274. deUint32List(0, MsgBin, RetList) ->
  275. {MsgBin, lists:reverse(RetList)};
  276. deUint32List(N, MsgBin, RetList) ->
  277. <<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
  278. deUint32List(N - 1, LeftBin, [Int | RetList]).
  279. deInt64List(0, MsgBin, RetList) ->
  280. {MsgBin, lists:reverse(RetList)};
  281. deInt64List(N, MsgBin, RetList) ->
  282. <<Int:64/big-signed, LeftBin/binary>> = MsgBin,
  283. deInt64List(N - 1, LeftBin, [Int | RetList]).
  284. deUint64List(0, MsgBin, RetList) ->
  285. {MsgBin, lists:reverse(RetList)};
  286. deUint64List(N, MsgBin, RetList) ->
  287. <<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
  288. deUint64List(N - 1, LeftBin, [Int | RetList]).
  289. deIntegerList(0, MsgBin, RetList) ->
  290. {MsgBin, lists:reverse(RetList)};
  291. deIntegerList(N, MsgBin, RetList) ->
  292. <<IntBits:8, Int:IntBits/big-signed, LeftBin/binary>> = MsgBin,
  293. deIntegerList(N - 1, LeftBin, [Int | RetList]).
  294. deNumberList(0, MsgBin, RetList) ->
  295. {MsgBin, lists:reverse(RetList)};
  296. deNumberList(N, MsgBin, RetList) ->
  297. <<NumBits:8, NumBin/binary>> = MsgBin,
  298. case NumBits of
  299. 33 ->
  300. <<Float:32/big-float, LeftBin/binary>> = NumBin,
  301. deNumberList(N - 1, LeftBin, [Float | RetList]);
  302. 65 ->
  303. <<Float:64/big-float, LeftBin/binary>> = NumBin,
  304. deNumberList(N - 1, LeftBin, [Float | RetList]);
  305. _ ->
  306. <<Int:NumBits/big-signed, LeftBin/binary>> = NumBin,
  307. deNumberList(N - 1, LeftBin, [Int | RetList])
  308. end.
  309. deFloatList(0, MsgBin, RetList) ->
  310. {MsgBin, lists:reverse(RetList)};
  311. deFloatList(N, MsgBin, RetList) ->
  312. <<Float:32/big-float, LeftBin/binary>> = MsgBin,
  313. deFloatList(N - 1, LeftBin, [Float | RetList]).
  314. deDoubleList(0, MsgBin, RetList) ->
  315. {MsgBin, lists:reverse(RetList)};
  316. deDoubleList(N, MsgBin, RetList) ->
  317. <<Float:64/big-float, LeftBin/binary>> = MsgBin,
  318. deDoubleList(N - 1, LeftBin, [Float | RetList]).
  319. deStringList(0, MsgBin, RetList) ->
  320. {MsgBin, lists:reverse(RetList)};
  321. deStringList(N, MsgBin, RetList) ->
  322. <<Len:16/big, StrBin:Len/binary-unit:8, LeftBin/binary>> = MsgBin,
  323. deStringList(N - 1, LeftBin, [StrBin | RetList]).
  324. deRecordList(0, _RecordName, MsgBin, RetList) ->
  325. {MsgBin, lists:reverse(RetList)};
  326. deRecordList(N, RecordName, MsgBin, RetList) ->
  327. {LeftBin, Tuple} = decodeRec(RecordName, MsgBin),
  328. deRecordList(N - 1, RecordName, LeftBin, [Tuple | RetList]).
  329. getMsgType(1)-> test;
  330. getMsgType(2)-> phoneNumber;
  331. getMsgType(3)-> person;
  332. getMsgType(4)-> addressBook;
  333. getMsgType(5)-> union;
  334. getMsgType(6)-> tbool;
  335. getMsgType(7)-> tint8;
  336. getMsgType(8)-> tuint8;
  337. getMsgType(9)-> tint16;
  338. getMsgType(10)-> tuint16;
  339. getMsgType(11)-> tint32;
  340. getMsgType(12)-> tuint32;
  341. getMsgType(13)-> tint64;
  342. getMsgType(14)-> tuint64;
  343. getMsgType(15)-> tinteger;
  344. getMsgType(16)-> tnumber;
  345. getMsgType(17)-> tfloat;
  346. getMsgType(18)-> tdouble;
  347. getMsgType(19)-> tstring;
  348. getMsgType(20)-> tlistbool;
  349. getMsgType(21)-> tlistint8;
  350. getMsgType(22)-> tlistuint8;
  351. getMsgType(23)-> tlistint16;
  352. getMsgType(24)-> tlistuint16;
  353. getMsgType(25)-> tlistint32;
  354. getMsgType(26)-> tlistuint32;
  355. getMsgType(27)-> tlistint64;
  356. getMsgType(28)-> tlistuint64;
  357. getMsgType(29)-> tlistinteger;
  358. getMsgType(30)-> tlistnumber;
  359. getMsgType(31)-> tlistfloat;
  360. getMsgType(32)-> tlistdouble;
  361. getMsgType(33)-> tliststring;
  362. getMsgType(34)-> tlistunion;
  363. getMsgType(35)-> allType;
  364. getMsgType(_) -> undefined.
  365. getMsgId(test)-> 1;
  366. getMsgId(phoneNumber)-> 2;
  367. getMsgId(person)-> 3;
  368. getMsgId(addressBook)-> 4;
  369. getMsgId(union)-> 5;
  370. getMsgId(tbool)-> 6;
  371. getMsgId(tint8)-> 7;
  372. getMsgId(tuint8)-> 8;
  373. getMsgId(tint16)-> 9;
  374. getMsgId(tuint16)-> 10;
  375. getMsgId(tint32)-> 11;
  376. getMsgId(tuint32)-> 12;
  377. getMsgId(tint64)-> 13;
  378. getMsgId(tuint64)-> 14;
  379. getMsgId(tinteger)-> 15;
  380. getMsgId(tnumber)-> 16;
  381. getMsgId(tfloat)-> 17;
  382. getMsgId(tdouble)-> 18;
  383. getMsgId(tstring)-> 19;
  384. getMsgId(tlistbool)-> 20;
  385. getMsgId(tlistint8)-> 21;
  386. getMsgId(tlistuint8)-> 22;
  387. getMsgId(tlistint16)-> 23;
  388. getMsgId(tlistuint16)-> 24;
  389. getMsgId(tlistint32)-> 25;
  390. getMsgId(tlistuint32)-> 26;
  391. getMsgId(tlistint64)-> 27;
  392. getMsgId(tlistuint64)-> 28;
  393. getMsgId(tlistinteger)-> 29;
  394. getMsgId(tlistnumber)-> 30;
  395. getMsgId(tlistfloat)-> 31;
  396. getMsgId(tlistdouble)-> 32;
  397. getMsgId(tliststring)-> 33;
  398. getMsgId(tlistunion)-> 34;
  399. getMsgId(allType)-> 35;
  400. getMsgId(_) -> 0.
  401. encodeRec({test, V1}) ->
  402. [?string(V1)];
  403. encodeRec({phoneNumber, V1, V2}) ->
  404. [?record(V1), ?int32(V2)];
  405. encodeRec({person, V1, V2, V3, V4}) ->
  406. [?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)];
  407. encodeRec({addressBook, V1, V2}) ->
  408. [?list_record(V1), ?list_record(V2)];
  409. encodeRec({union, V1, V2}) ->
  410. [?string(V1), ?int32(V2)];
  411. encodeRec({tbool, V1}) ->
  412. [?bool(V1)];
  413. encodeRec({tint8, V1, V2}) ->
  414. [?int8(V1), ?int8(V2)];
  415. encodeRec({tuint8, V1, V2}) ->
  416. [?uint8(V1), ?uint8(V2)];
  417. encodeRec({tint16, V1, V2}) ->
  418. [?int16(V1), ?int16(V2)];
  419. encodeRec({tuint16, V1, V2}) ->
  420. [?uint16(V1), ?uint16(V2)];
  421. encodeRec({tint32, V1, V2}) ->
  422. [?int32(V1), ?int32(V2)];
  423. encodeRec({tuint32, V1, V2}) ->
  424. [?uint32(V1), ?uint32(V2)];
  425. encodeRec({tint64, V1, V2}) ->
  426. [?int64(V1), ?int64(V2)];
  427. encodeRec({tuint64, V1, V2}) ->
  428. [?uint64(V1), ?uint64(V2)];
  429. encodeRec({tinteger, V1, V2, V3, V4, V5, V6, V7, V8}) ->
  430. [?integer(V1), ?integer(V2), ?integer(V3), ?integer(V4), ?integer(V5), ?integer(V6), ?integer(V7), ?integer(V8)];
  431. encodeRec({tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) ->
  432. [?number(V1), ?number(V2), ?number(V3), ?number(V4), ?number(V5), ?number(V6), ?number(V7), ?number(V8), ?number(V9), ?number(V10)];
  433. encodeRec({tfloat, V1, V2}) ->
  434. [?float(V1), ?float(V2)];
  435. encodeRec({tdouble, V1, V2}) ->
  436. [?double(V1), ?double(V2)];
  437. encodeRec({tstring, V1, V2}) ->
  438. [?string(V1), ?string(V2)];
  439. encodeRec({tlistbool, V1}) ->
  440. [?list_bool(V1)];
  441. encodeRec({tlistint8, V1}) ->
  442. [?list_int8(V1)];
  443. encodeRec({tlistuint8, V1}) ->
  444. [?list_uint8(V1)];
  445. encodeRec({tlistint16, V1}) ->
  446. [?list_int16(V1)];
  447. encodeRec({tlistuint16, V1}) ->
  448. [?list_uint16(V1)];
  449. encodeRec({tlistint32, V1}) ->
  450. [?list_int32(V1)];
  451. encodeRec({tlistuint32, V1}) ->
  452. [?list_uint32(V1)];
  453. encodeRec({tlistint64, V1}) ->
  454. [?list_int64(V1)];
  455. encodeRec({tlistuint64, V1}) ->
  456. [?list_uint64(V1)];
  457. encodeRec({tlistinteger, V1}) ->
  458. [?list_integer(V1)];
  459. encodeRec({tlistnumber, V1}) ->
  460. [?list_number(V1)];
  461. encodeRec({tlistfloat, V1}) ->
  462. [?list_float(V1)];
  463. encodeRec({tlistdouble, V1}) ->
  464. [?list_double(V1)];
  465. encodeRec({tliststring, V1}) ->
  466. [?list_string(V1)];
  467. encodeRec({tlistunion, V1}) ->
  468. [?list_record(V1)];
  469. encodeRec({allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) ->
  470. [?bool(V1), ?int8(V2), ?uint8(V3), ?int16(V4), ?uint16(V5), ?int32(V6), ?uint32(V7), ?int64(V8), ?uint64(V9), ?integer(V10), ?integer(V11), ?integer(V12), ?integer(V13), ?integer(V14), ?integer(V15), ?integer(V16), ?integer(V17), ?number(V18), ?number(V19), ?number(V20), ?number(V21), ?number(V22), ?number(V23), ?number(V24), ?number(V25), ?number(V26), ?number(V27), ?float(V28), ?double(V29), ?string(V30), ?string(V31), ?record(V32), ?list_bool(V33), ?list_int8(V34), ?list_uint8(V35), ?list_int16(V36), ?list_uint16(V37), ?list_int32(V38), ?list_uint32(V39), ?list_int64(V40), ?list_uint64(V41), ?list_integer(V42), ?list_integer(V43), ?list_integer(V44), ?list_integer(V45), ?list_number(V46), ?list_number(V47), ?list_number(V48), ?list_number(V49), ?list_number(V50), ?list_number(V51), ?list_float(V52), ?list_double(V53), ?list_string(V54), ?list_record(V55)];
  471. encodeRec(_) ->
  472. [].
  473. getMsgSchema(test)->
  474. getMsgSchema(1);
  475. getMsgSchema(1)->
  476. [string];
  477. getMsgSchema(phoneNumber)->
  478. getMsgSchema(2);
  479. getMsgSchema(2)->
  480. [test,int32];
  481. getMsgSchema(person)->
  482. getMsgSchema(3);
  483. getMsgSchema(3)->
  484. [string,int32,string,{list,phoneNumber}];
  485. getMsgSchema(addressBook)->
  486. getMsgSchema(4);
  487. getMsgSchema(4)->
  488. [{list,person},{list,person}];
  489. getMsgSchema(union)->
  490. getMsgSchema(5);
  491. getMsgSchema(5)->
  492. [string,int32];
  493. getMsgSchema(tbool)->
  494. getMsgSchema(6);
  495. getMsgSchema(6)->
  496. [bool];
  497. getMsgSchema(tint8)->
  498. getMsgSchema(7);
  499. getMsgSchema(7)->
  500. [int8,int8];
  501. getMsgSchema(tuint8)->
  502. getMsgSchema(8);
  503. getMsgSchema(8)->
  504. [uint8,uint8];
  505. getMsgSchema(tint16)->
  506. getMsgSchema(9);
  507. getMsgSchema(9)->
  508. [int16,int16];
  509. getMsgSchema(tuint16)->
  510. getMsgSchema(10);
  511. getMsgSchema(10)->
  512. [uint16,uint16];
  513. getMsgSchema(tint32)->
  514. getMsgSchema(11);
  515. getMsgSchema(11)->
  516. [int32,int32];
  517. getMsgSchema(tuint32)->
  518. getMsgSchema(12);
  519. getMsgSchema(12)->
  520. [uint32,uint32];
  521. getMsgSchema(tint64)->
  522. getMsgSchema(13);
  523. getMsgSchema(13)->
  524. [int64,int64];
  525. getMsgSchema(tuint64)->
  526. getMsgSchema(14);
  527. getMsgSchema(14)->
  528. [uint64,uint64];
  529. getMsgSchema(tinteger)->
  530. getMsgSchema(15);
  531. getMsgSchema(15)->
  532. [integer,integer,integer,integer,integer,integer,integer,integer];
  533. getMsgSchema(tnumber)->
  534. getMsgSchema(16);
  535. getMsgSchema(16)->
  536. [number,number,number,number,number,number,number,number,number,number];
  537. getMsgSchema(tfloat)->
  538. getMsgSchema(17);
  539. getMsgSchema(17)->
  540. [float,float];
  541. getMsgSchema(tdouble)->
  542. getMsgSchema(18);
  543. getMsgSchema(18)->
  544. [double,double];
  545. getMsgSchema(tstring)->
  546. getMsgSchema(19);
  547. getMsgSchema(19)->
  548. [string,string];
  549. getMsgSchema(tlistbool)->
  550. getMsgSchema(20);
  551. getMsgSchema(20)->
  552. [{list,bool}];
  553. getMsgSchema(tlistint8)->
  554. getMsgSchema(21);
  555. getMsgSchema(21)->
  556. [{list,int8}];
  557. getMsgSchema(tlistuint8)->
  558. getMsgSchema(22);
  559. getMsgSchema(22)->
  560. [{list,uint8}];
  561. getMsgSchema(tlistint16)->
  562. getMsgSchema(23);
  563. getMsgSchema(23)->
  564. [{list,int16}];
  565. getMsgSchema(tlistuint16)->
  566. getMsgSchema(24);
  567. getMsgSchema(24)->
  568. [{list,uint16}];
  569. getMsgSchema(tlistint32)->
  570. getMsgSchema(25);
  571. getMsgSchema(25)->
  572. [{list,int32}];
  573. getMsgSchema(tlistuint32)->
  574. getMsgSchema(26);
  575. getMsgSchema(26)->
  576. [{list,uint32}];
  577. getMsgSchema(tlistint64)->
  578. getMsgSchema(27);
  579. getMsgSchema(27)->
  580. [{list,int64}];
  581. getMsgSchema(tlistuint64)->
  582. getMsgSchema(28);
  583. getMsgSchema(28)->
  584. [{list,uint64}];
  585. getMsgSchema(tlistinteger)->
  586. getMsgSchema(29);
  587. getMsgSchema(29)->
  588. [{list,integer}];
  589. getMsgSchema(tlistnumber)->
  590. getMsgSchema(30);
  591. getMsgSchema(30)->
  592. [{list,number}];
  593. getMsgSchema(tlistfloat)->
  594. getMsgSchema(31);
  595. getMsgSchema(31)->
  596. [{list,float}];
  597. getMsgSchema(tlistdouble)->
  598. getMsgSchema(32);
  599. getMsgSchema(32)->
  600. [{list,double}];
  601. getMsgSchema(tliststring)->
  602. getMsgSchema(33);
  603. getMsgSchema(33)->
  604. [{list,string}];
  605. getMsgSchema(tlistunion)->
  606. getMsgSchema(34);
  607. getMsgSchema(34)->
  608. [{list,union}];
  609. getMsgSchema(allType)->
  610. getMsgSchema(35);
  611. getMsgSchema(35)->
  612. [bool,int8,uint8,int16,uint16,int32,uint32,int64,uint64,integer,integer,
  613. integer,integer,integer,integer,integer,integer,number,number,number,number,
  614. number,number,number,number,number,number,float,double,string,string,union,
  615. {list,bool},
  616. {list,int8},
  617. {list,uint8},
  618. {list,int16},
  619. {list,uint16},
  620. {list,int32},
  621. {list,uint32},
  622. {list,int64},
  623. {list,uint64},
  624. {list,integer},
  625. {list,integer},
  626. {list,integer},
  627. {list,integer},
  628. {list,number},
  629. {list,number},
  630. {list,number},
  631. {list,number},
  632. {list,number},
  633. {list,number},
  634. {list,float},
  635. {list,double},
  636. {list,string},
  637. {list,union}];
  638. getMsgSchema(_) ->
  639. [].