示例#1
0
        public void TakeControlInternal(IMessageRider token)
        {
            NetAssert.False(Flags & EntityFlags.HAS_CONTROL);

            Flags |= EntityFlags.HAS_CONTROL;

            CommandQueue.Clear();
            CommandSequence     = 0;
            CommandLastExecuted = null;

            ControlGainedToken = token;
            ControlLostToken   = null;

            // call to serializer
            Serializer.OnControlGained();

            // raise user event
            GlobalEventListenerBase.ControlOfEntityGainedInvoke(UnityObject);

            // call to user behaviours
            foreach (IEntityBehaviour eb in Behaviours)
            {
                if (ReferenceEquals(eb.entity, this.UnityObject))
                {
                    eb.ControlGained();
                }
            }

            Freeze(false);
        }
示例#2
0
        public void Release(Packet stream)
        {
            NetAssert.False(stream.isPooled);

            lock (pool)
            {
                stream.Size     = 0;
                stream.Position = 0;
                stream.isPooled = true;

                pool.Push(stream);
            }
        }
示例#3
0
        public void PacketReceived(Packet Packet)
        {
            try
            {
                using (Packet packet = Core.AllocatePacket())
                {
                    packet.Set(Packet); //This copies the values into the newly acquired packet
                    //Read signature & frame
                    packet.Type  = packet.ReadByte();
                    packet.Frame = packet.ReadIntVB();

                    if (packet.Frame > remoteFrameActual)
                    {
                        remoteFrameAdjust = true;
                        remoteFrameActual = packet.Frame;
                    }

                    //OLD method
                    //bitsSecondInAcc += packet.ActualSize;
                    bitsSecondInAcc += packet.Position;
                    packetsReceived += 1;

                    for (int i = 0; i < channels.Length; ++i)
                    {
                        channels[i].Read(packet);
                    }
                    //for (int i = 0; i < channels.Length; ++i)
                    //{
                    //    channels[i].ReadDone();
                    //}

                    packetStatsIn.Enqueue(packet.Stats);

                    NetAssert.False(Packet.Overflowing);

                    //SocketLog.Info("Received packet of length {0}", packet.ActualSize);
                }
            }
            catch (Exception exn)
            {
                NetLog.Exception(exn);
                NetLog.Error("exception thrown while unpacking data from {0}, disconnecting", sockConn.RemoteEndPoint);
                Disconnect();
            }
        }
示例#4
0
        public void Set(int bit)
        {
            switch (bit / 64)
            {
            case 0: this.Bits0 |= (1UL << (bit % 64)); break;

            case 1: this.Bits1 |= (1UL << (bit % 64)); break;

            case 2: this.Bits2 |= (1UL << (bit % 64)); break;

            case 3: this.Bits3 |= (1UL << (bit % 64)); break;

            case 4: this.Bits4 |= (1UL << (bit % 64)); break;

            case 5: this.Bits5 |= (1UL << (bit % 64)); break;

            case 6: this.Bits6 |= (1UL << (bit % 64)); break;

            case 7: this.Bits7 |= (1UL << (bit % 64)); break;

            case 8: this.Bits8 |= (1UL << (bit % 64)); break;

            case 9: this.Bits9 |= (1UL << (bit % 64)); break;

            case 10: this.Bits10 |= (1UL << (bit % 64)); break;

            case 11: this.Bits11 |= (1UL << (bit % 64)); break;

            case 12: this.Bits12 |= (1UL << (bit % 64)); break;

            case 13: this.Bits13 |= (1UL << (bit % 64)); break;

            case 14: this.Bits14 |= (1UL << (bit % 64)); break;

            case 15: this.Bits15 |= (1UL << (bit % 64)); break;

            default:
                throw new IndexOutOfRangeException();
            }

            NetAssert.False(IsZero);
        }
示例#5
0
        public bool Send(out Packet sentPacket)
        {
            bool sent;

            try
            {
                Packet packet = Core.AllocatePacket();
                packet.Frame  = Core.Frame;
                packet.Number = ++packetCounter;
                packet.Type   = (byte)NetworkMsg.Data;

                packet.UserToken = packet;
                //Write signature & frame
                packet.WriteByte(packet.Type);
                packet.WriteIntVB(packet.Frame);

                for (int i = 0; i < channels.Length; ++i)
                {
                    channels[i].Pack(packet);
                }

                NetAssert.False(packet.Overflowing);

                sockConn.Send(packet);

                sent       = true;
                sentPacket = packet;
                sentPacket.Set(packet);
                //SocketLog.Info("Sending packet of length {0}", packet.ActualSize);

                bitsSecondOutAcc += packet.Position;
                packetStatsOut.Enqueue(packet.Stats);
            }
            catch (Exception exn)
            {
                NetLog.Exception(exn);
                throw;
            }

            return(sent);
        }
示例#6
0
        void AddPropertyToArray(int offsetProperties, int offsetObjects, NetworkProperty property)
        {
            NetAssert.Null(Properties[offsetProperties].Property);

            if (offsetProperties > 0)
            {
                NetAssert.NotNull(Properties[offsetProperties - 1].Property);
            }

            Properties[offsetProperties].Property      = property;
            Properties[offsetProperties].OffsetObjects = offsetObjects;

            for (int i = 0; i < 32; ++i)
            {
                int f = 1 << i;

                // this can't be set
                if (Filters[i] != null)
                {
                    NetAssert.False(Filters[i].IsSet(offsetProperties));
                }

                // if property is included in this filter, flag it
                if ((property.PropertyFilters & f) == f)
                {
                    if (Filters[i] == null)
                    {
                        Filters[i] = new BitSet();
                    }

                    Filters[i].Set(offsetProperties);

                    // now it must be set
                    NetAssert.True(Filters[i].IsSet(offsetProperties));
                }
            }
        }
示例#7
0
        private int PackUpdate(Packet packet, EntityProxy proxy)
        {
            int pos       = packet.Position;
            int packCount = 0;

            EntityProxyEnvelope env = proxy.CreateEnvelope();

            packet.WriteBool(true);
            packet.WriteNetworkId(proxy.NetworkId);

            if (packet.WriteBool(proxy.Entity.IsController(connection)))
            {
                packet.WriteToken(proxy.ControlTokenGained);
                proxy.ControlTokenLost = null;
            }
            else
            {
                packet.WriteToken(proxy.ControlTokenLost);
                proxy.ControlTokenGained = null;
            }

            if (packet.WriteBool(proxy.Flags & ProxyFlags.DESTROY_REQUESTED))
            {
                packet.WriteToken(proxy.Entity.DetachToken);
            }
            else
            {
                // data for first packet
                if (packet.WriteBool(proxy.Flags & ProxyFlags.CREATE_REQUESTED))
                {
                    packet.WriteToken(proxy.Entity.AttachToken);

                    packet.WritePrefabId(proxy.Entity.PrefabId);
                    packet.WriteTypeId(proxy.Entity.Serializer.TypeId);

                    packet.WriteVector3(proxy.Entity.UnityObject.transform.position);
                    packet.WriteQuaternion(proxy.Entity.UnityObject.transform.rotation);

                    if (packet.WriteBool(proxy.Entity.IsSceneObject))
                    {
                        NetAssert.False(proxy.Entity.SceneId.IsNone,
                                        string.Format("'{0}' is marked a scene object but has no scene id ",
                                                      proxy.Entity.UnityObject.gameObject));
                        packet.WriteUniqueId(proxy.Entity.SceneId);
                    }
                }

                packCount = proxy.Entity.Serializer.Pack(connection, packet, env);
            }

            if (packet.Overflowing)
            {
                packet.Position = pos;
                return(-1);
            }
            if (packCount == -1)
            {
                packet.Position = pos;
                return(0);
            }
            else
            {
                var isForce   = proxy.Flags & ProxyFlags.FORCE_SYNC;
                var isCreate  = proxy.Flags & ProxyFlags.CREATE_REQUESTED;
                var isDestroy = proxy.Flags & ProxyFlags.DESTROY_REQUESTED;

                // if we didn't pack anything and we are not creating or destroying this, just goto next
                if ((packCount == 0) && !isCreate && !isDestroy && !isForce)
                {
                    packet.Position = pos;
                    return(0);
                }

                // set in progress flags
                if (isDestroy)
                {
                    env.Flags = (proxy.Flags |= ProxyFlags.DESTROY_PENDING);
                }

                // clear force sync flag
                proxy.Flags &= ~ProxyFlags.FORCE_SYNC;

                // clear skipped count
                proxy.Skipped = 0;

                // set packet number
                env.PacketNumber = packet.Number;

                // put on packets list
                packet.EntityUpdates.Enqueue(env);

                // put on proxies pending queue
                // NetLog.Info("adding envelope to {0}, count: {1}", proxy, proxy.Envelopes.Count + 1);
                proxy.Envelopes.Enqueue(env);

                // keep going!
                return(1);
            }
        }
示例#8
0
        public void Attach()
        {
            NetAssert.NotNull(UnityObject);
            NetAssert.False(IsAttached);
            NetAssert.True((NetworkId.Packed == 0UL) || (Source != null));

            try
            {
                AttachIsRunning = true;

                // mark as don't destroy on load
                GameObject.DontDestroyOnLoad(UnityObject.gameObject);

                // assign network id
                if (Source == null)
                {
                    NetworkId = NetworkIdAllocator.Allocate();
                }

                // add to entities list
                Core.entitiesThawed.AddLast(this);

                // mark as attached
                Flags |= EntityFlags.ATTACHED;

                // call out to behaviours
                foreach (IEntityBehaviour eb in Behaviours)
                {
                    try
                    {
                        if (eb.Invoke && ReferenceEquals(eb.entity, this.UnityObject))
                        {
                            eb.Attached();
                        }
                    }
                    catch (Exception exn)
                    {
                        NetLog.Error("User code threw exception inside Attached callback");
                        NetLog.Exception(exn);
                    }
                }

                // call out to user
                try
                {
                    GlobalEventListenerBase.EntityAttachedInvoke(this.UnityObject);
                }
                catch (Exception exn)
                {
                    NetLog.Error("User code threw exception inside Attached callback");
                    NetLog.Exception(exn);
                }

                // log
                NetLog.Debug("Attached {0} (Token: {1})", this, AttachToken);
            }
            finally
            {
                AttachIsRunning = false;
            }
        }