示例#1
0
    static void test_sequence_buffer()
    {
        Log("test_sequence_buffer");
        const int Size   = 256;
        var       buffer = new SequenceBuffer <TestPacketData>(Size);

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

        for (int i = 0; i <= Size * 4; ++i)
        {
            int index = buffer.Insert((ushort)i);
            IsTrue(index != -1);
            IsTrue(buffer.id == i + 1);
            buffer.entries[index].sequence = (ushort)i;
        }

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

        ushort sequence = Size * 4;

        for (int i = 0; i < Size; ++i)
        {
            int index = buffer.Get(sequence);
            IsTrue(index >= 0);
            IsTrue(index < Size);
            IsTrue(buffer.entries[index].sequence == sequence);
            sequence--;
        }

        buffer.Reset();
        IsTrue(buffer.id == 0);

        for (int i = 0; i < Size; ++i)
        {
            IsTrue(buffer.Exists((ushort)i) == false);
            IsTrue(buffer.Available((ushort)i) == true);
            IsTrue(buffer.Get((ushort)i) == -1);
        }
    }
示例#2
0
    public bool AddCube(ushort packetId, int cubeId, ref CubeState state)
    {
        int id = buffer.Get(packetId);

        if (id == -1)
        {
            return(false);
        }

        int priorityId = buffer.entries[id].count;

        Assert.IsTrue(priorityId < MaxCubes);
        buffer.entries[id].priorityIds[cubeId] = priorityId;
        buffer.entries[id].cubeIds[priorityId] = cubeId;
        buffer.entries[id].states[priorityId]  = state;
        buffer.entries[id].count++;

        return(true);
    }
    public void AckPackets(ref PacketHeader h)
    {
        receivedPackets.Insert(h.id);

        for (int i = 0; i < 32; ++i)
        {
            if ((h.ackBits & 1) == 0)
            {
                h.ackBits >>= 1;
                continue;
            }

            var packetId = (ushort)(h.ack - i);
            int id       = sentPackets.Get(packetId);

            if (id != -1 && !sentPackets.entries[id].isAcked)
            {
                Ack(packetId);
                sentPackets.entries[id].isAcked = true;
            }
            h.ackBits >>= 1;
        }
    }