-
Notifications
You must be signed in to change notification settings - Fork 6
/
artemisBufferReader.js
147 lines (109 loc) · 4.15 KB
/
artemisBufferReader.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
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
// Helper class for reading/writing packets.
// Not really needed, but makes the code much more readable.
// It basically offers a wrapper of Buffer, with an internal
// pointer that is updated as more bytes are consumed.
var bitarray = require('node-bitarray');
var artemisBufferReader = function(buffer) {
this.buffer = buffer;
this.pointer = 0;
};
artemisBufferReader.prototype.readString = function() {
var strLen = this.buffer.readUInt32LE(this.pointer) * 2;
/// HACK: Node doesn't seem to handle little-endian UTF16 well enough on ARMel CPUs, so let's fall back to ASCII for the time being.
var str = this.buffer.toString('utf16le', this.pointer+4, this.pointer + strLen + 2)
// var str = this.buffer.toString('ascii', this.pointer+4, this.pointer + strLen + 2);
var strEnd = this.buffer.readUInt16LE(this.pointer + strLen + 2);
if (strEnd !== 0) {
console.warn("String does not end with 0x0000!!");
}
this.pointer += 4 + strLen;
return str;
};
artemisBufferReader.prototype.readAsciiString = function() {
// This is only called on the welcome packet; all other strings
// are UTF-16.
var strLen = this.buffer.readUInt32LE(this.pointer);
var str = this.buffer.toString('ascii', this.pointer+4, this.pointer + strLen + 4)
this.pointer += 4 + strLen;
return str;
};
artemisBufferReader.prototype.readByte = function() {
var number = this.buffer.readUInt8(this.pointer);
this.pointer += 1;
return number;
};
artemisBufferReader.prototype.readShort = function() {
var number = this.buffer.readUInt16LE(this.pointer);
this.pointer += 2;
return number;
};
artemisBufferReader.prototype.readLong = function() {
var number = this.buffer.readUInt32LE(this.pointer);
this.pointer += 4;
return number;
};
artemisBufferReader.prototype.readFloat = function() {
var number = this.buffer.readFloatLE(this.pointer);
this.pointer += 4;
return number;
};
artemisBufferReader.prototype.readBitArray = function(bytes) {
var slice = this.buffer.slice(this.pointer, this.pointer+bytes);
var bits = bitarray.fromBuffer(slice);
this.pointer += bytes;
return bits;
}
// Like readByte, but doesn't advance the pointer. Used for checking array bounds.
artemisBufferReader.prototype.peekByte = function() {
return this.buffer.readUInt8(this.pointer);
};
// Like readShort, but doesn't advance the pointer. Used for checking array bounds.
artemisBufferReader.prototype.peekShort = function() {
return this.buffer.readUInt16LE(this.pointer);
};
artemisBufferReader.prototype.peekLong = function() {
return this.buffer.readUInt32LE(this.pointer);
};
// Like readFloat, but doesn't advance the pointer. Used as a fallback for the version packet.
artemisBufferReader.prototype.peekFloat = function() {
return this.buffer.readFloatLE(this.pointer);
};
artemisBufferReader.prototype.writeByte = function(data) {
var number = this.buffer.writeUInt8(data,this.pointer);
this.pointer += 1;
return number;
};
artemisBufferReader.prototype.writeShort = function(data) {
var number = this.buffer.writeUInt16LE(data,this.pointer);
this.pointer += 2;
return number;
};
artemisBufferReader.prototype.writeLong = function(data) {
var number = this.buffer.writeUInt32LE(data,this.pointer);
this.pointer += 4;
return number;
};
artemisBufferReader.prototype.writeFloat = function(data) {
var number = this.buffer.writeFloatLE(data,this.pointer);
this.pointer += 4;
return number;
};
artemisBufferReader.prototype.writeString = function(data) {
var strLen = data.length;
this.buffer.writeUInt32LE(strLen+1, this.pointer);
this.pointer += 4;
/// HACK: Node doesn't seem to handle little-endian UTF16 well enough on ARMel CPUs, so let's fall back to ASCII for the time being.
this.buffer.write(data,this.pointer, strLen*2, 'utf16le');
// this.buffer.write(data,this.pointer, strLen*2, 'ascii');
this.pointer += strLen*2;
this.buffer.writeUInt16LE(0, this.pointer);
this.pointer += 2;
};
/// FIXME!!!
// artemisBufferReader.prototype.writeBitArray = function(bytes) {
// var slice = this.buffer.slice(this.pointer, this.pointer+bytes);
// var bits = bitarray.fromBuffer(slice);
// this.pointer += bytes;
// return bits;
// }
exports.artemisBufferReader = artemisBufferReader;