Improved MeshCtrl.js upload speed.

Ylian Saint-Hilaire committed Apr 26, 2021 at 19:21 UTC 7e1b1a68d4155889c0b510b885df3bf2f10355e7
1 file changed +21 -11
meshctrl.js
+21 -11
@@ -2007,18 +2007,26 @@ function connectTunnel(url) {
2007 try { cmd = JSON.parse(rawdata.toString()); } catch (ex) { return; }
2008 if (cmd.reqid == 'up') {
2009 if ((cmd.action == 'uploadack') || (cmd.action == 'uploadstart')) {
2010 - var buf = Buffer.alloc(16384);
2011 - var len = require('fs').readSync(settings.uploadFile, buf, 1, 16383, settings.uploadPtr);
2012 - var start = 1;
2013 - settings.uploadPtr += len;
2014 - if (len > 0) {
2015 - if ((buf[1] == 0) || (buf[1] == 123)) { start = 0; buf[0] = 0; len++; } // If the buffer starts with 0 or 123, we must add an extra 0 at the start of the buffer
2016 - settings.tunnelws.send(buf.slice(start, start + len));
2017 - } else {
2018 - console.log('Upload done, ' + settings.uploadPtr + ' bytes sent.');
2019 - if (settings.uploadFile != null) { require('fs').closeSync(settings.uploadFile); }
2020 - process.exit();
2010 + settings.inFlight--;
2011 + if (settings.uploadFile == null) { if (settings.inFlight == 0) { process.exit(); } return; } // If the file is closed and there is no more in-flight data, exit.
2012 + var loops = (cmd.action == 'uploadstart')?16:1; // If this is the first data to be sent, hot start now. We are going to have 16 blocks of data in-flight.
2013 + for (var i = 0; i < loops; i++) {
2014 + if (settings.uploadFile == null) continue;
2015 + var buf = Buffer.alloc(65565);
2016 + var len = require('fs').readSync(settings.uploadFile, buf, 1, 65564, settings.uploadPtr);
2017 + var start = 1;
2018 + settings.uploadPtr += len;
2019 + if (len > 0) {
2020 + if ((buf[1] == 0) || (buf[1] == 123)) { start = 0; buf[0] = 0; len++; } // If the buffer starts with 0 or 123, we must add an extra 0 at the start of the buffer
2021 + settings.inFlight++;
2022 + settings.tunnelws.send(buf.slice(start, start + len));
2023 + } else {
2024 + console.log('Upload done, ' + settings.uploadPtr + ' bytes sent.');
2025 + if (settings.uploadFile != null) { require('fs').closeSync(settings.uploadFile); delete settings.uploadFile; }
2026 + if (settings.inFlight == 0) { process.exit(); return; } // File is closed, if there is no more in-flight data, exit.
2027 + }
2028 }
2029 +
2030 } else if (cmd.action == 'uploaderror') {
2031 if (settings.uploadFile != null) { require('fs').closeSync(settings.uploadFile); }
2032 console.log('Upload error.');
@@ -2033,6 +2041,8 @@ function connectTunnel(url) {
2041 settings.uploadSize = require('fs').statSync(args.file).size;
2042 settings.uploadFile = require('fs').openSync(args.file, 'r');
2043 settings.uploadPtr = 0;
2044 + settings.inFlight = 1;
2045 + console.log('Uploading...');
2046 settings.tunnelws.send(JSON.stringify({ action: 'upload', reqid: 'up', path: args.target, name: require('path').basename(args.file), size: settings.uploadSize }));
2047 }
2048 });