Skip to content

Commit

Permalink
Merge pull request #1 from StarryGaming/main
Browse files Browse the repository at this point in the history
[修改]1. 修改消息线程问题
  • Loading branch information
AlianBlank authored Aug 12, 2024
2 parents 0c29f99 + a5b6064 commit ab36059
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 14 additions & 3 deletions Runtime/Network/Base/MessageHandlerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class MessageHandlerAttribute : Attribute
private MethodInfo _invokeMethod;
private IMessageHandler _messageHandler;

private MessageObject _messageObject;

/// <summary>
/// 网络消息处理器
/// </summary>
Expand All @@ -42,8 +44,17 @@ public MessageHandlerAttribute(Type message, string invokeMethodName)

MessageType = message;
}

/// <summary>
/// 设置消息对象
/// </summary>
/// <param name="messageObject"></param>
public void SetMessageObject(MessageObject messageObject)
{
_messageObject = messageObject;
}

internal void Invoke(MessageObject message)
internal void Invoke()
{
if (_invokeMethod == null)
{
Expand All @@ -52,11 +63,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 });
}
}

Expand Down
15 changes: 13 additions & 2 deletions Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected bool PActive
public Action<NetworkChannelBase, int> NetworkChannelMissHeartBeat;
public Action<NetworkChannelBase, NetworkErrorCode, SocketError, string> NetworkChannelError;
public Action<NetworkChannelBase, object> NetworkChannelCustomError;

private Queue<MessageHandlerAttribute> m_ExecutionQueue = new Queue<MessageHandlerAttribute>();

/// <summary>
/// 初始化网络频道基类的新实例。
Expand Down Expand Up @@ -263,6 +263,13 @@ public virtual void Update(float elapseSeconds, float realElapseSeconds)

ProcessHeartBeat(realElapseSeconds);
PRpcState.Update(elapseSeconds, realElapseSeconds);
lock (m_ExecutionQueue)
{
while (m_ExecutionQueue.Count > 0)
{
m_ExecutionQueue.Dequeue()?.Invoke();
}
}
}

/// <summary>
Expand Down Expand Up @@ -711,7 +718,11 @@ protected void InvokeMessageHandler(MessageObject messageObject)
{
try
{
handler.Invoke(messageObject);
lock (m_ExecutionQueue)
{
handler.SetMessageObject(messageObject);
m_ExecutionQueue.Enqueue(handler);
}
}
catch (Exception e)
{
Expand Down

0 comments on commit ab36059

Please sign in to comment.