import { MAX_DATA_LENGTH } from '../common' import { SmartBuffer } from '../smart-buffer' export class IncomingPacket extends SmartBuffer { fields: { messageType: number senderId: number length: number data: Uint8Array } constructor (data: ArrayBuffer) { super() this.data = new Uint8Array(data) this.fields = { messageType: this.readUInt16(), senderId: this.readUInt32(), length: this.readUInt16(), data: this.readBytes(this.length) } } } export class OutgoingPacket extends SmartBuffer { constructor (messageType: number, data: Uint8Array = new Uint8Array(0)) { super() if (data.length > MAX_DATA_LENGTH) { throw RangeError(`Specified data of length ${data.length} exceeds max data length ${MAX_DATA_LENGTH}.`) } this.writeUInt16(messageType) this.writeUInt16(data.length) this.writeBytes(data) } }