2022-11-06 12:30:06 +00:00
/***
Copyright ( C ) 2022 J Reece Wilson ( a / k / a " Reece " ) . All rights reserved .
2022-12-12 23:50:05 +00:00
File : AuCompressionInterceptor . cpp
2022-11-06 12:30:06 +00:00
Date : 2022 - 9 - 14
Author : Reece
* * */
# include <Source/RuntimeInternal.hpp>
2022-12-12 23:50:05 +00:00
# include "AuCompression.hpp"
# include "AuBaseStream.hpp"
# include "AuCompressionInterceptor.hpp"
2022-11-06 12:30:06 +00:00
namespace Aurora : : Compression
{
CompressionInterceptor : : CompressionInterceptor ( )
{
}
void CompressionInterceptor : : Init ( const AuSPtr < ICompressionStream > & pStream ,
const AuSPtr < BaseStream > & pBaseStream )
{
this - > pBaseStream_ = pBaseStream ;
this - > pStream_ = pStream ;
}
IO : : EStreamError CompressionInterceptor : : IsOpen ( )
{
return IO : : EStreamError : : eErrorNone ;
}
IO : : EStreamError CompressionInterceptor : : Read ( const Memory : : MemoryViewStreamWrite & parameters )
{
2022-11-20 10:31:13 +00:00
AuUInt uRemainingBytes = parameters . length ;
if ( this - > uCountMax_ )
{
uRemainingBytes = AuMin ( this - > uCountMax_ - this - > uCount_ , parameters . length ) ;
}
2022-11-06 12:30:06 +00:00
2022-11-07 22:46:35 +00:00
parameters . outVariable = pLastBuffer_ - > Read ( parameters . ptr , uRemainingBytes ) ;
this - > uCount_ + = parameters . outVariable ;
return parameters . outVariable = = 0 ? IO : : EStreamError : : eErrorEndOfStream : IO : : EStreamError : : eErrorNone ;
2022-11-06 12:30:06 +00:00
}
void CompressionInterceptor : : Close ( )
{
}
bool CompressionInterceptor : : HasFailed ( )
{
return this - > bErrorFlag_ ;
}
bool CompressionInterceptor : : OnDataAvailable ( const AuSPtr < Memory : : ByteBuffer > & pReadInByteBuffer ,
2023-06-10 19:33:19 +00:00
const AuSPtr < Memory : : ByteBuffer > & pWriteOutByteBuffer ,
const AuSPtr < IO : : Protocol : : IProtocolPiece > & pProtocolPiece )
2022-11-06 12:30:06 +00:00
{
this - > pLastBuffer_ = pReadInByteBuffer ;
2023-06-10 19:03:29 +00:00
this - > pBaseStream_ - > SetWeakBuffer ( pReadInByteBuffer ) ;
2022-11-06 12:30:06 +00:00
this - > pBaseStream_ - > SetBuffer ( pWriteOutByteBuffer ) ;
2022-11-08 19:24:08 +00:00
if ( this - > LimitHasHit ( ) )
{
2022-11-20 10:31:13 +00:00
if ( ! this - > bPassthrough_ )
{
2023-06-10 19:03:29 +00:00
this - > pBaseStream_ - > SetWeakBuffer ( { } ) ;
2022-11-20 10:31:13 +00:00
return true ;
}
auto uCount = pWriteOutByteBuffer - > WriteFromEx ( * pReadInByteBuffer , AuNumericLimits < AuUInt > : : max ( ) ) ;
pReadInByteBuffer - > readPtr + = uCount ;
2023-06-10 19:03:29 +00:00
this - > pBaseStream_ - > SetWeakBuffer ( { } ) ;
2022-11-20 10:31:13 +00:00
return bool ( uCount ) ;
2022-11-08 19:24:08 +00:00
}
2022-11-06 12:30:06 +00:00
bool bSuccess { true } ;
do
{
2022-11-07 13:34:28 +00:00
auto uReadable = pReadInByteBuffer - > RemainingBytes ( ) ;
2022-11-06 12:30:06 +00:00
if ( ! uReadable )
{
break ;
}
auto [ a , b ] = this - > pBaseStream_ - > Ingest ( uReadable ) ;
bSuccess = a ! = 0 ;
}
while ( bSuccess ) ;
2023-06-10 19:03:29 +00:00
this - > pBaseStream_ - > SetWeakBuffer ( { } ) ;
2022-11-08 19:24:08 +00:00
if ( ( this - > bAutoFlush_ ) | |
( AuExchange ( this - > bSendFlush_ , false ) ) )
{
if ( ! this - > pBaseStream_ - > Flush ( ) )
{
SysPushErrorIO ( " Couldn't flush stream. I hope this means no data was available/already flushed quirk occurred bc im still not setting bErrorFlag_ yet. This message might explain session instability. " ) ;
}
}
2022-11-06 12:30:06 +00:00
2022-11-20 10:31:13 +00:00
this - > pLastBuffer_ . reset ( ) ;
this - > pBaseStream_ - > SetBuffer ( { } ) ;
2022-11-07 22:46:35 +00:00
if ( this - > LimitHasHit ( ) )
{
2022-11-20 10:31:13 +00:00
if ( pReadInByteBuffer - > RemainingBytes ( ) )
{
if ( ! this - > bPassthrough_ )
{
return true ;
}
2022-11-07 22:46:35 +00:00
2022-11-20 10:31:13 +00:00
pReadInByteBuffer - > readPtr + = pWriteOutByteBuffer - > WriteFromEx ( * pReadInByteBuffer , AuNumericLimits < AuUInt > : : max ( ) ) ;
}
}
else
2022-11-06 12:30:06 +00:00
{
2022-11-20 10:31:13 +00:00
if ( ! bSuccess )
{
this - > bErrorFlag_ = true ;
}
2022-11-06 12:30:06 +00:00
}
2023-04-29 01:30:27 +00:00
return ! this - > bErrorFlag_ ;
2022-11-06 12:30:06 +00:00
}
2022-11-07 22:46:35 +00:00
2022-11-08 19:24:08 +00:00
void CompressionInterceptor : : FlushNextFrame ( )
{
this - > bSendFlush_ = true ;
}
bool CompressionInterceptor : : ConfigureAutoFlushPerFrame ( bool bAutoFlush )
{
return AuExchange ( this - > bAutoFlush_ , bAutoFlush ) ;
}
2022-11-07 22:46:35 +00:00
bool CompressionInterceptor : : LimitHasHit ( )
{
return this - > uCount_ > = this - > uCountMax_ ;
}
2022-11-20 10:31:13 +00:00
bool CompressionInterceptor : : LimitPassthroughOnOverflow ( bool bPassthrough )
{
return AuExchange ( this - > bPassthrough_ , bPassthrough ) ;
}
2022-11-07 22:46:35 +00:00
void CompressionInterceptor : : LimitReset ( )
{
this - > uCount_ = 0 ;
this - > uCountMax_ = 0 ;
}
AuUInt CompressionInterceptor : : LimitGetIndex ( )
{
return this - > uCount_ ;
}
void CompressionInterceptor : : LimitSet ( AuUInt uLength )
{
this - > uCountMax_ = uLength ;
}
2022-11-06 12:30:06 +00:00
AUKN_SYM AuSPtr < ICompressionInterceptor > NewDecompressionInterceptor ( const DecompressInfo & info )
{
auto pInterceptor = AuMakeShared < CompressionInterceptor > ( ) ;
if ( ! pInterceptor )
{
SysPushErrorMemory ( ) ;
return { } ;
}
auto pDecompressor = DecompressorShared ( AuUnsafeRaiiToShared ( pInterceptor . get ( ) ) ,
info ) ;
if ( ! pDecompressor )
{
SysPushErrorMemory ( ) ;
return { } ;
}
pInterceptor - > Init ( pDecompressor ,
AuStaticCast < Compression : : BaseStream > ( pDecompressor ) ) ;
return pInterceptor ;
}
AUKN_SYM AuSPtr < ICompressionInterceptor > NewCompressionInterceptor ( const CompressInfo & info )
{
auto pInterceptor = AuMakeShared < CompressionInterceptor > ( ) ;
if ( ! pInterceptor )
{
SysPushErrorMemory ( ) ;
return { } ;
}
auto pCompressor = CompressorShared ( AuUnsafeRaiiToShared ( pInterceptor . get ( ) ) ,
info ) ;
if ( ! pCompressor )
{
SysPushErrorMemory ( ) ;
return { } ;
}
2022-11-07 22:46:35 +00:00
2022-11-06 12:30:06 +00:00
pInterceptor - > Init ( pCompressor ,
AuStaticCast < Compression : : BaseStream > ( pCompressor ) ) ;
return pInterceptor ;
}
}