Exemplo n.º 1
0
        static VectorOffset SampleEvents(ITickObject obj, FlatBufferBuilder fbb, bool full)
        {
            VectorOffset vecOffset = default(VectorOffset);

            if (!full && obj.eventType != TickEventType.NONE)
            {
                var eventVector = OffsetArrayPool.Alloc <TickEvent>((int)snapshotOverTick);
                for (uint i = 0; i < snapshotOverTick; ++i)
                {
                    int eventOffset = obj.SnapshotEvent(fbb, tickCount - snapshotOverTick + i);
                    if (eventOffset > 0)
                    {
                        eventVector.offsets[eventVector.position++] = TickEvent.CreateTickEvent(
                            fbb,
                            tickCount - snapshotOverTick + i,
                            obj.eventType,
                            eventOffset);
                    }
                }

                TickObject.StartEventsVector(fbb, eventVector.position);
                vecOffset = Helpers.SetVector(fbb, eventVector);
                OffsetArrayPool.Dealloc(ref eventVector);
            }
            return(vecOffset);
        }
        Offset <Msg_SC_UpdatePlayers> SyncNewPlayer(FlatBufferBuilder fbb, int id)
        {
            VectorOffset pvOffset = default(VectorOffset);
            Player       p        = Get(id);

            if (null != p)
            {
                var array  = OffsetArrayPool.Alloc <Protocol.Player>(1);
                var offset = Protocol.Player.CreatePlayer(fbb, p.id, fbb.CreateString(p.playerName));
                array.offsets[array.position++] = offset;
                Msg_SC_UpdatePlayers.StartAddPlayersVector(fbb, array.position);
                pvOffset = Helpers.SetVector(fbb, array);
                OffsetArrayPool.Dealloc(ref array);
            }
            return(Msg_SC_UpdatePlayers.CreateMsg_SC_UpdatePlayers(fbb, false, pvOffset));
        }
        Offset <Msg_SC_UpdatePlayers> SyncPlayerList(FlatBufferBuilder fbb)
        {
            VectorOffset pvOffset = default(VectorOffset);

            if (mPlayers.Count > 0)
            {
                var array = OffsetArrayPool.Alloc <Protocol.Player>(mPlayers.Count);
                foreach (var p in mPlayers)
                {
                    var offset = Protocol.Player.CreatePlayer(fbb, p.id, fbb.CreateString(p.playerName));
                    array.offsets[array.position++] = offset;
                }
                Msg_SC_UpdatePlayers.StartAddPlayersVector(fbb, array.position);
                pvOffset = Helpers.SetVector(fbb, array);
                OffsetArrayPool.Dealloc(ref array);
            }
            return(Msg_SC_UpdatePlayers.CreateMsg_SC_UpdatePlayers(fbb, true, pvOffset));
        }
Exemplo n.º 4
0
    static void FB_Serialize(FlatBufferBuilder fbb)
    {
        fbb.Clear();
        var phoneArray  = OffsetArrayPool.Alloc <PhoneNumber>(10);
        var personArray = OffsetArrayPool.Alloc <Person>(10);

        for (int p = 0; p < 10; ++p)
        {
            phoneArray.Clear();
            for (int n = 0; n < 10; ++n)
            {
                StringOffset phoneNumberOffset = fbb.CreateString((p * 100 + n).ToString());
                PhoneNumber.StartPhoneNumber(fbb);
                PhoneNumber.AddNumber(fbb, phoneNumberOffset);
                PhoneNumber.AddType(fbb, (PhoneType)(n % 3));
                phoneArray.offsets[phoneArray.position++] = PhoneNumber.EndPhoneNumber(fbb);
            }

            StringOffset nameOffset  = fbb.CreateString(personName + p);
            StringOffset emailOffset = fbb.CreateString(string.Format("{0}{1}@gmail.com", personName, p));
            Person.StartPhonesVector(fbb, phoneArray.position);
            VectorOffset phoneArrayOffset = Helpers.SetVector(fbb, phoneArray);

            Person.StartPerson(fbb);
            Person.AddName(fbb, nameOffset);
            Person.AddId(fbb, p);
            Person.AddEmail(fbb, emailOffset);
            Person.AddPhones(fbb, phoneArrayOffset);
            personArray.offsets[personArray.position++] = Person.EndPerson(fbb);
        }

        AddressBook.StartPeopleVector(fbb, personArray.position);
        VectorOffset peopleArrayOffset = Helpers.SetVector(fbb, personArray);

        AddressBook.StartAddressBook(fbb);
        AddressBook.AddPeople(fbb, peopleArrayOffset);
        Offset <AddressBook> offset = AddressBook.EndAddressBook(fbb);

        fbb.Finish(offset.Value);

        OffsetArrayPool.Dealloc(ref phoneArray);
        OffsetArrayPool.Dealloc(ref personArray);
    }
Exemplo n.º 5
0
    static void OffsetArrayPoolTest()
    {
        var array = OffsetArrayPool.sArrays[typeof(PhoneNumber)];
        int size  = (OffsetArrayPool.maxSizePOT - OffsetArrayPool.minSizePOT + 1) * OffsetArrayPool.initialArraySize;

        Debug.Assert(array.Count == size);

        List <OffsetArrayPool.Array <PhoneNumber> > oalist = new List <OffsetArrayPool.Array <PhoneNumber> >();

        OffsetArrayPool.Array <PhoneNumber> oa = OffsetArrayPool.Alloc <PhoneNumber>(1);
        oalist.Add(oa);
        Debug.Assert(oa.length == (1 << OffsetArrayPool.minSizePOT));
        Debug.Assert(array.Count == size - oalist.Count);

        oa = OffsetArrayPool.Alloc <PhoneNumber>(1 << OffsetArrayPool.minSizePOT);
        oalist.Add(oa);
        Debug.Assert(oa.length == (1 << OffsetArrayPool.minSizePOT));
        Debug.Assert(array.Count == size - oalist.Count);

        oa = OffsetArrayPool.Alloc <PhoneNumber>((1 << OffsetArrayPool.minSizePOT) + 1);
        oalist.Add(oa);
        Debug.Assert(oa.length == (1 << (OffsetArrayPool.minSizePOT + 1)));
        Debug.Assert(array.Count == size - oalist.Count);

        oa = OffsetArrayPool.Alloc <PhoneNumber>(1);
        oalist.Add(oa);
        Debug.Assert(oa.length == (1 << OffsetArrayPool.minSizePOT));
        Debug.Assert(array.Count == size - oalist.Count + OffsetArrayPool.initialArraySize);

        for (int i = 0; i < oalist.Count; ++i)
        {
            oa = oalist[i];
            OffsetArrayPool.Dealloc(ref oa);
            Debug.Assert(oa == null);
        }
        oalist.Clear();
        Debug.Assert(array.Count == size + OffsetArrayPool.initialArraySize);

        Console.WriteLine("Array Pool Checked");
    }
Exemplo n.º 6
0
        static VectorOffset SampleSnapshot(IList <ITickObject> list, FlatBufferBuilder fbb, bool full)
        {
            var tickObjectOffset = default(VectorOffset);

            if (null != list && list.Count > 0)
            {
                var boxArray = OffsetArrayPool.Alloc <TickObject>(list.Count);
                foreach (var obj in list)
                {
                    boxArray.offsets[boxArray.position++] = TickObject.CreateTickObject(
                        fbb,
                        obj.id,
                        obj.type,
                        SampleData(obj, fbb, full),
                        SampleEvents(obj, fbb, full),
                        SampleSnapshot(obj.children, fbb, full));
                }

                Msg_SC_Snapshot.StartTickObjectVector(fbb, boxArray.position);
                tickObjectOffset = Helpers.SetVector(fbb, boxArray);
                OffsetArrayPool.Dealloc(ref boxArray);
            }
            return(tickObjectOffset);
        }