Beispiel #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);
        }
Beispiel #2
0
        public void UpdateInput(UdpConnector connector)
        {
            current = default(InputData);
            if (mIndex % mCmdOverTick == 0)
            {
                if (mChoke > 0)
                {
                    --mChoke;
                    return;
                }
            }

            current.index    = mIndex;
            current.keyboard = (byte)(GetKey(KeyCode.W) << 3 | GetKey(KeyCode.A) << 2 | GetKey(KeyCode.S) << 1 | GetKey(KeyCode.D));
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (current.mouseHasHit = Physics.Raycast(ray, out hit, 100f, (1 << Layers.Ground)))
            {
                current.mouseHit = hit.point;
            }
            mInputQueue.Add(current);

            if ((mIndex++) % mCmdOverTick == 0)
            {
                using (var builder = MessageBuilder.Get())
                {
                    FlatBufferBuilder fbb = builder.fbb;
                    try
                    {
                        var array = OffsetArrayPool.Alloc <Protocol.InputData>((int)mCmdOverTick);
                        for (int i = 0; i < mCmdOverTick; ++i)
                        {
                            InputData d = mInputQueue[mInputQueue.Count - (int)mCmdOverTick + i];
                            Protocol.InputData.StartInputData(fbb);
                            Protocol.InputData.AddIndex(fbb, d.index);
                            Protocol.InputData.AddKeyboard(fbb, d.keyboard);
                            Protocol.InputData.AddMouseHasHit(fbb, d.mouseHasHit);
                            if (d.mouseHasHit)
                            {
                                Protocol.InputData.AddMouseHit(fbb, Protocol.Vec3.CreateVec3(fbb, d.mouseHit.x, d.mouseHit.y, d.mouseHit.z));
                            }
                            array.offsets[array.position++] = Protocol.InputData.EndInputData(fbb);
                        }
                        Protocol.Msg_CS_InputDataArray.StartInputDataVector(fbb, array.position);
                        var offset = Helpers.SetVector(fbb, array);
                        fbb.Finish(Protocol.Msg_CS_InputDataArray.CreateMsg_CS_InputDataArray(fbb, offset).Value);
                        connector.SendMessage(
                            connector.CreateMessage(Protocol.MessageID.Msg_CS_InputDataArray, fbb),
                            NetDeliveryMethod.UnreliableSequenced,
                            1);
                    }
                    catch (Exception e)
                    {
                        TCLog.Exception(e);
                    }
                }
            }
        }
Beispiel #3
0
    static void SerializeTest()
    {
        OffsetArrayPool.sArrays.Clear();
        OffsetArrayPool.Add(typeof(PhoneNumber));
        OffsetArrayPool.Add(typeof(Person));

        FlatBufferBuilder fbb = new FlatBufferBuilder(8192);
        int size = (OffsetArrayPool.maxSizePOT - OffsetArrayPool.minSizePOT + 1) * OffsetArrayPool.initialArraySize;

        FB_Serialize(fbb);
        Debug.Assert(OffsetArrayPool.sArrays[typeof(PhoneNumber)].Count == size);
        Debug.Assert(OffsetArrayPool.sArrays[typeof(Person)].Count == size);
        FB_Deserialize(fbb.DataBuffer);

        Console.WriteLine("Serialize Checked");
    }
        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));
        }
Beispiel #6
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);
    }
Beispiel #7
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");
    }
Beispiel #8
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);
        }