-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnection.js
65 lines (62 loc) · 1.92 KB
/
connection.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
const udp = require('dgram');
const { debuglog } = require('util');
const Packet = require('./packet');
const debug = debuglog('tftp2:server');
class Connection extends udp.Socket {
constructor(rinfo) {
super('udp4');
this.socket = this;
this.setRemoteDescription(rinfo);
}
setRemoteDescription(rinfo) {
return Object.assign(this, this.rinfo = rinfo);
}
sendPacket(data) {
const { rinfo } = this;
if (data instanceof Packet)
data = data.toBuffer();
return new Promise((resolve, reject) => {
this.socket.send(data, rinfo.port, rinfo.address, (err, length) => {
if (err) return reject(err);
resolve(length);
});
});
}
sendRequest(opcode, filename) {
const packet = Packet.createRequest(opcode, filename);
return this.sendPacket(packet);
};
sendAck(block) {
const { rinfo } = this;
const packet = Packet.createAck(block);
debug('send ack block %s to %s:%s', block, rinfo.address, rinfo.port);
return this.sendPacket(packet);
}
sendBlock(block, data) {
const { rinfo } = this;
const packet = Packet.createData(block, data);
debug('send block %s size %s, to %s:%s', block, data.length, rinfo.address, rinfo.port);
return this.sendPacket(packet);
}
wait(fn) {
return new Promise((resolve, reject) => {
const onMessage = (message, rinfo) => {
const packet = Packet.parse(message);
packet.rinfo = rinfo;
fn(packet) && (resolve(packet), removeListener());
};
const removeListener = () =>
this.socket.removeListener('message', onMessage);
this.socket.on('message', onMessage);
});
}
waitAck(block) {
return this.wait(message =>
message.opcode === Packet.OPCODE.ACK && message.block === block);
}
waitBlock(block) {
return this.wait(message =>
message.opcode === Packet.OPCODE.DATA && message.block === block);
}
}
module.exports = Connection;