PacketHelper.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var BaseDefine = require("BaseDefine")
  2. var Global = require("Global")
  3. class RecvPacketHelper {
  4. constructor(DataPos, DataSize) {
  5. this.m_wDataPos = DataPos; //WORD 数据点
  6. this.m_wDataSize = DataSize; //WORD 数据大小
  7. }
  8. GetData(DataDescribe, dataView) {
  9. if (this.m_wDataPos >= this.m_wDataSize) {
  10. DataDescribe.wDataSize = 0;
  11. DataDescribe.wDataDescribe = BaseDefine.DTP_NULL;
  12. return null;
  13. }
  14. if (this.m_wDataPos <= this.m_wDataSize) {
  15. DataDescribe.wDataSize = dataView.getUint16(this.m_wDataPos, true);
  16. DataDescribe.wDataDescribe = dataView.getUint16(this.m_wDataPos + 2, true);
  17. }
  18. //效验数据
  19. if ((this.m_wDataPos + DataDescribe.wDataSize) > this.m_wDataSize) {
  20. DataDescribe.wDataSize = 0;
  21. DataDescribe.wDataDescribe = BaseDefine.DTP_NULL;
  22. return null;
  23. }
  24. var buff = "";
  25. if (DataDescribe.wDataSize > 0) {
  26. buff = dataView.buffer.slice(4 + this.m_wDataPos, 4 + this.m_wDataPos + DataDescribe.wDataSize);
  27. }
  28. this.m_wDataPos += 4 + DataDescribe.wDataSize;
  29. return buff;
  30. }
  31. };
  32. class SendPacketHelper {
  33. constructor(DataSize, MaxBytes) {
  34. this.m_wDataSize = DataSize; //WORD 数据大小
  35. this.m_wMaxBytes = MaxBytes; //WORD 缓冲大小
  36. }
  37. AddPacket(DataDescribe, dataView, buf) {
  38. if (DataDescribe.wDataDescribe === BaseDefine.DTP_NULL) return false;
  39. if ((DataDescribe.wDataSize + 4 + this.m_wDataSize) > this.m_wMaxBytes) return false;
  40. dataView.setUint16(this.m_wDataSize, DataDescribe.wDataSize, true);
  41. this.m_wDataSize += 2;
  42. dataView.setUint16(this.m_wDataSize, DataDescribe.wDataDescribe, true);
  43. this.m_wDataSize += 2;
  44. if (DataDescribe.wDataSize > 0) {
  45. // var strBuf = Global.ToUTF_8(buf);
  46. // for (var num = 0; num < strBuf.length - 2; ++num) {
  47. // dv.setUint8(this.m_wDataSize + num, strBuf[2 + i], true);
  48. // }
  49. Global.WriteStr2DataView(dataView, this.m_wDataSize, buf);
  50. this.m_wDataSize += DataDescribe.wDataSize;
  51. }
  52. return true;
  53. }
  54. GetDataSize() { return this.m_wDataSize; }
  55. };
  56. module.exports = { RecvPacketHelper, SendPacketHelper }