Revert "Changes as per Cascade - Crashing"
This reverts commit 503f0bd157
.
This commit is contained in:
parent
7bc6431d44
commit
540ab9b8e2
@ -37,14 +37,6 @@ const PACKET_TYPES = {
|
|||||||
'ERROR': 0xff // Error notification
|
'ERROR': 0xff // Error notification
|
||||||
};
|
};
|
||||||
|
|
||||||
// ====================================
|
|
||||||
// Socket State Management
|
|
||||||
// Tracks the state of the socket connection
|
|
||||||
// ====================================
|
|
||||||
function isSocketActive(socket) {
|
|
||||||
return socket && !socket.destroyed && socket.writable;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ====================================
|
// ====================================
|
||||||
// Packet Handler
|
// Packet Handler
|
||||||
// Processes incoming packets based on their type
|
// Processes incoming packets based on their type
|
||||||
@ -55,40 +47,22 @@ function handlePacket(socket, audioStream, packet) {
|
|||||||
|
|
||||||
switch (packetType) {
|
switch (packetType) {
|
||||||
case PACKET_TYPES.TERMINATE:
|
case PACKET_TYPES.TERMINATE:
|
||||||
console.log('Terminate packet received. Initiating graceful shutdown.');
|
console.log('Terminate packet received. Closing connection.');
|
||||||
// Clean up streams and pending operations
|
socket.end();
|
||||||
if (audioStream) {
|
|
||||||
audioStream.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a flag to prevent new operations
|
|
||||||
socket.isTerminating = true;
|
|
||||||
|
|
||||||
// Give time for pending operations to complete
|
|
||||||
setTimeout(() => {
|
|
||||||
if (isSocketActive(socket)) {
|
|
||||||
socket.end();
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PACKET_TYPES.UUID:
|
case PACKET_TYPES.UUID:
|
||||||
if (!isSocketActive(socket)) return;
|
|
||||||
const uuid = toUUID(packet.slice(3, 19).toString('hex'));
|
const uuid = toUUID(packet.slice(3, 19).toString('hex'));
|
||||||
socket.uuid = uuid;
|
socket.uuid = uuid;
|
||||||
console.log('UUID packet received: ' + uuid);
|
console.log('UUID packet received: ' + uuid);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PACKET_TYPES.AUDIO:
|
case PACKET_TYPES.AUDIO:
|
||||||
if (!isSocketActive(socket) || socket.isTerminating) return;
|
|
||||||
const audioData = packet.slice(3, 3 + packetLength);
|
const audioData = packet.slice(3, 3 + packetLength);
|
||||||
if (audioStream && !audioStream.destroyed) {
|
audioStream.write(audioData);
|
||||||
audioStream.write(audioData);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PACKET_TYPES.ERROR:
|
case PACKET_TYPES.ERROR:
|
||||||
if (!isSocketActive(socket)) return;
|
|
||||||
const errorCode = packetLength > 0 ? packet.readUInt8(3) : null;
|
const errorCode = packetLength > 0 ? packet.readUInt8(3) : null;
|
||||||
console.log('Error packet received with code: ' + errorCode);
|
console.log('Error packet received with code: ' + errorCode);
|
||||||
break;
|
break;
|
||||||
@ -98,17 +72,6 @@ function handlePacket(socket, audioStream, packet) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safe write function to prevent write-after-end errors
|
|
||||||
function safeSocketWrite(socket, data) {
|
|
||||||
if (isSocketActive(socket) && !socket.isTerminating) {
|
|
||||||
try {
|
|
||||||
socket.write(data);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error writing to socket:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ====================================
|
// ====================================
|
||||||
// Main Server Implementation
|
// Main Server Implementation
|
||||||
// Creates and manages the TCP server that handles client connections
|
// Creates and manages the TCP server that handles client connections
|
||||||
@ -214,7 +177,7 @@ const server = net.createServer(async socket => {
|
|||||||
header.writeUInt16BE(chunk.length, 1);
|
header.writeUInt16BE(chunk.length, 1);
|
||||||
|
|
||||||
const packet = Buffer.concat([header, chunk]);
|
const packet = Buffer.concat([header, chunk]);
|
||||||
safeSocketWrite(socket, packet);
|
socket.write(packet);
|
||||||
await new Promise(resolve => setTimeout(resolve, 20));
|
await new Promise(resolve => setTimeout(resolve, 20));
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user