1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- var BaseDefine = require("BaseDefine")
- var Global = require("Global")
- class RecvPacketHelper {
- constructor(DataPos, DataSize) {
- this.m_wDataPos = DataPos; //WORD 数据点
- this.m_wDataSize = DataSize; //WORD 数据大小
- }
- GetData(DataDescribe, dataView) {
- if (this.m_wDataPos >= this.m_wDataSize) {
- DataDescribe.wDataSize = 0;
- DataDescribe.wDataDescribe = BaseDefine.DTP_NULL;
- return null;
- }
- if (this.m_wDataPos <= this.m_wDataSize) {
- DataDescribe.wDataSize = dataView.getUint16(this.m_wDataPos, true);
- DataDescribe.wDataDescribe = dataView.getUint16(this.m_wDataPos + 2, true);
- }
- //效验数据
- if ((this.m_wDataPos + DataDescribe.wDataSize) > this.m_wDataSize) {
- DataDescribe.wDataSize = 0;
- DataDescribe.wDataDescribe = BaseDefine.DTP_NULL;
- return null;
- }
- var buff = "";
- if (DataDescribe.wDataSize > 0) {
- buff = dataView.buffer.slice(4 + this.m_wDataPos, 4 + this.m_wDataPos + DataDescribe.wDataSize);
- }
- this.m_wDataPos += 4 + DataDescribe.wDataSize;
- return buff;
- }
- };
- class SendPacketHelper {
- constructor(DataSize, MaxBytes) {
- this.m_wDataSize = DataSize; //WORD 数据大小
- this.m_wMaxBytes = MaxBytes; //WORD 缓冲大小
- }
- AddPacket(DataDescribe, dataView, buf) {
- if (DataDescribe.wDataDescribe === BaseDefine.DTP_NULL) return false;
- if ((DataDescribe.wDataSize + 4 + this.m_wDataSize) > this.m_wMaxBytes) return false;
- dataView.setUint16(this.m_wDataSize, DataDescribe.wDataSize, true);
- this.m_wDataSize += 2;
- dataView.setUint16(this.m_wDataSize, DataDescribe.wDataDescribe, true);
- this.m_wDataSize += 2;
- if (DataDescribe.wDataSize > 0) {
- // var strBuf = Global.ToUTF_8(buf);
- // for (var num = 0; num < strBuf.length - 2; ++num) {
- // dv.setUint8(this.m_wDataSize + num, strBuf[2 + i], true);
- // }
- Global.WriteStr2DataView(dataView, this.m_wDataSize, buf);
- this.m_wDataSize += DataDescribe.wDataSize;
- }
- return true;
- }
- GetDataSize() { return this.m_wDataSize; }
- };
- module.exports = { RecvPacketHelper, SendPacketHelper }
|