engine: common: netchan: only compress with BZip2 when it's efficient

This commit is contained in:
Alibek Omarov 2024-10-10 23:36:23 +03:00
parent 9188905145
commit 6bdc1941e6

View file

@ -744,14 +744,18 @@ static void Netchan_CreateFragments_( netchan_t *chan, sizebuf_t *msg )
{ {
#if !XASH_DEDICATED #if !XASH_DEDICATED
byte pbOut[0x10000]; byte pbOut[0x10000];
uint uSourceSize = MSG_GetNumBytesWritten( msg );
uint uCompressedSize = MSG_GetNumBytesWritten( msg ) - 4; uint uCompressedSize = MSG_GetNumBytesWritten( msg ) - 4;
if( !BZ2_bzBuffToBuffCompress( pbOut, &uCompressedSize, MSG_GetData( msg ), MSG_GetNumBytesWritten( msg ), 9, 0, 30 )) if( !BZ2_bzBuffToBuffCompress( pbOut, &uCompressedSize, MSG_GetData( msg ), uSourceSize, 9, 0, 30 ))
{ {
Con_Reportf( "Compressing split packet with BZip2 (%d -> %d bytes)\n", MSG_GetNumBytesWritten( msg ), uCompressedSize ); if( uCompressedSize < uSourceSize )
{
Con_Reportf( "Compressing split packet with BZip2 (%d -> %d bytes)\n", uSourceSize, uCompressedSize );
memcpy( msg->pData, "BZ2", 4 ); memcpy( msg->pData, "BZ2", 4 );
memcpy( msg->pData + 4, pbOut, uCompressedSize ); memcpy( msg->pData + 4, pbOut, uCompressedSize );
MSG_SeekToBit( msg, uCompressedSize << 3, SEEK_SET ); MSG_SeekToBit( msg, uCompressedSize << 3, SEEK_SET );
} }
}
#else #else
Host_Error( "%s: BZ2 compression is not supported for server", __func__ ); Host_Error( "%s: BZ2 compression is not supported for server", __func__ );
#endif #endif