-
Notifications
You must be signed in to change notification settings - Fork 2
/
StreamDecoder.js
116 lines (97 loc) · 3.22 KB
/
StreamDecoder.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const MessageHeader = require('./MessageHeader')
const MessageType = require('./MessageType')
const Message = require('./Message')
class StreamDecoder {
constructor(data, cbControlMessage, cbDataMessage) {
this.Buffer = data
this.bufferOffset = 0
this.decodeIndex = 0
this.messageDataLength = 0
this.previousRemainedCount = 0
this.msgHeader = undefined
this.cbControlMessage = cbControlMessage
this.cbDataMessage = cbDataMessage
}
// Main function
Decode (length) {
let self = this
let decodeLength = length
length += this.previousRemainedCount
let need = 0
let totalMessageLength = this.GetTotalMessageLength(length, need)
return this.messageDataLength > 0
}
// 0: Get total message length (4 bytes)
GetTotalMessageLength(length, need) {
let self = this
if (!self.CheckAvailable(length, 4, need)) {
return 0
}
self.messageDataLength = self.Buffer.readUInt32BE(self.decodeIndex)
self.decodeIndex += 4
length -= 4
console.log("Message length: %s bytes", self.messageDataLength)
return this.GetMessageHeader(length, need)
}
// 1: Get message header (10 bytes)
GetMessageHeader (length, need) {
let self = this
if (!self.CheckAvailable(length, 10, need)) {
return 1
}
// Decode header (10 bytes)
this.msgHeader = MessageHeader.decode(this.Buffer.slice(this.decodeIndex, (this.decodeIndex + 10)))
this.decodeIndex += 10
this.messageDataLength -= 10
length -= 10
// console.log("GetMessageHeader :: Message Data length: %s bytes", self.messageDataLength)
if (this.messageDataLength === 0) {
if (this.msgHeader.MessageType === MessageType.DataMessage) {
console.log("DataMessage hit")
this.cbDataMessage(this.msgHeader, new Message(
this.msgHeader.S, this.msgHeader.F, "", null, this.msgHeader.ReplyExpected
))
} else {
this.cbControlMessage(this.msgHeader)
}
}
// console.log("GetMessageHeader :: Length: %s bytes", length)
if (length > this.messageDataLength) {
console.log("DataMessage hit 2", length, this.messageDataLength)
// console.log(this.msgHeader)
this.cbDataMessage(this.msgHeader, new Message(
this.msgHeader.S, this.msgHeader.F, "", this.Buffer, this.msgHeader.ReplyExpected
))
length -= this.messageDataLength
this.messageDataLength = 0
return 0
}
return this.GetItemHeader(length, need)
}
GetItemHeader (length, need) {
let self = this
if (!self.CheckAvailable(length, 1, need)) {
return 2
}
let format = this.Buffer[this.decodeIndex] // & 0xFC
let lengthBits = this.Buffer[this.decodeIndex] // & 3
this.decodeIndex++
this.messageDataLength--
length--
console.log(`format: ${format}`)
return this.GetItemLength(length, need)
}
GetItemLength (length, need) {}
GetItem (length, need) {}
CheckAvailable (length, required, need) {
need = required - length
if (need > 0)
{
return false
}
need = 0
return true
}
BufferedDecodeItem () {}
}
module.exports = StreamDecoder