34#if defined (_TEC_USE_ZLIB)
129 int _level = CompressionParams::kDefaultCompressionLevel,
130 size_t _min_size = CompressionParams::kMinSize)
163#if defined (ZLIB_VERSION)
164 if (
type_ == CompressionParams::kCompressionZlib) {
166 return compress_zlib(nd);
206 if (compression == CompressionParams::kNoCompression) {
211#if defined (ZLIB_VERSION)
212 if (compression == CompressionParams::kCompressionZlib) {
213 return uncompress_zlib(nd);
227#if defined (ZLIB_VERSION)
249 auto size_compressed = ::compressBound(nd.
header.
size);
250 if (size_compressed == 0) {
259 tmp.header.set_compression(
type_);
260 tmp.header.set_compression_level(
level_);
262 tmp.bytes().resize(size_compressed);
267 auto result = ::compress2(
268 (Bytef*)tmp.bytes().ptr(0), &size_compressed,
271 if (result != Z_OK) {
277 tmp.header.size = size_compressed;
278 nd.
move_from(std::move(tmp), size_compressed);
279 TEC_TRACE(
"Compressed to {} bytes with ratio {}.",
293 virtual Status uncompress_zlib(NetData& nd)
const {
294 TEC_ENTER(
"NdCompress::uncompress_zlib");
295 if (nd.header.get_compression() == CompressionParams::kNoCompression) {
303 tmp.header = nd.header;
305 uLongf dest_len = nd.header.size_uncompressed;
306 tmp.bytes().resize(dest_len);
310 TEC_TRACE(
"Uncompressing {} bytes...", nd.size());
312 (Bytef*)tmp.data(), &dest_len,
313 (
const Bytef*)nd.data(), nd.size());
314 if (result != Z_OK) {
320 tmp.header.size = dest_len;
321 tmp.header.size_uncompressed = 0;
322 nd.move_from(std::move(tmp));
323 TEC_TRACE(
"Upcompressed to {} bytes.", nd.size());
const char * ptr(long pos) const
Returns a const pointer to a specific position in the buffer.
Definition tec_memfile.hpp:273
Compression wrapper for NetData objects with pluggable backends.
Definition tec_nd_compress.hpp:89
NdCompress(int _type, int _level=CompressionParams::kDefaultCompressionLevel, size_t _min_size=CompressionParams::kMinSize)
Constructs compressor with explicit settings.
Definition tec_nd_compress.hpp:128
virtual Status compress(NetData &nd) const
Compresses the payload of a NetData object in-place (if configured)
Definition tec_nd_compress.hpp:161
virtual Status uncompress(NetData &nd) const
Decompresses the payload of a NetData object in-place (if compressed)
Definition tec_nd_compress.hpp:202
NdCompress()
Default constructor — uses library-wide default compression settings.
Definition tec_nd_compress.hpp:108
size_t min_size_
Minimum payload size (in bytes) below which compression is skipped.
Definition tec_nd_compress.hpp:93
int level_
Compression level (meaning depends on backend — usually [1..9] for Zlib)
Definition tec_nd_compress.hpp:92
int type_
Compression algorithm identifier (from CompressionParams)
Definition tec_nd_compress.hpp:91
Lightweight binary serialization container optimized for network communication.
Definition tec_net_data.hpp:51
size_t size() const
Returns current logical size of the message (according to header)
Definition tec_net_data.hpp:127
void rewind()
Resets read position to the beginning of the buffer.
Definition tec_net_data.hpp:119
void move_from(NetData &&src, size_t size_to_shrink=0)
Efficiently moves content from another NetData instance.
Definition tec_net_data.hpp:111
Header header
Global message header.
Definition tec_net_data.hpp:54
const Bytes & bytes() const
Returns const reference to internal byte buffer.
Definition tec_net_data.hpp:85
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
#define TEC_TRACE(...)
Logs a formatted trace message.
Definition tec_trace.hpp:222
Definition tec_compression.hpp:34
@ RuntimeErr
Runtime error during execution.
@ Unsupported
The feature is unsupported.
Common definitions and utilities for the tec namespace.
Lightweight binary serialization optimized for network communication.
Defines error handling types and utilities for the tec namespace.
TStatus< int, std::string > Status
Type alias for a default TStatus with int code and std::string description.
Definition tec_status.hpp:223
Provides a thread-safe tracing utility for debugging in the tec namespace.