浏览代码

简化MessageInfo代码,修复NetworkComponent removecallback可能为null的bug

tanghai 9 年之前
父节点
当前提交
b2f5c4b865

+ 4 - 11
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -107,12 +107,12 @@ namespace Model
 			return messageAttribute.Opcode;
 		}
 
-		public void Handle(Session session, ushort opcode, byte[] messageBytes, int offset, uint rpcId)
+		public void Handle(Session session, MessageInfo messageInfo)
 		{
 			List<IMHandler> actions;
-			if (!this.handlers.TryGetValue(opcode, out actions))
+			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
 			{
-				Log.Error($"消息 {opcode} 没有处理");
+				Log.Error($"消息 {messageInfo.Opcode} 没有处理");
 				return;
 			}
 
@@ -120,14 +120,7 @@ namespace Model
 			{
 				try
 				{
-					ev.Handle(session, opcode, new MessageInfo
-						{
-							MessageBytes = messageBytes,
-							Offset = offset,
-							Count = messageBytes.Length - offset,
-							RpcId = rpcId
-						}
-					);
+					ev.Handle(session, messageInfo);
 				}
 				catch (Exception e)
 				{

+ 10 - 10
Unity/Assets/Scripts/Component/NetworkComponent.cs

@@ -12,7 +12,7 @@ namespace Model
 		private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
 		private readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
 
-		private event Action<Session> removeCallback;
+		private event Action<Session> removeCallback = session => { };
 
 		public event Action<Session> RemoveCallback
 		{
@@ -138,6 +138,15 @@ namespace Model
 			return session;
 		}
 
+		public void Update()
+		{
+			if (this.Service == null)
+			{
+				return;
+			}
+			this.Service.Update();
+		}
+
 		public override void Dispose()
 		{
 			if (this.Id == 0)
@@ -154,14 +163,5 @@ namespace Model
 			
 			this.Service.Dispose();
 		}
-
-		public void Update()
-		{
-			if (this.Service == null)
-			{
-				return;
-			}
-			this.Service.Update();
-		}
 	}
 }

+ 10 - 16
Unity/Assets/Scripts/Entity/Session.cs

@@ -87,29 +87,23 @@ namespace Model
 
 		private void RunDecompressedBytes(ushort opcode, uint rpcId, uint rpcFlag, byte[] messageBytes, int offset)
 		{
-			// 普通消息
-			if (rpcId == 0)
+			// 普通消息或者是Rpc请求消息
+			if (rpcFlag == 0)
 			{
-				this.scene.GetComponent<MessageDispatherComponent>().Handle(this, opcode, messageBytes, offset, 0);
+				MessageInfo messageInfo = new MessageInfo(opcode, messageBytes, offset, rpcId);
+				this.scene.GetComponent<MessageDispatherComponent>().Handle(this, messageInfo);
 				return;
 			}
 
 			// rpcFlag>0 表示这是一个rpc响应消息
-			if (rpcFlag > 0)
+			Action<byte[], int, int> action;
+			// Rpc回调有找不着的可能,因为client可能取消Rpc调用
+			if (!this.requestCallback.TryGetValue(rpcId, out action))
 			{
-				Action<byte[], int, int> action;
-				// Rpc回调有找不着的可能,因为client可能取消Rpc调用
-				if (!this.requestCallback.TryGetValue(rpcId, out action))
-				{
-					return;
-				}
-				this.requestCallback.Remove(rpcId);
-				action(messageBytes, offset, messageBytes.Length - offset);
-			}
-			else // 这是一个rpc请求消息
-			{
-				this.scene.GetComponent<MessageDispatherComponent>().Handle(this, opcode, messageBytes, offset, rpcId);
+				return;
 			}
+			this.requestCallback.Remove(rpcId);
+			action(messageBytes, offset, messageBytes.Length - offset);
 		}
 
 		/// <summary>

+ 4 - 4
Unity/Assets/Scripts/Message/AMHandler.cs

@@ -7,7 +7,7 @@ namespace Model
 	{
 		protected abstract void Run(Session session, Message message);
 		
-		public void Handle(Session session, ushort opcode, MessageInfo messageInfo)
+		public void Handle(Session session, MessageInfo messageInfo)
 		{
 			Message message;
 			try
@@ -16,7 +16,7 @@ namespace Model
 			}
 			catch (Exception e)
 			{
-				throw new Exception($"解释消息失败: {opcode}", e);
+				throw new Exception($"解释消息失败: {messageInfo.Opcode}", e);
 			}
 
 			this.Run(session, message);
@@ -34,7 +34,7 @@ namespace Model
 	{
 		protected abstract void Run(Session session, Request message, Action<Response> reply);
 
-		public void Handle(Session session, ushort opcode, MessageInfo messageInfo)
+		public void Handle(Session session, MessageInfo messageInfo)
 		{
 			try
 			{
@@ -52,7 +52,7 @@ namespace Model
 			}
 			catch (Exception e)
 			{
-				throw new Exception($"解释消息失败: {opcode}", e);
+				throw new Exception($"解释消息失败: {messageInfo.Opcode}", e);
 			}
 		}
 

+ 21 - 5
Unity/Assets/Scripts/Message/IMHandler.cs

@@ -4,15 +4,31 @@ namespace Model
 {
 	public class MessageInfo
 	{
-		public byte[] MessageBytes;
-		public int Offset;
-		public int Count;
-		public uint RpcId;
+		public ushort Opcode { get; }
+		public byte[] MessageBytes { get; }
+		public int Offset { get; }
+		public uint RpcId { get; }
+
+		public MessageInfo(ushort opcode, byte[] messageBytes, int offset, uint rpcId)
+		{
+			this.Opcode = opcode;
+			this.MessageBytes = messageBytes;
+			this.Offset = offset;
+			this.RpcId = rpcId;
+		}
+
+		public int Count
+		{
+			get
+			{
+				return this.MessageBytes.Length - this.Offset;
+			}
+		}
 	}
 
 	public interface IMHandler
 	{
-		void Handle(Session session, ushort opcode, MessageInfo messageInfo);
+		void Handle(Session session, MessageInfo messageInfo);
 		Type GetMessageType();
 	}
 }