-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.js
62 lines (50 loc) · 1.47 KB
/
protocol.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
/* server.js: Run this with node to start your server.
* Copyright © Hexanome H4101 INSA IF.
*/
// Sending messages.
function readmessage(buffer) {
try {
var type = buffer.readUInt8(0).toString(),
size = +buffer.readUInt32BE(1, true),
message = buffer.slice((8+32)/8, (8+32)/8+size).toString();
} catch (e) {
console.log('PROTOCOL: ERROR: buffer not read properly',
'while reading message ' + buffer.inspect());
}
return {type:type, size:size, message:message};
}
// Reading messages.
function cca(c) { return c.charCodeAt(0); }
// `nbparts` is the number of part types.
function craftinit(nbparts) {
var buf = new Buffer(1 + (32*nbparts.length / 8));
buf[0] = cca('i');
for (var i = 0; i < nbparts.length; i++) {
buf.writeUInt32BE(nbparts[i], 1 + (32*i / 8));
}
return buf;
}
// Parameter `choice` must be either `"c"` (to continue) or `"s"` (to stop).
function craftanswer(choice) {
var buf = new Buffer(2);
buf[0] = cca('a');
buf[1] = cca(choice);
return buf;
}
// A command has the following parameters, per the spec:
//
// nbPalette1:Number
// nbPalette2:Number
function craftcommand(nbpal1, nbpal2) {
var buf = new Buffer((8+32+32)/8);
buf[0] = cca('c');
buf.writeUInt32BE(nbpal1, (8)/8);
buf.writeUInt32BE(nbpal2, (8+32)/8);
return buf;
}
// Exports are here.
//
exports.readmessage = readmessage;
exports.craftinit = craftinit;
exports.craftanswer = craftanswer;
exports.craftcommand = craftcommand;