StartMessage() public méthode

This begins a new message, which should be completed with FinishMessage() once the payload has been written.

public StartMessage ( short msgType ) : void
msgType short Message type.
Résultat void
Exemple #1
0
        internal bool SendFragmentBytes(byte[] bytes, int bytesToSend)
        {
            const int fragmentHeaderSize = 32;
            int       pos = 0;

            while (bytesToSend > 0)
            {
                int    diff   = Math.Min(bytesToSend, m_MaxPacketSize - fragmentHeaderSize);
                byte[] buffer = new byte[diff];
                Array.Copy(bytes, pos, buffer, 0, diff);

                s_FragmentWriter.StartMessage(MsgType.Fragment);
                s_FragmentWriter.Write((byte)0);
                s_FragmentWriter.WriteBytesFull(buffer);
                s_FragmentWriter.FinishMessage();
                SendWriter(s_FragmentWriter);

                pos         += diff;
                bytesToSend -= diff;
            }

            // send finish
            s_FragmentWriter.StartMessage(MsgType.Fragment);
            s_FragmentWriter.Write((byte)1);
            s_FragmentWriter.FinishMessage();
            SendWriter(s_FragmentWriter);

            return(true);
        }
Exemple #2
0
 public virtual bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     m_Writer.StartMessage(msgType);
     msg.Serialize(m_Writer);
     m_Writer.FinishMessage();
     return(SendWriter(m_Writer, channelId));
 }
        void SendTransform()
        {
            if (!HasMoved() || ClientScene.readyConnection == null)
            {
                return;
            }

            m_LocalTransformWriter.StartMessage(MsgType.LocalChildTransform);
            m_LocalTransformWriter.Write(netId);
            m_LocalTransformWriter.WritePackedUInt32(m_ChildIndex);
            SerializeModeTransform(m_LocalTransformWriter);

            m_PrevPosition = m_Target.localPosition;
            m_PrevRotation = m_Target.localRotation;


            m_LocalTransformWriter.FinishMessage();

#if UNITY_EDITOR
            UnityEditor.NetworkDetailStats.IncrementStat(
                UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
                MsgType.LocalChildTransform, "16:LocalChildTransform", 1);
#endif
            ClientScene.readyConnection.SendWriter(m_LocalTransformWriter, GetNetworkChannel());
        }
Exemple #4
0
        private void SendMsg(Operation op, int itemIndex, T item)
        {
            if (m_Behaviour == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("SyncList not initialized");
                }
                return;
            }
            NetworkIdentity component = m_Behaviour.GetComponent <NetworkIdentity>();

            if (component == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("SyncList no NetworkIdentity");
                }
                return;
            }
            NetworkWriter networkWriter = new NetworkWriter();

            networkWriter.StartMessage(9);
            networkWriter.Write(component.netId);
            networkWriter.WritePackedUInt32((uint)m_CmdHash);
            networkWriter.Write((byte)op);
            networkWriter.WritePackedUInt32((uint)itemIndex);
            SerializeItem(networkWriter, item);
            networkWriter.FinishMessage();
            NetworkServer.SendWriterToReady(component.gameObject, networkWriter, 0);
            if (m_Behaviour.isServer && m_Behaviour.isClient && m_Callback != null)
            {
                m_Callback(op, itemIndex);
            }
        }
Exemple #5
0
 private void PostInternalMessage(short msgType)
 {
     NetworkWriter writer = new NetworkWriter();
     writer.StartMessage(msgType);
     writer.FinishMessage();
     this.PostInternalMessage(writer.AsArray(), 0);
 }
Exemple #6
0
 internal void InvokeHandlerOnClient(short msgType, MessageBase msg, int channelId)
 {
     NetworkWriter writer = new NetworkWriter();
     writer.StartMessage(msgType);
     msg.Serialize(writer);
     writer.FinishMessage();
     this.InvokeBytesOnClient(writer.AsArray(), channelId);
 }
        private void PostInternalMessage(short msgType)
        {
            NetworkWriter networkWriter = new NetworkWriter();

            networkWriter.StartMessage(msgType);
            networkWriter.FinishMessage();
            this.PostInternalMessage(networkWriter.AsArray(), 0);
        }
Exemple #8
0
 public bool Send(short msgType, MessageBase msg)
 {
     s_SendWriter.StartMessage(msgType);
     msg.Serialize(s_SendWriter);
     s_SendWriter.FinishMessage();
     numMsgsOut++;
     return(SendWriter(s_SendWriter));
 }
        internal void InvokeHandlerOnClient(short msgType, MessageBase msg, int channelId)
        {
            NetworkWriter writer = new NetworkWriter();

            writer.StartMessage(msgType);
            msg.Serialize(writer);
            writer.FinishMessage();
            this.InvokeBytesOnClient(writer.AsArray(), channelId);
        }
        public bool Send(short msgType, MessageBase msg)
        {
            // build the stream
            s_SendWriter.StartMessage(msgType);
            msg.Serialize(s_SendWriter);
            s_SendWriter.FinishMessage();

            numMsgsOut += 1;
            return SendWriter(s_SendWriter);
        }
Exemple #11
0
        internal void UNetUpdate()
        {
            uint num = 0u;

            for (int i = 0; i < m_NetworkBehaviours.Length; i++)
            {
                NetworkBehaviour networkBehaviour = m_NetworkBehaviours[i];
                int dirtyChannel = networkBehaviour.GetDirtyChannel();
                if (dirtyChannel != -1)
                {
                    num = (uint)((int)num | (1 << dirtyChannel));
                }
            }
            if (num == 0)
            {
                return;
            }
            for (int j = 0; j < NetworkServer.numChannels; j++)
            {
                if (((int)num & (1 << j)) == 0)
                {
                    continue;
                }
                s_UpdateWriter.StartMessage(8);
                s_UpdateWriter.Write(netId);
                bool flag = false;
                for (int k = 0; k < m_NetworkBehaviours.Length; k++)
                {
                    short            position          = s_UpdateWriter.Position;
                    NetworkBehaviour networkBehaviour2 = m_NetworkBehaviours[k];
                    if (networkBehaviour2.GetDirtyChannel() != j)
                    {
                        networkBehaviour2.OnSerialize(s_UpdateWriter, initialState: false);
                        continue;
                    }
                    if (networkBehaviour2.OnSerialize(s_UpdateWriter, initialState: false))
                    {
                        networkBehaviour2.ClearAllDirtyBits();
                        flag = true;
                    }
                    if (s_UpdateWriter.Position - position > NetworkServer.maxPacketSize && LogFilter.logWarn)
                    {
                        Debug.LogWarning("Large state update of " + (s_UpdateWriter.Position - position) + " bytes for netId:" + netId + " from script:" + networkBehaviour2);
                    }
                }
                if (flag)
                {
                    s_UpdateWriter.FinishMessage();
                    NetworkServer.SendWriterToReady(base.gameObject, s_UpdateWriter, j);
                }
            }
        }
        void SendMsg(Operation op, int itemIndex, T item)
        {
            if (m_Behaviour == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("SyncList not initialized");
                }
                return;
            }

            var uv = m_Behaviour.GetComponent <NetworkIdentity>();

            if (uv == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("SyncList no NetworkIdentity");
                }
                return;
            }

            if (!uv.isServer)
            {
                // object is not spawned yet, so no need to send updates.
                return;
            }

            NetworkWriter writer = new NetworkWriter();

            writer.StartMessage(MsgType.SyncList);
            writer.Write(uv.netId);
            writer.WritePackedUInt32((uint)m_CmdHash);
            writer.Write((byte)op);
            writer.WritePackedUInt32((uint)itemIndex);
            SerializeItem(writer, item);
            writer.FinishMessage();

            NetworkServer.SendWriterToReady(uv.gameObject, writer, m_Behaviour.GetNetworkChannel());

#if UNITY_EDITOR
            UnityEditor.NetworkDetailStats.IncrementStat(
                UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
                MsgType.SyncList, op.ToString(), 1);
#endif

            // ensure it is invoked on host
            if (m_Behaviour.isServer && m_Behaviour.isClient && m_Callback != null)
            {
                m_Callback.Invoke(op, itemIndex);
            }
        }
Exemple #13
0
        internal void UNetUpdate()
        {
            uint num = 0;

            for (int i = 0; i < this.m_NetworkBehaviours.Length; i++)
            {
                int dirtyChannel = this.m_NetworkBehaviours[i].GetDirtyChannel();
                if (dirtyChannel != -1)
                {
                    num |= ((uint)1) << dirtyChannel;
                }
            }
            if (num != 0)
            {
                for (int j = 0; j < NetworkServer.numChannels; j++)
                {
                    if ((num & (((int)1) << j)) != 0)
                    {
                        s_UpdateWriter.StartMessage(8);
                        s_UpdateWriter.Write(this.netId);
                        bool flag = false;
                        for (int k = 0; k < this.m_NetworkBehaviours.Length; k++)
                        {
                            short            position   = s_UpdateWriter.Position;
                            NetworkBehaviour behaviour2 = this.m_NetworkBehaviours[k];
                            if (behaviour2.GetDirtyChannel() != j)
                            {
                                behaviour2.OnSerialize(s_UpdateWriter, false);
                            }
                            else
                            {
                                if (behaviour2.OnSerialize(s_UpdateWriter, false))
                                {
                                    behaviour2.ClearAllDirtyBits();
                                    NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, 8, behaviour2.GetType().Name, 1);
                                    flag = true;
                                }
                                if (((s_UpdateWriter.Position - position) > NetworkServer.maxPacketSize) && LogFilter.logWarn)
                                {
                                    Debug.LogWarning(string.Concat(new object[] { "Large state update of ", s_UpdateWriter.Position - position, " bytes for netId:", this.netId, " from script:", behaviour2 }));
                                }
                            }
                        }
                        if (flag)
                        {
                            s_UpdateWriter.FinishMessage();
                            NetworkServer.SendWriterToReady(base.gameObject, s_UpdateWriter, j);
                        }
                    }
                }
            }
        }
Exemple #14
0
        internal bool SendFragmentBytes(byte[] bytes, int bytesToSend)
        {
            int num = 0;

            while (bytesToSend > 0)
            {
                int    num2  = Math.Min(bytesToSend, m_MaxPacketSize - 32);
                byte[] array = new byte[num2];
                Array.Copy(bytes, num, array, 0, num2);
                s_FragmentWriter.StartMessage(17);
                s_FragmentWriter.Write((byte)0);
                s_FragmentWriter.WriteBytesFull(array);
                s_FragmentWriter.FinishMessage();
                SendWriter(s_FragmentWriter);
                num         += num2;
                bytesToSend -= num2;
            }
            s_FragmentWriter.StartMessage(17);
            s_FragmentWriter.Write((byte)1);
            s_FragmentWriter.FinishMessage();
            SendWriter(s_FragmentWriter);
            return(true);
        }
        void SendTransform()
        {
            if (!HasMoved() || ClientScene.readyConnection == null)
            {
                return;
            }

            m_LocalTransformWriter.StartMessage(SkeletonMsg);
            m_LocalTransformWriter.Write(netId);
            SerializeModeTransform(m_LocalTransformWriter);

            m_LocalTransformWriter.FinishMessage();

            ClientScene.readyConnection.SendWriter(m_LocalTransformWriter, GetNetworkChannel());
        }
Exemple #16
0
        private void SendTransform()
        {
            if (HasMoved() && ClientScene.readyConnection != null)
            {
                m_LocalTransformWriter.StartMessage(6);
                m_LocalTransformWriter.Write(base.netId);
                switch (transformSyncMode)
                {
                case TransformSyncMode.SyncNone:
                    return;

                case TransformSyncMode.SyncTransform:
                    SerializeModeTransform(m_LocalTransformWriter);
                    break;

                case TransformSyncMode.SyncRigidbody3D:
                    SerializeMode3D(m_LocalTransformWriter);
                    break;

                case TransformSyncMode.SyncRigidbody2D:
                    SerializeMode2D(m_LocalTransformWriter);
                    break;

                case TransformSyncMode.SyncCharacterController:
                    SerializeModeCharacterController(m_LocalTransformWriter);
                    break;
                }
                if (m_RigidBody3D != null)
                {
                    m_PrevPosition = m_RigidBody3D.position;
                    m_PrevRotation = m_RigidBody3D.rotation;
                    m_PrevVelocity = m_RigidBody3D.velocity.sqrMagnitude;
                }
                else if (m_RigidBody2D != null)
                {
                    m_PrevPosition   = m_RigidBody2D.position;
                    m_PrevRotation2D = m_RigidBody2D.rotation;
                    m_PrevVelocity   = m_RigidBody2D.velocity.sqrMagnitude;
                }
                else
                {
                    m_PrevPosition = base.transform.position;
                    m_PrevRotation = base.transform.rotation;
                }
                m_LocalTransformWriter.FinishMessage();
                ClientScene.readyConnection.SendWriter(m_LocalTransformWriter, GetNetworkChannel());
            }
        }
 private void SendMsg(SyncList <T> .Operation op, int itemIndex, T item)
 {
     if ((Object)this.m_Behaviour == (Object)null)
     {
         if (!LogFilter.logError)
         {
             return;
         }
         Debug.LogError((object)"SyncList not initialized");
     }
     else
     {
         NetworkIdentity component = this.m_Behaviour.GetComponent <NetworkIdentity>();
         if ((Object)component == (Object)null)
         {
             if (!LogFilter.logError)
             {
                 return;
             }
             Debug.LogError((object)"SyncList no NetworkIdentity");
         }
         else
         {
             if (!component.isServer)
             {
                 return;
             }
             NetworkWriter writer = new NetworkWriter();
             writer.StartMessage((short)9);
             writer.Write(component.netId);
             writer.WritePackedUInt32((uint)this.m_CmdHash);
             writer.Write((byte)op);
             writer.WritePackedUInt32((uint)itemIndex);
             this.SerializeItem(writer, item);
             writer.FinishMessage();
             NetworkServer.SendWriterToReady(component.gameObject, writer, this.m_Behaviour.GetNetworkChannel());
             NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, (short)9, op.ToString(), 1);
             if (!this.m_Behaviour.isServer || !this.m_Behaviour.isClient || this.m_Callback == null)
             {
                 return;
             }
             this.m_Callback(op, itemIndex);
         }
     }
 }
Exemple #18
0
        internal void UNetUpdate()
        {
            uint num = 0;

            for (int i = 0; i < this.m_NetworkBehaviours.Length; i++)
            {
                int dirtyChannel = this.m_NetworkBehaviours[i].GetDirtyChannel();
                if (dirtyChannel != -1)
                {
                    num |= ((uint)1) << dirtyChannel;
                }
            }
            if (num != 0)
            {
                for (int j = 0; j < NetworkServer.numChannels; j++)
                {
                    if ((num & (((int)1) << j)) != 0)
                    {
                        s_UpdateWriter.StartMessage(8);
                        s_UpdateWriter.Write(this.netId);
                        bool flag = false;
                        for (int k = 0; k < this.m_NetworkBehaviours.Length; k++)
                        {
                            NetworkBehaviour behaviour2 = this.m_NetworkBehaviours[k];
                            if (behaviour2.GetDirtyChannel() != j)
                            {
                                behaviour2.OnSerialize(s_UpdateWriter, false);
                            }
                            else if (behaviour2.OnSerialize(s_UpdateWriter, false))
                            {
                                behaviour2.ClearAllDirtyBits();
                                NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, 8, behaviour2.GetType().Name, 1);
                                flag = true;
                            }
                        }
                        if (flag)
                        {
                            s_UpdateWriter.FinishMessage();
                            NetworkServer.SendWriterToReady(base.gameObject, s_UpdateWriter, j);
                        }
                    }
                }
            }
        }
        void SendTransform()
        {
            if (!HasMoved() || ClientScene.readyConnection == null)
            {
                return;
            }

            m_LocalTransformWriter.StartMessage(MsgType.LocalChildTransform);
            m_LocalTransformWriter.Write(netId);
            m_LocalTransformWriter.WritePackedUInt32(m_ChildIndex);
            SerializeModeTransform(m_LocalTransformWriter);

            m_PrevPosition = m_Target.localPosition;
            m_PrevRotation = m_Target.localRotation;


            m_LocalTransformWriter.FinishMessage();

            ClientScene.readyConnection.SendWriter(m_LocalTransformWriter, GetNetworkChannel());
        }
 public override void Serialize(NetworkWriter writer) {
     writer.StartMessage(MSGID);
     //Send the number of elements
     writer.Write(inputsList.Count);//int32
     //Add the entire list
     foreach(Inputs i in inputsList) {
         writer.Write(i.crouch);//bool
         writer.Write(i.jump);//bool
         writer.Write(i.move);//bool
         writer.Write(i.pitch);//float
         writer.Write(i.rotate);//bool
         writer.Write(i.timeStamp);//long
         writer.Write(i.walk);//bool
         writer.Write(i.wasd[0]);//bool
         writer.Write(i.wasd[1]);//bool
         writer.Write(i.wasd[2]);//bool
         writer.Write(i.wasd[3]);//bool
         writer.Write(i.yaw);//float
     }
     writer.FinishMessage();
 }
Exemple #21
0
 private void SendMsg(Operation <T> op, int itemIndex, T item)
 {
     if (this.m_Behaviour == null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("SyncList not initialized");
         }
     }
     else
     {
         NetworkIdentity component = this.m_Behaviour.GetComponent <NetworkIdentity>();
         if (component == null)
         {
             if (LogFilter.logError)
             {
                 Debug.LogError("SyncList no NetworkIdentity");
             }
         }
         else
         {
             NetworkWriter writer = new NetworkWriter();
             writer.StartMessage(9);
             writer.Write(component.netId);
             writer.WritePackedUInt32((uint)this.m_CmdHash);
             writer.Write((byte)op);
             writer.WritePackedUInt32((uint)itemIndex);
             this.SerializeItem(writer, item);
             writer.FinishMessage();
             NetworkServer.SendWriterToReady(component.gameObject, writer, 0);
             NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, 9, op.ToString(), 1);
             if ((this.m_Behaviour.isServer && this.m_Behaviour.isClient) && (this.m_Callback != null))
             {
                 this.m_Callback(op, itemIndex);
             }
         }
     }
 }
        void SendTransform()
        {
            if (!HasMoved() || ClientScene.readyConnection == null)
            {
                return;
            }

            m_LocalTransformWriter.StartMessage(99);// MsgType.LocalPlayerTransform);
            m_LocalTransformWriter.Write(netId);

            switch (transformSyncMode)
            {
            case TransformSyncMode.SyncNone:
            {
                return;
            }

            case TransformSyncMode.SyncTransform:
            {
                SerializeModeTransform(m_LocalTransformWriter);
                break;
            }

            case TransformSyncMode.SyncCharacterController:
            {
                SerializeModeCharacterController(m_LocalTransformWriter);
                break;
            }
            }

            m_PrevPosition = transform.localPosition;
            m_PrevRotation = transform.localRotation;

            m_LocalTransformWriter.FinishMessage();

            ClientScene.readyConnection.SendWriter(m_LocalTransformWriter, GetNetworkChannel());
        }
Exemple #23
0
        // invoked by unity runtime immediately after the regular "Update()" function.
        internal void UNetUpdate()
        {
            // check if any behaviours are ready to send
            uint dirtyChannelBits = 0;

            for (int i = 0; i < m_NetworkBehaviours.Length; i++)
            {
                NetworkBehaviour comp = m_NetworkBehaviours[i];
                int channelId         = comp.GetDirtyChannel();
                if (channelId != -1)
                {
                    dirtyChannelBits |= (uint)(1 << channelId);
                }
            }
            if (dirtyChannelBits == 0)
            {
                return;
            }

            for (int channelId = 0; channelId < NetworkServer.numChannels; channelId++)
            {
                if ((dirtyChannelBits & (uint)(1 << channelId)) != 0)
                {
                    s_UpdateWriter.StartMessage(MsgType.UpdateVars);
                    s_UpdateWriter.Write(netId);

                    bool  wroteData = false;
                    short oldPos;
                    for (int i = 0; i < m_NetworkBehaviours.Length; i++)
                    {
                        oldPos = s_UpdateWriter.Position;
                        NetworkBehaviour comp = m_NetworkBehaviours[i];
                        if (comp.GetDirtyChannel() != channelId)
                        {
                            // component could write more than one dirty-bits, so call the serialize func
                            comp.OnSerialize(s_UpdateWriter, false);
                            continue;
                        }

                        if (comp.OnSerialize(s_UpdateWriter, false))
                        {
                            comp.ClearAllDirtyBits();

#if UNITY_EDITOR
                            UnityEditor.NetworkDetailStats.IncrementStat(
                                UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
                                MsgType.UpdateVars, comp.GetType().Name, 1);
#endif

                            wroteData = true;
                        }
                        if (s_UpdateWriter.Position - oldPos > NetworkServer.maxPacketSize)
                        {
                            if (LogFilter.logWarn)
                            {
                                Debug.LogWarning("Large state update of " + (s_UpdateWriter.Position - oldPos) + " bytes for netId:" + netId + " from script:" + comp);
                            }
                        }
                    }

                    if (!wroteData)
                    {
                        // nothing to send.. this could be a script with no OnSerialize function setting dirty bits
                        continue;
                    }

                    s_UpdateWriter.FinishMessage();
                    NetworkServer.SendWriterToReady(gameObject, s_UpdateWriter, channelId);
                }
            }
        }