packet_parser.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. 'use strict';
  2. const Packet = require('./packets/packet.js');
  3. const MAX_PACKET_LENGTH = 16777215;
  4. function readPacketLength(b, off) {
  5. const b0 = b[off];
  6. const b1 = b[off + 1];
  7. const b2 = b[off + 2];
  8. if (b1 + b2 === 0) {
  9. return b0;
  10. }
  11. return b0 + (b1 << 8) + (b2 << 16);
  12. }
  13. class PacketParser {
  14. constructor(onPacket, packetHeaderLength) {
  15. // 4 for normal packets, 7 for comprssed protocol packets
  16. if (typeof packetHeaderLength === 'undefined') {
  17. packetHeaderLength = 4;
  18. }
  19. // array of last payload chunks
  20. // only used when current payload is not complete
  21. this.buffer = [];
  22. // total length of chunks on buffer
  23. this.bufferLength = 0;
  24. this.packetHeaderLength = packetHeaderLength;
  25. // incomplete header state: number of header bytes received
  26. this.headerLen = 0;
  27. // expected payload length
  28. this.length = 0;
  29. this.largePacketParts = [];
  30. this.firstPacketSequenceId = 0;
  31. this.onPacket = onPacket;
  32. this.execute = PacketParser.prototype.executeStart;
  33. this._flushLargePacket =
  34. packetHeaderLength === 7
  35. ? this._flushLargePacket7
  36. : this._flushLargePacket4;
  37. }
  38. _flushLargePacket4() {
  39. const numPackets = this.largePacketParts.length;
  40. this.largePacketParts.unshift(Buffer.from([0, 0, 0, 0])); // insert header
  41. const body = Buffer.concat(this.largePacketParts);
  42. const packet = new Packet(this.firstPacketSequenceId, body, 0, body.length);
  43. this.largePacketParts.length = 0;
  44. packet.numPackets = numPackets;
  45. this.onPacket(packet);
  46. }
  47. _flushLargePacket7() {
  48. const numPackets = this.largePacketParts.length;
  49. this.largePacketParts.unshift(Buffer.from([0, 0, 0, 0, 0, 0, 0])); // insert header
  50. const body = Buffer.concat(this.largePacketParts);
  51. this.largePacketParts.length = 0;
  52. const packet = new Packet(this.firstPacketSequenceId, body, 0, body.length);
  53. packet.numPackets = numPackets;
  54. this.onPacket(packet);
  55. }
  56. executeStart(chunk) {
  57. let start = 0;
  58. const end = chunk.length;
  59. while (end - start >= 3) {
  60. this.length = readPacketLength(chunk, start);
  61. if (end - start >= this.length + this.packetHeaderLength) {
  62. // at least one full packet
  63. const sequenceId = chunk[start + 3];
  64. if (
  65. this.length < MAX_PACKET_LENGTH &&
  66. this.largePacketParts.length === 0
  67. ) {
  68. this.onPacket(
  69. new Packet(
  70. sequenceId,
  71. chunk,
  72. start,
  73. start + this.packetHeaderLength + this.length
  74. )
  75. );
  76. } else {
  77. // first large packet - remember it's id
  78. if (this.largePacketParts.length === 0) {
  79. this.firstPacketSequenceId = sequenceId;
  80. }
  81. this.largePacketParts.push(
  82. chunk.slice(
  83. start + this.packetHeaderLength,
  84. start + this.packetHeaderLength + this.length
  85. )
  86. );
  87. if (this.length < MAX_PACKET_LENGTH) {
  88. this._flushLargePacket();
  89. }
  90. }
  91. start += this.packetHeaderLength + this.length;
  92. } else {
  93. // payload is incomplete
  94. this.buffer = [chunk.slice(start + 3, end)];
  95. this.bufferLength = end - start - 3;
  96. this.execute = PacketParser.prototype.executePayload;
  97. return;
  98. }
  99. }
  100. if (end - start > 0) {
  101. // there is start of length header, but it's not full 3 bytes
  102. this.headerLen = end - start; // 1 or 2 bytes
  103. this.length = chunk[start];
  104. if (this.headerLen === 2) {
  105. this.length = chunk[start] + (chunk[start + 1] << 8);
  106. this.execute = PacketParser.prototype.executeHeader3;
  107. } else {
  108. this.execute = PacketParser.prototype.executeHeader2;
  109. }
  110. }
  111. }
  112. executePayload(chunk) {
  113. let start = 0;
  114. const end = chunk.length;
  115. const remainingPayload =
  116. this.length - this.bufferLength + this.packetHeaderLength - 3;
  117. if (end - start >= remainingPayload) {
  118. // last chunk for payload
  119. const payload = Buffer.allocUnsafe(this.length + this.packetHeaderLength);
  120. let offset = 3;
  121. for (let i = 0; i < this.buffer.length; ++i) {
  122. this.buffer[i].copy(payload, offset);
  123. offset += this.buffer[i].length;
  124. }
  125. chunk.copy(payload, offset, start, start + remainingPayload);
  126. const sequenceId = payload[3];
  127. if (
  128. this.length < MAX_PACKET_LENGTH &&
  129. this.largePacketParts.length === 0
  130. ) {
  131. this.onPacket(
  132. new Packet(
  133. sequenceId,
  134. payload,
  135. 0,
  136. this.length + this.packetHeaderLength
  137. )
  138. );
  139. } else {
  140. // first large packet - remember it's id
  141. if (this.largePacketParts.length === 0) {
  142. this.firstPacketSequenceId = sequenceId;
  143. }
  144. this.largePacketParts.push(
  145. payload.slice(
  146. this.packetHeaderLength,
  147. this.packetHeaderLength + this.length
  148. )
  149. );
  150. if (this.length < MAX_PACKET_LENGTH) {
  151. this._flushLargePacket();
  152. }
  153. }
  154. this.buffer = [];
  155. this.bufferLength = 0;
  156. this.execute = PacketParser.prototype.executeStart;
  157. start += remainingPayload;
  158. if (end - start > 0) {
  159. return this.execute(chunk.slice(start, end));
  160. }
  161. } else {
  162. this.buffer.push(chunk);
  163. this.bufferLength += chunk.length;
  164. }
  165. return null;
  166. }
  167. executeHeader2(chunk) {
  168. this.length += chunk[0] << 8;
  169. if (chunk.length > 1) {
  170. this.length += chunk[1] << 16;
  171. this.execute = PacketParser.prototype.executePayload;
  172. return this.executePayload(chunk.slice(2));
  173. }
  174. this.execute = PacketParser.prototype.executeHeader3;
  175. return null;
  176. }
  177. executeHeader3(chunk) {
  178. this.length += chunk[0] << 16;
  179. this.execute = PacketParser.prototype.executePayload;
  180. return this.executePayload(chunk.slice(1));
  181. }
  182. }
  183. module.exports = PacketParser;