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.
The default constructor, creates an empty DBuffer.
Creates a DBuffer that is a copy of source.
Copies source to this DBuffer, overwriting any previous contents.
Appends source to this DBuffer.
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.
Appends an array of bytes to this DBuffer. length is the number of elements to copy.
Appends a single byte to this DBuffer.
Appends a string to the end of the DBuffer.
Removes all elements from the DBuffer.
Returns the byte at the specified index.
Inserts a single byte to the front of the DBuffer.
Removes the first numRemovals bytes from the front of the DBuffer. Clears the DBuffer if numRemovals is larger than the buffer's size.
Sets the value of the byte at element index to value. Throws an exception if index is out of bounds.
Returns the size of the DBuffer in bytes.
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.
Converts the DBuffer contents to a string. Note: this will corrupt binary data.
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.
Calculates a 32-bit CRC value for the contained data, using the 'fast' CRC algorithm.
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.
Returns a description of the most recent error.
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.
Similar to appendVariable, but it inserts theVariable at the beginning of the data.
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;