erlang自定义二进制协议
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

671 lines
20 KiB

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