Skip to content

Commit 9531cf0

Browse files
committed
fix bug
1 parent 51743dc commit 9531cf0

6 files changed

Lines changed: 54 additions & 13 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
*.log
33
yarn.lock
4+
node-tftp-test-data
45

56
node_modules/

connection.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
const udp = require('dgram');
22
const { debuglog } = require('util');
3-
const EventEmitter = require('events');
43
const Packet = require('./packet');
54

65
const debug = debuglog('tftp2:server');
76

8-
class Connection extends EventEmitter {
7+
class Connection extends udp.Socket {
98
constructor(rinfo) {
109
super('udp4');
10+
this.socket = this;
1111
this.setRemoteDescription(rinfo);
1212
}
1313
setRemoteDescription(rinfo) {
1414
return Object.assign(this, this.rinfo = rinfo);
1515
}
16-
send(data) {
16+
sendPacket(data) {
1717
const { rinfo } = this;
1818
if (data instanceof Packet)
1919
data = data.toBuffer();
@@ -26,19 +26,19 @@ class Connection extends EventEmitter {
2626
}
2727
sendRequest(opcode, filename) {
2828
const packet = Packet.createRequest(opcode, filename);
29-
return this.send(packet);
29+
return this.sendPacket(packet);
3030
};
3131
sendAck(block) {
3232
const { rinfo } = this;
3333
const packet = Packet.createAck(block);
3434
debug('send ack block %s to %s:%s', block, rinfo.address, rinfo.port);
35-
return this.send(packet);
35+
return this.sendPacket(packet);
3636
}
3737
sendBlock(block, data) {
3838
const { rinfo } = this;
3939
const packet = Packet.createData(block, data);
4040
debug('send block %s size %s, to %s:%s', block, data.length, rinfo.address, rinfo.port);
41-
return this.send(packet);
41+
return this.sendPacket(packet);
4242
}
4343
wait(fn) {
4444
return new Promise((resolve, reject) => {

example/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ const readChunk = filename =>
1313
(async () => {
1414

1515
const filename = `node-tftp-test-data`;
16-
await write(filename, Buffer.allocUnsafe(0xffffff));
16+
const a = Buffer.allocUnsafe(0xffffff);
17+
await write(filename, a);
1718

1819
console.log('write done');
1920
const b = await readChunk(filename);
2021

22+
console.log('read done');
2123
assert.equal(a.length, 0xffffff);
2224
assert.equal(b.length, 0xffffff);
2325
assert.deepEqual(a, b);

index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@ const debug = debuglog('tftp2');
1111
const TFTP = (host, port, { BLOCK_SIZE = 512 } = {}) => {
1212
const rinfo = { address: host, port };
1313
const client = new Connection(rinfo);
14-
const init = () => client.setRemoteDescription(rinfo);
14+
const reset = () => client.setRemoteDescription(rinfo);
1515
return {
16+
close() {
17+
return client.close();
18+
},
19+
/**
20+
* read
21+
* @param {*} filename
22+
* @param {*} push
23+
* @param {*} done
24+
*/
1625
async read(filename, push, done) {
1726
let block = 0, rinfo, data;
18-
await init();
27+
await reset();
1928
await client.sendRequest(Packet.OPCODE.RRQ, filename);
2029
while (true) {
2130
({ rinfo, block, data } = await client.waitBlock(block + 1));
2231
debug('received block(%s) size(%s), from %s:%s', block, data.length, rinfo.address, rinfo.port);
23-
push(data);
32+
await push(data);
2433
await client.setRemoteDescription(rinfo);
2534
await client.sendAck(block);
2635
if (data.length < BLOCK_SIZE) {
@@ -29,9 +38,14 @@ const TFTP = (host, port, { BLOCK_SIZE = 512 } = {}) => {
2938
}
3039
}
3140
},
41+
/**
42+
* write
43+
* @param {*} filename
44+
* @param {*} data
45+
*/
3246
async write(filename, data) {
3347
let block, rinfo;
34-
await init();
48+
await reset();
3549
await client.sendRequest(Packet.OPCODE.WRQ, filename);
3650
const blocks = Math.floor(data.length / BLOCK_SIZE | 0) + 1;
3751
for (var i = 0; i < blocks; i++) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tftp2",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"description": "simple tftp server and client in node.js",
55
"main": "index.js",
66
"scripts": {

test/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,28 @@ test('Packet#decode', () => {
3737
assert.equal(a.opcode, b.opcode);
3838
assert.equal(a.filename, b.filename);
3939
assert.equal(b.mode, 'octet');
40-
});
40+
});
41+
42+
const { read, write, close } = tftp('127.0.0.1', '6969');
43+
44+
const readChunk = filename =>
45+
new Promise(async (resolve, reject) => {
46+
const buffer = [];
47+
await read(filename, chunk => buffer.push(chunk), () =>
48+
resolve(Buffer.concat(buffer)));
49+
});
50+
51+
const filename = `node-tftp-test-data`;
52+
const a = Buffer.allocUnsafe(0xffffff);
53+
54+
test('tftp#write', async () => {
55+
await write(filename, a);
56+
});
57+
58+
test('tftp#read', async () => {
59+
const b = await readChunk(filename);
60+
assert.equal(a.length, 0xffffff);
61+
assert.equal(b.length, 0xffffff);
62+
assert.deepEqual(a, b);
63+
close();
64+
});

0 commit comments

Comments
 (0)