-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet.js
37 lines (33 loc) · 926 Bytes
/
packet.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
const PacketBase = require('./packets/base.js')
const ByteStream = require('./byteStream.js')
function read(packetType, packet) {
readPacket = new packetType()
Object.assign(readPacket, packet)
var bs = new ByteStream(Buffer.from(packet.dataBuffer))
readPacket.read(bs)
return readPacket
}
function write(packetType, args) {
packet = new packetType()
packet.write(...args)
return packet
}
function loadFromBuffer(buffer) {
var bs = new ByteStream(buffer);
var packets = []
var beginningOfNextPacket = 0
while (!bs.empty()) {
var packet = new PacketBase()
packet.length = bs.readVarInt();
beginningOfNextPacket = packet.length + bs.i
packet.packetID = bs.readVarInt();
packet.dataBuffer = bs.tail(beginningOfNextPacket);
packets.push(packet)
}
return packets
}
module.exports = {
read: read,
write: write,
loadFromBuffer: loadFromBuffer
}