-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketStreamWrapper.js
45 lines (36 loc) · 1.07 KB
/
PacketStreamWrapper.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
const EventEmitter = require('events').EventEmitter;
const WAITING_RESPONSE_SIZE = 0;
const WAITING_PACKET = 1;
class PacketStreamWrapper extends EventEmitter {
constructor(stream) {
super();
let responseSize = 0;
let response = null;
let state = WAITING_RESPONSE_SIZE;
this.stream = stream;
this.stream.on('readable', () => {
while (true) {
switch(state) {
case WAITING_RESPONSE_SIZE:
const size = this.stream.read(4);
if(size === null) return;
responseSize = size.readInt32BE(0);
state = WAITING_PACKET;
break;
case WAITING_PACKET:
response = this.stream.read(responseSize);
if(response === null) return;
this.emit('packet', response);
state = WAITING_RESPONSE_SIZE;
break;
}
}
});
}
write(buf) {
const header = new Buffer(4);
header.writeUInt32BE(buf.length, 0);
this.stream.write(Buffer.concat([header, buf]));
}
}
module.exports = PacketStreamWrapper;