network chunk v8
This commit is contained in:
@@ -11,7 +11,6 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
|
||||
private aesKey: Buffer | null;
|
||||
private aesIv: Buffer | null;
|
||||
private chunkBuffers: { [messageId: string]: string[] };
|
||||
private networkSpeed: number | null = null; // Estimated network speed in bytes/ms
|
||||
|
||||
constructor(socket: Socket, ip: string, port: number, operationHandler: OperationHandler) {
|
||||
super(ip, port, operationHandler);
|
||||
@@ -102,33 +101,17 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
|
||||
}
|
||||
}
|
||||
|
||||
private async scanNetworkSpeed(): Promise<number> {
|
||||
const testMessage = 'PING_TEST'.repeat(100); // A test message of known size
|
||||
const startTime = Date.now();
|
||||
await this.writeToSocket(testMessage);
|
||||
await new Promise((resolve) => this.socket.once('data', resolve));
|
||||
const endTime = Date.now();
|
||||
const duration = endTime - startTime; // Time in ms
|
||||
this.networkSpeed = testMessage.length / duration; // Bytes/ms
|
||||
return this.networkSpeed;
|
||||
}
|
||||
|
||||
private calculateOptimalChunkSize(messageLength: number): number {
|
||||
let chunkSize = this.networkSpeed ? Math.min(Math.floor(this.networkSpeed * 100), 1024) : 1024;
|
||||
while (messageLength % chunkSize !== 0 && chunkSize > 0) {
|
||||
chunkSize -= 4;
|
||||
}
|
||||
return chunkSize || 1024;
|
||||
}
|
||||
|
||||
async sendChunkedMessage(
|
||||
operationCode: string,
|
||||
metaInfo?: { [key: string]: any },
|
||||
fileContent?: Buffer
|
||||
): Promise<void> {
|
||||
// Check if latency-based chunk size needs calculation
|
||||
if (!this.networkSpeed) {
|
||||
await this.scanNetworkSpeed();
|
||||
this.networkSpeed = await this.scanNetworkLatency();
|
||||
}
|
||||
|
||||
// Format the message for sending
|
||||
const message = MessageHandler.formatMessage(operationCode, metaInfo, fileContent);
|
||||
let outgoingMessage: string;
|
||||
switch (operationCode) {
|
||||
@@ -141,9 +124,13 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
|
||||
default:
|
||||
outgoingMessage = this.encryptWithAes(message);
|
||||
}
|
||||
const optimalChunkSize = this.calculateOptimalChunkSize(outgoingMessage.length);
|
||||
|
||||
// Calculate optimal chunk size based on network latency
|
||||
const optimalChunkSize = await this.calculateOptimalChunkSize(outgoingMessage.length);
|
||||
const totalChunks = Math.ceil(outgoingMessage.length / optimalChunkSize);
|
||||
const messageId = Date.now().toString();
|
||||
|
||||
// Send each chunk with a delay between them
|
||||
for (let i = 0; i < totalChunks; i++) {
|
||||
const chunk = outgoingMessage.slice(i * optimalChunkSize, (i + 1) * optimalChunkSize);
|
||||
const chunkHeader = JSON.stringify({
|
||||
@@ -153,7 +140,10 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
|
||||
});
|
||||
const chunkWithHeader = `${chunkHeader}|${chunk}`;
|
||||
await this.writeToSocket(chunkWithHeader);
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
|
||||
// Set delay based on latency for smoother transmission
|
||||
const delay = Math.max(100, Math.min(300, this.networkSpeed * 10));
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +155,7 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
|
||||
this.handlerResult = await this.operationHandler.handleOperation(messageToProcess);
|
||||
}
|
||||
|
||||
private writeToSocket(message: string): Promise<void> {
|
||||
protected writeToSocket(message: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.socket.write(message, (err: any) => {
|
||||
if (err) {
|
||||
|
||||
Reference in New Issue
Block a user