Skip to content

Commit

Permalink
feat: Added packet timeout for cctalk parser (#1887)
Browse files Browse the repository at this point in the history
* Added packet timeout for cctalk parser

BREAKING: changed defaults to have a timeout
  • Loading branch information
maxvgi authored and reconbot committed Aug 14, 2019
1 parent a7d1937 commit 714e438
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/parser-cctalk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [4.0.0](https://github.com/serialport/node-serialport/compare/@serialport/[email protected]...@serialport/[email protected]) (2019-08-05)

### implementing packet byte timeouts due to cctalk documentation

* Added timeout parameter to constructor
* Implemented the reset of packet receiving process after timeout ([#1886])

# [3.0.0](https://github.com/serialport/node-serialport/compare/@serialport/[email protected]...@serialport/[email protected]) (2019-05-16)


Expand Down
13 changes: 12 additions & 1 deletion packages/parser-cctalk/cctalk.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@ const parser = port.pipe(new CCtalk())
parser.on('data', console.log)
*/
class CCTalkParser extends Transform {
constructor() {
constructor(maxDelayBetweenBytesMs = 50) {
super()
this.array = []
this.cursor = 0
this.lastByteFetchTime = 0
this.maxDelayBetweenBytesMs = maxDelayBetweenBytesMs
}
_transform(buffer, _, cb) {
if (this.maxDelayBetweenBytesMs > 0) {
const now = Date.now()
if (now - this.lastByteFetchTime > this.maxDelayBetweenBytesMs) {
this.array = []
this.cursor = 0
}
this.lastByteFetchTime = now
}

this.cursor += buffer.length
// TODO: Better Faster es7 no supported by node 4
// ES7 allows directly push [...buffer]
Expand Down
24 changes: 24 additions & 0 deletions packages/parser-cctalk/cctalk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,28 @@ describe('CCTalkParser', () => {
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([2, 2, 1, 252, 1, 1, 217]))
assert.deepEqual(spy.getCall(4).args[0], Buffer.from([2, 0, 1, 253, 217]))
})
it('resets incomplete message after timeout', () => {
const spy = sinon.spy()
const clock = sinon.useFakeTimers(Date.now())
const parser = new CCTalkParser(50)
parser.on('data', spy)
parser.write(Buffer.from([2, 2, 1]))
clock.tick(51)
parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217]))
assert.equal(spy.callCount, 1)
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]))
clock.restore()
})
it('disabled message timeout', () => {
const spy = sinon.spy()
const clock = sinon.useFakeTimers(Date.now())
const parser = new CCTalkParser(0)
parser.on('data', spy)
parser.write(Buffer.from([2, 2, 1]))
clock.tick(100)
parser.write(Buffer.from([254, 1, 1, 217]))
assert.equal(spy.callCount, 1)
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]))
clock.restore()
})
})
2 changes: 1 addition & 1 deletion packages/parser-cctalk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serialport/parser-cctalk",
"version": "3.0.0",
"version": "4.0.0",
"main": "cctalk.js",
"engines": {
"node": ">=8.6.0"
Expand Down

0 comments on commit 714e438

Please sign in to comment.