Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[修改]1. 修改消息线程问题 #1

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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