TeaSpeak-Client/native/serverconnection/test/js/ft.ts
2019-10-25 21:38:09 +02:00

91 lines
2.7 KiB
TypeScript

/// <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";
import * as handle from "teaclient_connection";
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());
});
});
};
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"));
const transfer = handle.ft.spawn_connection({
client_transfer_id: 0,
server_transfer_id: 0,
object: destination,
transfer_key: "ft_download_data",
//object: source,
//transfer_key: "ft_upload_data__",
remote_address: "localhost",
remote_port: 30303
});
transfer.callback_failed = message => {
console.log("FT failed: %o", message);
};
transfer.callback_finished = success => {
console.log("FT done (%o)", success);
console.log("Buffer: %o", String.fromCharCode.apply(null, target_buffer));
//console.log("A: %o", transfer);
};
transfer.callback_start = () => {
console.log("FT start");
};
transfer.start();
});
setInterval(() => {
if(global && global.gc)
global.gc();
}, 1000);