-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdlt-buffer.js
More file actions
75 lines (64 loc) · 1.97 KB
/
Copy pathdlt-buffer.js
File metadata and controls
75 lines (64 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* This file is part of dlt-node.
*
* dlt-node is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dlt-node is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dlt-node. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Lassi Marttala, Maxpower (C) 2016
*/
const EventEmitter = require('events');
const util = require('util');
const ByteBuffer = require('bytebuffer');
const DltPacket = require(__dirname+'/protocol/dltpacket.js');
class DltBuffer extends EventEmitter {
constructor() {
super();
this.buf = new ByteBuffer();
}
buffer(data) {
this.buf.append(data);
// Parse until all messages have been processed
while(this.parseBuffer());
};
parseBuffer() {
const bufLen = this.buf.offset;
// Do we have at least minimum header in buffer?
if(bufLen < 3) {
return false;
}
// Get the complete packet size from header
this.buf.offset = 2;
const packetLen = this.buf.readInt16();
// Not a complete packet in buffer
if(packetLen > bufLen) {
this.buf.offset = bufLen;
return false;
}
// Copy the packet data from buffer
const packetData = this.buf.copy(0, packetLen);
// Let the packet object fill itself from data
const packet = new DltPacket(packetData);
// Signal that we have a new packet
this.emit('packet', packet);
// Discard the used data
if(packetLen < bufLen) {
this.buf = this.buf.copy(packetLen, bufLen);
this.buf.offset = bufLen - packetLen;
}
else {
this.buf.offset = 0;
}
return true;
}
}
module.exports = DltBuffer;