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

113 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-10-25 19:51:40 -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";
2019-10-29 13:11:47 -04:00
import * as os from "os";
import * as path from "path";
2019-10-25 19:51:40 -04:00
const original_require = require;
2019-10-29 13:11:47 -04:00
require = (module => original_require(path.join(__dirname, "..", "..", "..", "build", os.platform() + "_" + os.arch(), module + ".node"))) as any;
2019-10-25 19:51:40 -04:00
import * as handle from "teaclient_connection";
require = original_require;
const buffer_size = 24;
const start_server = async () => {
2019-10-29 13:11:47 -04:00
const server: net.Server = net.createServer();
2019-10-25 19:51:40 -04:00
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-10-29 13:11:47 -04:00
const address = server.address();
console.log("[SERVER] Listening on %o", address);
return typeof address === "string" ? address : address.address;
2019-10-25 19:51:40 -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);
2019-10-29 13:11:47 -04:00
}).then(address => {
2019-10-25 19:51:40 -04:00
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");
2019-10-29 13:11:47 -04:00
//const source = handle.ft.upload_transfer_object_from_file("/home/wolverindev/Downloads", "xxx.iso");
const source = handle.ft.upload_transfer_object_from_file("C:\\Users\\WolverinDEV\\Downloads", "ütest.txt");
2019-10-25 19:51:40 -04:00
console.log(source);
const upload = true;
const transfer = handle.ft.spawn_connection({
client_transfer_id: 0,
server_transfer_id: 0,
object: upload ? source : destination,
transfer_key: upload ? "ft_upload_data__" : "ft_download_data",
2019-10-29 13:11:47 -04:00
remote_address: address,
2019-10-25 19:51:40 -04:00
remote_port: 30303
});
transfer.callback_failed = message => {
console.log("[FT] failed: %o", message);
};
transfer.callback_finished = aborted => {
console.log("[FT] done (Aborted %o)", aborted);
if(!upload)
console.log("[FT] Buffer: %o", String.fromCharCode.apply(null, target_buffer));
//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));
};
transfer.callback_start = () => {
console.log("[FT] start");
};
transfer.start();
});
setInterval(() => {
if(global && global.gc)
global.gc();
}, 1000);