From 0df129549169ef1ec2f4f7df6630c9627537df24 Mon Sep 17 00:00:00 2001 From: Artem Titoulenko Date: Tue, 7 Sep 2021 17:08:57 -0400 Subject: [PATCH] read ints using Buffer methods --- src/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 698d1f5..9d38b48 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,7 @@ function logDataStream(data){ function TLV(buf) { this.type = buf.slice(0, 2); - this.len = parseInt(buf.slice(2, 4).toString('hex'), 16); + this.len = buf.slice(2, 4).readInt16BE(0) this.payload = buf.slice(4, 4 + this.len); this.toString = () => `TLV(${this.type.toString('hex')}, ${this.len}, ${this.payload.toString('ascii')})`; } @@ -30,7 +30,7 @@ function SNAC(buf) { this.family = buf.slice(0,2).readInt16BE(0); this.service = buf.slice(2,4).readInt16BE(0); this.flags = buf.slice(4, 6); - this.requestID = parseInt(buf.slice(6, 10).toString('hex'), 16); + this.requestID = buf.slice(6, 10).readInt32BE(0); this.payload = new TLV(buf.slice(10)); this.toString = () => `SNAC(${this.family.toString(16)},${this.service.toString(16)}) #${this.requestID}\n ${this.payload}`; } @@ -38,8 +38,8 @@ function SNAC(buf) { function FLAP(buf) { assert.equal(buf[0], 0x2a, 'Expected 0x2a FLAP header'); this.channel = buf[1]; - this.datagramNumber = parseInt(buf.slice(2,4).toString('hex'), 16); - this.payloadLength = parseInt(buf.slice(4, 6).toString('hex'), 16); + this.datagramNumber = buf.slice(2,4).readInt16BE(0); + this.payloadLength = buf.slice(4, 6).readInt16BE(0); this.payload = buf.slice(6, 6 + this.payloadLength); this.toString = () => `ch:${this.channel}, dn: ${this.datagramNumber}, len: ${this.payloadLength}, payload:\n ${ this.payload instanceof SNAC ? this.payload.toString() : logDataStream(this.payload).split('\n').join('\n ')}`; @@ -128,6 +128,6 @@ server.on('error', (err) => { throw err; }); -server.listen(5190, '10.0.1.29', () => { - console.log('OSCAR ready on 10.0.1.29:5190'); +server.listen(5190, () => { + console.log('OSCAR ready on :5190'); });