From dfb879f7b3d0287e9fb438e2d05752f3034549fd Mon Sep 17 00:00:00 2001 From: chenxin16 Date: Fri, 9 Aug 2024 17:01:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?[=E4=BF=AE=E6=94=B9]1.=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=BA=BF=E7=A8=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/Network/Base/MessageHandlerAttribute.cs | 8 +++++--- .../Network/NetworkManager.NetworkChannelBase.cs | 13 +++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Runtime/Network/Base/MessageHandlerAttribute.cs b/Runtime/Network/Base/MessageHandlerAttribute.cs index e6cda46..0290737 100644 --- a/Runtime/Network/Base/MessageHandlerAttribute.cs +++ b/Runtime/Network/Base/MessageHandlerAttribute.cs @@ -20,6 +20,8 @@ public class MessageHandlerAttribute : Attribute private MethodInfo _invokeMethod; private IMessageHandler _messageHandler; + public MessageObject messageObject; + /// /// 网络消息处理器 /// @@ -43,7 +45,7 @@ public MessageHandlerAttribute(Type message, string invokeMethodName) MessageType = message; } - internal void Invoke(MessageObject message) + internal void Invoke() { if (_invokeMethod == null) { @@ -52,11 +54,11 @@ internal void Invoke(MessageObject message) if (_invokeMethod.IsStatic) { - _invokeMethod?.Invoke(null, new object[] { message }); + _invokeMethod?.Invoke(null, new object[] { messageObject }); } else { - _invokeMethod?.Invoke(_messageHandler, new object[] { message }); + _invokeMethod?.Invoke(_messageHandler, new object[] { messageObject }); } } diff --git a/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs b/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs index 0a8f6ec..8bdccde 100644 --- a/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs +++ b/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs @@ -74,7 +74,7 @@ protected bool PActive public Action NetworkChannelMissHeartBeat; public Action NetworkChannelError; public Action NetworkChannelCustomError; - + private Queue m_ExecutionQueue = new Queue(); /// /// 初始化网络频道基类的新实例。 @@ -263,6 +263,11 @@ public virtual void Update(float elapseSeconds, float realElapseSeconds) ProcessHeartBeat(realElapseSeconds); PRpcState.Update(elapseSeconds, realElapseSeconds); + + while (m_ExecutionQueue.Count > 0) + { + m_ExecutionQueue.Dequeue().Invoke(); + } } /// @@ -711,7 +716,11 @@ protected void InvokeMessageHandler(MessageObject messageObject) { try { - handler.Invoke(messageObject); + lock (m_ExecutionQueue) + { + handler.messageObject = messageObject; + m_ExecutionQueue.Enqueue(handler); + } } catch (Exception e) { From b32f15565e4b7bf4d2b49d556a867db31c7b0077 Mon Sep 17 00:00:00 2001 From: StarryGames Date: Sat, 10 Aug 2024 00:27:17 +0800 Subject: [PATCH 2/3] =?UTF-8?q?[=E4=BF=AE=E6=94=B9]1.=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=B6=88=E6=81=AFHandler=E7=BA=BF=E7=A8=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/Network/Base/MessageHandlerAttribute.cs | 15 ++++++++++++--- .../Network/NetworkManager.NetworkChannelBase.cs | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Runtime/Network/Base/MessageHandlerAttribute.cs b/Runtime/Network/Base/MessageHandlerAttribute.cs index 0290737..4700edc 100644 --- a/Runtime/Network/Base/MessageHandlerAttribute.cs +++ b/Runtime/Network/Base/MessageHandlerAttribute.cs @@ -20,7 +20,7 @@ public class MessageHandlerAttribute : Attribute private MethodInfo _invokeMethod; private IMessageHandler _messageHandler; - public MessageObject messageObject; + private MessageObject _messageObject; /// /// 网络消息处理器 @@ -44,6 +44,15 @@ public MessageHandlerAttribute(Type message, string invokeMethodName) MessageType = message; } + + /// + /// 设置消息对象 + /// + /// + public void SetMessageObject(MessageObject messageObject) + { + _messageObject = messageObject; + } internal void Invoke() { @@ -54,11 +63,11 @@ internal void Invoke() if (_invokeMethod.IsStatic) { - _invokeMethod?.Invoke(null, new object[] { messageObject }); + _invokeMethod?.Invoke(null, new object[] { _messageObject }); } else { - _invokeMethod?.Invoke(_messageHandler, new object[] { messageObject }); + _invokeMethod?.Invoke(_messageHandler, new object[] { _messageObject }); } } diff --git a/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs b/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs index 8bdccde..01c60c9 100644 --- a/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs +++ b/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs @@ -266,7 +266,7 @@ public virtual void Update(float elapseSeconds, float realElapseSeconds) while (m_ExecutionQueue.Count > 0) { - m_ExecutionQueue.Dequeue().Invoke(); + m_ExecutionQueue.Dequeue()?.Invoke(); } } @@ -718,7 +718,7 @@ protected void InvokeMessageHandler(MessageObject messageObject) { lock (m_ExecutionQueue) { - handler.messageObject = messageObject; + handler.SetMessageObject(messageObject); m_ExecutionQueue.Enqueue(handler); } } From a5b606492f2e20c0595901e9e819ccdd0f620ff2 Mon Sep 17 00:00:00 2001 From: StarryGames Date: Sat, 10 Aug 2024 14:16:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?[=E4=BF=AE=E6=94=B9]1.=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=AE=B9=E5=99=A8=E9=94=81=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Network/NetworkManager.NetworkChannelBase.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs b/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs index 01c60c9..2ebfec4 100644 --- a/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs +++ b/Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs @@ -263,11 +263,13 @@ public virtual void Update(float elapseSeconds, float realElapseSeconds) ProcessHeartBeat(realElapseSeconds); PRpcState.Update(elapseSeconds, realElapseSeconds); - - while (m_ExecutionQueue.Count > 0) + lock (m_ExecutionQueue) { - m_ExecutionQueue.Dequeue()?.Invoke(); - } + while (m_ExecutionQueue.Count > 0) + { + m_ExecutionQueue.Dequeue()?.Invoke(); + } + } } ///