proto_tools.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. export default class proto_tools{
  2. public static header_size: number = 10;
  3. // cmd_buf dataview
  4. public static read_int8(cmd_buf, offset) {
  5. // return cmd_buf.readInt8(offset);
  6. return cmd_buf.getInt8(offset);
  7. }
  8. public static write_int8(cmd_buf, offset, value) {
  9. // cmd_buf.writeInt8(value, offset);
  10. cmd_buf.setInt8(offset, value);
  11. }
  12. public static read_int16(cmd_buf, offset) {
  13. return cmd_buf.getInt16(offset, true);
  14. }
  15. public static write_int16(cmd_buf, offset, value) {
  16. // cmd_buf.writeInt16LE(value, offset);
  17. cmd_buf.setInt16(offset, value, true);
  18. }
  19. public static read_int32(cmd_buf, offset) {
  20. // return cmd_buf.readInt32LE(offset);
  21. return cmd_buf.getInt32(offset, true);
  22. }
  23. public static write_int32(cmd_buf, offset, value) {
  24. // cmd_buf.writeInt32LE(value, offset);
  25. cmd_buf.setInt32(offset, value, true);
  26. }
  27. public static read_uint32(cmd_buf, offset) {
  28. return cmd_buf.getUint32(offset, true);
  29. }
  30. public static write_uint32(cmd_buf, offset, value) {
  31. cmd_buf.setUint32(offset, value, true);
  32. }
  33. public static read_str(cmd_buf, offset, byte_len) {
  34. // return cmd_buf.toString("utf8", offset, offset + byte_len);
  35. return cmd_buf.read_utf8(offset, byte_len);
  36. }
  37. public static write_str(cmd_buf, offset, str) {
  38. // cmd_buf.write(str, offset);
  39. cmd_buf.write_utf8(offset, str);
  40. }
  41. public static read_float(cmd_buf, offset) {
  42. // return cmd_buf.readFloatLE(offset);
  43. return cmd_buf.getFloat32(offset, true);
  44. }
  45. public static write_float(cmd_buf, offset, value) {
  46. // cmd_buf.writeFloatLE(value, offset);
  47. cmd_buf.setFloat32(offset, value, true);
  48. }
  49. public static alloc_buffer(total_len) {
  50. // return Buffer.allocUnsafe(total_len);
  51. var buf = new ArrayBuffer(total_len);
  52. var dataview = new DataView(buf);
  53. return dataview;
  54. }
  55. public static write_cmd_header_inbuf(cmd_buf, stype, ctype) {
  56. this.write_int16(cmd_buf, 0, stype);
  57. this.write_int16(cmd_buf, 2, ctype);
  58. this.write_uint32(cmd_buf, 4, 0);
  59. return this.header_size;
  60. }
  61. public static write_prototype_inbuf(cmd_buf, proto_type) {
  62. this.write_int16(cmd_buf, 8, proto_type);
  63. }
  64. public static write_str_inbuf(cmd_buf, offset, str, byte_len) {
  65. // 写入2个字节字符串长度信息;
  66. this.write_int16(cmd_buf, offset, byte_len);
  67. offset += 2;
  68. this.write_str(cmd_buf, offset, str);
  69. offset += byte_len;
  70. return offset;
  71. }
  72. // 返回 str, offset
  73. public static read_str_inbuf(cmd_buf, offset) {
  74. var byte_len = this.read_int16(cmd_buf, offset);
  75. offset += 2;
  76. var str = this.read_str(cmd_buf, offset, byte_len);
  77. offset += byte_len;
  78. return [str, offset];
  79. }
  80. public static decode_empty_cmd(cmd_buf) {
  81. var cmd = {};
  82. cmd[0] = this.read_int16(cmd_buf, 0);
  83. cmd[1] = this.read_int16(cmd_buf, 2);
  84. cmd[2] = null;
  85. return cmd;
  86. }
  87. public static encode_empty_cmd(stype, ctype, body) {
  88. var cmd_buf = this.alloc_buffer(this.header_size);
  89. this.write_cmd_header_inbuf(cmd_buf, stype, ctype);
  90. return cmd_buf;
  91. }
  92. public static encode_status_cmd(stype, ctype, status) {
  93. var cmd_buf = this.alloc_buffer(this.header_size + 2);
  94. this.write_cmd_header_inbuf(cmd_buf, stype, ctype);
  95. this.write_int16(cmd_buf, this.header_size, status);
  96. return cmd_buf;
  97. }
  98. public static decode_status_cmd(cmd_buf) {
  99. var cmd = {};
  100. cmd[0] = this.read_int16(cmd_buf, 0);
  101. cmd[1] = this.read_int16(cmd_buf, 2);
  102. cmd[2] = this.read_int16(cmd_buf, this.header_size);
  103. return cmd;
  104. }
  105. public static encode_str_cmd(stype, ctype, str) {
  106. var byte_len = str.utf8_byte_len();
  107. var total_len = this.header_size + 2 + byte_len;
  108. var cmd_buf = this.alloc_buffer(total_len);
  109. var offset = this.write_cmd_header_inbuf(cmd_buf, stype, ctype);
  110. offset = this.write_str_inbuf(cmd_buf, offset, str, byte_len);
  111. return cmd_buf;
  112. }
  113. public static decode_str_cmd(cmd_buf) {
  114. var cmd = {};
  115. cmd[0] = this.read_int16(cmd_buf, 0);
  116. cmd[1] = this.read_int16(cmd_buf, 2);
  117. var ret = this.read_str_inbuf(cmd_buf, this.header_size);
  118. cmd[2] = ret[0];
  119. return cmd;
  120. }
  121. }