network chunk v2

This commit is contained in:
andrei-mihnea-cerbu
2024-11-12 23:14:08 +02:00
parent 44df437425
commit fa4750ea3b
4 changed files with 53 additions and 49 deletions
+3 -9
View File
@@ -28,24 +28,18 @@ export class FileSharer {
async start(): Promise<void> {
setInterval(async () => {
if (!this.isBusy) { // Check if the queue is already being processed
this.isBusy = true; // Set busy flag to true before starting
this.log("Start successfully. Processing the queue.");
await this.processQueue(); // Process the queue at regular intervals
this.log("Queue processing completed.");
await this.processQueue();
}
}, 10000); // 10 seconds interval
}
// Method to process the queue
private async processQueue(): Promise<void> {
if (this.isBusy) {
this.log("Queue is already being processed. Skipping this interval.");
return;
}
this.isBusy = true; // Set busy flag to true before starting
while (!this.queueManager.isEmpty()) {
const task = this.queueManager.peek();
this.log('trimiti fisier');
if (task) {
this.log(`Processing task for file: ${task.path} to IP: ${task.ip}`);
@@ -116,6 +116,8 @@ export class UserToUserOperations implements OperationPlugin {
const appInfo = await jsonManager.readValue('shareDirectory');
const shareDirectory = appInfo?.path || '';
console.log(`\n\nShare directory: ${shareDirectory}\n\n`);
if (!shareDirectory) {
return { operationCode: operationCodes.ERR, metaInfo: { message: 'Share directory missing.' }};
}
@@ -113,37 +113,41 @@ export class TcpClientCommunicator extends SocketCommunicatorBase {
}
async handleIncomingChunk(data: Buffer): Promise<void> {
const incomingMessage = data.toString();
this.messageBuffer += incomingMessage;
const incomingMessage = data.toString().trim();
if (this.messageBuffer.includes(END_OF_MESSAGE)) {
const messages = this.messageBuffer.split(END_OF_MESSAGE);
for (let i = 0; i < messages.length - 1; i++) {
const completeMessage = messages[i];
if (completeMessage) {
await this.processCompleteMessage(completeMessage);
}
}
this.messageBuffer = messages[messages.length - 1];
// Check if the message is the end marker <EOM>
if (incomingMessage === END_OF_MESSAGE) {
// Process the complete message when EOM is received
await this.processCompleteMessage();
return;
}
}
private async processCompleteMessage(completeMessage: string): Promise<void> {
const [headerJson, chunkContent] = completeMessage.split('|');
// Extract header and chunk content from incoming data
const [headerJson, chunkContent] = incomingMessage.split('|');
const header = JSON.parse(headerJson);
// Initialize chunk array if this is the first chunk for this messageId
if (!this.chunkBuffers[header.messageId]) {
this.chunkBuffers[header.messageId] = new Array(header.totalChunks);
}
// Place the chunk in the correct position in the chunk buffer
this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent;
}
if (this.chunkBuffers[header.messageId].every((chunk) => chunk !== undefined)) {
const fullMessage = this.chunkBuffers[header.messageId].join('');
await this.handleIncomingMessage(fullMessage);
delete this.chunkBuffers[header.messageId];
// Process the complete message when EOM is received
private async processCompleteMessage(): Promise<void> {
for (const [messageId, chunks] of Object.entries(this.chunkBuffers)) {
if (chunks.every((chunk) => chunk !== undefined)) {
// Join all chunks to form the full message
const fullMessage = chunks.join('');
// Handle the completed and possibly decrypted message
await this.handleIncomingMessage(fullMessage);
// Clean up buffer after processing
delete this.chunkBuffers[messageId];
}
}
}
@@ -97,37 +97,41 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
// Handle incoming chunks of data
async handleIncomingChunk(data: Buffer): Promise<void> {
const incomingMessage = data.toString();
this.messageBuffer += incomingMessage;
const incomingMessage = data.toString().trim();
if (this.messageBuffer.includes(END_OF_MESSAGE)) {
const messages = this.messageBuffer.split(END_OF_MESSAGE);
for (let i = 0; i < messages.length - 1; i++) {
const completeMessage = messages[i];
if (completeMessage) {
await this.processCompleteMessage(completeMessage);
}
}
this.messageBuffer = messages[messages.length - 1];
// Check if the message is the end marker <EOM>
if (incomingMessage === END_OF_MESSAGE) {
// Process the complete message when EOM is received
await this.processCompleteMessage();
return;
}
}
private async processCompleteMessage(completeMessage: string): Promise<void> {
const [headerJson, chunkContent] = completeMessage.split('|');
// Extract header and chunk content from incoming data
const [headerJson, chunkContent] = incomingMessage.split('|');
const header = JSON.parse(headerJson);
// Initialize chunk array if this is the first chunk for this messageId
if (!this.chunkBuffers[header.messageId]) {
this.chunkBuffers[header.messageId] = new Array(header.totalChunks);
}
// Place the chunk in the correct position in the chunk buffer
this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent;
}
if (this.chunkBuffers[header.messageId].every((chunk) => chunk !== undefined)) {
const fullMessage = this.chunkBuffers[header.messageId].join('');
await this.handleIncomingMessage(fullMessage);
delete this.chunkBuffers[header.messageId];
// Process the complete message when EOM is received
private async processCompleteMessage(): Promise<void> {
for (const [messageId, chunks] of Object.entries(this.chunkBuffers)) {
if (chunks.every((chunk) => chunk !== undefined)) {
// Join all chunks to form the full message
const fullMessage = chunks.join('');
// Handle the completed and possibly decrypted message
await this.handleIncomingMessage(fullMessage);
// Clean up buffer after processing
delete this.chunkBuffers[messageId];
}
}
}