Skip to content

Commit

Permalink
Improve WriteCompressed() and ReadCompressed()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiddinglife committed Nov 14, 2015
1 parent beb10e2 commit 7007ec6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 72 deletions.
127 changes: 91 additions & 36 deletions Source/BitStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,10 @@ void BitStream::SetData( unsigned char *inByteArray )
void BitStream::WriteCompressed( const unsigned char* inByteArray,
const unsigned int size, const bool unsignedData )
{
BitSize_t currentByte = ( size >> 3 ) - 1; // PCs
static bool truee = true;

This comment has been minimized.

Copy link
@larku

larku Mar 4, 2016

Owner

What's the purpose of these two statics? (excuse my ignorance).

This comment has been minimized.

Copy link
@Kiddinglife

Kiddinglife Mar 15, 2016

they are both used in the codes below

This comment has been minimized.

Copy link
@Kiddinglife

Kiddinglife Mar 15, 2016

please check my project named Seriousness/server which is a re-making-wheel-project. you should understand the meaning of recreating-wheel, right?
go to the link:
https://github.com/Kiddinglife/Seriousness/tree/master/server/source

In the file JackieBits.h, I did lot of improvements.

This comment has been minimized.

Copy link
@Kiddinglife

Kiddinglife Mar 15, 2016

When I was rewriting Raknet, I found there were many bugs in its data structure. So, I am currently rewriting Raknet based on C++ 11 standards using stl containers, thread and so on. I think the core value of Raknet is the ReliableLayer class. Don't think so?

This comment has been minimized.

Copy link
@Kiddinglife

Kiddinglife Mar 15, 2016

I will make pull-request to your repos when i get free. Now i am very busy with company's project. Sorry for late reply, mate.

This comment has been minimized.

Copy link
@Kiddinglife

Kiddinglife Mar 15, 2016

I hope we can discuss about Raknet quite often. It is very hard to fina a guy who is also good at raknet at source codes level. Thanks.

This comment has been minimized.

Copy link
@larku

larku Apr 8, 2016

Owner

Hey - I'm still here working on this, I'm caught up in some other code at the moment but I'll be back here very soon :)

Regarding the change to stl containers (I'm a huge fan of them) - my understanding is that RakNet uses its own custom containers for performance reasons (reduce object copies, allocations, etc). I typically only ever use stl containers myself, but I've yet had any issue with bugs caused by the current RakNet containers. I'd question if it's a good idea or not (performance benchmarks will tell us for sure :) ).

static bool falsee = false;

BitSize_t currentByte;

unsigned char byteMatch;

Expand All @@ -505,27 +508,61 @@ void BitStream::WriteCompressed( const unsigned char* inByteArray,

// Write upper bytes with a single 1
// From high byte to low byte, if high byte is a byteMatch then write a 1 bit. Otherwise write a 0 bit and then write the remaining bytes
while ( currentByte > 0 )
if (!IsBigEndian())
{
if ( inByteArray[ currentByte ] == byteMatch ) // If high byte is byteMatch (0 of 0xff) then it would have the same value shifted
/// get the highest byte with highest index PCs
currentByte = (size >> 3) - 1;

/// From high byte to low byte,
/// if high byte is a byteMatch then write a 1 bit.
/// Otherwise write a 0 bit and then write the remaining bytes
while (currentByte > 0)
{
bool b = true;
Write( b );
/// If high byte is byteMatch (0 or 0xff)
/// then it would have the same value shifted
if (inByteArray[currentByte] == byteMatch)
{
Write(truee);
currentByte--;
}
else /// the first byte is not matched
{
Write(falsee);
// Write the remainder of the data after writing bit false
WriteBits(inByteArray, (currentByte + 1) << 3, true);
return;
}
}
else
{
// Write the remainder of the data after writing 0
bool b = false;
Write( b );

WriteBits( inByteArray, ( currentByte + 1 ) << 3, true );
// currentByte--;

/// make sure we are now on the lowest byte (index 0)
RakAssert(currentByte == 0);
}
else
{
/// get the highest byte with highest index PCs
currentByte = 0;

return ;
/// From high byte to low byte,
/// if high byte is a byteMatch then write a 1 bit.
/// Otherwise write a 0 bit and then write the remaining bytes
while (currentByte < ((size >> 3) - 1))
{
/// If high byte is byteMatch (0 or 0xff)
/// then it would have the same value shifted
if (inByteArray[currentByte] == byteMatch)
{
Write(truee);
currentByte++;
}
else /// the first byte is not matched
{
Write(falsee);
// Write the remainder of the data after writing bit false
WriteBits(inByteArray + currentByte, size - (currentByte << 3), true);
return;
}
}

currentByte--;
/// make sure we are now on the lowest byte (index highest)
RakAssert(currentByte == ((size >> 3) - 1));
}

// If the upper half of the last byte is a 0 (positive) or 16 (negative) then write a 1 and the remaining 4 bits. Otherwise write a 0 and the 8 bites.
Expand Down Expand Up @@ -617,7 +654,7 @@ bool BitStream::ReadBits( unsigned char *inOutByteArray, BitSize_t numberOfBitsT
bool BitStream::ReadCompressed( unsigned char* inOutByteArray,
const unsigned int size, const bool unsignedData )
{
unsigned int currentByte = ( size >> 3 ) - 1;
unsigned int currentByte;


unsigned char byteMatch, halfByteMatch;
Expand All @@ -636,28 +673,46 @@ bool BitStream::ReadCompressed( unsigned char* inOutByteArray,

// Upper bytes are specified with a single 1 if they match byteMatch
// From high byte to low byte, if high byte is a byteMatch then write a 1 bit. Otherwise write a 0 bit and then write the remaining bytes
while ( currentByte > 0 )
if (!IsBigEndian())
{
// If we read a 1 then the data is byteMatch.

bool b;

if ( Read( b ) == false )
return false;

if ( b ) // Check that bit
currentByte = (size >> 3) - 1;
while (currentByte > 0)
{
inOutByteArray[ currentByte ] = byteMatch;
currentByte--;
// If we read a 1 then the data is byteMatch.
bool b;
Read(b);
if (b) // Check that bit
{
data[currentByte] = byteMatch;
currentByte--;
}
else /// the first byte is not matched
{
// Read the rest of the bytes
ReadBits(data, (currentByte + 1) << 3);
return;
}
}
else
}
else
{
currentByte = 0;
while (currentByte < ((size >> 3) - 1))
{
// Read the rest of the bytes

if ( ReadBits( inOutByteArray, ( currentByte + 1 ) << 3 ) == false )
return false;

return true;
// If we read a 1 then the data is byteMatch.
bool b;
Read(b);
if (b) // Check that bit
{
data[currentByte] = byteMatch;
currentByte++;
}
else /// the first byte is not matched
{
// Read the rest of the bytes
ReadBits(data, size - (currentByte << 3));
return;
}
}
}

Expand Down
37 changes: 1 addition & 36 deletions Source/BitStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -1278,25 +1278,8 @@ namespace RakNet
#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 <>
Expand Down Expand Up @@ -1616,25 +1599,7 @@ namespace RakNet
#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 );
}
return ReadCompressed((unsigned char*)&outTemplateVar, sizeof(templateType) * 8, true);
}

template <>
Expand Down

0 comments on commit 7007ec6

Please sign in to comment.