erlang自定义二进制协议
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

679 rader
21 KiB

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