Skip to content

Commit

Permalink
Added possibility to write value in exact number of bits
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcidev committed Apr 19, 2017
1 parent da52f04 commit 00694a6
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public void TestPacketBuild()
packet.WriteGuidByteStreamInOrder(123456, 1, 2, 3, 4, 5, 6, 7, 0);

Assert.True(buildedPacket.Data.SequenceEqual(packet.Data));
packet.Dispose();
buildedPacket.Dispose();
}
}
}
30 changes: 30 additions & 0 deletions Arci.Networking.Tests/DataTests/ByteBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,35 @@ public void AppendByteBufferTest()
}
}
}

[TestMethod]
public void TestWriteBits()
{
byte val = 0x1 + 0x4 + 0x10;
var packet1 = new Packet(packetOpcode);
packet1.WriteBits(val, 5);
packet1.FlushBits();

var packet2 = new Packet(packet1.Data);
Assert.AreEqual(val, packet2.ReadBits(5));

packet1.Dispose();
packet1.Dispose();
}

[TestMethod]
public void TestWriteBits2()
{
var val = uint.MaxValue;
var packet1 = new Packet(packetOpcode);
packet1.WriteBits(val, 16);
packet1.FlushBits();

var packet2 = new Packet(packet1.Data);
Assert.AreEqual(ushort.MaxValue, packet2.ReadBits(16));

packet1.Dispose();
packet1.Dispose();
}
}
}
72 changes: 72 additions & 0 deletions Arci.Networking/Builder/PacketBuilderTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public PacketBuilder WriteBit(Int16 bit)
packet.WriteBit(bit);
return this;
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
/// <returns>This PacketBuilder</returns>
public PacketBuilder WriteBits(Int16 value, byte bitsCount)
{
packet.WriteBits(value, bitsCount);
return this;
}

/// <summary>
/// Writes Int32 value to packet
Expand All @@ -49,6 +61,18 @@ public PacketBuilder WriteBit(Int32 bit)
packet.WriteBit(bit);
return this;
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
/// <returns>This PacketBuilder</returns>
public PacketBuilder WriteBits(Int32 value, byte bitsCount)
{
packet.WriteBits(value, bitsCount);
return this;
}

/// <summary>
/// Writes SByte value to packet
Expand All @@ -71,6 +95,18 @@ public PacketBuilder WriteBit(SByte bit)
packet.WriteBit(bit);
return this;
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
/// <returns>This PacketBuilder</returns>
public PacketBuilder WriteBits(SByte value, byte bitsCount)
{
packet.WriteBits(value, bitsCount);
return this;
}

/// <summary>
/// Writes UInt16 value to packet
Expand All @@ -93,6 +129,18 @@ public PacketBuilder WriteBit(UInt16 bit)
packet.WriteBit(bit);
return this;
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
/// <returns>This PacketBuilder</returns>
public PacketBuilder WriteBits(UInt16 value, byte bitsCount)
{
packet.WriteBits(value, bitsCount);
return this;
}

/// <summary>
/// Writes UInt32 value to packet
Expand All @@ -115,6 +163,18 @@ public PacketBuilder WriteBit(UInt32 bit)
packet.WriteBit(bit);
return this;
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
/// <returns>This PacketBuilder</returns>
public PacketBuilder WriteBits(UInt32 value, byte bitsCount)
{
packet.WriteBits(value, bitsCount);
return this;
}

/// <summary>
/// Writes Byte value to packet
Expand All @@ -137,6 +197,18 @@ public PacketBuilder WriteBit(Byte bit)
packet.WriteBit(bit);
return this;
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
/// <returns>This PacketBuilder</returns>
public PacketBuilder WriteBits(Byte value, byte bitsCount)
{
packet.WriteBits(value, bitsCount);
return this;
}

/// <summary>
/// Writes bit value to stream
Expand Down
12 changes: 12 additions & 0 deletions Arci.Networking/Builder/PacketBuilderTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ namespace Arci.Networking.Builder
packet.WriteBit(bit);
return this;
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
/// <returns>This PacketBuilder</returns>
public PacketBuilder WriteBits(<#= type.Name #> value, byte bitsCount)
{
packet.WriteBits(value, bitsCount);
return this;
}
<#
} #>

Expand Down
16 changes: 16 additions & 0 deletions Arci.Networking/Data/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ public bool ReadBit()
return ((curBitVal >> (7 - bitPos)) & 1) != 0;
}

/// <summary>
/// Reads exact number of bits and return result as a value
/// </summary>
/// <param name="bitsCount">Bits to be read</param>
/// <returns>Value represented by bits that have been read</returns>
public UInt32 ReadBits(byte bitsCount)
{
UInt32 value = 0;
for (int i = bitsCount - 1; i >= 0; i--)
{
if (ReadBit())
value |= (UInt32)1 << i;
}
return value;
}

/// <summary>
/// Writes string value
/// </summary>
Expand Down
66 changes: 66 additions & 0 deletions Arci.Networking/Data/ByteBufferGenericTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public void WriteBit(Int16 bit)
if (bitPos == 0)
WriteCurBitVal();
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
public void WriteBits(Int16 value, byte bitsCount)
{
for (int i = bitsCount - 1; i >= 0; i--)
WriteBit(((Int16)1 << i) & value);
}

/// <summary>
/// Reads Int32 value from stream
Expand Down Expand Up @@ -69,6 +80,17 @@ public void WriteBit(Int32 bit)
if (bitPos == 0)
WriteCurBitVal();
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
public void WriteBits(Int32 value, byte bitsCount)
{
for (int i = bitsCount - 1; i >= 0; i--)
WriteBit(((Int32)1 << i) & value);
}

/// <summary>
/// Reads SByte value from stream
Expand Down Expand Up @@ -101,6 +123,17 @@ public void WriteBit(SByte bit)
if (bitPos == 0)
WriteCurBitVal();
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
public void WriteBits(SByte value, byte bitsCount)
{
for (int i = bitsCount - 1; i >= 0; i--)
WriteBit(((SByte)1 << i) & value);
}

/// <summary>
/// Reads UInt16 value from stream
Expand Down Expand Up @@ -133,6 +166,17 @@ public void WriteBit(UInt16 bit)
if (bitPos == 0)
WriteCurBitVal();
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
public void WriteBits(UInt16 value, byte bitsCount)
{
for (int i = bitsCount - 1; i >= 0; i--)
WriteBit(((UInt16)1 << i) & value);
}

/// <summary>
/// Reads UInt32 value from stream
Expand Down Expand Up @@ -165,6 +209,17 @@ public void WriteBit(UInt32 bit)
if (bitPos == 0)
WriteCurBitVal();
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
public void WriteBits(UInt32 value, byte bitsCount)
{
for (int i = bitsCount - 1; i >= 0; i--)
WriteBit(((UInt32)1 << i) & value);
}

/// <summary>
/// Reads Byte value from stream
Expand Down Expand Up @@ -197,6 +252,17 @@ public void WriteBit(Byte bit)
if (bitPos == 0)
WriteCurBitVal();
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
public void WriteBits(Byte value, byte bitsCount)
{
for (int i = bitsCount - 1; i >= 0; i--)
WriteBit(((Byte)1 << i) & value);
}

/// <summary>
/// Writes bit value to stream
Expand Down
11 changes: 11 additions & 0 deletions Arci.Networking/Data/ByteBufferGenericTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ namespace Arci.Networking.Data
if (bitPos == 0)
WriteCurBitVal();
}

/// <summary>
/// Writes value with specified number of bits
/// </summary>
/// <param name="value">Value to be written</param>
/// <param name="bitsCount">Number of bits that value should written with</param>
public void WriteBits(<#= type.Name #> value, byte bitsCount)
{
for (int i = bitsCount - 1; i >= 0; i--)
WriteBit(((<#= type.Name #>)1 << i) & value);
}
<#
} #>

Expand Down

0 comments on commit 00694a6

Please sign in to comment.