LZ4Compressor.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Math/Crc.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <AzNetworking/Framework/ICompressor.h>
  12. #include <AzCore/Casting/numeric_cast.h>
  13. namespace MultiplayerCompression
  14. {
  15. static const char* CompressorName = "LZ4";
  16. static const AzNetworking::CompressorType CompressorType = aznumeric_cast<AzNetworking::CompressorType>(static_cast<AZ::u32>(AZ::Crc32(CompressorName)));
  17. /**
  18. * Implements an LZ4 Compressor against Multiplayer's Compressor interface for use with AzNetworking.
  19. * Handles edge and error cases specific to LZ4 that are otherwise not covered in AzNetworking
  20. * (where a Compressor is applied).
  21. */
  22. class LZ4Compressor
  23. : public AzNetworking::ICompressor
  24. {
  25. public:
  26. AZ_CLASS_ALLOCATOR(LZ4Compressor, AZ::SystemAllocator);
  27. LZ4Compressor() = default;
  28. const char* GetName() const { return CompressorName; }
  29. AzNetworking::CompressorType GetType() const override { return CompressorType; };
  30. bool Init() override { return true; }
  31. size_t GetMaxChunkSize(size_t maxCompSize) const override;
  32. size_t GetMaxCompressedBufferSize(size_t uncompSize) const override;
  33. AzNetworking::CompressorError Compress(const void* uncompData, size_t uncompSize, void* compData, size_t compDataSize, size_t& compSize) override;
  34. AzNetworking::CompressorError Decompress(const void* compData, size_t compDataSize, void* uncompData, size_t uncompDataSize, size_t& consumedSize, size_t& uncompSize) override;
  35. };
  36. }