Class DBuffer



The DBuffer class contains data to be sent or received across the network. It can compress the data, serialize primitive variables, and compute simple checksums. The DBuffer performs bounds-checking, and allows data to be added to the front and back. Appending and sending data is much faster than with an STL container.

A DBytes can hold a maximum of 2^31 - 1 bytes. (One byte less than two gigabytes.)

Note: Some of the functions below are from DBuffer's parent class, DBytes. To keep things simple, both are shown here.

Constructors

Data Manipulation

Compression and Hashing

Serialization


DBuffer(void)

The default constructor, creates an empty DBuffer.


DBuffer(const DBuffer &source)

Creates a DBuffer that is a copy of source.


DBuffer& operator= (const DBuffer &source)

Copies source to this DBuffer, overwriting any previous contents.


void append(const DBuffer &source)

Appends source to this DBuffer.


void append(const DBuffer &source, long int index, long int length)

Appends a portion of source to this DBuffer. Copies length elements, starting at element index. Throws an exception if index + length is out of bounds.


void appendArray(const uint8_t *source, long int length)

Appends an array of bytes to this DBuffer. length is the number of elements to copy.


void appendByte(uint8_t source)

Appends a single byte to this DBuffer.


void appendString(const string &source)

Appends a string to the end of the DBuffer.


void clear(void)

Removes all elements from the DBuffer.


uint8_t getByte(long int index)

Returns the byte at the specified index.


void insertByte(uint8_t source)

Inserts a single byte to the front of the DBuffer.


void removeFirst(long int numRemovals)

Removes the first numRemovals bytes from the front of the DBuffer. Clears the DBuffer if numRemovals is larger than the buffer's size.


void setByte(long int index, uint8_t value)

Sets the value of the byte at element index to value. Throws an exception if index is out of bounds.


unsigned long int size(void)

Returns the size of the DBuffer in bytes.


void toArray(uint8_t *theArray, uint64_t arraySize)

Copies the DBuffer contents into the provided array. theArray should already be allocated, and arraySize should be set to the maximum number of bytes to copy.


string toString(void)

Converts the DBuffer contents to a string. Note: this will corrupt binary data.


DBuffer compress(int compressionLevel)

Compresses the DBuffer contents using zlib. compressionLevel should range between 0 (no compression) and 1 (max compression). If a compression error occurs it returns an empty DBuffer, and sets errMsg with a description of the problem.


uint32_t crc32(void)

Calculates a 32-bit CRC value for the contained data, using the 'fast' CRC algorithm.


DBuffer decompress(void)

Decompresses the DBuffer contents using zlib. When decompress is called, the entire DBuffer should contain nothing but the compressed data. Returns an empty DBuffer if it couldn't decompress the data and sets errMsg with a description of the problem.


string getErrMsg(void)

Returns a description of the most recent error.


unsigned int appendVariable(T theVariable)

Converts theVariable into an array of bytes, and appends them to the DBuffer. The return value is the number of bytes that were added. (This will be equal to the size of theVariable). You can use sizeof() to determine variable lengths.

I recommend using fixed-width integers:

    Unsigned integers: uint8_t  uint16_t  uint32_t  uint64_t
    Signed integers:    int8_t   int16_t   int32_t   int64_t

The 'standard' int, long int, etc. have different sizes on machines with different architectures. For example, int is usually four bytes on a 32-bit system, but eight bytes on a 64-bit system. If you use int32_t, you're guaranteed to get an integer that's four bytes long.

All values are stored in big-endian format.


unsigned int insertVariable(T theVariable)

Similar to appendVariable, but it inserts theVariable at the beginning of the data.


void extractVariable(long int index, T &theVariable)

Copies binary data from the DBuffer and uses it to assign a value to theVariable. It copies the first byte from position index, and continues until enough bytes to fill theVariable have been copied. You can use the sizeof() function to determine variable sizes:

DBuffer theData;
int32_t myNumber = 12345;
int32_t numberTest = 0;

cout << "Number of bytes in myNumber: " << sizeof(myNumber) << endl;

theData.appendVariable(myNumber);
theData.extractVariable(0, numberTest);

cout << "Extracted variable: " << numberTest << endl;





















Back to class reference