Ejemplo n.º 1
0
        public void TestWriteBits()
        {
            using (MemoryStream s = new MemoryStream())
                using (BitWriter writer = new BitWriter(s))
                {
                    writer.WriteBit(1);
                    writer.WriteBit(1);
                    writer.WriteBit(0);
                    writer.WriteBit(1);
                    writer.FlushBits();

                    Assert.AreEqual(new byte[] { 0xd0 }, s.ToArray());

                    writer.WriteBit(1);
                    writer.WriteBit(1);
                    writer.WriteBit(0);
                    writer.WriteBit(1);
                    writer.WriteBit(0);
                    writer.WriteBit(0);
                    writer.WriteBit(0);
                    writer.WriteBit(1);

                    Assert.AreEqual(new byte[] { 0xd0, 0xd1 }, s.ToArray());

                    writer.WriteBit(1);
                    writer.WriteBit(0);
                    writer.Write((byte)1);

                    writer.WriteBit(1);
                    writer.WriteBit(1);
                    writer.FlushBits();

                    Assert.AreEqual(new byte[] { 0xd0, 0xd1, 0x01, 0x80, 0xc0 }, s.ToArray());
                }
        }
Ejemplo n.º 2
0
 public void BitWriterWideTest()
 {
     using (MemoryStream ms = new MemoryStream()) {
         BitWriter bw = new BitWriter()
         {
             Output     = ms,
             CodeLength = 12,
         };
         bw.WriteBits(5);
         bw.WriteBits(7);
         bw.WriteBits(3);
         bw.WriteBits(7);
         bw.FlushBits();
         bw.FlushBytes();
         byte[] bytes = ms.ToArray();
         Assert.AreEqual(7, bytes.Length);
         Assert.AreEqual(0x06, bytes[0]);
         Assert.AreEqual(0x05, bytes[1]);
         Assert.AreEqual(0x70, bytes[2]);
         Assert.AreEqual(0x00, bytes[3]);
         Assert.AreEqual(0x03, bytes[4]);
         Assert.AreEqual(0x70, bytes[5]);
         Assert.AreEqual(0x00, bytes[6]);
     }
 }
Ejemplo n.º 3
0
        public static byte[] Encode(byte[] data, int bitsInterval)
        {
            byte[] reverseBytes = Convert.ReverseBitsInBytes(data);

            using (var s = new MemoryStream(reverseBytes))
                using (var reader = new BitReader(s))
                {
                    using (var encoded = new MemoryStream())
                        using (var writer = new BitWriter(encoded))
                        {
                            int counter = 0;
                            while (reader.CanRead)
                            {
                                byte bit = reader.ReadBits(1);
                                writer.WriteBit(bit);
                                counter = (bit == 1) ? counter + 1 : 0;
                                if (counter == bitsInterval)
                                {
                                    writer.WriteBit(0);
                                    counter = 0;
                                }
                            }

                            writer.FlushBits();
                            return(Convert.ReverseBitsInBytes(encoded.ToArray()));
                        }
                }
        }
Ejemplo n.º 4
0
        public void BroadcastBitStream(BitWriter bs, PacketReliability reliablity, int sequenceChannel)
        {
            bs.FlushBits();

            foreach (var client in clients)
            {
                client.EnqueueMessage(bs);
            }
        }
Ejemplo n.º 5
0
        public void EnqueueMessage(Connection connection, BitWriter bs)
        {
            bs.FlushBits();

            var message = new Message();

            message.connection = connection;
            message.data       = bs.DataWritten.ToArray();
            messageQueue.Enqueue(message);
        }
Ejemplo n.º 6
0
        public void QueueServerRpc(BitWriter bs, RpcTarget target)
        {
            bs.FlushBits();

            var qrpc = new QueuedRpc()
            {
                bs     = bs,
                target = target
            };

            queuedRpcs.Add(qrpc);
        }
Ejemplo n.º 7
0
 public void BitWriterEmptyTest()
 {
     using (MemoryStream ms = new MemoryStream()) {
         BitWriter bw = new BitWriter()
         {
             Output     = ms,
             CodeLength = 4,
         };
         bw.FlushBits();
         bw.FlushBytes();
         byte[] bytes = ms.ToArray();
         Assert.AreEqual(0, bytes.Length);
     }
 }
Ejemplo n.º 8
0
 public void BitWriterOneUnitTest()
 {
     using (MemoryStream ms = new MemoryStream()) {
         BitWriter bw = new BitWriter()
         {
             Output     = ms,
             CodeLength = 4,
         };
         bw.WriteBits(5);
         bw.FlushBits();
         bw.FlushBytes();
         byte[] bytes = ms.ToArray();
         Assert.AreEqual(2, bytes.Length);
         Assert.AreEqual(0x01, bytes[0]);
         Assert.AreEqual(0x05, bytes[1]);
     }
 }
Ejemplo n.º 9
0
        public void Test_LossyFloat()
        {
            for (int i = -32; i < 32; ++i)
            {
                var bw = new BitWriter();
                bw.WriteLossyFloat(i, -32, 32, 1);
                bw.FlushBits();

                var br = new BitReader(bw);
                Assert.AreEqual(i, br.ReadLossyFloat(-32, 32, 1));
            }
            {
                var bw = new BitWriter();
                bw.WriteLossyFloat(1.5f, 0, 3, 0.5f);
                bw.FlushBits();

                var br = new BitReader(bw);
                Assert.AreEqual(1.5f, br.ReadLossyFloat(0, 3, 0.5f));
            }
        }
Ejemplo n.º 10
0
 public void BitWriterUnalignedTest()
 {
     using (MemoryStream ms = new MemoryStream()) {
         BitWriter bw = new BitWriter()
         {
             Output     = ms,
             CodeLength = 5,
         };
         bw.WriteBits(5);
         bw.WriteBits(7);
         bw.WriteBits(3);
         bw.WriteBits(7);
         bw.FlushBits();
         bw.FlushBytes();
         byte[] bytes = ms.ToArray();
         Assert.AreEqual(4, bytes.Length);
         Assert.AreEqual(0x03, bytes[0]);
         Assert.AreEqual(0xE5, bytes[1]);
         Assert.AreEqual(0x8C, bytes[2]);
         Assert.AreEqual(0x03, bytes[3]);
     }
 }
Ejemplo n.º 11
0
        public void Send(BitWriter bs, PacketReliability reliablity, Connection connection, int sequenceChannel)
        {
            bs.FlushBits();

            LocalClientInterface targetClient = null;

            foreach (var client in clients)
            {
                if (client.connection == connection)
                {
                    targetClient = client;
                    break;
                }
            }

            if (targetClient == null)
            {
                throw new Exception("Client not found.");
            }

            targetClient.EnqueueMessage(bs);
        }
Ejemplo n.º 12
0
 public void BitWriterWpTest()
 {
     using (MemoryStream ms = new MemoryStream()) {
         BitWriter bw = new BitWriter()
         {
             Output     = ms,
             CodeLength = 9,
         };
         // http://en.wikipedia.org/wiki/Graphics_Interchange_Format
         bw.WriteBits(0x100);
         bw.WriteBits(0x028);
         bw.WriteBits(0x0FF);
         bw.WriteBits(0x103);
         bw.WriteBits(0x102);
         bw.WriteBits(0x103);
         bw.WriteBits(0x106);
         bw.WriteBits(0x107);
         bw.WriteBits(0x101);
         bw.FlushBits();
         bw.FlushBytes();
         byte[] bytes = ms.ToArray();
         Assert.AreEqual(12, bytes.Length);
         Assert.AreEqual(0x0B, bytes[0]);
         Assert.AreEqual(0x00, bytes[1]);
         Assert.AreEqual(0x51, bytes[2]);
         Assert.AreEqual(0xFC, bytes[3]);
         Assert.AreEqual(0x1B, bytes[4]);
         Assert.AreEqual(0x28, bytes[5]);
         Assert.AreEqual(0x70, bytes[6]);
         Assert.AreEqual(0xA0, bytes[7]);
         Assert.AreEqual(0xC1, bytes[8]);
         Assert.AreEqual(0x83, bytes[9]);
         Assert.AreEqual(0x01, bytes[10]);
         Assert.AreEqual(0x01, bytes[11]);
     }
 }
Ejemplo n.º 13
0
        public void EnqueueMessage(BitWriter bs)
        {
            bs.FlushBits();

            messageQueue.Enqueue(bs.DataWritten.ToArray());
        }