proto_man.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import proto_tools from './proto_tools';
  2. export default class proto_man {
  3. // buf协议的编码/解码管理 stype, ctype --> encoder/decoder
  4. static decoders = {}; // 保存当前我们buf协议所有的解码函数, stype,ctype --> decoder;
  5. static encoders = {}; // 保存当前我们buf协议所有的编码函数, stype, ctype --> encoder
  6. static PROTO_JSON: number = 1;
  7. static PROTO_BUF: number = 2;
  8. // 加密
  9. static encrypt_cmd(str_of_buf) {
  10. return str_of_buf;
  11. }
  12. // 解密
  13. static decrypt_cmd(str_of_buf) {
  14. return str_of_buf;
  15. }
  16. static _json_encode(stype, ctype, body) {
  17. var cmd = {};
  18. cmd[0] = body;
  19. var str = JSON.stringify(cmd);
  20. var cmd_buf = proto_tools.encode_str_cmd(stype, ctype, str);
  21. return cmd_buf;
  22. }
  23. static _json_decode(cmd_buf) {
  24. var cmd = proto_tools.decode_str_cmd(cmd_buf);
  25. var cmd_json = cmd[2];
  26. try {
  27. var body_set = JSON.parse(cmd_json);
  28. cmd[2] = body_set[0];
  29. }
  30. catch(e) {
  31. return null;
  32. }
  33. if (!cmd ||
  34. typeof(cmd[0])=="undefined" ||
  35. typeof(cmd[1])=="undefined" ||
  36. typeof(cmd[2])=="undefined") {
  37. return null;
  38. }
  39. return cmd;
  40. }
  41. // key, value, stype + ctype -->key: value
  42. static get_key(stype, ctype) {
  43. return (stype * 65536 + ctype);
  44. }
  45. // 参数1: 协议类型 json, buf协议;
  46. // 参数2: 服务类型
  47. // 参数3: 命令号;
  48. // 参数4: 发送的数据本地,js对象/js文本,...
  49. // 返回是一段编码后的数据;
  50. static encode_cmd(proto_type, stype, ctype, body) {
  51. var buf = null;
  52. var dataview;
  53. if (proto_type == proto_man.PROTO_JSON) {
  54. dataview = this._json_encode(stype, ctype, body);
  55. }
  56. else { // buf协议
  57. var key = this.get_key(stype, ctype);
  58. if (!this.encoders[key]) {
  59. return null;
  60. }
  61. // end
  62. dataview = this.encoders[key](stype, ctype, body);
  63. }
  64. proto_tools.write_prototype_inbuf(dataview, proto_type);
  65. buf = dataview.buffer;
  66. if (buf) {
  67. buf = this.encrypt_cmd(buf); // 加密
  68. }
  69. return buf;
  70. }
  71. // 参数1: 协议类型
  72. // 参数2: 接手到的数据命令
  73. // 返回: {0: stype, 1, ctype, 2: body}
  74. static decode_cmd(proto_type, str_or_buf) {
  75. str_or_buf = this.decrypt_cmd(str_or_buf); // 解密
  76. var cmd = null;
  77. var dataview = new DataView(str_or_buf);
  78. if (dataview.byteLength < proto_tools.header_size) {
  79. return null;
  80. }
  81. if (proto_type == proto_man.PROTO_JSON) {
  82. return this._json_decode(dataview);
  83. }
  84. /*if (str_or_buf.length < 4) {
  85. return null;
  86. }*/
  87. var stype = proto_tools.read_int16(dataview, 0);
  88. var ctype = proto_tools.read_int16(dataview, 2);
  89. var key = this.get_key(stype, ctype);
  90. if (!this.decoders[key]) {
  91. return null;
  92. }
  93. // cmd = decoders[key](str_or_buf);
  94. cmd = this.decoders[key](dataview);
  95. return cmd;
  96. }
  97. // encode_func(body) return 二进制bufffer对象
  98. static reg_buf_encoder(stype, ctype, encode_func) {
  99. var key = this.get_key(stype, ctype);
  100. if (this.encoders[key]) { // 已经注册过了,是否搞错了
  101. // log.warn("stype: " + stype + " ctype: " + ctype + "is reged!!!");
  102. }
  103. this.encoders[key] = encode_func;
  104. }
  105. // decode_func(cmd_buf) return cmd { 0: 服务号, 1: 命令号, 2: body};
  106. static reg_buf_decoder(stype, ctype, decode_func) {
  107. var key = this.get_key(stype, ctype);
  108. if (this.decoders[key]) { // 已经注册过了,是否搞错了
  109. // log.warn("stype: " + stype + " ctype: " + ctype + "is reged!!!");
  110. }
  111. this.decoders[key] = decode_func;
  112. }
  113. }