From 8584a0683f540ae68793bffecafb76046bcb989d Mon Sep 17 00:00:00 2001 From: andrei-mihnea-cerbu Date: Tue, 12 Nov 2024 23:28:12 +0200 Subject: [PATCH] network chunk v3 --- .../tcp_client_communicator.ts | 17 +++++------------ .../tcp_server_communicator.ts | 17 ++++------------- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/User/src/network/socket_communicator/tcp_client_communicator.ts b/User/src/network/socket_communicator/tcp_client_communicator.ts index 1305457..b2fa608 100644 --- a/User/src/network/socket_communicator/tcp_client_communicator.ts +++ b/User/src/network/socket_communicator/tcp_client_communicator.ts @@ -5,7 +5,6 @@ import { SocketCommunicatorBase } from './socket_communicator_base'; import { OperationHandler } from '../operations_base/operation_handler'; import { operationCodes } from '../operation_codes'; -const END_OF_MESSAGE = ''; // Unique marker for the end of message const CHUNK_SIZE = 1024; // Define chunk size export class TcpClientCommunicator extends SocketCommunicatorBase { @@ -87,17 +86,13 @@ export class TcpClientCommunicator extends SocketCommunicatorBase { const chunk = outgoingMessage.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE); const chunkHeader = JSON.stringify({ messageId, - sequenceNumber: i, + sequenceNumber: i + 1, totalChunks, }); const chunkWithHeader = `${chunkHeader}|${chunk}`; await this.writeToSocket(chunkWithHeader); - - if (i === totalChunks - 1) { - await this.writeToSocket(END_OF_MESSAGE); - } } } @@ -115,12 +110,7 @@ export class TcpClientCommunicator extends SocketCommunicatorBase { async handleIncomingChunk(data: Buffer): Promise { const incomingMessage = data.toString().trim(); - // Check if the message is the end marker - if (incomingMessage === END_OF_MESSAGE) { - // Process the complete message when EOM is received - await this.processCompleteMessage(); - return; - } + console.log(`\n\n${incomingMessage}\n\n`); // Extract header and chunk content from incoming data const [headerJson, chunkContent] = incomingMessage.split('|'); @@ -133,6 +123,9 @@ export class TcpClientCommunicator extends SocketCommunicatorBase { // Place the chunk in the correct position in the chunk buffer this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent; + if(header.sequenceNumber === header.totalChunks) { + await this.processCompleteMessage(); + } } // Process the complete message when EOM is received diff --git a/User/src/network/socket_communicator/tcp_server_communicator.ts b/User/src/network/socket_communicator/tcp_server_communicator.ts index 4f03d2c..dc6382d 100644 --- a/User/src/network/socket_communicator/tcp_server_communicator.ts +++ b/User/src/network/socket_communicator/tcp_server_communicator.ts @@ -4,7 +4,6 @@ import { MessageHandler } from '../message_handler'; import { SocketCommunicatorBase } from './socket_communicator_base'; import { OperationHandler } from '../operations_base/operation_handler'; -const END_OF_MESSAGE = ''; // Define a unique marker for end of message const CHUNK_SIZE = 1024; // Define chunk size export class TcpServerCommunicator extends SocketCommunicatorBase { @@ -99,13 +98,6 @@ export class TcpServerCommunicator extends SocketCommunicatorBase { async handleIncomingChunk(data: Buffer): Promise { const incomingMessage = data.toString().trim(); - // Check if the message is the end marker - if (incomingMessage === END_OF_MESSAGE) { - // Process the complete message when EOM is received - await this.processCompleteMessage(); - return; - } - // Extract header and chunk content from incoming data const [headerJson, chunkContent] = incomingMessage.split('|'); const header = JSON.parse(headerJson); @@ -117,6 +109,9 @@ export class TcpServerCommunicator extends SocketCommunicatorBase { // Place the chunk in the correct position in the chunk buffer this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent; + if(header.sequenceNumber === header.totalChunks) { + await this.processCompleteMessage(); + } } // Process the complete message when EOM is received @@ -158,17 +153,13 @@ export class TcpServerCommunicator extends SocketCommunicatorBase { const chunk = outgoingMessage.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE); const chunkHeader = JSON.stringify({ messageId, - sequenceNumber: i, + sequenceNumber: i + 1, totalChunks, }); const chunkWithHeader = `${chunkHeader}|${chunk}`; await this.writeToSocket(chunkWithHeader); - - if (i === totalChunks - 1) { - await this.writeToSocket(END_OF_MESSAGE); - } } }