From eb9b7f556d709b22f17060666d0c57b129691f47 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Sun, 9 Dec 2018 09:03:04 -0600 Subject: [PATCH] Simplify local message processing (#146) --- Mirror/Runtime/LocalClient.cs | 42 ++++++++--------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/Mirror/Runtime/LocalClient.cs b/Mirror/Runtime/LocalClient.cs index 7b5ef8828..c8d3b4fc5 100644 --- a/Mirror/Runtime/LocalClient.cs +++ b/Mirror/Runtime/LocalClient.cs @@ -6,13 +6,8 @@ namespace Mirror { sealed class LocalClient : NetworkClient { - struct InternalMsg - { - internal ushort msgType; - internal byte[] content; - } - - List m_InternalMsgs = new List(); + + Queue m_InternalMsgs = new Queue(); bool m_Connected; @@ -68,10 +63,11 @@ internal void AddLocalPlayer(NetworkIdentity localPlayer) private void PostInternalMessage(short msgType, byte[] content) { - InternalMsg msg = new InternalMsg(); - msg.msgType = (ushort)msgType; - msg.content = content; - m_InternalMsgs.Add(msg); + NetworkMessage msg = new NetworkMessage(); + msg.msgType = msgType; + msg.reader = new NetworkReader(content); + msg.conn = connection; + m_InternalMsgs.Enqueue(msg); } private void PostInternalMessage(short msgType) @@ -83,29 +79,9 @@ private void PostInternalMessage(short msgType) private void ProcessInternalMessages() { - if (m_InternalMsgs.Count == 0) + while (m_InternalMsgs.Count > 0) { - return; - } - - // we will process all existing messages, - // but new messages might be added in the process because host mode - // local client messages don't go over the network. they are added - // here directly. (probably...) - // so we make a copy of the list first - List tmp = new List(m_InternalMsgs); - m_InternalMsgs.Clear(); - - // iterate through existing set - for (int i = 0; i < tmp.Count; i++) - { - InternalMsg msg = tmp[i]; - - NetworkMessage internalMessage = new NetworkMessage(); - internalMessage.msgType = (short)msg.msgType; - internalMessage.reader = new NetworkReader(msg.content); - internalMessage.conn = connection; - + NetworkMessage internalMessage = m_InternalMsgs.Dequeue(); m_Connection.InvokeHandler(internalMessage); connection.lastMessageTime = Time.time; }