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

549 行
14 KiB

  1. -module(test).
  2. -include("protoMsg.hrl").
  3. -compile(export_all).
  4. test1() ->
  5. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tbool{bool = true}))).
  6. test21() ->
  7. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint8{int1 = 123, int2 = -22}))).
  8. test22() ->
  9. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint8{int1 = 123, int2 = 182}))).
  10. test31() ->
  11. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint16{int1 = 12343, int2 = -3422}))).
  12. test32() ->
  13. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint16{int1 = 43244, int2 = 43243}))).
  14. test41() ->
  15. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint32{int1 = 12343434, int2 = -34434322}))).
  16. test42() ->
  17. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint32{int1 = 432444343, int2 = 432443433}))).
  18. test51() ->
  19. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint64{int1 = 12344343434, int2 = -344343434322}))).
  20. test52() ->
  21. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint64{int1 = 4343432444343, int2 = 4324434343433}))).
  22. test6() ->
  23. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434}))).
  24. test7() ->
  25. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tnumber{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434, float1 = -34234343.343, float2 = 43242342342342.434}))).
  26. test81() ->
  27. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tfloat{int1 = -34234343.343, int2 = 42342342.434}))).
  28. test82() ->
  29. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tdouble{int1 = -342343433333.343, int2 = 423423423333.434}))).
  30. test9() ->
  31. protoMsg:decode(iolist_to_binary(protoMsg:encode(#tstring{int1 = "dfdf143242", int2 = "发地方撒发送"}))).
  32. allType() ->
  33. AllType = #allType{
  34. bool = false
  35. ,int8 = -66
  36. ,uint8 = 66
  37. ,int16 = -666
  38. ,uint16 = 666
  39. ,int32 = -66666
  40. ,uint32 = 66666
  41. ,int64 = -5294967296
  42. ,uint64 = 5294967296
  43. ,inte8 = -88
  44. ,uinte8 = 88
  45. ,inte16 = -8888
  46. ,uinte16 = 8888
  47. ,inte32 = -888888
  48. ,uinte32 = 888888
  49. ,inte64 = -8888888888
  50. ,uinte64 = 8888888888
  51. ,num8 = -99
  52. ,unum8 = 99
  53. ,num16 = -9999
  54. ,unum16 = 9999
  55. ,num32 = -999999
  56. ,unum32 = 999999
  57. ,num64 = -9999999999
  58. ,unum64 = 9999999999
  59. ,numfloat = 999999.99999
  60. ,numdouble = 9999999999.99999
  61. ,float = 123456.789321
  62. ,double = 4300000000.789321
  63. ,string1 = "this is a test!!!!"
  64. ,string2 = "这是一个测试, 等会看结果~!!!"
  65. ,lbool = [true, false, true, false]
  66. ,lint8 = [123, -123, 66, -66, 88, -88]
  67. ,luint8 = [1,2,3,123,67,88]
  68. ,lint16 = [-12345, 12345, 6666, -6666]
  69. ,luint16 = [12345, 12345, 6666, 6666]
  70. ,lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666]
  71. ,luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666]
  72. ,lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666]
  73. ,luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666]
  74. ,linte8 = [123, 12, -66, 66, 34]
  75. ,linte16 = [12334, 12, -6666, 6666, 3412]
  76. ,linte32 = [12334, 12, -666666666, 6666, 3412]
  77. ,linte64 = [12334, 12, -666666666666, 6666, 3412]
  78. ,lnum8 = [123, 12.123, -66.456789, 66, 34]
  79. ,lnum16 = [12334, -12, -6666.6666, 6666, 3412]
  80. ,lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412]
  81. ,lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999]
  82. ,lnfloat32 = [-666666.88888,4434.434, 434.43, 3434]
  83. ,lnfloat64 = [-666666666666.88888,4434.434, 434.43, 11111111111.34343,5566]
  84. ,lfloat = [1.1, 2.2, 3.3, 666666.666]
  85. ,ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666]
  86. ,lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式">>]
  87. ,lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}]
  88. },
  89. List = protoMsg:encode(AllType),
  90. io:format("~p~n",[List]),
  91. protoMsg:decode(iolist_to_binary(List)).
  92. tall1(0) ->
  93. ok;
  94. tall1(N) ->
  95. AllType = #allType{
  96. bool = false
  97. ,int8 = -66
  98. ,uint8 = 66
  99. ,int16 = -666
  100. ,uint16 = 666
  101. ,int32 = -66666
  102. ,uint32 = 66666
  103. ,int64 = -5294967296
  104. ,uint64 = 5294967296
  105. ,inte8 = -88
  106. ,uinte8 = 88
  107. ,inte16 = -8888
  108. ,uinte16 = 8888
  109. ,inte32 = -888888
  110. ,uinte32 = 888888
  111. ,inte64 = -8888888888
  112. ,uinte64 = 8888888888
  113. ,num8 = -99
  114. ,unum8 = 99
  115. ,num16 = -9999
  116. ,unum16 = 9999
  117. ,num32 = -999999
  118. ,unum32 = 999999
  119. ,num64 = -9999999999
  120. ,unum64 = 9999999999
  121. ,numfloat = 999999.99999
  122. ,numdouble = 9999999999.99999
  123. ,float = 123456.789321
  124. ,double = 4300000000.789321
  125. ,string1 = "this is a test!!!!"
  126. ,string2 = "这是一个测试, 等会看结果~!!!"
  127. ,lbool = [true, false, true, false]
  128. ,lint8 = [123, -123, 66, -66, 88, -88]
  129. ,luint8 = [1,2,3,123,67,88]
  130. ,lint16 = [-12345, 12345, 6666, -6666]
  131. ,luint16 = [12345, 12345, 6666, 6666]
  132. ,lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666]
  133. ,luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666]
  134. ,lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666]
  135. ,luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666]
  136. ,linte8 = [123, 12, -66, 66, 34]
  137. ,linte16 = [12334, 12, -6666, 6666, 3412]
  138. ,linte32 = [12334, 12, -666666666, 6666, 3412]
  139. ,linte64 = [12334, 12, -666666666666, 6666, 3412]
  140. ,lnum8 = [123, 12.123, -66.456789, 66, 34]
  141. ,lnum16 = [12334, -12, -6666.6666, 6666, 3412]
  142. ,lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412]
  143. ,lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999]
  144. ,lnfloat32 = [-666666.88888,4434.434, 434.43, 3434]
  145. ,lnfloat64 = [-666666666666.88888,4434.434, 434.43, 11111111111.34343,5566]
  146. ,lfloat = [1.1, 2.2, 3.3, 666666.666]
  147. ,ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666]
  148. ,lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式">>]
  149. ,lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}]
  150. },
  151. %protoMsg:encode(AllType),
  152. term_to_binary(AllType),
  153. tall1(N - 1).
  154. tall(N) ->
  155. AllType = #allType{
  156. bool = false
  157. ,int8 = -66
  158. ,uint8 = 66
  159. ,int16 = -666
  160. ,uint16 = 666
  161. ,int32 = -66666
  162. ,uint32 = 66666
  163. ,int64 = -5294967296
  164. ,uint64 = 5294967296
  165. ,inte8 = -88
  166. ,uinte8 = 88
  167. ,inte16 = -8888
  168. ,uinte16 = 8888
  169. ,inte32 = -888888
  170. ,uinte32 = 888888
  171. ,inte64 = -8888888888
  172. ,uinte64 = 8888888888
  173. ,num8 = -99
  174. ,unum8 = 99
  175. ,num16 = -9999
  176. ,unum16 = 9999
  177. ,num32 = -999999
  178. ,unum32 = 999999
  179. ,num64 = -9999999999
  180. ,unum64 = 9999999999
  181. ,numfloat = 999999.99999
  182. ,numdouble = 9999999999.99999
  183. ,float = 123456.789321
  184. ,double = 4300000000.789321
  185. ,string1 = "this is a test!!!!"
  186. ,string2 = "这是一个测试, 等会看结果~!!!"
  187. ,lbool = [true, false, true, false]
  188. ,lint8 = [123, -123, 66, -66, 88, -88]
  189. ,luint8 = [1,2,3,123,67,88]
  190. ,lint16 = [-12345, 12345, 6666, -6666]
  191. ,luint16 = [12345, 12345, 6666, 6666]
  192. ,lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666]
  193. ,luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666]
  194. ,lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666]
  195. ,luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666]
  196. ,linte8 = [123, 12, -66, 66, 34]
  197. ,linte16 = [12334, 12, -6666, 6666, 3412]
  198. ,linte32 = [12334, 12, -666666666, 6666, 3412]
  199. ,linte64 = [12334, 12, -666666666666, 6666, 3412]
  200. ,lnum8 = [123, 12.123, -66.456789, 66, 34]
  201. ,lnum16 = [12334, -12, -6666.6666, 6666, 3412]
  202. ,lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412]
  203. ,lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999]
  204. ,lnfloat32 = [-666666.88888,4434.434, 434.43, 3434]
  205. ,lnfloat64 = [-666666666666.88888,4434.434, 434.43, 11111111111.34343,5566]
  206. ,lfloat = [1.1, 2.2, 3.3, 666666.666]
  207. ,ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666]
  208. ,lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式">>]
  209. ,lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}]
  210. },
  211. %List = protoMsg:encode(AllType),
  212. tall(N, term_to_binary(AllType)).
  213. tall(0, Bin) ->
  214. Bin;
  215. tall(N, Bin) ->
  216. %protoMsg:decode(Bin),
  217. binary_to_term(Bin),
  218. tall(N - 1, Bin).
  219. hh1(0) ->
  220. ok;
  221. hh1(N) ->
  222. A = [tt1,tt2,tt3,tt4,{tet,tt1},{tet, tt2},{tet,tt3}, yyy],
  223. test(A),
  224. hh1(N -1).
  225. hh2(0) ->
  226. ok;
  227. hh2(N) ->
  228. A = [tt1,tt2,tt3,tt4,{tet,tt1},{tet, tt2},{tet,tt3}, yyy],
  229. tet(A),
  230. hh2(N -1).
  231. test([]) ->
  232. ok;
  233. test([A | T]) ->
  234. case A of
  235. tt1 ->
  236. ok;
  237. tt2 ->
  238. ok;
  239. tt3 ->
  240. ok;
  241. tt4 ->
  242. ok;
  243. {tet, tt1} ->
  244. ok;
  245. {tet, tt2} ->
  246. ok;
  247. {tet, tt3} ->
  248. ok;
  249. _ ->
  250. ok
  251. end,
  252. test(T).
  253. tet([]) ->
  254. ok;
  255. tet([tt1 | T]) ->
  256. ok,
  257. tet(T);
  258. tet([tt2 | T]) ->
  259. ok,
  260. tet(T);
  261. tet([tt3 | T]) ->
  262. ok,
  263. tet(T);
  264. tet([tt4 | T]) ->
  265. ok,
  266. tet(T);
  267. tet([{tet, tt1} | T]) ->
  268. ok,
  269. tet(T);
  270. tet([{tet, tt2} | T]) ->
  271. ok,
  272. tet(T);
  273. tet([{tet, tt3} | T]) ->
  274. ok,
  275. tet(T);
  276. tet([YY | T]) ->
  277. ok,
  278. tet(T).
  279. tt1(0)->
  280. AddressBook = #addressBook{
  281. person = [
  282. #person{
  283. name = "Alice",
  284. id = 10000,
  285. phone = [
  286. #phoneNumber{ number = #test{aa = "123456789"} , type = 1 },
  287. #phoneNumber{ number = #test{aa = "87654321"} , type = 2 }
  288. ]
  289. },
  290. #person{
  291. name = "Bob",
  292. id = 20000,
  293. phone = [
  294. #phoneNumber{number = #test{aa = "01234567890"} , type = 3 }
  295. ]
  296. }
  297. ]
  298. },
  299. Bin = protoMsg:encode(AddressBook);
  300. tt1(N) ->
  301. tt1(),
  302. tt1(N- 1).
  303. tt1() ->
  304. AddressBook = #addressBook{
  305. person = [
  306. #person{
  307. name = "Alice",
  308. id = 10000,
  309. phone = [
  310. #phoneNumber{ number = #test{aa = "123456789"} , type = 1 },
  311. #phoneNumber{ number = #test{aa = "87654321"} , type = 2 }
  312. ]
  313. },
  314. #person{
  315. name = "Bob",
  316. id = 20000,
  317. phone = [
  318. #phoneNumber{number = #test{aa = "01234567890"} , type = 3 }
  319. ]
  320. }
  321. ]
  322. },
  323. %"others" => [
  324. % #{
  325. % "name" => "Carol",
  326. % "id" => 30000,
  327. % "phone" => [
  328. % #{ "number" => #{"aa" => "9876543210"} }
  329. % ]
  330. % }
  331. %]
  332. %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] },
  333. %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 },
  334. Bin = protoMsg:encode(AddressBook).
  335. %ok = file:write_file("fff.bin", Bin),
  336. %print_bin(Bin),
  337. %Bin = protoCode:encode(AddressBook),
  338. %ok = file:write_file("fff.bin", Bin),
  339. %print_bin(Bin),
  340. %MsgId = protoMsg:getMsgId(element(1, AddressBook)),
  341. %<<MsgId:16/little, Bin/binary>>.
  342. tt(N) ->
  343. AddressBook = #addressBook{
  344. person = [
  345. #person{
  346. name = "Alice",
  347. id = 10000,
  348. phone = [
  349. #phoneNumber{ number = #test{aa = "123456789"} , type = 1 },
  350. #phoneNumber{ number = #test{aa = "87654321"} , type = 2 }
  351. ]
  352. },
  353. #person{
  354. name = "Bob",
  355. id = 20000,
  356. phone = [
  357. #phoneNumber{number = #test{aa = "01234567890"} , type = 3 }
  358. ]
  359. }
  360. ]
  361. },
  362. %"others" => [
  363. % #{
  364. % "name" => "Carol",
  365. % "id" => 30000,
  366. % "phone" => [
  367. % #{ "number" => #{"aa" => "9876543210"} }
  368. % ]
  369. % }
  370. %]
  371. %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] },
  372. %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 },
  373. Bin = protoMsg:encode(AddressBook),
  374. %ok = file:write_file("fff.bin", Bin),
  375. %print_bin(Bin),
  376. tt(N, iolist_to_binary(Bin)).
  377. tt(0, Bin) ->
  378. protoMsg:decode(Bin);
  379. tt(N, Bin) ->
  380. protoMsg:decode(Bin),
  381. tt(N-1, Bin).
  382. %{Len, List, RestBin} = protoMsg("AddressBook", Bin),
  383. %io:format("Len:~p, RestBin:~p~n", [Len, RestBin]),
  384. %io:format("List:~p~n", [List]),
  385. %{Map, _, _} = sproto:decode2("AddressBook", Bin),
  386. %Map.
  387. ttt1(0)->
  388. AddressBook = #addressBook{
  389. person = [
  390. #person{
  391. name = "Alice",
  392. id = 10000,
  393. phone = [
  394. #phoneNumber{ number = #test{aa = "你好啊 嘿嘿"} , type = 1 },
  395. #phoneNumber{ number = #test{aa = "87654321"} , type = 2 }
  396. ]
  397. },
  398. #person{
  399. name = "Bob",
  400. id = 20000,
  401. phone = [
  402. #phoneNumber{number = #test{aa = "范德萨地方范德萨发"} , type = 3 }
  403. ]
  404. }
  405. ]
  406. },
  407. term_to_binary(AddressBook);
  408. ttt1(N) ->
  409. ttt1(),
  410. ttt1(N- 1).
  411. ttt1() ->
  412. AddressBook = #addressBook{
  413. person = [
  414. #person{
  415. name = "Alice",
  416. id = 10000,
  417. phone = [
  418. #phoneNumber{ number = #test{aa = "你好啊 嘿嘿"} , type = 1 },
  419. #phoneNumber{ number = #test{aa = "87654321"} , type = 2 }
  420. ]
  421. },
  422. #person{
  423. name = "Bob",
  424. id = 20000,
  425. phone = [
  426. #phoneNumber{number = #test{aa = "范德萨地方范德萨发"} , type = 3 }
  427. ]
  428. }
  429. ]
  430. },
  431. %"others" => [
  432. % #{
  433. % "name" => "Carol",
  434. % "id" => 30000,
  435. % "phone" => [
  436. % #{ "number" => #{"aa" => "9876543210"} }
  437. % ]
  438. % }
  439. %]
  440. %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] },
  441. %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 },
  442. Bin = term_to_binary(AddressBook).
  443. %ok = file:write_file("fff.bin", Bin),
  444. %print_bin(Bin),
  445. %Bin = protoCode:encode(AddressBook),
  446. %ok = file:write_file("fff.bin", Bin),
  447. %print_bin(Bin),
  448. %MsgId = protoMsg:getMsgId(element(1, AddressBook)),
  449. %<<MsgId:16/little, Bin/binary>>.
  450. ttt(N) ->
  451. AddressBook = #addressBook{
  452. person = [
  453. #person{
  454. name = "Alice",
  455. id = 10000,
  456. phone = [
  457. #phoneNumber{ number = #test{aa = "123456789"} , type = 1 },
  458. #phoneNumber{ number = #test{aa = "87654321"} , type = 2 }
  459. ]
  460. },
  461. #person{
  462. name = "Bob",
  463. id = 20000,
  464. phone = [
  465. #phoneNumber{number = #test{aa = "01234567890"} , type = 3 }
  466. ]
  467. }
  468. ]
  469. },
  470. %"others" => [
  471. % #{
  472. % "name" => "Carol",
  473. % "id" => 30000,
  474. % "phone" => [
  475. % #{ "number" => #{"aa" => "9876543210"} }
  476. % ]
  477. % }
  478. %]
  479. %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] },
  480. %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 },
  481. %ok = file:write_file("fff.bin", Bin),
  482. %print_bin(Bin),
  483. ttt(N, term_to_binary(AddressBook)).
  484. ttt(0, Bin) ->
  485. binary_to_term(Bin);
  486. ttt(N, Bin) ->
  487. binary_to_term(Bin),
  488. ttt(N-1, Bin).
  489. print_bin(Bin) ->
  490. ByteList = lists:reverse(bin_to_hex(Bin, [])),
  491. Fun = fun(Byte, Acc) ->
  492. io:format("~2.16.0b ", [Byte]),
  493. case Acc rem 8 =:= 0 of
  494. true -> io:format("~n", []);
  495. false -> ok
  496. end,
  497. Acc + 1
  498. end,
  499. lists:foldl(Fun, 1, ByteList),
  500. io:format("bytes:~p~n", [byte_size(Bin)]).
  501. bin_to_hex(<<>>, Acc) ->
  502. Acc;
  503. bin_to_hex(Bin, Acc) ->
  504. <<A:8, Bin2/binary>> = Bin,
  505. bin_to_hex(Bin2, [A|Acc]).