fix the buffer issue

This commit is contained in:
NguyenVanDat
2025-10-15 18:45:26 +07:00
parent d757c3137b
commit 668f808883
@@ -395,7 +395,7 @@ namespace CSNetwork
Octets currentData = new Octets( Octets currentData = new Octets(
_receiveOctets.RawBuffer, _receiveOctets.RawBuffer,
0, 0,
originalBlockLength _receiveOctets.Size
); );
// Update returns a NEW Octets object with processed data // Update returns a NEW Octets object with processed data
dataToProcess = currentIsec!.Update(currentData); dataToProcess = currentIsec!.Update(currentData);
@@ -473,7 +473,7 @@ namespace CSNetwork
{ {
// If security was applied and we processed/consumed *any* bytes from the processed stream, // If security was applied and we processed/consumed *any* bytes from the processed stream,
// assume the whole original block was consumed. // assume the whole original block was consumed.
bytesConsumedFromOriginal = processedAnyProtocols ? originalBlockLength : 0; bytesConsumedFromOriginal = totalConsumedFromProcessedStream;
} }
else else
{ {
@@ -491,7 +491,7 @@ namespace CSNetwork
// *** Helper to compact the original receive buffer *** // *** Helper to compact the original receive buffer ***
private void CompactDecryptedBuffer(int bytesToConsume, int originalLength) private void CompactDecryptedBuffer(int bytesToConsume, int originalLength)
{ {
if (bytesToConsume <= 0 || _receiveOctets == null) // Add null check if (bytesToConsume <= 0 || _decryptedOctets == null) // Add null check
{ {
return; // Nothing to consume/compact return; // Nothing to consume/compact
} }
@@ -510,15 +510,14 @@ namespace CSNetwork
0, 0,
remaining remaining
); );
_receiveOctets.SetSize(remaining); _decryptedOctets.SetSize(remaining);
} }
else // Consumed all else // Consumed all
{ {
// All data processed or skipped, clear buffer // All data processed or skipped, clear buffer
// _logger.Log(LogType.Info, $"Clearing original buffer: Consumed {bytesToConsume} >= Original {originalLength}"); // _logger.Log(LogType.Info, $"Clearing original buffer: Consumed {bytesToConsume} >= Original {originalLength}");
_receiveOctets.SetSize(0); _decryptedOctets.SetSize(0);
} }
_receiveBufferStream.Position = _receiveOctets.Length; // Reset stream pos just in case
} }
// Helper to raise ErrorOccurred event // Helper to raise ErrorOccurred event