TeaSpeak-Client/native/serverconnection/test/js/ft.ts

106 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-06-26 16:09:01 -04:00
/// <reference path="../../exports/exports.d.ts" />
module.paths.push("../../build/linux_amd64");
module.paths.push("../../build/win32_64");
import * as fs from "fs";
import * as net from "net";
const original_require = require;
require = (module => original_require("/home/wolverindev/TeaSpeak-Client/client/native/build/linux_x64/" + module + ".node")) as any;
2019-06-26 16:09:01 -04:00
import * as handle from "teaclient_connection";
require = original_require;
2019-06-26 16:09:01 -04:00
const buffer_size = 24;
const start_server = async () => {
const server = net.createServer();
await new Promise(resolve => server.listen(30303, "localhost", resolve));
server.on('connection', socket => {
console.log("[SERVER] Received new client from %s:%d", socket.remoteAddress, socket.remotePort);
let key: string = "";
socket.on('data', buffer => {
if(key.length < 16) {
key += buffer.toString();
if(key.length >= 16) {
console.log("Received key: %s", key.substr(0, 16));
if(key.length > 16) {
console.log("Overhead: %s", key.substr(16));
key = key.substr(0, 16);
}
if(key == "ft_upload_data__") {
} else {
//They expect stuff
socket.write("123456789123456789aabb222"); //Must be equal to buffer_size
}
}
}
//console.log("[SERVER] Received data: %s", buffer.toString());
2019-06-26 16:09:01 -04:00
});
});
};
function str2ab(str) {
var buf = new ArrayBuffer(str.length); // 2 bytes for each char
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
start_server().catch(error => {
console.error("Failed to start FT server (%o)", error);
}).then(() => {
const target_buffer = new Uint8Array(buffer_size);
const destination = handle.ft.download_transfer_object_from_buffer(target_buffer.buffer);
//const source = handle.ft.upload_transfer_object_from_buffer(str2ab("Hello World"));
//console.log(source);
//const source = handle.ft.upload_transfer_object_from_file(__dirname, "test_upload.txt");
const source = handle.ft.upload_transfer_object_from_file("/home/wolverindev/Downloads", "xxx.iso");
console.log(source);
const upload = true;
2019-06-26 16:09:01 -04:00
const transfer = handle.ft.spawn_connection({
2019-06-26 16:09:01 -04:00
client_transfer_id: 0,
server_transfer_id: 0,
object: upload ? source : destination,
transfer_key: upload ? "ft_upload_data__" : "ft_download_data",
2019-06-26 16:09:01 -04:00
remote_address: "localhost",
remote_port: 30303
});
transfer.callback_failed = message => {
console.log("[FT] failed: %o", message);
2019-06-26 16:09:01 -04:00
};
transfer.callback_finished = aborted => {
console.log("[FT] done (Aborted %o)", aborted);
if(!upload)
console.log("[FT] Buffer: %o", String.fromCharCode.apply(null, target_buffer));
2019-06-26 16:09:01 -04:00
//console.log("A: %o", transfer);
};
let last = 0;
transfer.callback_progress = (current, max) => {
const diff = current - last;
last = current;
console.log("[FT] Progress: %d|%d (%d) %dmb/s", current, max, Math.ceil(current / max * 100), Math.ceil(diff / 1024 / 1024));
};
2019-06-26 16:09:01 -04:00
transfer.callback_start = () => {
console.log("[FT] start");
2019-06-26 16:09:01 -04:00
};
transfer.start();
});
setInterval(() => {
if(global && global.gc)
global.gc();
}, 1000);