Example #1
0
    static void test_sequence_buffer()
    {
        Debug.Log("test_sequence_buffer");

        const int Size = 256;

        var sequenceBuffer = new Network.SequenceBuffer <TestPacketData>(Size);

        for (int i = 0; i < Size; ++i)
        {
            TestPacketData entry;
            entry.sequence = 0;
            Assert.IsTrue(sequenceBuffer.Exists((ushort)i) == false);
            Assert.IsTrue(sequenceBuffer.Available((ushort)i) == true);
            Assert.IsTrue(sequenceBuffer.Find((ushort)i) == -1);
        }

        for (int i = 0; i <= Size * 4; ++i)
        {
            int index = sequenceBuffer.Insert((ushort)i);
            Assert.IsTrue(index != -1);
            Assert.IsTrue(sequenceBuffer.GetSequence() == i + 1);
            sequenceBuffer.Entries[index].sequence = (ushort)i;
        }

        for (int i = 0; i <= Size; ++i)
        {
            // note: outside bounds!
            int index = sequenceBuffer.Insert((ushort)i);
            Assert.IsTrue(index == -1);
        }

        ushort sequence = Size * 4;

        for (int i = 0; i < Size; ++i)
        {
            int index = sequenceBuffer.Find(sequence);
            Assert.IsTrue(index >= 0);
            Assert.IsTrue(index < Size);
            Assert.IsTrue(sequenceBuffer.Entries[index].sequence == sequence);
            sequence--;
        }

        sequenceBuffer.Reset();

        Assert.IsTrue(sequenceBuffer.GetSequence() == 0);

        for (int i = 0; i < Size; ++i)
        {
            Assert.IsTrue(sequenceBuffer.Exists((ushort)i) == false);
            Assert.IsTrue(sequenceBuffer.Available((ushort)i) == true);
            Assert.IsTrue(sequenceBuffer.Find((ushort)i) == -1);
        }
    }
Example #2
0
        public static void GenerateAckBits <T>(SequenceBuffer <T> sequenceBuffer, out ushort ack, out uint ack_bits)
        {
            ack      = (ushort)(sequenceBuffer.GetSequence() - 1);
            ack_bits = 0;
            uint mask = 1;

            for (int i = 0; i < 32; ++i)
            {
                ushort sequence = (ushort)(ack - i);
                if (sequenceBuffer.Exists(sequence))
                {
                    ack_bits |= mask;
                }
                mask <<= 1;
            }
        }
        public static void GenerateAckBits <T>(SequenceBuffer <T> buffer, out ushort ack, out uint bits)
        {
            var mask = 1u;

            ack  = (ushort)(buffer.id - 1);
            bits = 0;

            for (int i = 0; i < 32; ++i)
            {
                if (buffer.Exists((ushort)(ack - i)))
                {
                    bits |= mask;
                }

                mask <<= 1;
            }
        }