Shadowrun: Awakened 29 September 2011 - Build 871
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
RakNet::BitStream Class Reference

#include <BitStream.h>

List of all members.

Public Member Functions

void AddBitsAndReallocate (const BitSize_t numberOfBitsToWrite)
 Reallocates (if necessary) in preparation of writing numberOfBitsToWrite.
void AlignReadToByteBoundary (void)
 Align the next write and/or read to a byte boundary.
void AlignWriteToByteBoundary (void)
 Align the next write and/or read to a byte boundary.
void AssertCopyData (void)
 If we used the constructor version with copy data off, this makes sure it is set to on and the data pointed to is copied.
void AssertStreamEmpty (void)
 This is good to call when you are done with the stream to make sure you didn't leave any data left over void.
 BitStream (const unsigned int initialBytesToAllocate)
 Create the bitstream, with some number of bytes to immediately allocate.
 BitStream ()
 Default Constructor.
 BitStream (unsigned char *_data, const unsigned int lengthInBytes, bool _copyData)
 Initialize the BitStream, immediately setting the data it contains to a predefined pointer.
BitSize_t CopyData (unsigned char **_data) const
 Makes a copy of the internal data for you _data will point to the stream. Partial bytes are left aligned.
void EndianSwapBytes (int byteOffset, int length)
unsigned char * GetData (void) const
BitSize_t GetNumberOfBitsAllocated (void) const
BitSize_t GetNumberOfBitsUsed (void) const
 Returns the length in bits of the stream.
BitSize_t GetNumberOfBytesUsed (void) const
 Returns the length in bytes of the stream.
BitSize_t GetNumberOfUnreadBits (void) const
 Returns the number of bits left in the stream that haven't been read.
BitSize_t GetReadOffset (void) const
 Returns the number of bits into the stream that we have read.
BitSize_t GetWriteOffset (void) const
void IgnoreBits (const BitSize_t numberOfBits)
 Ignore data we don't intend to read.
void IgnoreBytes (const unsigned int numberOfBytes)
 Ignore data we don't intend to read.
void PadWithZeroToByteLength (unsigned int bytes)
 Write zeros until the bitstream is filled up to bytes.
void PrintBits (char *out) const
 RAKNET_DEBUG_PRINTF the bits in the stream. Great for debugging.
void PrintBits (void) const
void PrintHex (char *out) const
void PrintHex (void) const
bool Read (BitStream *bitStream)
bool Read (BitStream *bitStream, BitSize_t numberOfBits)
 Read one bitstream to another.
template<>
bool Read (wchar_t *&varString)
bool Read (BitStream &bitStream, BitSize_t numberOfBits)
template<>
bool Read (unsigned char *&varString)
template<>
bool Read (bool &outTemplateVar)
 Read a bool from a bitstream.
template<>
bool Read (SystemAddress &outTemplateVar)
 Read a systemAddress from a bitstream.
template<>
bool Read (uint24_t &outTemplateVar)
template<>
bool Read (RakNetGUID &outTemplateVar)
template<>
bool Read (RakString &outTemplateVar)
template<>
bool Read (RakWString &outTemplateVar)
template<>
bool Read (char *&varString)
bool Read (BitStream &bitStream)
bool Read (char *output, const unsigned int numberOfBytes)
 Read an array or casted stream of byte.
bool Read (char *varString)
 Read strings, non reference.
bool Read (unsigned char *varString)
template<class templateType >
bool Read (templateType &outTemplateVar)
 Read any integral type from a bitstream.
bool ReadAlignedBytes (unsigned char *inOutByteArray, const unsigned int numberOfBytesToRead)
 Read bits, starting at the next aligned bits.
bool ReadAlignedBytesSafe (char *inOutByteArray, int &inputLength, const int maxBytesToRead)
 Reads what was written by WriteAlignedBytesSafe.
bool ReadAlignedBytesSafe (char *inOutByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead)
bool ReadAlignedBytesSafeAlloc (char **outByteArray, int &inputLength, const unsigned int maxBytesToRead)
 Same as ReadAlignedBytesSafe() but allocates the memory for you using new, rather than assuming it is safe to write to.
bool ReadAlignedBytesSafeAlloc (char **outByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead)
bool ReadAlignedVar16 (char *inOutByteArray)
bool ReadAlignedVar32 (char *inOutByteArray)
bool ReadAlignedVar8 (char *inOutByteArray)
bool ReadBit (void)
 Reads 1 bit and returns true if that bit is 1 and false if it is 0.
bool ReadBits (unsigned char *inOutByteArray, BitSize_t numberOfBitsToRead, const bool alignBitsToRight=true)
 Read numberOfBitsToRead bits to the output source.
template<class templateType , class rangeType >
bool ReadBitsFromIntegerRange (templateType &value, const rangeType minimum, const rangeType maximum, bool allowOutsideRange=false)
template<class templateType , class rangeType >
bool ReadBitsFromIntegerRange (templateType &value, const rangeType minimum, const rangeType maximum, const int requiredBits, bool allowOutsideRange=false)
template<class serializationType , class sourceType >
bool ReadCasted (sourceType &value)
template<>
bool ReadCompressed (SystemAddress &outTemplateVar)
template<>
bool ReadCompressed (uint24_t &outTemplateVar)
template<>
bool ReadCompressed (RakNetGUID &outTemplateVar)
template<>
bool ReadCompressed (bool &outTemplateVar)
template<>
bool ReadCompressed (float &outTemplateVar)
 For values between -1 and 1.
template<>
bool ReadCompressed (double &outTemplateVar)
 For values between -1 and 1.
template<>
bool ReadCompressed (RakWString &outTemplateVar)
template<>
bool ReadCompressed (char *&outTemplateVar)
template<>
bool ReadCompressed (wchar_t *&outTemplateVar)
template<>
bool ReadCompressed (unsigned char *&outTemplateVar)
template<class templateType >
bool ReadCompressed (templateType &outTemplateVar)
 Read any integral type from a bitstream.
template<>
bool ReadCompressed (RakString &outTemplateVar)
 For strings.
template<class templateType >
bool ReadCompressedDelta (templateType &outTemplateVar)
 Read any integral type from a bitstream.
template<>
bool ReadCompressedDelta (bool &outTemplateVar)
 Read a bool from a bitstream.
template<>
bool ReadDelta (bool &outTemplateVar)
 Read a bool from a bitstream.
template<class templateType >
bool ReadDelta (templateType &outTemplateVar)
 Read any integral type from a bitstream.
bool ReadFloat16 (float &outFloat, float floatMin, float floatMax)
 Read a float into 2 bytes, spanning the range between floatMin and floatMax.
template<class templateType >
bool ReadNormQuat (templateType &w, templateType &x, templateType &y, templateType &z)
 Read a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes.
template<class templateType >
bool ReadNormVector (templateType &x, templateType &y, templateType &z)
 Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes.
template<class templateType >
bool ReadOrthMatrix (templateType &m00, templateType &m01, templateType &m02, templateType &m10, templateType &m11, templateType &m12, templateType &m20, templateType &m21, templateType &m22)
 Read an orthogonal matrix from a quaternion, reading 3 components of the quaternion in 2 bytes each and extrapolatig the 4th.
template<class templateType >
bool ReadVector (templateType &x, templateType &y, templateType &z)
 Read 3 floats or doubles, using 10 bytes, where those float or doubles comprise a vector.
void Reset (void)
 Resets the bitstream for reuse.
void ResetReadPointer (void)
 Sets the read pointer back to the beginning of your data.
void ResetWritePointer (void)
 Sets the write pointer back to the beginning of your data.
template<class templateType >
bool Serialize (bool writeToBitstream, templateType &inOutTemplateVar)
 Bidirectional serialize/deserialize any integral type to/from a bitstream.
bool Serialize (bool writeToBitstream, char *inOutByteArray, const unsigned int numberOfBytes)
 Bidirectional serialize/deserialize an array or casted stream or raw data. This does NOT do endian swapping.
bool SerializeBits (bool writeToBitstream, unsigned char *inOutByteArray, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits=true)
 Bidirectional serialize/deserialize numberToSerialize bits to/from the input.
template<class templateType , class rangeType >
bool SerializeBitsFromIntegerRange (bool writeToBitstream, templateType &value, const rangeType minimum, const rangeType maximum, const int requiredBits, bool allowOutsideRange=false)
template<class templateType , class rangeType >
bool SerializeBitsFromIntegerRange (bool writeToBitstream, templateType &value, const rangeType minimum, const rangeType maximum, bool allowOutsideRange=false)
template<class serializationType , class sourceType >
bool SerializeCasted (bool writeToBitstream, sourceType &value)
template<class templateType >
bool SerializeCompressed (bool writeToBitstream, templateType &inOutTemplateVar)
 Bidirectional serialize/deserialize any integral type to/from a bitstream.
template<class templateType >
bool SerializeCompressedDelta (bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue)
 Bidirectional serialize/deserialize any integral type to/from a bitstream.
template<class templateType >
bool SerializeCompressedDelta (bool writeToBitstream, templateType &inOutTemplateVar)
 Save as SerializeCompressedDelta(templateType &currentValue, const templateType &lastValue) when we have an unknown second parameter.
template<class templateType >
bool SerializeDelta (bool writeToBitstream, templateType &inOutCurrentValue)
 Bidirectional version of SerializeDelta when you don't know what the last value is, or there is no last value.
template<class templateType >
bool SerializeDelta (bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue)
 Bidirectional serialize/deserialize any integral type to/from a bitstream.
bool SerializeFloat16 (bool writeToBitstream, float &inOutFloat, float floatMin, float floatMax)
 Serialize a float into 2 bytes, spanning the range between floatMin and floatMax.
template<class templateType >
bool SerializeNormQuat (bool writeToBitstream, templateType &w, templateType &x, templateType &y, templateType &z)
 Bidirectional serialize/deserialize a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.
template<class templateType >
bool SerializeNormVector (bool writeToBitstream, templateType &x, templateType &y, templateType &z)
 Bidirectional serialize/deserialize a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes.
template<class templateType >
bool SerializeOrthMatrix (bool writeToBitstream, templateType &m00, templateType &m01, templateType &m02, templateType &m10, templateType &m11, templateType &m12, templateType &m20, templateType &m21, templateType &m22)
 Bidirectional serialize/deserialize an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each.
template<class templateType >
bool SerializeVector (bool writeToBitstream, templateType &x, templateType &y, templateType &z)
 Bidirectional serialize/deserialize a vector, using 10 bytes instead of 12.
void SetData (unsigned char *inByteArray)
void SetNumberOfBitsAllocated (const BitSize_t lengthInBits)
 Use this if you pass a pointer copy to the constructor (_copyData==false) and want to overallocate to prevent reallocation.
void SetReadOffset (const BitSize_t newReadOffset)
 Sets the read bit index.
void SetWriteOffset (const BitSize_t offset)
 Move the write pointer to a position on the array.
void Write (BitStream &bitStream, BitSize_t numberOfBits)
void Write (const char *inputByteArray, const unsigned int numberOfBytes)
 Write an array or casted stream or raw data. This does NOT do endian swapping.
void Write (const wchar_t *const inStringVar)
void Write (BitStream *bitStream, BitSize_t numberOfBits)
 Write one bitstream to another.
void Write (unsigned char *const inTemplateVar)
template<>
void Write (const bool &inTemplateVar)
 Write a bool to a bitstream.
void Write (BitStream *bitStream)
void Write (const char *const inStringVar)
template<>
void Write (const wchar_t *const &inStringVar)
template<>
void Write (const unsigned char *const &inTemplateVar)
template<>
void Write (const RakWString &inTemplateVar)
template<class templateType >
void Write (const templateType &inTemplateVar)
 Write any integral type to a bitstream.
template<>
void Write (const uint24_t &inTemplateVar)
void Write (char *const inTemplateVar)
template<>
void Write (const char *const &inStringVar)
template<>
void Write (const RakString &inTemplateVar)
 Write a string to a bitstream.
template<>
void Write (const RakNetGUID &inTemplateVar)
template<>
void Write (char *const &inTemplateVar)
template<>
void Write (const SystemAddress &inTemplateVar)
 Write a systemAddress to a bitstream.
void Write (const unsigned char *const inTemplateVar)
void Write (BitStream &bitStream)
template<>
void Write (unsigned char *const &inTemplateVar)
void Write0 (void)
 Write a 0.
void Write1 (void)
 Write a 1.
void WriteAlignedBytes (const unsigned char *inByteArray, const unsigned int numberOfBytesToWrite)
 Align the bitstream to the byte boundary and then write the specified number of bits.
void WriteAlignedBytesSafe (const char *inByteArray, const unsigned int inputLength, const unsigned int maxBytesToWrite)
 Aligns the bitstream, writes inputLength, and writes input. Won't write beyond maxBytesToWrite.
void WriteAlignedVar16 (const char *inByteArray)
void WriteAlignedVar32 (const char *inByteArray)
void WriteAlignedVar8 (const char *inByteArray)
void WriteBits (const unsigned char *inByteArray, BitSize_t numberOfBitsToWrite, const bool rightAlignedBits=true)
 Write numberToWrite bits from the input source.
template<class templateType , class rangeType >
void WriteBitsFromIntegerRange (const templateType value, const rangeType minimum, const rangeType maximum, const int requiredBits, bool allowOutsideRange=false)
template<class templateType , class rangeType >
void WriteBitsFromIntegerRange (const templateType value, const rangeType minimum, const rangeType maximum, bool allowOutsideRange=false)
template<class serializationType , class sourceType >
void WriteCasted (const sourceType &value)
template<>
void WriteCompressed (const RakNetGUID &inTemplateVar)
template<>
void WriteCompressed (char *const &inTemplateVar)
template<>
void WriteCompressed (unsigned char *const &inTemplateVar)
template<>
void WriteCompressed (const SystemAddress &inTemplateVar)
template<>
void WriteCompressed (const char *const &inStringVar)
void WriteCompressed (const char *const inStringVar)
void WriteCompressed (const unsigned char *const inTemplateVar)
template<>
void WriteCompressed (const float &inTemplateVar)
 For values between -1 and 1.
void WriteCompressed (char *const inTemplateVar)
template<>
void WriteCompressed (const double &inTemplateVar)
 For values between -1 and 1.
template<>
void WriteCompressed (const uint24_t &var)
template<>
void WriteCompressed (const wchar_t *const &inStringVar)
template<>
void WriteCompressed (const RakWString &inTemplateVar)
void WriteCompressed (unsigned char *const inTemplateVar)
template<>
void WriteCompressed (const RakString &inTemplateVar)
 Compress the string.
template<>
void WriteCompressed (const unsigned char *const &inTemplateVar)
void WriteCompressed (const wchar_t *const inStringVar)
template<class templateType >
void WriteCompressed (const templateType &inTemplateVar)
 Write any integral type to a bitstream.
template<>
void WriteCompressed (const bool &inTemplateVar)
template<class templateType >
void WriteCompressedDelta (const templateType &currentValue)
 Save as WriteCompressedDelta(const templateType &currentValue, const templateType &lastValue) when we have an unknown second parameter.
template<class templateType >
void WriteCompressedDelta (const templateType &currentValue, const templateType &lastValue)
 Write any integral type to a bitstream.
template<>
void WriteCompressedDelta (const bool &currentValue)
 Save as WriteCompressedDelta(bool currentValue, const templateType &lastValue) when we have an unknown second bool.
template<>
void WriteCompressedDelta (const bool &currentValue, const bool &lastValue)
 Write a bool delta. Same thing as just calling Write.
template<>
void WriteDelta (const bool &currentValue, const bool &lastValue)
 Write a bool delta. Same thing as just calling Write.
template<class templateType >
void WriteDelta (const templateType &currentValue, const templateType &lastValue)
 Write any integral type to a bitstream.
template<class templateType >
void WriteDelta (const templateType &currentValue)
 WriteDelta when you don't know what the last value is, or there is no last value.
void WriteFloat16 (float x, float floatMin, float floatMax)
 Write a float into 2 bytes, spanning the range between floatMin and floatMax.
template<class templateType >
void WriteNormQuat (templateType w, templateType x, templateType y, templateType z)
 Write a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.
template<class templateType >
void WriteNormVector (templateType x, templateType y, templateType z)
 Write a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes.
template<class templateType >
void WriteOrthMatrix (templateType m00, templateType m01, templateType m02, templateType m10, templateType m11, templateType m12, templateType m20, templateType m21, templateType m22)
 Write an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each.
template<class templateType >
void WritePtr (templateType *inTemplateVar)
 Write the dereferenced pointer to any integral type to a bitstream.
template<class templateType >
void WriteVector (templateType x, templateType y, templateType z)
 Write a vector, using 10 bytes instead of 12.
 ~BitStream ()

Static Public Member Functions

static void DestroyInstance (BitStream *i)
static bool DoEndianSwap (void)
static BitStreamGetInstance (void)
static bool IsBigEndian (void)
static bool IsNetworkOrder (void)
static bool IsNetworkOrderInternal (void)
static int NumberOfLeadingZeroes (uint32_t x)
static int NumberOfLeadingZeroes (uint16_t x)
static int NumberOfLeadingZeroes (int16_t x)
static int NumberOfLeadingZeroes (int32_t x)
static int NumberOfLeadingZeroes (uint8_t x)
static int NumberOfLeadingZeroes (int8_t x)
static int NumberOfLeadingZeroes (int64_t x)
static int NumberOfLeadingZeroes (uint64_t x)
static void ReverseBytes (unsigned char *inByteArray, unsigned char *inOutByteArray, const unsigned int length)
static void ReverseBytesInPlace (unsigned char *inOutData, const unsigned int length)

Private Member Functions

 BitStream (const BitStream &invalid)
bool ReadCompressed (unsigned char *inOutByteArray, const unsigned int size, const bool unsignedData)
 Assume the input source points to a compressed native type. Decompress and read it.
void WriteCompressed (const unsigned char *inByteArray, const unsigned int size, const bool unsignedData)
 Assume the input source points to a native type, compress and write it.

Private Attributes

bool copyData
 true if the internal buffer is copy of the data passed to the constructor
unsigned char * data
BitSize_t numberOfBitsAllocated
BitSize_t numberOfBitsUsed
BitSize_t readOffset
unsigned char stackData [256]
 BitStreams that use less than BITSTREAM_STACK_ALLOCATION_SIZE use the stack, rather than the heap to store data. It switches over if BITSTREAM_STACK_ALLOCATION_SIZE is exceeded.

Detailed Description

This class allows you to write and read native types as a string of bits. BitStream is used extensively throughout RakNet and is designed to be used by users as well.

See also:
BitStreamSample.txt

Definition at line 40 of file BitStream.h.


Constructor & Destructor Documentation

RakNet::BitStream::BitStream ( )
RakNet::BitStream::BitStream ( const unsigned int  initialBytesToAllocate)

There is no benefit to calling this, unless you know exactly how many bytes you need and it is greater than BITSTREAM_STACK_ALLOCATION_SIZE. In that case all it does is save you one or more realloc calls.

Parameters:
[in]initialBytesToAllocatethe number of bytes to pre-allocate.
RakNet::BitStream::BitStream ( unsigned char *  _data,
const unsigned int  lengthInBytes,
bool  _copyData 
)

Set _copyData to true if you want to make an internal copy of the data you are passing. Set it to false to just save a pointer to the data. You shouldn't call Write functions with _copyData as false, as this will write to unallocated memory 99% of the time you will use this function to cast Packet::data to a bitstream for reading, in which case you should write something as follows:

 RakNet::BitStream bs(packet->data, packet->length, false);
Parameters:
[in]_dataAn array of bytes.
[in]lengthInBytesSize of the _data.
[in]_copyDatatrue or false to make a copy of _data or not.
RakNet::BitStream::~BitStream ( )
RakNet::BitStream::BitStream ( const BitStream invalid) [inline, private]

Definition at line 896 of file BitStream.h.

References RakAssert.

                                             {
            (void) invalid;
            RakAssert(0);
        }

Member Function Documentation

void RakNet::BitStream::AddBitsAndReallocate ( const BitSize_t  numberOfBitsToWrite)

Referenced by Write().

void RakNet::BitStream::AlignReadToByteBoundary ( void  ) [inline]

This can be used to 'waste' bits to byte align for efficiency reasons It can also be used to force coalesced bitstreams to start on byte boundaries so so WriteAlignedBits and ReadAlignedBits both calculate the same offset when aligning.

Definition at line 587 of file BitStream.h.

Referenced by DataStructures::RangeList< range_type >::Deserialize(), and Read().

{readOffset += 8 - ( (( readOffset - 1 ) & 7 ) + 1 );}
void RakNet::BitStream::AlignWriteToByteBoundary ( void  ) [inline]

This can be used to 'waste' bits to byte align for efficiency reasons It can also be used to force coalesced bitstreams to start on byte boundaries so so WriteAlignedBits and ReadAlignedBits both calculate the same offset when aligning.

Definition at line 580 of file BitStream.h.

Referenced by DataStructures::RangeList< range_type >::Serialize(), and Write().

{numberOfBitsUsed += 8 - ( (( numberOfBitsUsed - 1 ) & 7) + 1 );}
void RakNet::BitStream::AssertCopyData ( void  )
void RakNet::BitStream::AssertStreamEmpty ( void  )
BitSize_t RakNet::BitStream::CopyData ( unsigned char **  _data) const
Parameters:
[out]_dataThe allocated copy of GetData()
Returns:
The length in bits of the stream.
static void RakNet::BitStream::DestroyInstance ( BitStream i) [static]
static bool RakNet::BitStream::DoEndianSwap ( void  ) [inline, static]

---- Member function template specialization declarations ----

Definition at line 877 of file BitStream.h.

Referenced by Read(), ReadCompressed(), Write(), WriteCompressed(), and WritePtr().

                                              {
#ifndef __BITSTREAM_NATIVE_END
            return IsNetworkOrder()==false;
#else
            return false;
#endif
        }
void RakNet::BitStream::EndianSwapBytes ( int  byteOffset,
int  length 
)
unsigned char* RakNet::BitStream::GetData ( void  ) const [inline]

Gets the data that BitStream is writing to / reading from. Partial bytes are left aligned.

Returns:
A pointer to the internal state

Definition at line 522 of file BitStream.h.

Referenced by RakNet::VariableListDeltaTracker::WriteVar().

{return data;}
static BitStream* RakNet::BitStream::GetInstance ( void  ) [static]
BitSize_t RakNet::BitStream::GetNumberOfBitsAllocated ( void  ) const
BitSize_t RakNet::BitStream::GetNumberOfBitsUsed ( void  ) const [inline]

Definition at line 494 of file BitStream.h.

Referenced by DataStructures::RangeList< range_type >::Serialize().

{return GetWriteOffset();}
BitSize_t RakNet::BitStream::GetNumberOfBytesUsed ( void  ) const [inline]

Definition at line 498 of file BitStream.h.

References BITS_TO_BYTES.

Referenced by RakNet::VariableListDeltaTracker::WriteVar().

BitSize_t RakNet::BitStream::GetNumberOfUnreadBits ( void  ) const [inline]

Definition at line 507 of file BitStream.h.

BitSize_t RakNet::BitStream::GetReadOffset ( void  ) const [inline]

Definition at line 501 of file BitStream.h.

{return readOffset;}
BitSize_t RakNet::BitStream::GetWriteOffset ( void  ) const [inline]

Definition at line 495 of file BitStream.h.

Referenced by DataStructures::RangeList< range_type >::Serialize().

{return numberOfBitsUsed;}
void RakNet::BitStream::IgnoreBits ( const BitSize_t  numberOfBits)
Parameters:
[in]numberOfBitsThe number of bits to ignore
void RakNet::BitStream::IgnoreBytes ( const unsigned int  numberOfBytes)
Parameters:
[in]numberOfBitsThe number of bytes to ignore
static bool RakNet::BitStream::IsBigEndian ( void  ) [inline, static]

Definition at line 884 of file BitStream.h.

Referenced by Read(), ReadBitsFromIntegerRange(), Write(), and WriteBitsFromIntegerRange().

        {
            return IsNetworkOrder();
        }
static bool RakNet::BitStream::IsNetworkOrder ( void  ) [inline, static]

Definition at line 888 of file BitStream.h.

{static const bool r = IsNetworkOrderInternal(); return r;}
static bool RakNet::BitStream::IsNetworkOrderInternal ( void  ) [static]
static int RakNet::BitStream::NumberOfLeadingZeroes ( uint8_t  x) [static]

Get the number of leading zeros for a number

Parameters:
[in]xNumber to test

Referenced by ReadBitsFromIntegerRange(), SerializeBitsFromIntegerRange(), and WriteBitsFromIntegerRange().

static int RakNet::BitStream::NumberOfLeadingZeroes ( uint16_t  x) [static]
static int RakNet::BitStream::NumberOfLeadingZeroes ( uint32_t  x) [static]
static int RakNet::BitStream::NumberOfLeadingZeroes ( uint64_t  x) [static]
static int RakNet::BitStream::NumberOfLeadingZeroes ( int8_t  x) [static]
static int RakNet::BitStream::NumberOfLeadingZeroes ( int16_t  x) [static]
static int RakNet::BitStream::NumberOfLeadingZeroes ( int32_t  x) [static]
static int RakNet::BitStream::NumberOfLeadingZeroes ( int64_t  x) [static]
void RakNet::BitStream::PadWithZeroToByteLength ( unsigned int  bytes)
void RakNet::BitStream::PrintBits ( char *  out) const
void RakNet::BitStream::PrintBits ( void  ) const
void RakNet::BitStream::PrintHex ( char *  out) const
void RakNet::BitStream::PrintHex ( void  ) const
bool RakNet::BitStream::Read ( BitStream bitStream,
BitSize_t  numberOfBits 
)
bool RakNet::BitStream::Read ( BitStream bitStream)
bool RakNet::BitStream::Read ( BitStream bitStream)
template<>
bool RakNet::BitStream::Read ( bool outTemplateVar) [inline]
Parameters:
[in]outTemplateVarThe value to read

Definition at line 1461 of file BitStream.h.

References data, numberOfBitsUsed, and readOffset.

    {
        if ( readOffset + 1 > numberOfBitsUsed )
            return false;

        if ( data[ readOffset >> 3 ] & ( 0x80 >> ( readOffset & 7 ) ) )   // Is it faster to just write it out here?
            outTemplateVar = true;
        else
            outTemplateVar = false;

        // Has to be on a different line for Mac
        readOffset++;

        return true;
    }
template<>
bool RakNet::BitStream::Read ( SystemAddress outTemplateVar) [inline]
Parameters:
[in]outTemplateVarThe value to read

Definition at line 1480 of file BitStream.h.

References RakNet::SystemAddress::address, RakNet::SystemAddress::debugPort, Read(), and ReadBits().

    {
        unsigned char ipVersion;
        Read(ipVersion);
        if (ipVersion==4)
        {
            outTemplateVar.address.addr4.sin_family=AF_INET;
            // Read(var.binaryAddress);
            // Don't endian swap the address or port
            uint32_t binaryAddress;
            ReadBits( ( unsigned char* ) & binaryAddress, sizeof(binaryAddress) * 8, true );
            // Unhide the IP address, done to prevent routers from changing it
            outTemplateVar.address.addr4.sin_addr.s_addr=~binaryAddress;
            bool b = ReadBits(( unsigned char* ) & outTemplateVar.address.addr4.sin_port, sizeof(outTemplateVar.address.addr4.sin_port) * 8, true);
            outTemplateVar.debugPort=ntohs(outTemplateVar.address.addr4.sin_port);
            return b;
        }
        else
        {
#if RAKNET_SUPPORT_IPV6==1
            bool b = ReadBits((unsigned char*) &outTemplateVar.address.addr6, sizeof(outTemplateVar.address.addr6)*8, true);
            outTemplateVar.debugPort=ntohs(outTemplateVar.address.addr6.sin6_port);
            return b;
#else
            return false;
#endif
        }   
    }
template<>
bool RakNet::BitStream::Read ( uint24_t outTemplateVar) [inline]

Definition at line 1510 of file BitStream.h.

References AlignReadToByteBoundary(), data, IsBigEndian(), numberOfBitsUsed, readOffset, and RakNet::uint24_t::val.

    {
        AlignReadToByteBoundary();
        if ( readOffset + 3*8 > numberOfBitsUsed )
            return false;

        if (IsBigEndian()==false)
        {
            ((char *)&outTemplateVar.val)[0]=data[ (readOffset >> 3) + 0];
            ((char *)&outTemplateVar.val)[1]=data[ (readOffset >> 3) + 1];
            ((char *)&outTemplateVar.val)[2]=data[ (readOffset >> 3) + 2];
            ((char *)&outTemplateVar.val)[3]=0;
        }
        else
        {

            ((char *)&outTemplateVar.val)[3]=data[ (readOffset >> 3) + 0];
            ((char *)&outTemplateVar.val)[2]=data[ (readOffset >> 3) + 1];
            ((char *)&outTemplateVar.val)[1]=data[ (readOffset >> 3) + 2];
            ((char *)&outTemplateVar.val)[0]=0;
        }

        readOffset+=3*8;
        return true;
    }
template<>
bool RakNet::BitStream::Read ( RakNetGUID outTemplateVar) [inline]

Definition at line 1537 of file BitStream.h.

References RakNet::RakNetGUID::g, and Read().

    {
        return Read(outTemplateVar.g);
    }
template<>
bool RakNet::BitStream::Read ( RakString outTemplateVar) [inline]

Definition at line 1544 of file BitStream.h.

References RakNet::RakString::Deserialize().

    {
        return outTemplateVar.Deserialize(this);
    }
template<>
bool RakNet::BitStream::Read ( RakWString &  outTemplateVar) [inline]

Definition at line 1549 of file BitStream.h.

    {
        return outTemplateVar.Deserialize(this);
    }
template<>
bool RakNet::BitStream::Read ( char *&  varString) [inline]

Definition at line 1554 of file BitStream.h.

References RakNet::RakString::Deserialize().

    {
        return RakString::Deserialize(varString,this);
    }
template<>
bool RakNet::BitStream::Read ( wchar_t *&  varString) [inline]

Definition at line 1559 of file BitStream.h.

    {
        return RakWString::Deserialize(varString,this);
    }
template<>
bool RakNet::BitStream::Read ( unsigned char *&  varString) [inline]

Definition at line 1564 of file BitStream.h.

References RakNet::RakString::Deserialize().

    {
        return RakString::Deserialize((char*) varString,this);
    }
bool RakNet::BitStream::Read ( char *  varString)
bool RakNet::BitStream::Read ( char *  output,
const unsigned int  numberOfBytes 
)

The array is raw data. There is no automatic endian conversion with this function

Parameters:
[in]outputThe result byte array. It should be larger than numberOfBytes.
[in]numberOfBytesThe number of byte to read
Returns:
true on success false if there is some missing bytes.
bool RakNet::BitStream::Read ( unsigned char *  varString)
bool RakNet::BitStream::Read ( BitStream bitStream,
BitSize_t  numberOfBits 
)
Parameters:
[in]numberOfBitsbits to read
bitStreamthe bitstream to read into from
Returns:
true on success, false on failure.
template<class templateType >
bool RakNet::BitStream::Read ( templateType &  outTemplateVar) [inline]

Read any integral type from a bitstream. Define __BITSTREAM_NATIVE_END if you need endian swapping.

Define __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:
[in]outTemplateVarThe value to read
Returns:
true on success, false on failure.
Parameters:
[in]outTemplateVarThe value to read

Definition at line 1429 of file BitStream.h.

References DoEndianSwap(), ReadBits(), and ReverseBytes().

Referenced by SraNetwork::SraServerInfo::Deserialize(), SraNetwork::SraPacket::Deserialize(), SraNetwork::SendMessageToPlayerPacket::Deserialize(), SraNetwork::SendMessageToChannelPacket::Deserialize(), SraNetwork::ReplyPacket::Deserialize(), SraNetwork::LoginReplyPacket::Deserialize(), SraNetwork::LoginPacket::Deserialize(), SraNetwork::LeaveChannelPacket::Deserialize(), SraNetwork::JoinChannelPacket::Deserialize(), SraNetwork::GetServerListPacket::Deserialize(), SraNetwork::GetClientCharactersPacket::Deserialize(), DataStructures::RangeList< range_type >::Deserialize(), SraNetwork::ConnectToServerPacket::Deserialize(), SraNetwork::ChatMessage::Deserialize(), SraNetwork::BasicCharacterInfo::Deserialize(), operator>>(), RakNet::operator>>(), Read(), ReadBitsFromIntegerRange(), ReadCasted(), ReadCompressed(), ReadCompressedDelta(), ReadDelta(), ReadNormQuat(), RakNet::VariableListDeltaTracker::ReadVarFromBitstream(), ReadVector(), and Serialize().

    {
#ifdef _MSC_VER
#pragma warning(disable:4127)   // conditional expression is constant
#endif
        if (sizeof(outTemplateVar)==1)
            return ReadBits( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true );
        else
        {
#ifndef __BITSTREAM_NATIVE_END
#ifdef _MSC_VER
#pragma warning(disable:4244)   // '=' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
#endif
            if (DoEndianSwap())
            {
                unsigned char output[sizeof(templateType)];
                if (ReadBits( ( unsigned char* ) output, sizeof(templateType) * 8, true ))
                {
                    ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType));
                    return true;
                }
                return false;
            }
            else
#endif
                return ReadBits( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true );
        }
    }
bool RakNet::BitStream::ReadAlignedBytes ( unsigned char *  inOutByteArray,
const unsigned int  numberOfBytesToRead 
)

Note that the modulus 8 starting offset of the sequence must be the same as was used with WriteBits. This will be a problem with packet coalescence unless you byte align the coalesced packets.

Parameters:
[in]inOutByteArrayThe byte array larger than numberOfBytesToRead
[in]numberOfBytesToReadThe number of byte to read from the internal state
Returns:
true if there is enough byte.
bool RakNet::BitStream::ReadAlignedBytesSafe ( char *  inOutByteArray,
unsigned int &  inputLength,
const unsigned int  maxBytesToRead 
)
bool RakNet::BitStream::ReadAlignedBytesSafe ( char *  inOutByteArray,
int &  inputLength,
const int  maxBytesToRead 
)
Parameters:
[in]inOutByteArrayThe data
[in]maxBytesToReadMaximum number of bytes to read
Returns:
true on success, false on failure.
bool RakNet::BitStream::ReadAlignedBytesSafeAlloc ( char **  outByteArray,
int &  inputLength,
const unsigned int  maxBytesToRead 
)
Parameters:
[in]outByteArrayoutByteArray will be deleted if it is not a pointer to 0
Returns:
true on success, false on failure.
bool RakNet::BitStream::ReadAlignedBytesSafeAlloc ( char **  outByteArray,
unsigned int &  inputLength,
const unsigned int  maxBytesToRead 
)
bool RakNet::BitStream::ReadAlignedVar16 ( char *  inOutByteArray)
bool RakNet::BitStream::ReadAlignedVar32 ( char *  inOutByteArray)
bool RakNet::BitStream::ReadAlignedVar8 ( char *  inOutByteArray)
bool RakNet::BitStream::ReadBit ( void  )
bool RakNet::BitStream::ReadBits ( unsigned char *  inOutByteArray,
BitSize_t  numberOfBitsToRead,
const bool  alignBitsToRight = true 
)

alignBitsToRight should be set to true to convert internal bitstream data to userdata. It should be false if you used WriteBits with rightAlignedBits false

Parameters:
[in]inOutByteArrayThe resulting bits array
[in]numberOfBitsToReadThe number of bits to read
[in]alignBitsToRightif true bits will be right aligned.
Returns:
true if there is enough bits to read

Referenced by Read(), ReadBitsFromIntegerRange(), and SerializeBits().

template<class templateType , class rangeType >
bool RakNet::BitStream::ReadBitsFromIntegerRange ( templateType &  value,
const rangeType  minimum,
const rangeType  maximum,
bool  allowOutsideRange = false 
)

Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range Then read only those bits

Note:
A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that minimum and are fixed values for a given line of code for the life of the program
Parameters:
[in]valueInteger value to read, which should be between minimum and maximum
[in]minimumMinimum value of value
[in]maximumMaximum value of value
[in]allowOutsideRangeIf true, all sends will take an extra bit, however value can deviate from outside minimum and maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Write().

Definition at line 1861 of file BitStream.h.

References BYTES_TO_BITS, and NumberOfLeadingZeroes().

Referenced by SerializeBitsFromIntegerRange().

    {
        static int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum));
        return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
    }
template<class templateType , class rangeType >
bool RakNet::BitStream::ReadBitsFromIntegerRange ( templateType &  value,
const rangeType  minimum,
const rangeType  maximum,
const int  requiredBits,
bool  allowOutsideRange = false 
)
Parameters:
[in]requiredBitsPrimarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum

Definition at line 1867 of file BitStream.h.

References IsBigEndian(), RakAssert, Read(), ReadBits(), and ReverseBytesInPlace().

    {
        RakAssert(maximum>=minimum);
        if (allowOutsideRange)
        {
            bool isOutsideRange;
            Read(isOutsideRange);
            if (isOutsideRange)
                return Read(value);
        }
        unsigned char output[sizeof(templateType)];
        memset(output,0,sizeof(output));
        bool success = ReadBits(output,requiredBits);
        if (success)
        {
            if (IsBigEndian()==true)
                ReverseBytesInPlace(output,sizeof(output));
            memcpy(&value,output,sizeof(output));

            value+=minimum;
        }

        return success;
    }
template<class serializationType , class sourceType >
bool RakNet::BitStream::ReadCasted ( sourceType &  value)

Read one type serialized to another (smaller) type, to save bandwidth serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t Example: int num; ReadCasted<uint8_t>(num); would read 1 bytefrom the stream, and put the value in an integer

Parameters:
[in]valueThe value to write

Definition at line 1852 of file BitStream.h.

References Read().

    {
        serializationType val;
        bool success = Read(val);
        value=(sourceType) val;
        return success;
    }
bool RakNet::BitStream::ReadCompressed ( unsigned char *  inOutByteArray,
const unsigned int  size,
const bool  unsignedData 
) [private]
template<>
bool RakNet::BitStream::ReadCompressed ( float outTemplateVar) [inline]

Definition at line 1652 of file BitStream.h.

References Read().

    {
        unsigned short compressedFloat;
        if (Read(compressedFloat))
        {
            outTemplateVar = ((float)compressedFloat / 32767.5f - 1.0f);
            return true;
        }
        return false;
    }
template<>
bool RakNet::BitStream::ReadCompressed ( RakWString &  outTemplateVar) [inline]

Definition at line 1683 of file BitStream.h.

    {
        return outTemplateVar.Deserialize(this);
    }
template<>
bool RakNet::BitStream::ReadCompressed ( RakNetGUID outTemplateVar) [inline]

Definition at line 1639 of file BitStream.h.

References Read().

    {
        return Read(outTemplateVar);
    }
template<>
bool RakNet::BitStream::ReadCompressed ( SystemAddress outTemplateVar) [inline]

Definition at line 1627 of file BitStream.h.

References Read().

    {
        return Read(outTemplateVar);
    }
template<>
bool RakNet::BitStream::ReadCompressed ( uint24_t outTemplateVar) [inline]

Definition at line 1633 of file BitStream.h.

References Read().

    {
        return Read(outTemplateVar);
    }
template<>
bool RakNet::BitStream::ReadCompressed ( bool outTemplateVar) [inline]

Definition at line 1645 of file BitStream.h.

References Read().

    {
        return Read(outTemplateVar);
    }
template<>
bool RakNet::BitStream::ReadCompressed ( double &  outTemplateVar) [inline]

Definition at line 1665 of file BitStream.h.

References Read().

    {
        uint32_t compressedFloat;
        if (Read(compressedFloat))
        {
            outTemplateVar = ((double)compressedFloat / 2147483648.0 - 1.0);
            return true;
        }
        return false;
    }
template<>
bool RakNet::BitStream::ReadCompressed ( wchar_t *&  outTemplateVar) [inline]

Definition at line 1693 of file BitStream.h.

    {
        return RakWString::Deserialize(outTemplateVar,this);
    }
template<>
bool RakNet::BitStream::ReadCompressed ( unsigned char *&  outTemplateVar) [inline]

Definition at line 1698 of file BitStream.h.

References RakNet::RakString::DeserializeCompressed().

    {
        return RakString::DeserializeCompressed((char*) outTemplateVar,this,false);
    }
template<>
bool RakNet::BitStream::ReadCompressed ( char *&  outTemplateVar) [inline]

Definition at line 1688 of file BitStream.h.

References RakNet::RakString::DeserializeCompressed().

    {
        return RakString::DeserializeCompressed(outTemplateVar,this,false);
    }
template<class templateType >
bool RakNet::BitStream::ReadCompressed ( templateType &  outTemplateVar) [inline]

Undefine __BITSTREAM_NATIVE_END if you need endian swapping. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:
[in]outTemplateVarThe value to read
Returns:
true on success, false on failure.

Undefine __BITSTREAM_NATIVE_END if you need endian swapping. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:
[in]outTemplateVarThe value to read

Definition at line 1600 of file BitStream.h.

References DoEndianSwap(), and ReverseBytes().

Referenced by ReadCompressedDelta(), ReadVector(), and SerializeCompressed().

    {
#ifdef _MSC_VER
#pragma warning(disable:4127)   // conditional expression is constant
#endif
        if (sizeof(outTemplateVar)==1)
            return ReadCompressed( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true );
        else
        {
#ifndef __BITSTREAM_NATIVE_END
            if (DoEndianSwap())
            {
                unsigned char output[sizeof(templateType)];
                if (ReadCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true ))
                {
                    ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType));
                    return true;
                }
                return false;
            }
            else
#endif
                return ReadCompressed( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true );
        }
    }
template<>
bool RakNet::BitStream::ReadCompressed ( RakString outTemplateVar) [inline]

Definition at line 1678 of file BitStream.h.

References RakNet::RakString::DeserializeCompressed().

    {
        return outTemplateVar.DeserializeCompressed(this,false);
    }
template<>
bool RakNet::BitStream::ReadCompressedDelta ( bool outTemplateVar) [inline]
Parameters:
[in]outTemplateVarThe value to read

Definition at line 1726 of file BitStream.h.

References Read().

    {
        return Read(outTemplateVar);
    }
template<class templateType >
bool RakNet::BitStream::ReadCompressedDelta ( templateType &  outTemplateVar) [inline]

If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. the current value will be updated. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte ReadCompressedDelta is only valid from a previous call to WriteDelta

Parameters:
[in]outTemplateVarThe value to read
Returns:
true on success, false on failure.

If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. the current value will be updated. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte ReadCompressedDelta is only valid from a previous call to WriteDelta

Parameters:
[in]outTemplateVarThe value to read

Definition at line 1713 of file BitStream.h.

References Read(), and ReadCompressed().

Referenced by SerializeCompressedDelta().

    {
        bool dataWritten;
        bool success;
        success=Read(dataWritten);
        if (dataWritten)
            success=ReadCompressed(outTemplateVar);
        return success;
    }
template<class templateType >
bool RakNet::BitStream::ReadDelta ( templateType &  outTemplateVar) [inline]

If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. ReadDelta is only valid from a previous call to WriteDelta

Parameters:
[in]outTemplateVarThe value to read
Returns:
true on success, false on failure.

If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. ReadDelta is only valid from a previous call to WriteDelta

Parameters:
[in]outTemplateVarThe value to read

Definition at line 1575 of file BitStream.h.

References Read().

Referenced by SerializeDelta().

    {
        bool dataWritten;
        bool success;
        success=Read(dataWritten);
        if (dataWritten)
            success=Read(outTemplateVar);
        return success;
    }
template<>
bool RakNet::BitStream::ReadDelta ( bool outTemplateVar) [inline]
Parameters:
[in]outTemplateVarThe value to read

Definition at line 1588 of file BitStream.h.

References Read().

    {
        return Read(outTemplateVar);
    }
bool RakNet::BitStream::ReadFloat16 ( float outFloat,
float  floatMin,
float  floatMax 
)
Parameters:
[in]outFloatThe float to read
[in]floatMinPredetermined minimum value of f
[in]floatMaxPredetermined maximum value of f

Referenced by ReadNormVector().

template<class templateType >
bool RakNet::BitStream::ReadNormQuat ( templateType &  w,
templateType &  x,
templateType &  y,
templateType &  z 
)
Parameters:
[in]ww
[in]xx
[in]yy
[in]zz
Returns:
true on success, false on failure.

Definition at line 1943 of file BitStream.h.

References Read().

Referenced by ReadOrthMatrix(), and SerializeNormQuat().

    {
        bool cwNeg=false, cxNeg=false, cyNeg=false, czNeg=false;
        unsigned short cx,cy,cz;
        Read(cwNeg);
        Read(cxNeg);
        Read(cyNeg);
        Read(czNeg);
        Read(cx);
        Read(cy);
        if (!Read(cz))
            return false;

        // Calculate w from x,y,z
        x=(templateType)(cx/65535.0);
        y=(templateType)(cy/65535.0);
        z=(templateType)(cz/65535.0);
        if (cxNeg) x=-x;
        if (cyNeg) y=-y;
        if (czNeg) z=-z;
        float difference = 1.0f - x*x - y*y - z*z;
        if (difference < 0.0f)
            difference=0.0f;
        w = (templateType)(sqrt(difference));
        if (cwNeg)
            w=-w;

        return true;
    }
template<class templateType >
bool RakNet::BitStream::ReadNormVector ( templateType &  x,
templateType &  y,
templateType &  z 
)

Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.

Parameters:
[in]xx
[in]yy
[in]zz
Returns:
true on success, false on failure.

Definition at line 1893 of file BitStream.h.

References ReadFloat16().

Referenced by SerializeNormVector().

    {
        float xIn,yIn,zIn;
        ReadFloat16(xIn,-1.0f,1.0f);
        ReadFloat16(yIn,-1.0f,1.0f);
        ReadFloat16(zIn,-1.0f,1.0f);
        x=xIn;
        y=yIn;
        z=zIn;
        return true;
    }
template<class templateType >
bool RakNet::BitStream::ReadOrthMatrix ( templateType &  m00,
templateType &  m01,
templateType &  m02,
templateType &  m10,
templateType &  m11,
templateType &  m12,
templateType &  m20,
templateType &  m21,
templateType &  m22 
)

Use 6 bytes instead of 36 Lossy, although the result is renormalized

Returns:
true on success, false on failure.

Definition at line 1974 of file BitStream.h.

References ReadNormQuat().

Referenced by SerializeOrthMatrix().

    {
        float qw,qx,qy,qz;
        if (!ReadNormQuat(qw,qx,qy,qz))
            return false;

        // Quat to orthogonal rotation matrix
        // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
        double sqw = (double)qw*(double)qw;
        double sqx = (double)qx*(double)qx;
        double sqy = (double)qy*(double)qy;
        double sqz = (double)qz*(double)qz;
        m00 =  (templateType)(sqx - sqy - sqz + sqw); // since sqw + sqx + sqy + sqz =1
        m11 = (templateType)(-sqx + sqy - sqz + sqw);
        m22 = (templateType)(-sqx - sqy + sqz + sqw);

        double tmp1 = (double)qx*(double)qy;
        double tmp2 = (double)qz*(double)qw;
        m10 = (templateType)(2.0 * (tmp1 + tmp2));
        m01 = (templateType)(2.0 * (tmp1 - tmp2));

        tmp1 = (double)qx*(double)qz;
        tmp2 = (double)qy*(double)qw;
        m20 =(templateType)(2.0 * (tmp1 - tmp2));
        m02 = (templateType)(2.0 * (tmp1 + tmp2));
        tmp1 = (double)qy*(double)qz;
        tmp2 = (double)qx*(double)qw;
        m21 = (templateType)(2.0 * (tmp1 + tmp2));
        m12 = (templateType)(2.0 * (tmp1 - tmp2));

        return true;
    }
template<class templateType >
bool RakNet::BitStream::ReadVector ( templateType &  x,
templateType &  y,
templateType &  z 
)

Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.

Parameters:
[in]xx
[in]yy
[in]zz
Returns:
true on success, false on failure.

Definition at line 1906 of file BitStream.h.

References Read(), and ReadCompressed().

Referenced by SerializeVector().

    {
        float magnitude;
        //unsigned short sx,sy,sz;
        if (!Read(magnitude))
            return false;
        if (magnitude>0.00001f)
        {
            //  Read(sx);
            //  Read(sy);
            //  if (!Read(sz))
            //      return false;
            //  x=((float)sx / 32767.5f - 1.0f) * magnitude;
            //  y=((float)sy / 32767.5f - 1.0f) * magnitude;
            //  z=((float)sz / 32767.5f - 1.0f) * magnitude;
            float cx,cy,cz;
            ReadCompressed(cx);
            ReadCompressed(cy);
            if (!ReadCompressed(cz))
                return false;
            x=cx;
            y=cy;
            z=cz;
            x*=magnitude;
            y*=magnitude;
            z*=magnitude;
        }
        else
        {
            x=0.0;
            y=0.0;
            z=0.0;
        }
        return true;
    }
void RakNet::BitStream::Reset ( void  )
void RakNet::BitStream::ResetReadPointer ( void  )
void RakNet::BitStream::ResetWritePointer ( void  )
static void RakNet::BitStream::ReverseBytes ( unsigned char *  inByteArray,
unsigned char *  inOutByteArray,
const unsigned int  length 
) [static]
static void RakNet::BitStream::ReverseBytesInPlace ( unsigned char *  inOutData,
const unsigned int  length 
) [static]
template<class templateType >
bool RakNet::BitStream::Serialize ( bool  writeToBitstream,
templateType &  inOutTemplateVar 
) [inline]

Undefine __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutTemplateVarThe value to write
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 924 of file BitStream.h.

References Read(), and Write().

Referenced by Write(), and WriteCompressed().

        {
            if (writeToBitstream)
                Write(inOutTemplateVar);
            else
                return Read(inOutTemplateVar);
            return true;
        }
bool RakNet::BitStream::Serialize ( bool  writeToBitstream,
char *  inOutByteArray,
const unsigned int  numberOfBytes 
) [inline]
Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutByteArraya byte buffer
[in]numberOfBytesthe size of input in bytes
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 983 of file BitStream.h.

References Read(), and Write().

        {
            if (writeToBitstream)
                Write(inOutByteArray, numberOfBytes);
            else
                return Read(inOutByteArray, numberOfBytes);
            return true;
        }
bool RakNet::BitStream::SerializeBits ( bool  writeToBitstream,
unsigned char *  inOutByteArray,
const BitSize_t  numberOfBitsToSerialize,
const bool  rightAlignedBits = true 
) [inline]

Right aligned data means in the case of a partial byte, the bits are aligned from the right (bit 0) rather than the left (as in the normal internal representation) You would set this to true when writing user data, and false when copying bitstream data, such as writing one bitstream to another

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutByteArrayThe data
[in]numberOfBitsToSerializeThe number of bits to write
[in]rightAlignedBitsif true data will be right aligned
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 1058 of file BitStream.h.

References ReadBits(), and WriteBits().

        {
            if (writeToBitstream)
                WriteBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits);
            else
                return ReadBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits);
            return true;
        }
template<class templateType , class rangeType >
bool RakNet::BitStream::SerializeBitsFromIntegerRange ( bool  writeToBitstream,
templateType &  value,
const rangeType  minimum,
const rangeType  maximum,
bool  allowOutsideRange = false 
)

Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range Then serialize only those bits

Note:
A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that minimum and are fixed values for a given line of code for the life of the program
Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]valueInteger value to write, which should be between minimum and maximum
[in]minimumMinimum value of value
[in]maximumMaximum value of value
[in]allowOutsideRangeIf true, all sends will take an extra bit, however value can deviate from outside minimum and maximum. If false, will assert if the value deviates

Definition at line 1001 of file BitStream.h.

References BYTES_TO_BITS, and NumberOfLeadingZeroes().

        {
            static int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum));
            return SerializeBitsFromIntegerRange(writeToBitstream,value,minimum,maximum,requiredBits,allowOutsideRange);
        }
template<class templateType , class rangeType >
bool RakNet::BitStream::SerializeBitsFromIntegerRange ( bool  writeToBitstream,
templateType &  value,
const rangeType  minimum,
const rangeType  maximum,
const int  requiredBits,
bool  allowOutsideRange = false 
)
Parameters:
[in]requiredBitsPrimarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum

Definition at line 1007 of file BitStream.h.

References ReadBitsFromIntegerRange(), and WriteBitsFromIntegerRange().

        {
            if (writeToBitstream) WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
            else return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
            return true;
        }
template<class serializationType , class sourceType >
bool RakNet::BitStream::SerializeCasted ( bool  writeToBitstream,
sourceType &  value 
)

Serialize one type casted to another (smaller) type, to save bandwidth serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t Example: int num=53; SerializeCasted<uint8_t>(true, num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes)

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]valueThe value to serialize

Definition at line 993 of file BitStream.h.

        {
            if (writeToBitstream) WriteCasted<serializationType>(value);
            else return ReadCasted<serializationType>(value);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeCompressed ( bool  writeToBitstream,
templateType &  inOutTemplateVar 
) [inline]

Undefine __BITSTREAM_NATIVE_END if you need endian swapping. If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutTemplateVarThe value to write
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 954 of file BitStream.h.

References ReadCompressed(), and WriteCompressed().

Referenced by WriteCompressed().

        {
            if (writeToBitstream)
                WriteCompressed(inOutTemplateVar);
            else
                return ReadCompressed(inOutTemplateVar);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeCompressedDelta ( bool  writeToBitstream,
templateType &  inOutCurrentValue,
const templateType &  lastValue 
) [inline]

If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutCurrentValueThe current value to write
[in]lastValueThe last value to compare against. Only used if writeToBitstream is true.
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 964 of file BitStream.h.

References ReadCompressedDelta(), and WriteCompressedDelta().

        {
            if (writeToBitstream)
                WriteCompressedDelta(inOutCurrentValue,lastValue);
            else
                return ReadCompressedDelta(inOutCurrentValue);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeCompressedDelta ( bool  writeToBitstream,
templateType &  inOutTemplateVar 
) [inline]
Returns:
true on data read. False on insufficient data in bitstream

Definition at line 974 of file BitStream.h.

References ReadCompressedDelta(), and WriteCompressedDelta().

        {
            if (writeToBitstream)
                WriteCompressedDelta(inOutCurrentValue);
            else
                return ReadCompressedDelta(inOutCurrentValue);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeDelta ( bool  writeToBitstream,
templateType &  inOutCurrentValue,
const templateType &  lastValue 
) [inline]

If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutCurrentValueThe current value to write
[in]lastValueThe last value to compare against. Only used if writeToBitstream is true.
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 934 of file BitStream.h.

References ReadDelta(), and WriteDelta().

        {
            if (writeToBitstream)
                WriteDelta(inOutCurrentValue, lastValue);
            else
                return ReadDelta(inOutCurrentValue);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeDelta ( bool  writeToBitstream,
templateType &  inOutCurrentValue 
) [inline]
Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutCurrentValueThe current value to write
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 944 of file BitStream.h.

References ReadDelta(), and WriteDelta().

        {
            if (writeToBitstream)
                WriteDelta(inOutCurrentValue);
            else
                return ReadDelta(inOutCurrentValue);
            return true;
        }
bool RakNet::BitStream::SerializeFloat16 ( bool  writeToBitstream,
float inOutFloat,
float  floatMin,
float  floatMax 
)
Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]inOutFloatThe float to write
[in]floatMinPredetermined minimum value of f
[in]floatMaxPredetermined maximum value of f
template<class templateType >
bool RakNet::BitStream::SerializeNormQuat ( bool  writeToBitstream,
templateType &  w,
templateType &  x,
templateType &  y,
templateType &  z 
) [inline]
Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]ww
[in]xx
[in]yy
[in]zz
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 1035 of file BitStream.h.

References ReadNormQuat(), and WriteNormQuat().

        {
            if (writeToBitstream)
                WriteNormQuat(w,x,y,z);
            else
                return ReadNormQuat(w,x,y,z);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeNormVector ( bool  writeToBitstream,
templateType &  x,
templateType &  y,
templateType &  z 
) [inline]

Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]xx
[in]yy
[in]zz
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 1015 of file BitStream.h.

References ReadNormVector(), and WriteNormVector().

        {
            if (writeToBitstream)
                WriteNormVector(x,y,z);
            else
                return ReadNormVector(x,y,z);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeOrthMatrix ( bool  writeToBitstream,
templateType &  m00,
templateType &  m01,
templateType &  m02,
templateType &  m10,
templateType &  m11,
templateType &  m12,
templateType &  m20,
templateType &  m21,
templateType &  m22 
) [inline]

Use 6 bytes instead of 36 Lossy, although the result is renormalized

Returns:
true on success, false on failure.

Definition at line 1045 of file BitStream.h.

References ReadOrthMatrix(), and WriteOrthMatrix().

        {
            if (writeToBitstream)
                WriteOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22);
            else
                return ReadOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22);
            return true;
        }
template<class templateType >
bool RakNet::BitStream::SerializeVector ( bool  writeToBitstream,
templateType &  x,
templateType &  y,
templateType &  z 
) [inline]

Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.

Parameters:
[in]writeToBitstreamtrue to write from your data to this bitstream. False to read from this bitstream and write to your data
[in]xx
[in]yy
[in]zz
Returns:
true if writeToBitstream is true. true if writeToBitstream is false and the read was successful. false if writeToBitstream is false and the read was not successful.

Definition at line 1025 of file BitStream.h.

References ReadVector(), and WriteVector().

        {
            if (writeToBitstream)
                WriteVector(x,y,z);
            else
                return ReadVector(x,y,z);
            return true;
        }
void RakNet::BitStream::SetData ( unsigned char *  inByteArray)
void RakNet::BitStream::SetNumberOfBitsAllocated ( const BitSize_t  lengthInBits)
void RakNet::BitStream::SetReadOffset ( const BitSize_t  newReadOffset) [inline]

Definition at line 504 of file BitStream.h.

{readOffset=newReadOffset;}
void RakNet::BitStream::SetWriteOffset ( const BitSize_t  offset)
Parameters:
[in]offsetthe offset from the start of the array.
Attention:

Dangerous if you don't know what you are doing! For efficiency reasons you can only write mid-stream if your data is byte aligned.

template<>
void RakNet::BitStream::Write ( unsigned char *const &  inTemplateVar) [inline]

Definition at line 1211 of file BitStream.h.

References Write().

    {
        Write((const char*)inTemplateVar);
    }
template<class templateType >
void RakNet::BitStream::Write ( const templateType &  inTemplateVar) [inline]

Undefine __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:
[in]inTemplateVarThe value to write

Definition at line 1068 of file BitStream.h.

References DoEndianSwap(), ReverseBytes(), and WriteBits().

Referenced by operator<<(), RakNet::operator<<(), SraNetwork::SraServerInfo::Serialize(), SraNetwork::SraPacket::Serialize(), SraNetwork::SendMessageToPlayerPacket::Serialize(), SraNetwork::SendMessageToChannelPacket::Serialize(), SraNetwork::ReplyPacket::Serialize(), SraNetwork::LoginReplyPacket::Serialize(), SraNetwork::LoginPacket::Serialize(), SraNetwork::LeaveChannelPacket::Serialize(), SraNetwork::JoinChannelPacket::Serialize(), SraNetwork::GetServerListPacket::Serialize(), SraNetwork::GetClientCharactersPacket::Serialize(), DataStructures::RangeList< range_type >::Serialize(), SraNetwork::ConnectToServerPacket::Serialize(), SraNetwork::ChatMessage::Serialize(), Serialize(), SraNetwork::BasicCharacterInfo::Serialize(), RakNet::VariableDeltaSerializer::SerializeVariable(), Write(), WriteBitsFromIntegerRange(), WriteCasted(), WriteCompressed(), WriteCompressedDelta(), WriteDelta(), WriteNormQuat(), RakNet::VariableListDeltaTracker::WriteVar(), RakNet::VariableListDeltaTracker::WriteVarToBitstream(), and WriteVector().

    {
#ifdef _MSC_VER
#pragma warning(disable:4127)   // conditional expression is constant
#endif
        if (sizeof(inTemplateVar)==1)
            WriteBits( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true );
        else
        {
#ifndef __BITSTREAM_NATIVE_END
            if (DoEndianSwap())
            {
                unsigned char output[sizeof(templateType)];
                ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType));
                WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true );
            }
            else
#endif
                WriteBits( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true );
        }
    }
template<>
void RakNet::BitStream::Write ( const RakString inTemplateVar) [inline]
Parameters:
[in]varThe value to write

Definition at line 1181 of file BitStream.h.

References RakNet::RakString::Serialize().

    {
        inTemplateVar.Serialize(this);
    }
void RakNet::BitStream::Write ( const wchar_t *const  inStringVar) [inline]

Definition at line 659 of file BitStream.h.

        {
            RakWString::Serialize(inStringVar, this);
        }
template<>
void RakNet::BitStream::Write ( const unsigned char *const &  inTemplateVar) [inline]

Definition at line 1201 of file BitStream.h.

References Write().

    {
        Write((const char*)inTemplateVar);
    }
void RakNet::BitStream::Write ( const char *  inputByteArray,
const unsigned int  numberOfBytes 
)
Parameters:
[in]inputByteArraya byte buffer
[in]numberOfBytesthe size of input in bytes
void RakNet::BitStream::Write ( const unsigned char *const  inTemplateVar) [inline]

Definition at line 663 of file BitStream.h.

        {
            Write((const char*)inTemplateVar);
        }
void RakNet::BitStream::Write ( BitStream bitStream,
BitSize_t  numberOfBits 
)
Parameters:
[in]numberOfBitsbits to write
bitStreamthe bitstream to copy from
void RakNet::BitStream::Write ( BitStream bitStream)
void RakNet::BitStream::Write ( BitStream bitStream,
BitSize_t  numberOfBits 
)
template<>
void RakNet::BitStream::Write ( const RakNetGUID inTemplateVar) [inline]

Definition at line 1173 of file BitStream.h.

References RakNet::RakNetGUID::g, and Write().

        {
            Write(inTemplateVar.g);
        }
template<>
void RakNet::BitStream::Write ( char *const &  inTemplateVar) [inline]

Definition at line 1206 of file BitStream.h.

References Write().

    {
        Write((const char*)inTemplateVar);
    }
template<>
void RakNet::BitStream::Write ( const char *const &  inStringVar) [inline]

Definition at line 1191 of file BitStream.h.

References Serialize().

    {
        RakString::Serialize(inStringVar, this);
    }
template<>
void RakNet::BitStream::Write ( const wchar_t *const &  inStringVar) [inline]

Definition at line 1196 of file BitStream.h.

References Serialize().

    {
        RakWString::Serialize(inStringVar, this);
    }
template<>
void RakNet::BitStream::Write ( const SystemAddress inTemplateVar) [inline]
Parameters:
[in]inTemplateVarThe value to write

Definition at line 1128 of file BitStream.h.

References RakNet::SystemAddress::address, RakNet::SystemAddress::GetIPVersion(), RakNet::SystemAddress::GetPortNetworkOrder(), skilltest::var2, Write(), and WriteBits().

    {
        Write(inTemplateVar.GetIPVersion());
        if (inTemplateVar.GetIPVersion()==4)
        {
            // Hide the address so routers don't modify it
            SystemAddress var2=inTemplateVar;
            uint32_t binaryAddress=~inTemplateVar.address.addr4.sin_addr.s_addr;
            // Don't endian swap the address or port
            WriteBits((unsigned char*)&binaryAddress, sizeof(binaryAddress)*8, true);
            unsigned short p = var2.GetPortNetworkOrder();
            WriteBits((unsigned char*)&p, sizeof(unsigned short)*8, true);
        }
        else
        {
#if RAKNET_SUPPORT_IPV6==1
            // Don't endian swap
            WriteBits((const unsigned char*) &inTemplateVar.address.addr6, sizeof(inTemplateVar.address.addr6)*8, true);
#endif
        }
    }
void RakNet::BitStream::Write ( const char *const  inStringVar) [inline]

Definition at line 655 of file BitStream.h.

References RakNet::RakString::Serialize().

        {
            RakString::Serialize(inStringVar, this);
        }
template<>
void RakNet::BitStream::Write ( const RakWString &  inTemplateVar) [inline]

Definition at line 1186 of file BitStream.h.

    {
        inTemplateVar.Serialize(this);
    }
void RakNet::BitStream::Write ( unsigned char *const  inTemplateVar) [inline]

Definition at line 671 of file BitStream.h.

        {
            Write((const char*)inTemplateVar);
        }
void RakNet::BitStream::Write ( BitStream bitStream)
template<>
void RakNet::BitStream::Write ( const uint24_t inTemplateVar) [inline]

Definition at line 1151 of file BitStream.h.

References AddBitsAndReallocate(), AlignWriteToByteBoundary(), data, IsBigEndian(), numberOfBitsUsed, and RakNet::uint24_t::val.

    {
        AlignWriteToByteBoundary();
        AddBitsAndReallocate(3*8);

        if (IsBigEndian()==false)
        {
            data[( numberOfBitsUsed >> 3 ) + 0] = ((char *)&inTemplateVar.val)[0];
            data[( numberOfBitsUsed >> 3 ) + 1] = ((char *)&inTemplateVar.val)[1];
            data[( numberOfBitsUsed >> 3 ) + 2] = ((char *)&inTemplateVar.val)[2];
        }
        else
        {
            data[( numberOfBitsUsed >> 3 ) + 0] = ((char *)&inTemplateVar.val)[3];
            data[( numberOfBitsUsed >> 3 ) + 1] = ((char *)&inTemplateVar.val)[2];
            data[( numberOfBitsUsed >> 3 ) + 2] = ((char *)&inTemplateVar.val)[1];
        }

        numberOfBitsUsed+=3*8;
    }
template<>
void RakNet::BitStream::Write ( const bool inTemplateVar) [inline]
Parameters:
[in]inTemplateVarThe value to write

Definition at line 1116 of file BitStream.h.

References Write0(), and Write1().

        {
            if ( inTemplateVar )
                Write1();
            else
                Write0();
        }
void RakNet::BitStream::Write ( char *const  inTemplateVar) [inline]

Definition at line 667 of file BitStream.h.

        {
            Write((const char*)inTemplateVar);
        }
void RakNet::BitStream::Write0 ( void  )

Referenced by Write().

void RakNet::BitStream::Write1 ( void  )

Referenced by Write().

void RakNet::BitStream::WriteAlignedBytes ( const unsigned char *  inByteArray,
const unsigned int  numberOfBytesToWrite 
)

This is faster than WriteBits but wastes the bits to do the alignment and requires you to call ReadAlignedBits at the corresponding read position.

Parameters:
[in]inByteArrayThe data
[in]numberOfBytesToWriteThe size of input.
void RakNet::BitStream::WriteAlignedBytesSafe ( const char *  inByteArray,
const unsigned int  inputLength,
const unsigned int  maxBytesToWrite 
)
Parameters:
[in]inByteArrayThe data
[in]inputLengthThe size of input.
[in]maxBytesToWriteMax bytes to write
void RakNet::BitStream::WriteAlignedVar16 ( const char *  inByteArray)
void RakNet::BitStream::WriteAlignedVar32 ( const char *  inByteArray)
void RakNet::BitStream::WriteAlignedVar8 ( const char *  inByteArray)
void RakNet::BitStream::WriteBits ( const unsigned char *  inByteArray,
BitSize_t  numberOfBitsToWrite,
const bool  rightAlignedBits = true 
)

Right aligned data means in the case of a partial byte, the bits are aligned from the right (bit 0) rather than the left (as in the normal internal representation) You would set this to true when writing user data, and false when copying bitstream data, such as writing one bitstream to another.

Parameters:
[in]inByteArrayThe data
[in]numberOfBitsToWriteThe number of bits to write
[in]rightAlignedBitsif true data will be right aligned

Referenced by SerializeBits(), Write(), WriteBitsFromIntegerRange(), and WritePtr().

template<class templateType , class rangeType >
void RakNet::BitStream::WriteBitsFromIntegerRange ( const templateType  value,
const rangeType  minimum,
const rangeType  maximum,
bool  allowOutsideRange = false 
)

Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range Then write only those bits

Note:
A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that minimum and are fixed values for a given line of code for the life of the program
Parameters:
[in]valueInteger value to write, which should be between minimum and maximum
[in]minimumMinimum value of value
[in]maximumMaximum value of value
[in]allowOutsideRangeIf true, all sends will take an extra bit, however value can deviate from outside minimum and maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Read().

Definition at line 1739 of file BitStream.h.

References BYTES_TO_BITS, and NumberOfLeadingZeroes().

Referenced by SerializeBitsFromIntegerRange().

    {
        static int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum));
        WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
    }
template<class templateType , class rangeType >
void RakNet::BitStream::WriteBitsFromIntegerRange ( const templateType  value,
const rangeType  minimum,
const rangeType  maximum,
const int  requiredBits,
bool  allowOutsideRange = false 
)
Parameters:
[in]requiredBitsPrimarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum

Definition at line 1745 of file BitStream.h.

References IsBigEndian(), RakAssert, ReverseBytes(), Write(), and WriteBits().

    {
        RakAssert(maximum>=minimum);
        RakAssert(allowOutsideRange==true || (value>=minimum && value<=maximum));
        if (allowOutsideRange)
        {
            if (value<minimum || value>maximum)
            {
                Write(true);
                Write(value);
                return;
            }
            Write(false);
        }
        templateType valueOffMin=value-minimum;
        if (IsBigEndian()==true)
        {
            unsigned char output[sizeof(templateType)];
            ReverseBytes((unsigned char*)&valueOffMin, output, sizeof(templateType));
            WriteBits(output,requiredBits);
        }
        else
        {
            WriteBits((unsigned char*) &valueOffMin,requiredBits);
        }
    }
template<class destinationType , class sourceType >
void RakNet::BitStream::WriteCasted ( const sourceType &  value)

Write one type serialized as another (smaller) type, to save bandwidth serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t Example: int num=53; WriteCasted<uint8_t>(num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes)

Parameters:
[in]valueThe value to write

Definition at line 1732 of file BitStream.h.

References Write().

    {
        destinationType val = (destinationType) value;
        Write(val);
    }
template<>
void RakNet::BitStream::WriteCompressed ( const unsigned char *const &  inTemplateVar) [inline]

Definition at line 1360 of file BitStream.h.

References WriteCompressed().

    {
        WriteCompressed((const char*) inTemplateVar);
    }
void RakNet::BitStream::WriteCompressed ( const wchar_t *const  inStringVar) [inline]

Definition at line 679 of file BitStream.h.

        {
            RakWString::Serialize(inStringVar,this);
        }
void RakNet::BitStream::WriteCompressed ( const unsigned char *const  inTemplateVar) [inline]

Definition at line 683 of file BitStream.h.

        {
            WriteCompressed((const char*) inTemplateVar);
        }
template<>
void RakNet::BitStream::WriteCompressed ( const RakNetGUID inTemplateVar) [inline]

Definition at line 1295 of file BitStream.h.

References Write().

    {
        Write(inTemplateVar);
    }
void RakNet::BitStream::WriteCompressed ( const unsigned char *  inByteArray,
const unsigned int  size,
const bool  unsignedData 
) [private]
template<>
void RakNet::BitStream::WriteCompressed ( const float inTemplateVar) [inline]

Definition at line 1314 of file BitStream.h.

References RakAssert, and Write().

    {
        RakAssert(inTemplateVar > -1.01f && inTemplateVar < 1.01f);
        float varCopy=inTemplateVar;
        if (varCopy < -1.0f)
            varCopy=-1.0f;
        if (varCopy > 1.0f)
            varCopy=1.0f;
        Write((unsigned short)((varCopy+1.0f)*32767.5f));
    }
void RakNet::BitStream::WriteCompressed ( unsigned char *const  inTemplateVar) [inline]

Definition at line 691 of file BitStream.h.

        {
            WriteCompressed((const char*) inTemplateVar);
        }
template<>
void RakNet::BitStream::WriteCompressed ( const RakString inTemplateVar) [inline]

Definition at line 1340 of file BitStream.h.

References RakNet::RakString::SerializeCompressed().

    {
        inTemplateVar.SerializeCompressed(this,0,false);
    }
template<class templateType >
void RakNet::BitStream::WriteCompressed ( const templateType &  inTemplateVar) [inline]

Undefine __BITSTREAM_NATIVE_END if you need endian swapping. If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type

Parameters:
[in]inTemplateVarThe value to write

Undefine __BITSTREAM_NATIVE_END if you need endian swapping. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:
[in]inTemplateVarThe value to write

Definition at line 1262 of file BitStream.h.

References DoEndianSwap(), and ReverseBytes().

Referenced by SerializeCompressed(), WriteCompressed(), WriteCompressedDelta(), and WriteVector().

    {
#ifdef _MSC_VER
#pragma warning(disable:4127)   // conditional expression is constant
#endif
        if (sizeof(inTemplateVar)==1)
            WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true );
        else
        {
#ifndef __BITSTREAM_NATIVE_END
#ifdef _MSC_VER
#pragma warning(disable:4244)   // '=' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
#endif

            if (DoEndianSwap())
            {
                unsigned char output[sizeof(templateType)];
                ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType));
                WriteCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true );
            }
            else
#endif
                WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true );
        }
    }
template<>
void RakNet::BitStream::WriteCompressed ( const wchar_t *const &  inStringVar) [inline]

Definition at line 1355 of file BitStream.h.

References Serialize().

    {
        RakWString::Serialize(inStringVar,this);
    }
template<>
void RakNet::BitStream::WriteCompressed ( const SystemAddress inTemplateVar) [inline]

Definition at line 1289 of file BitStream.h.

References Write().

    {
        Write(inTemplateVar);
    }
template<>
void RakNet::BitStream::WriteCompressed ( const char *const &  inStringVar) [inline]

Definition at line 1350 of file BitStream.h.

References SerializeCompressed().

    {
        RakString::SerializeCompressed(inStringVar,this,0,false);
    }
void RakNet::BitStream::WriteCompressed ( const char *const  inStringVar) [inline]

Definition at line 675 of file BitStream.h.

References RakNet::RakString::SerializeCompressed().

        {
            RakString::SerializeCompressed(inStringVar,this,0,false);
        }
template<>
void RakNet::BitStream::WriteCompressed ( const double &  inTemplateVar) [inline]

Definition at line 1327 of file BitStream.h.

References RakAssert, and Write().

    {
        RakAssert(inTemplateVar > -1.01 && inTemplateVar < 1.01);
        double varCopy=inTemplateVar;
        if (varCopy < -1.0f)
            varCopy=-1.0f;
        if (varCopy > 1.0f)
            varCopy=1.0f;
        Write((uint32_t)((varCopy+1.0)*2147483648.0));
    }
template<>
void RakNet::BitStream::WriteCompressed ( const bool inTemplateVar) [inline]

Definition at line 1307 of file BitStream.h.

References Write().

    {
        Write(inTemplateVar);
    }
template<>
void RakNet::BitStream::WriteCompressed ( const RakWString &  inTemplateVar) [inline]

Definition at line 1345 of file BitStream.h.

    {
        inTemplateVar.Serialize(this);
    }
template<>
void RakNet::BitStream::WriteCompressed ( char *const &  inTemplateVar) [inline]

Definition at line 1365 of file BitStream.h.

References WriteCompressed().

    {
        WriteCompressed((const char*) inTemplateVar);
    }
void RakNet::BitStream::WriteCompressed ( char *const  inTemplateVar) [inline]

Definition at line 687 of file BitStream.h.

        {
            WriteCompressed((const char*) inTemplateVar);
        }
template<>
void RakNet::BitStream::WriteCompressed ( unsigned char *const &  inTemplateVar) [inline]

Definition at line 1370 of file BitStream.h.

References WriteCompressed().

    {
        WriteCompressed((const char*) inTemplateVar);
    }
template<>
void RakNet::BitStream::WriteCompressed ( const uint24_t var) [inline]

Definition at line 1301 of file BitStream.h.

References Write().

    {
        Write(var);
    }
template<class templateType >
void RakNet::BitStream::WriteCompressedDelta ( const templateType &  currentValue,
const templateType &  lastValue 
) [inline]

If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte

Parameters:
[in]currentValueThe current value to write
[in]lastValueThe last value to compare against

Definition at line 1385 of file BitStream.h.

References Write(), and WriteCompressed().

Referenced by SerializeCompressedDelta().

    {
        if (currentValue==lastValue)
        {
            Write(false);
        }
        else
        {
            Write(true);
            WriteCompressed(currentValue);
        }
    }
template<>
void RakNet::BitStream::WriteCompressedDelta ( const bool currentValue) [inline]

Definition at line 1421 of file BitStream.h.

References Write().

    {
        Write(currentValue);
    }
template<>
void RakNet::BitStream::WriteCompressedDelta ( const bool currentValue,
const bool lastValue 
) [inline]
Parameters:
[in]currentValueThe current value to write
[in]lastValueThe last value to compare against

Definition at line 1402 of file BitStream.h.

References Write().

    {
        (void) lastValue;

        Write(currentValue);
    }
template<class templateType >
void RakNet::BitStream::WriteCompressedDelta ( const templateType &  currentValue) [inline]

Definition at line 1412 of file BitStream.h.

References Write(), and WriteCompressed().

    {
        Write(true);
        WriteCompressed(currentValue);
    }
template<class templateType >
void RakNet::BitStream::WriteDelta ( const templateType &  currentValue) [inline]
Parameters:
[in]currentValueThe current value to write

Definition at line 1249 of file BitStream.h.

References Write().

    {
        Write(true);
        Write(currentValue);
    }
template<>
void RakNet::BitStream::WriteDelta ( const bool currentValue,
const bool lastValue 
) [inline]
Parameters:
[in]currentValueThe current value to write
[in]lastValueThe last value to compare against

Definition at line 1239 of file BitStream.h.

References Write().

    {
        (void) lastValue;

        Write(currentValue);
    }
template<class templateType >
void RakNet::BitStream::WriteDelta ( const templateType &  currentValue,
const templateType &  lastValue 
) [inline]

If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written

Parameters:
[in]currentValueThe current value to write
[in]lastValueThe last value to compare against

Definition at line 1222 of file BitStream.h.

References Write().

Referenced by SerializeDelta().

    {
        if (currentValue==lastValue)
        {
            Write(false);
        }
        else
        {
            Write(true);
            Write(currentValue);
        }
    }
void RakNet::BitStream::WriteFloat16 ( float  x,
float  floatMin,
float  floatMax 
)
Parameters:
[in]xThe float to write
[in]floatMinPredetermined minimum value of f
[in]floatMaxPredetermined maximum value of f

Referenced by WriteNormVector().

template<class templateType >
void RakNet::BitStream::WriteNormQuat ( templateType  w,
templateType  x,
templateType  y,
templateType  z 
)
Parameters:
[in]ww
[in]xx
[in]yy
[in]zz

Definition at line 1801 of file BitStream.h.

References Write().

Referenced by SerializeNormQuat(), and WriteOrthMatrix().

    {
        Write((bool)(w<0.0));
        Write((bool)(x<0.0));
        Write((bool)(y<0.0));
        Write((bool)(z<0.0));
        Write((unsigned short)(fabs(x)*65535.0));
        Write((unsigned short)(fabs(y)*65535.0));
        Write((unsigned short)(fabs(z)*65535.0));
        // Leave out w and calculate it on the target
    }
template<class templateType >
void RakNet::BitStream::WriteNormVector ( templateType  x,
templateType  y,
templateType  z 
)

Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.

Parameters:
[in]xx
[in]yy
[in]zz

Definition at line 1773 of file BitStream.h.

References RakAssert, and WriteFloat16().

Referenced by SerializeNormVector().

    {
#ifdef _DEBUG
        RakAssert(x <= 1.01 && y <= 1.01 && z <= 1.01 && x >= -1.01 && y >= -1.01 && z >= -1.01);
#endif

        WriteFloat16((float)x,-1.0f,1.0f);
        WriteFloat16((float)y,-1.0f,1.0f);
        WriteFloat16((float)z,-1.0f,1.0f);
    }
template<class templateType >
void RakNet::BitStream::WriteOrthMatrix ( templateType  m00,
templateType  m01,
templateType  m02,
templateType  m10,
templateType  m11,
templateType  m12,
templateType  m20,
templateType  m21,
templateType  m22 
)

Use 6 bytes instead of 36 Lossy, although the result is renormalized

Definition at line 1814 of file BitStream.h.

References _copysign, and WriteNormQuat().

Referenced by SerializeOrthMatrix().

    {

        double qw;
        double qx;
        double qy;
        double qz;

        // Convert matrix to quat
        // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
        float sum;
        sum = 1 + m00 + m11 + m22;
        if (sum < 0.0f) sum=0.0f;
        qw = sqrt( sum  ) / 2;
        sum = 1 + m00 - m11 - m22;
        if (sum < 0.0f) sum=0.0f;
        qx = sqrt( sum  ) / 2;
        sum = 1 - m00 + m11 - m22;
        if (sum < 0.0f) sum=0.0f;
        qy = sqrt( sum  ) / 2;
        sum = 1 - m00 - m11 + m22;
        if (sum < 0.0f) sum=0.0f;
        qz = sqrt( sum  ) / 2;
        if (qw < 0.0) qw=0.0;
        if (qx < 0.0) qx=0.0;
        if (qy < 0.0) qy=0.0;
        if (qz < 0.0) qz=0.0;
        qx = _copysign( (double) qx, (double) (m21 - m12) );
        qy = _copysign( (double) qy, (double) (m02 - m20) );
        qz = _copysign( (double) qz, (double) (m10 - m01) );

        WriteNormQuat(qw,qx,qy,qz);
    }
template<class templateType >
void RakNet::BitStream::WritePtr ( templateType *  inTemplateVar) [inline]

Undefine __BITSTREAM_NATIVE_END if you need endian swapping.

Parameters:
[in]inTemplateVarThe value to write

Definition at line 1091 of file BitStream.h.

References DoEndianSwap(), ReverseBytes(), and WriteBits().

    {
#ifdef _MSC_VER
#pragma warning(disable:4127)   // conditional expression is constant
#endif
        if (sizeof(templateType)==1)
            WriteBits( ( unsigned char* ) inTemplateVar, sizeof( templateType ) * 8, true );
        else
        {
#ifndef __BITSTREAM_NATIVE_END
            if (DoEndianSwap())
            {
                unsigned char output[sizeof(templateType)];
                ReverseBytes((unsigned char*) inTemplateVar, output, sizeof(templateType));
                WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true );
            }
            else
#endif
                WriteBits( ( unsigned char* ) inTemplateVar, sizeof(templateType) * 8, true );
        }
    }
template<class templateType >
void RakNet::BitStream::WriteVector ( templateType  x,
templateType  y,
templateType  z 
)

Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.

Parameters:
[in]xx
[in]yy
[in]zz

Definition at line 1785 of file BitStream.h.

References Write(), and WriteCompressed().

Referenced by SerializeVector().

    {
        templateType magnitude = sqrt(x * x + y * y + z * z);
        Write((float)magnitude);
        if (magnitude > 0.00001f)
        {
            WriteCompressed((float)(x/magnitude));
            WriteCompressed((float)(y/magnitude));
            WriteCompressed((float)(z/magnitude));
            //  Write((unsigned short)((x/magnitude+1.0f)*32767.5f));
            //  Write((unsigned short)((y/magnitude+1.0f)*32767.5f));
            //  Write((unsigned short)((z/magnitude+1.0f)*32767.5f));
        }
    }

Member Data Documentation

Definition at line 917 of file BitStream.h.

unsigned char* RakNet::BitStream::data [private]

Definition at line 914 of file BitStream.h.

Referenced by Read(), and Write().

Definition at line 910 of file BitStream.h.

Definition at line 908 of file BitStream.h.

Referenced by Read(), and Write().

Definition at line 912 of file BitStream.h.

Referenced by Read().

unsigned char RakNet::BitStream::stackData[256] [private]

Definition at line 920 of file BitStream.h.


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

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net