TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::NdCompress Class Reference

Compression wrapper for NetData objects with pluggable backends. More...

#include <tec_nd_compress.hpp>

Public Member Functions

 NdCompress ()
 Default constructor — uses library-wide default compression settings.
 
 NdCompress (int _type, int _level=CompressionParams::kDefaultCompressionLevel, size_t _min_size=CompressionParams::kMinSize)
 Constructs compressor with explicit settings.
 
virtual Status compress (NetData &nd) const
 Compresses the payload of a NetData object in-place (if configured)
 
virtual Status uncompress (NetData &nd) const
 Decompresses the payload of a NetData object in-place (if compressed)
 

Protected Attributes

int type_
 Compression algorithm identifier (from CompressionParams)
 
int level_
 Compression level (meaning depends on backend — usually [1..9] for Zlib)
 
size_t min_size_
 Minimum payload size (in bytes) below which compression is skipped.
 

Detailed Description

Compression wrapper for NetData objects with pluggable backends.

Currently supports Zlib (deflate) when _TEC_USE_ZLIB / ZLIB_VERSION is defined. Designed to be used as a strategy object — either as a member or passed by const reference.

Main features:

  • Configurable compression type, level, and minimum size threshold
  • In-place modification of NetData (moves internal buffer when compression occurs)
  • Skips compression for small payloads (controlled by min_size_)
  • Sets appropriate fields in NetData::header (compression type, level, uncompressed size)
Note
All public methods are const — the object is immutable after construction.
Currently only Zlib is implemented; other algorithms can be added by extending the compress() / uncompress() dispatch logic.

Basic usage examples

// 1. Zlib (usually Zlib or no-op depending on build)
NdCompress compressor(CompressionParams::kCompressionZlib);
NetData msg = ...; // filled message
compressor.compress(msg); // may compress in-place
// Later on receiver side
compressor.uncompress(msg); // restores original if was compressed
// 2. Explicit Zlib with custom level
NdCompress fast_compressor(
CompressionParams::kCompressionZlib,
3, // fast compression
512 // don't compress < 512 bytes
);
NetData large_payload = create_large_payload();
auto st = fast_compressor.compress(large_payload);
if (st) {
send_over_network(large_payload);
}
Compression wrapper for NetData objects with pluggable backends.
Definition tec_nd_compress.hpp:89
Lightweight binary serialization container optimized for network communication.
Definition tec_net_data.hpp:51

Constructor & Destructor Documentation

◆ NdCompress() [1/2]

tec::NdCompress::NdCompress ( )
inline

Default constructor — uses library-wide default compression settings.

Initializes with:

  • type = CompressionParams::kDefaultCompression
  • level = CompressionParams::kDefaultCompressionLevel
  • min_size = CompressionParams::kMinSize
NdCompress compressor; // most common usage

◆ NdCompress() [2/2]

tec::NdCompress::NdCompress ( int  _type,
int  _level = CompressionParams::kDefaultCompressionLevel,
size_t  _min_size = CompressionParams::kMinSize 
)
inlineexplicit

Constructs compressor with explicit settings.

Parameters
_typeCompression algorithm (e.g. CompressionParams::kCompressionZlib)
_levelCompression level (backend-specific; default = library default)
_min_sizeSmallest payload size worth compressing (default = library minimum)
// Aggressive compression for archival/large messages
NdCompress archiver(CompressionParams::kCompressionZlib, 9, 1024);
// Very fast compression, skip tiny messages
NdCompress low_latency(CompressionParams::kCompressionZlib, 1, 256);

Member Function Documentation

◆ compress()

virtual Status tec::NdCompress::compress ( NetData nd) const
inlinevirtual

Compresses the payload of a NetData object in-place (if configured)

Parameters
ndNetwork data object to compress (modified in-place on success)
Returns
Status::OK on success, error otherwise

Behavior:

  • If payload size < min_size_, sets compression = kNoCompression and returns
  • Otherwise delegates to backend-specific implementation (currently only zlib)
  • On success: updates header fields (compression, compression_level, size_uncompressed)
  • On failure: nd remains unchanged
NdCompress compressor(CompressionParams::kCompressionZlib, 6);
NetData frame = build_video_frame();
Status st = compressor.compress(frame);
if (!st) {
LOG(ERROR) << "Compression failed: " << st.message();
return;
}
// frame is now potentially smaller + header updated
network.send(frame);

◆ uncompress()

virtual Status tec::NdCompress::uncompress ( NetData nd) const
inlinevirtual

Decompresses the payload of a NetData object in-place (if compressed)

Parameters
ndNetwork data object to decompress (modified in-place on success)
Returns
Status::OK on success, error otherwise

Behavior:

  • If header indicates kNoCompression → returns immediately (no-op)
  • Otherwise delegates to backend-specific decompressor
  • On success: clears size_uncompressed and updates size
  • On failure: nd remains unchanged
// Receiver side
NdCompress compressor; // same settings as sender (or just default)
NetData received;
network.receive(received);
Status st = compressor.uncompress(received);
if (!st) {
LOG(ERROR) << "Decompression failed: " << st.message();
return;
}
// received.payload() now contains original uncompressed data
process_message(received);
virtual Status uncompress(NetData &nd) const
Decompresses the payload of a NetData object in-place (if compressed)
Definition tec_nd_compress.hpp:202

The documentation for this class was generated from the following file: