network chunk v3
This commit is contained in:
@@ -5,7 +5,6 @@ import { SocketCommunicatorBase } from './socket_communicator_base';
|
|||||||
import { OperationHandler } from '../operations_base/operation_handler';
|
import { OperationHandler } from '../operations_base/operation_handler';
|
||||||
import { operationCodes } from '../operation_codes';
|
import { operationCodes } from '../operation_codes';
|
||||||
|
|
||||||
const END_OF_MESSAGE = '<EOM>'; // Unique marker for the end of message
|
|
||||||
const CHUNK_SIZE = 1024; // Define chunk size
|
const CHUNK_SIZE = 1024; // Define chunk size
|
||||||
|
|
||||||
export class TcpClientCommunicator extends SocketCommunicatorBase {
|
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 chunk = outgoingMessage.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE);
|
||||||
const chunkHeader = JSON.stringify({
|
const chunkHeader = JSON.stringify({
|
||||||
messageId,
|
messageId,
|
||||||
sequenceNumber: i,
|
sequenceNumber: i + 1,
|
||||||
totalChunks,
|
totalChunks,
|
||||||
});
|
});
|
||||||
|
|
||||||
const chunkWithHeader = `${chunkHeader}|${chunk}`;
|
const chunkWithHeader = `${chunkHeader}|${chunk}`;
|
||||||
|
|
||||||
await this.writeToSocket(chunkWithHeader);
|
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<void> {
|
async handleIncomingChunk(data: Buffer): Promise<void> {
|
||||||
const incomingMessage = data.toString().trim();
|
const incomingMessage = data.toString().trim();
|
||||||
|
|
||||||
// Check if the message is the end marker <EOM>
|
console.log(`\n\n${incomingMessage}\n\n`);
|
||||||
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
|
// Extract header and chunk content from incoming data
|
||||||
const [headerJson, chunkContent] = incomingMessage.split('|');
|
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
|
// Place the chunk in the correct position in the chunk buffer
|
||||||
this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent;
|
this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent;
|
||||||
|
if(header.sequenceNumber === header.totalChunks) {
|
||||||
|
await this.processCompleteMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the complete message when EOM is received
|
// Process the complete message when EOM is received
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { MessageHandler } from '../message_handler';
|
|||||||
import { SocketCommunicatorBase } from './socket_communicator_base';
|
import { SocketCommunicatorBase } from './socket_communicator_base';
|
||||||
import { OperationHandler } from '../operations_base/operation_handler';
|
import { OperationHandler } from '../operations_base/operation_handler';
|
||||||
|
|
||||||
const END_OF_MESSAGE = '<EOM>'; // Define a unique marker for end of message
|
|
||||||
const CHUNK_SIZE = 1024; // Define chunk size
|
const CHUNK_SIZE = 1024; // Define chunk size
|
||||||
|
|
||||||
export class TcpServerCommunicator extends SocketCommunicatorBase {
|
export class TcpServerCommunicator extends SocketCommunicatorBase {
|
||||||
@@ -99,13 +98,6 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
|
|||||||
async handleIncomingChunk(data: Buffer): Promise<void> {
|
async handleIncomingChunk(data: Buffer): Promise<void> {
|
||||||
const incomingMessage = data.toString().trim();
|
const incomingMessage = data.toString().trim();
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract header and chunk content from incoming data
|
// Extract header and chunk content from incoming data
|
||||||
const [headerJson, chunkContent] = incomingMessage.split('|');
|
const [headerJson, chunkContent] = incomingMessage.split('|');
|
||||||
const header = JSON.parse(headerJson);
|
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
|
// Place the chunk in the correct position in the chunk buffer
|
||||||
this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent;
|
this.chunkBuffers[header.messageId][header.sequenceNumber] = chunkContent;
|
||||||
|
if(header.sequenceNumber === header.totalChunks) {
|
||||||
|
await this.processCompleteMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the complete message when EOM is received
|
// 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 chunk = outgoingMessage.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE);
|
||||||
const chunkHeader = JSON.stringify({
|
const chunkHeader = JSON.stringify({
|
||||||
messageId,
|
messageId,
|
||||||
sequenceNumber: i,
|
sequenceNumber: i + 1,
|
||||||
totalChunks,
|
totalChunks,
|
||||||
});
|
});
|
||||||
|
|
||||||
const chunkWithHeader = `${chunkHeader}|${chunk}`;
|
const chunkWithHeader = `${chunkHeader}|${chunk}`;
|
||||||
|
|
||||||
await this.writeToSocket(chunkWithHeader);
|
await this.writeToSocket(chunkWithHeader);
|
||||||
|
|
||||||
if (i === totalChunks - 1) {
|
|
||||||
await this.writeToSocket(END_OF_MESSAGE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user