iosignal 1.6.1 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +254 -247
- package/auth_file.json +13 -13
- package/auth_file.mjs +23 -23
- package/dist/iosignal.cjs +2 -2
- package/dist/iosignal.esm.js.map +1 -1
- package/dist/iosignal.min.js.map +1 -1
- package/index.js +19 -19
- package/indexWebBrowser.js +13 -13
- package/package.json +60 -59
- package/rollup.config.js +69 -69
- package/src/api/RedisAPI.js +65 -68
- package/src/api/api_constant.js +7 -7
- package/src/api/api_reply.js +31 -34
- package/src/api/api_sudo.js +91 -96
- package/src/auth/Auth_Env.js +76 -78
- package/src/auth/Auth_File.js +71 -75
- package/src/auth/Auth_Redis.js +44 -50
- package/src/auth/BohoAuth.js +164 -168
- package/src/client/CongPacket.js +141 -150
- package/src/client/IOCongSocket.js +81 -88
- package/src/client/IOCore.js +822 -851
- package/src/client/IOWS.js +80 -81
- package/src/client/IOWebSocket.js +111 -112
- package/src/common/constants.js +148 -159
- package/src/common/payload.js +81 -82
- package/src/common/quotaTable.js +68 -68
- package/src/common/util.js +61 -61
- package/src/server/FileLogger.js +30 -30
- package/src/server/Manager.js +433 -449
- package/src/server/Metric.js +155 -154
- package/src/server/Remote.js +207 -0
- package/src/server/{ServerRemoteCore.js → RemoteCore.js} +439 -467
- package/src/server/Server.js +163 -166
- package/src/server/serverOption.js +56 -56
- package/test-browser/favicon.ico +0 -0
- package/test-browser/index.html +96 -93
- package/test-browser/server.js +11 -11
- package/test-client-nodejs/client.cjs +17 -17
- package/test-client-nodejs/client.mjs +17 -17
- package/test-client-nodejs/client_api_reply.mjs +15 -15
- package/test-client-nodejs/stateChange.mjs +19 -19
- package/test-server/server-Auth_Env.js +19 -19
- package/test-server/server-Auth_File.js +12 -12
- package/test-server/server-api_reply.js +6 -6
- package/test-server/server-api_sudo.js +7 -7
- package/test-server/server-commonjs.cjs +8 -8
- package/test-server/server-esm.mjs +20 -19
- package/test-server/simple-server.mjs +11 -11
- package/test-signal-payloadType/signalPack.js +34 -34
- package/test-signal-payloadType/signaling.js +60 -60
- package/src/server/ServerRemote.js +0 -227
package/src/auth/BohoAuth.js
CHANGED
|
@@ -1,168 +1,164 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
//
|
|
146
|
-
peer.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
1
|
+
import { MBP } from 'meta-buffer-pack'
|
|
2
|
+
import { BohoMsg, Meta } from 'boho'
|
|
3
|
+
import { quotaTable } from '../common/quotaTable.js'
|
|
4
|
+
import { serverOption } from '../server/serverOption.js'
|
|
5
|
+
import { IOMsg, CLIENT_STATE } from '../common/constants.js'
|
|
6
|
+
import { FileLogger } from '../server/FileLogger.js'
|
|
7
|
+
|
|
8
|
+
const decoder = new TextDecoder()
|
|
9
|
+
export class BohoAuth {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.authLogger;
|
|
12
|
+
if (serverOption.fileLogger.auth.use) {
|
|
13
|
+
this.authLogger = new FileLogger(serverOption.fileLogger.auth.path)
|
|
14
|
+
console.log('Auth: begin file logger.[auth]')
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
send_auth_fail(peer, reason) {
|
|
19
|
+
if (this.authLogger) {
|
|
20
|
+
let peerInfo = `FAIL #${peer.ssid} reason:${reason} `
|
|
21
|
+
this.authLogger.log(peerInfo)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
console.log('## AUTH_FAIL reason:', reason)
|
|
25
|
+
// peer.send( Buffer.from( [BohoMsg.AUTH_FAIL] ))
|
|
26
|
+
peer.setState(CLIENT_STATE.AUTH_FAIL)
|
|
27
|
+
setTimeout(e => {
|
|
28
|
+
peer.send(Buffer.from([BohoMsg.AUTH_FAIL]))
|
|
29
|
+
}, serverOption.auth.delay_auth_fail)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async verify_auth_hmac(auth_hmac, peer) {
|
|
33
|
+
try {
|
|
34
|
+
//1. unpack
|
|
35
|
+
let infoPack = MBP.unpack(auth_hmac, Meta.AUTH_HMAC)
|
|
36
|
+
if (!infoPack) {
|
|
37
|
+
this.send_auth_fail(peer, 'unpack auth_pack fail');
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let id = ""
|
|
42
|
+
if (infoPack.id8.includes(0)) {
|
|
43
|
+
id = decoder.decode(infoPack.id8.subarray(0, infoPack.id8.indexOf(0)));
|
|
44
|
+
} else {
|
|
45
|
+
id = decoder.decode(infoPack.id8);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//2. get key of id from DB
|
|
49
|
+
let authInfo = await this.getAuth(id)
|
|
50
|
+
|
|
51
|
+
// console.log('##### authInfo',authInfo )
|
|
52
|
+
if (serverOption.debug.showAuthInfo) {
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!authInfo) {
|
|
56
|
+
this.send_auth_fail(peer, 'NO ID:' + id);
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// console.log('db authInfo.key: ', authInfo.key)
|
|
61
|
+
peer.boho.copy_id8(infoPack.id8)
|
|
62
|
+
// type of key
|
|
63
|
+
let authKey;
|
|
64
|
+
const SHA256_HASH_BASE64_LEN = 44
|
|
65
|
+
if (authInfo.key.length == SHA256_HASH_BASE64_LEN) {
|
|
66
|
+
// hashed key
|
|
67
|
+
authKey = Buffer.from(authInfo.key, 'base64')
|
|
68
|
+
peer.boho.copy_key(authKey)
|
|
69
|
+
} else {
|
|
70
|
+
// plain key
|
|
71
|
+
peer.boho.set_key(authInfo.key)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//3. check hmac
|
|
75
|
+
// console.log('-- found: authKey of id: ', id, authKey.toString('hex'))
|
|
76
|
+
let auth_ack = peer.boho.check_auth_hmac(infoPack)
|
|
77
|
+
|
|
78
|
+
// console.log('auth_ack', auth_ack )
|
|
79
|
+
if (!auth_ack) {
|
|
80
|
+
this.send_auth_fail(peer, 'hmac dismatched');
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//4. get info
|
|
85
|
+
// console.log('#### auth success' )
|
|
86
|
+
|
|
87
|
+
//5. check duplicate login.
|
|
88
|
+
// current policy. Deny duplicate login
|
|
89
|
+
|
|
90
|
+
// duplicate login policy.
|
|
91
|
+
// 1. send clear_auth signal to the old connection.
|
|
92
|
+
// 2. close old connection.
|
|
93
|
+
// 3. close new connection. retry from begining.
|
|
94
|
+
if (peer.manager.cid2remote.has(authInfo.cid)) {
|
|
95
|
+
let old = peer.manager.cid2remote.get(authInfo.cid)
|
|
96
|
+
if (old == peer) {
|
|
97
|
+
console.log('## trying RELOGIN with SAME ID. ignored.')
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
console.log('### clear_auth and close old connection:', old.cid)
|
|
101
|
+
old.send(Buffer.from([IOMsg.SERVER_CLEAR_AUTH]))
|
|
102
|
+
old.close()
|
|
103
|
+
peer.close() // auto relogin
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
//6. delete current (temp rand)cid if exist.
|
|
109
|
+
if (peer.cid) {
|
|
110
|
+
peer.manager.cid2remote.delete(peer.cid)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
//7. setting info.
|
|
114
|
+
peer.did = id
|
|
115
|
+
peer.cid = authInfo.cid
|
|
116
|
+
peer.nick = authInfo.cid // temporary: nick as cid
|
|
117
|
+
|
|
118
|
+
//8. setting quota level.
|
|
119
|
+
let quotaLevel = serverOption.defaultQuotaIndex;
|
|
120
|
+
if (authInfo.level) quotaLevel = authInfo.level;
|
|
121
|
+
quotaLevel = parseInt(quotaLevel)
|
|
122
|
+
let newQuota = quotaTable[quotaLevel];
|
|
123
|
+
if (!newQuota) {
|
|
124
|
+
let err = 'no index quotaTable for auth.level: ' + quotaLevel
|
|
125
|
+
console.log('##AUTH:DATA ERROR##', err)
|
|
126
|
+
this.send_auth_fail(peer, err);
|
|
127
|
+
return
|
|
128
|
+
} else {
|
|
129
|
+
peer.level = quotaLevel;
|
|
130
|
+
peer.quota = newQuota;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (peer.level === serverOption.adminLevel) {
|
|
134
|
+
console.log('## LOGIN ADMIN CID:', peer.cid)
|
|
135
|
+
peer.isAdmin = true;
|
|
136
|
+
}
|
|
137
|
+
// console.log('auth: peer.quota', peer.quota )
|
|
138
|
+
// send quota.level
|
|
139
|
+
peer.send(Buffer.from([IOMsg.QUOTA_LEVEL, quotaLevel]))
|
|
140
|
+
|
|
141
|
+
//9. set cid2remote
|
|
142
|
+
peer.manager.cid2remote.set(peer.cid, peer)
|
|
143
|
+
// console.log( 'done: cid2remote:', peer.manager.cid2remote.keys() )
|
|
144
|
+
// console.log("LOGIN: ", `id: ${ peer.did}(${peer.cid})` )
|
|
145
|
+
//10. send ack.
|
|
146
|
+
peer.send(auth_ack)
|
|
147
|
+
peer.setState(CLIENT_STATE.AUTH_READY)
|
|
148
|
+
if (this.authLogger) {
|
|
149
|
+
let peerInfo = `OK #${peer.ssid} cid: ${peer.cid} did:${peer.did}`
|
|
150
|
+
if (peer.isAdmin) peerInfo = "#ADMIN# " + peerInfo
|
|
151
|
+
this.authLogger.log(peerInfo)
|
|
152
|
+
}
|
|
153
|
+
return authInfo
|
|
154
|
+
} catch (error) {
|
|
155
|
+
this.send_auth_fail(peer, 'caught: unknown error' + error);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
|
package/src/client/CongPacket.js
CHANGED
|
@@ -1,150 +1,141 @@
|
|
|
1
|
-
import { Transform } from 'stream'
|
|
2
|
-
import { MBP } from 'meta-buffer-pack'
|
|
3
|
-
|
|
4
|
-
export const CongType = {
|
|
5
|
-
TYPE_LEN1: 1,
|
|
6
|
-
TYPE_LEN2: 2,
|
|
7
|
-
TYPE_LEN3: 3,
|
|
8
|
-
TYPE_LEN4: 4
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// support LittleEndian system.
|
|
12
|
-
export function pack(payload) {
|
|
13
|
-
|
|
14
|
-
if (payload.byteLength < 256) { //one byte len
|
|
15
|
-
return MBP.pack(
|
|
16
|
-
MBP.MB('#type', '8', CongType.TYPE_LEN1),
|
|
17
|
-
MBP.MB('#payloadLen1', '8', payload.byteLength),
|
|
18
|
-
MBP.MB('#payload', payload)
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
} else if (payload.byteLength < 65536) { // 2bytes len
|
|
22
|
-
return MBP.pack(
|
|
23
|
-
MBP.MB('#type', '8', CongType.TYPE_LEN2),
|
|
24
|
-
MBP.MB('#payloadLen2', '16L', payload.byteLength),
|
|
25
|
-
MBP.MB('#payload', payload)
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
} else if (payload.byteLength < 2 ** 24) { // 3bytes len
|
|
29
|
-
let len4Buffer = Buffer.alloc(4)
|
|
30
|
-
len4Buffer.writeUint32LE(
|
|
31
|
-
let cropLen3 = len4Buffer.subarray(0,3)
|
|
32
|
-
return MBP.pack(
|
|
33
|
-
MBP.MB('#type', '8', CongType.TYPE_LEN3),
|
|
34
|
-
MBP.MB('#payloadLen3', cropLen3),
|
|
35
|
-
MBP.MB('#payload', payload)
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
} else { //use 4 bytes.
|
|
39
|
-
return MBP.pack(
|
|
40
|
-
MBP.MB('#type', '8', CongType.TYPE_LEN4),
|
|
41
|
-
MBP.MB('#payloadLen4', '32L', payload.byteLength),
|
|
42
|
-
MBP.MB('#payload', payload)
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
this.frames
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
this.buffer
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
1
|
+
import { Transform } from 'stream'
|
|
2
|
+
import { MBP } from 'meta-buffer-pack'
|
|
3
|
+
|
|
4
|
+
export const CongType = {
|
|
5
|
+
TYPE_LEN1: 1,
|
|
6
|
+
TYPE_LEN2: 2,
|
|
7
|
+
TYPE_LEN3: 3,
|
|
8
|
+
TYPE_LEN4: 4
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// support LittleEndian system.
|
|
12
|
+
export function pack(payload) {
|
|
13
|
+
|
|
14
|
+
if (payload.byteLength < 256) { //one byte len
|
|
15
|
+
return MBP.pack(
|
|
16
|
+
MBP.MB('#type', '8', CongType.TYPE_LEN1),
|
|
17
|
+
MBP.MB('#payloadLen1', '8', payload.byteLength),
|
|
18
|
+
MBP.MB('#payload', payload)
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
} else if (payload.byteLength < 65536) { // 2bytes len
|
|
22
|
+
return MBP.pack(
|
|
23
|
+
MBP.MB('#type', '8', CongType.TYPE_LEN2),
|
|
24
|
+
MBP.MB('#payloadLen2', '16L', payload.byteLength),
|
|
25
|
+
MBP.MB('#payload', payload)
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
} else if (payload.byteLength < 2 ** 24) { // 3bytes len
|
|
29
|
+
let len4Buffer = Buffer.alloc(4)
|
|
30
|
+
len4Buffer.writeUint32LE(payload.byteLength)
|
|
31
|
+
let cropLen3 = len4Buffer.subarray(0, 3)
|
|
32
|
+
return MBP.pack(
|
|
33
|
+
MBP.MB('#type', '8', CongType.TYPE_LEN3),
|
|
34
|
+
MBP.MB('#payloadLen3', cropLen3),
|
|
35
|
+
MBP.MB('#payload', payload)
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
} else { //use 4 bytes.
|
|
39
|
+
return MBP.pack(
|
|
40
|
+
MBP.MB('#type', '8', CongType.TYPE_LEN4),
|
|
41
|
+
MBP.MB('#payloadLen4', '32L', payload.byteLength),
|
|
42
|
+
MBP.MB('#payload', payload)
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class CongRx extends Transform {
|
|
49
|
+
constructor(options) {
|
|
50
|
+
super(options)
|
|
51
|
+
this.buffer = Buffer.alloc(0)
|
|
52
|
+
this.frames = []
|
|
53
|
+
this.rxi = 0;
|
|
54
|
+
this.rxi_zero = 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
_transform(chunk, encoding, callback) {
|
|
60
|
+
this.addData(chunk)
|
|
61
|
+
if (this.frames.length > 0) {
|
|
62
|
+
this.frames.forEach(frame => {
|
|
63
|
+
this.push(frame)
|
|
64
|
+
})
|
|
65
|
+
this.frames = []
|
|
66
|
+
}
|
|
67
|
+
callback()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
addData(chunk) {
|
|
72
|
+
let c = chunk.byteLength;
|
|
73
|
+
let i = 0;
|
|
74
|
+
while (c--) {
|
|
75
|
+
this.rxi++
|
|
76
|
+
if (chunk[i++] == 0) {
|
|
77
|
+
this.rxi_zero++
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (this.buffer.byteLength > 0) {
|
|
82
|
+
this.buffer = Buffer.concat([this.buffer, chunk])
|
|
83
|
+
} else {
|
|
84
|
+
this.buffer = chunk
|
|
85
|
+
}
|
|
86
|
+
this.parse()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
parse() {
|
|
91
|
+
let head = this.buffer[0]
|
|
92
|
+
let headerLen;
|
|
93
|
+
let payloadSize;
|
|
94
|
+
|
|
95
|
+
if (head == CongType.TYPE_LEN1) {
|
|
96
|
+
headerLen = 2;
|
|
97
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
98
|
+
payloadSize = this.buffer.readUint8(1)
|
|
99
|
+
|
|
100
|
+
} else if (head == CongType.TYPE_LEN2) {
|
|
101
|
+
headerLen = 3;
|
|
102
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
103
|
+
payloadSize = this.buffer.readUint16LE(1)
|
|
104
|
+
|
|
105
|
+
} else if (head == CongType.TYPE_LEN3) {
|
|
106
|
+
headerLen = 4;
|
|
107
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
108
|
+
payloadSize = this.buffer.readUint16LE(1) + this.buffer.readUint8(3) * 65536
|
|
109
|
+
|
|
110
|
+
} else if (head == CongType.TYPE_LEN4) {
|
|
111
|
+
headerLen = 5;
|
|
112
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
113
|
+
payloadSize = this.buffer.readUint32LE(1)
|
|
114
|
+
|
|
115
|
+
} else {
|
|
116
|
+
this.emit('wrong', this.buffer)
|
|
117
|
+
this.buffer = Buffer.alloc(0)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if (payloadSize == this.buffer.byteLength - headerLen) {
|
|
122
|
+
this.frames.push(this.buffer.subarray(headerLen))
|
|
123
|
+
this.buffer = Buffer.alloc(0)
|
|
124
|
+
return
|
|
125
|
+
} else if (payloadSize < this.buffer.byteLength - headerLen) {
|
|
126
|
+
this.frames.push(this.buffer.subarray(headerLen, headerLen + payloadSize))
|
|
127
|
+
this.buffer = this.buffer.subarray(headerLen + payloadSize)
|
|
128
|
+
this.parse()
|
|
129
|
+
} else {
|
|
130
|
+
// not ready
|
|
131
|
+
// console.log('+')
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|