Просмотр исходного кода

1.opcode小于1000并且不是rpc消息即是普通hotfix消息,增加普通消息分发到hotfix层功能
2.去掉了消息的Header结构,Header结构增加了一次序列化,想想还是不划算
3.目前消息结构是size(2byte) + flag(1byte) + opcode(2byte) + rpcid(4byte可选) + 消息内容

tanghai 8 лет назад
Родитель
Сommit
17ff4ade30

+ 4 - 10
Server/Hotfix/Module/Network/InnerMessageDispatcher.cs

@@ -7,7 +7,7 @@ namespace Hotfix
 	{
 		public void Dispatch(Session session, PacketInfo packetInfo)
 		{
-			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Header.Opcode);
+			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Opcode);
 			IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 
 			// 收到actor rpc request
@@ -21,20 +21,14 @@ namespace Hotfix
 					{
 						Error = ErrorCode.ERR_NotFoundActor
 					};
-					session.Reply(packetInfo.Header.RpcId, response);
+					session.Reply(packetInfo.RpcId, response);
 					return;
 				}
-				entity.GetComponent<ActorComponent>().Add(new ActorMessageInfo() { Session = session, RpcId = packetInfo.Header.RpcId, Message = actorRpcRequest });
+				entity.GetComponent<ActorComponent>().Add(new ActorMessageInfo() { Session = session, RpcId = packetInfo.RpcId, Message = actorRpcRequest });
 				return;
 			}
 			
-			if (message is IMessage || message is IRequest)
-			{
-				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, packetInfo.Header.RpcId, message);
-				return;
-			}
-
-			throw new Exception($"message type error: {message.GetType().FullName}");
+			Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, packetInfo.RpcId, message);
 		}
 	}
 }

+ 4 - 3
Server/Hotfix/Module/Network/OuterMessageDispatcher.cs

@@ -7,7 +7,8 @@ namespace Hotfix
 	{
 		public async void Dispatch(Session session, PacketInfo packetInfo)
 		{
-			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Header.Opcode);
+			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Opcode);
+			Log.Debug($"111111111111 {MongoHelper.ToJson(packetInfo)}");
 			IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 
 			// gate session收到actor消息直接转发给actor自己去处理
@@ -25,13 +26,13 @@ namespace Hotfix
 				long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
 				ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(unitId);
 				IResponse response = await actorProxy.Call(aActorRequest);
-				session.Reply(packetInfo.Header.RpcId, response);
+				session.Reply(packetInfo.RpcId, response);
 				return;
 			}
 
 			if (message != null)
 			{
-				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, packetInfo.Header.RpcId, message);
+				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, packetInfo.RpcId, message);
 				return;
 			}
 

+ 2 - 0
Unity/Assets/Scripts/Base/Event/EventIdType.cs

@@ -4,6 +4,8 @@
 	{
 		InitSceneStart = 0,
 
+		RecvHotfixMessage,
+
 		BehaviorTreeRunTreeEvent,
 		BehaviorTreeOpenEditor,
 		BehaviorTreeClickNode,

+ 13 - 9
Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs

@@ -6,7 +6,17 @@ namespace Model
 	{
 		public void Dispatch(Session session, PacketInfo packetInfo)
 		{
-			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Header.Opcode);
+#if ILRuntime
+// 热更消息抛到hotfix层
+			if (OpcodeHelper.IsClientHotfixMessage(packetInfo.Header.Opcode))
+			{
+				Game.EventSystem.Run(EventIdType.RecvHotfixMessage, packetInfo);
+				return;
+			}
+#endif
+
+
+			Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Opcode);
 			IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 
 			// 如果是帧同步消息,交给ClientFrameComponent处理
@@ -18,14 +28,8 @@ namespace Model
 			}
 
 			// 普通消息或者是Rpc请求消息
-			if (message is IMessage || message is IRequest)
-			{
-				MessageInfo messageInfo = new MessageInfo(packetInfo.Header.Opcode, message);
-				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, messageInfo);
-				return;
-			}
-
-			throw new Exception($"message type error: {message.GetType().FullName}");
+			MessageInfo messageInfo = new MessageInfo(packetInfo.Opcode, message);
+			Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, messageInfo);
 		}
 	}
 }

+ 2 - 20
Unity/Assets/Scripts/Base/Message/IMessage.cs

@@ -6,31 +6,13 @@ namespace Model
 {
 	public struct PacketInfo
 	{
-		public Header Header;
-	
+		public ushort Opcode;
+		public uint RpcId;
 		public byte[] Bytes;
 		public ushort Index;
 		public ushort Length;
 	}
 
-	[BsonIgnoreExtraElements]
-	[ProtoContract]
-	public class Header
-	{
-		[BsonElement("a")]
-		[ProtoMember(1)]
-		public byte Flag;
-
-		[BsonElement("b")]
-		[ProtoMember(2)]
-		public ushort Opcode;
-
-		[BsonElement("c")]
-		[BsonIgnoreIfDefault]
-		[ProtoMember(3)]
-		public uint RpcId;
-	}
-
 	[ProtoContract]
 	public partial class MessageObject
 	{

+ 5 - 0
Unity/Assets/Scripts/Base/Message/OpcodeHelper.cs

@@ -21,5 +21,10 @@ namespace Model
 
 			return false;
 		}
+
+		public static bool IsClientHotfixMessage(ushort opcode)
+		{
+			return opcode < 1000;
+		}
 	}
 }

+ 17 - 0
Unity/Assets/Scripts/Base/Network/TNet/PacketParser.cs

@@ -11,6 +11,8 @@ namespace Model
 	public struct Packet
 	{
 		public const int MinSize = 2;
+		public const int OpcodeIndex = 1;
+		public const int RpcIdIndex = 3;
 
 		/// <summary>
 		/// 只读,不允许修改
@@ -29,6 +31,21 @@ namespace Model
 			this.Bytes = bytes;
 			this.Length = (ushort)bytes.Length;
 		}
+
+		public byte Flag()
+		{
+			return this.Bytes[0];
+		}
+
+		public ushort Opcode()
+		{
+			return BitConverter.ToUInt16(this.Bytes, OpcodeIndex);
+		}
+
+		public uint RpcId()
+		{
+			return BitConverter.ToUInt32(this.Bytes, RpcIdIndex);
+		}
 	}
 
 	internal class PacketParser

+ 48 - 48
Unity/Assets/Scripts/Entity/Message/Opcode.cs

@@ -2,53 +2,53 @@ namespace Model
 {
 	public static partial class Opcode
 	{
-		 public const ushort Actor_Test = 105;
-		 public const ushort Actor_TestRequest = 106;
-		 public const ushort Actor_TestResponse = 107;
-		 public const ushort Actor_TransferRequest = 108;
-		 public const ushort Actor_TransferResponse = 109;
-		 public const ushort C2G_EnterMap = 110;
-		 public const ushort G2C_EnterMap = 111;
-		 public const ushort UnitInfo = 112;
-		 public const ushort Actor_CreateUnits = 113;
-		 public const ushort FrameMessageInfo = 114;
-		 public const ushort FrameMessage = 115;
-		 public const ushort Frame_ClickMap = 116;
-		 public const ushort C2M_Reload = 117;
-		 public const ushort M2C_Reload = 118;
-		 public const ushort C2R_Ping = 119;
-		 public const ushort R2C_Ping = 120;
-		 public const ushort M2M_TrasferUnitRequest = 1005;
-		 public const ushort M2M_TrasferUnitResponse = 1006;
-		 public const ushort M2A_Reload = 1007;
-		 public const ushort A2M_Reload = 1008;
-		 public const ushort G2G_LockRequest = 1009;
-		 public const ushort G2G_LockResponse = 1010;
-		 public const ushort G2G_LockReleaseRequest = 1011;
-		 public const ushort G2G_LockReleaseResponse = 1012;
-		 public const ushort DBSaveRequest = 1013;
-		 public const ushort DBSaveBatchResponse = 1014;
-		 public const ushort DBSaveBatchRequest = 1015;
-		 public const ushort DBSaveResponse = 1016;
-		 public const ushort DBQueryRequest = 1017;
-		 public const ushort DBQueryResponse = 1018;
-		 public const ushort DBQueryBatchRequest = 1019;
-		 public const ushort DBQueryBatchResponse = 1020;
-		 public const ushort DBQueryJsonRequest = 1021;
-		 public const ushort DBQueryJsonResponse = 1022;
-		 public const ushort ObjectAddRequest = 1023;
-		 public const ushort ObjectAddResponse = 1024;
-		 public const ushort ObjectRemoveRequest = 1025;
-		 public const ushort ObjectRemoveResponse = 1026;
-		 public const ushort ObjectLockRequest = 1027;
-		 public const ushort ObjectLockResponse = 1028;
-		 public const ushort ObjectUnLockRequest = 1029;
-		 public const ushort ObjectUnLockResponse = 1030;
-		 public const ushort ObjectGetRequest = 1031;
-		 public const ushort ObjectGetResponse = 1032;
-		 public const ushort R2G_GetLoginKey = 1033;
-		 public const ushort G2R_GetLoginKey = 1034;
-		 public const ushort G2M_CreateUnit = 1035;
-		 public const ushort M2G_CreateUnit = 1036;
+		 public const ushort Actor_Test = 1105;
+		 public const ushort Actor_TestRequest = 1106;
+		 public const ushort Actor_TestResponse = 1107;
+		 public const ushort Actor_TransferRequest = 1108;
+		 public const ushort Actor_TransferResponse = 1109;
+		 public const ushort C2G_EnterMap = 1110;
+		 public const ushort G2C_EnterMap = 1111;
+		 public const ushort UnitInfo = 1112;
+		 public const ushort Actor_CreateUnits = 1113;
+		 public const ushort FrameMessageInfo = 1114;
+		 public const ushort FrameMessage = 1115;
+		 public const ushort Frame_ClickMap = 1116;
+		 public const ushort C2M_Reload = 1117;
+		 public const ushort M2C_Reload = 1118;
+		 public const ushort C2R_Ping = 1119;
+		 public const ushort R2C_Ping = 1120;
+		 public const ushort M2M_TrasferUnitRequest = 2005;
+		 public const ushort M2M_TrasferUnitResponse = 2006;
+		 public const ushort M2A_Reload = 2007;
+		 public const ushort A2M_Reload = 2008;
+		 public const ushort G2G_LockRequest = 2009;
+		 public const ushort G2G_LockResponse = 2010;
+		 public const ushort G2G_LockReleaseRequest = 2011;
+		 public const ushort G2G_LockReleaseResponse = 2012;
+		 public const ushort DBSaveRequest = 2013;
+		 public const ushort DBSaveBatchResponse = 2014;
+		 public const ushort DBSaveBatchRequest = 2015;
+		 public const ushort DBSaveResponse = 2016;
+		 public const ushort DBQueryRequest = 2017;
+		 public const ushort DBQueryResponse = 2018;
+		 public const ushort DBQueryBatchRequest = 2019;
+		 public const ushort DBQueryBatchResponse = 2020;
+		 public const ushort DBQueryJsonRequest = 2021;
+		 public const ushort DBQueryJsonResponse = 2022;
+		 public const ushort ObjectAddRequest = 2023;
+		 public const ushort ObjectAddResponse = 2024;
+		 public const ushort ObjectRemoveRequest = 2025;
+		 public const ushort ObjectRemoveResponse = 2026;
+		 public const ushort ObjectLockRequest = 2027;
+		 public const ushort ObjectLockResponse = 2028;
+		 public const ushort ObjectUnLockRequest = 2029;
+		 public const ushort ObjectUnLockResponse = 2030;
+		 public const ushort ObjectGetRequest = 2031;
+		 public const ushort ObjectGetResponse = 2032;
+		 public const ushort R2G_GetLoginKey = 2033;
+		 public const ushort G2R_GetLoginKey = 2034;
+		 public const ushort G2M_CreateUnit = 2035;
+		 public const ushort M2G_CreateUnit = 2036;
 	}
 }

+ 55 - 37
Unity/Assets/Scripts/Entity/Session.cs

@@ -27,8 +27,10 @@ namespace Model
 		private AChannel channel;
 
 		private readonly Dictionary<uint, Action<PacketInfo>> requestCallback = new Dictionary<uint, Action<PacketInfo>>();
-		
+
+		private readonly byte[] flagBytes = new byte[1];
 		private readonly List<byte[]> byteses = new List<byte[]>() {new byte[0], new byte[0], new byte[0]};
+		private readonly List<byte[]> rpcByteses = new List<byte[]>() { new byte[0], new byte[0], new byte[0], new byte[0] };
 
 		public NetworkComponent Network
 		{
@@ -130,31 +132,42 @@ namespace Model
 				return;
 			}
 
-			ushort headerSize = BitConverter.ToUInt16(packet.Bytes, 0);
-			Header header = this.Network.MessagePacker.DeserializeFrom<Header>(packet.Bytes, 2, headerSize);
-			byte flag = header.Flag;
+			byte flag = packet.Flag();
+			ushort opcode = packet.Opcode();
 			PacketInfo packetInfo = new PacketInfo
 			{
-				Header = header,
-				Index = (ushort)(headerSize + 2),
-				Bytes = packet.Bytes,
-				Length = (ushort)(packet.Length - 2 - headerSize)
+				Opcode = opcode,
+				Bytes = packet.Bytes
 			};
 
-			// flag第2位表示这是rpc返回消息
-			if ((flag & 0x40) > 0)
+			if ((flag & 0xC0) > 0)
 			{
-				uint rpcId = header.RpcId;
-				Action<PacketInfo> action;
-				if (!this.requestCallback.TryGetValue(rpcId, out action))
+				uint rpcId = packet.RpcId();
+				packetInfo.RpcId = rpcId;
+				packetInfo.Index = Packet.RpcIdIndex + 4;
+				packetInfo.Length = (ushort)(packet.Length - packetInfo.Index);
+
+				// flag第2位表示这是rpc返回消息
+				if ((flag & 0x40) > 0)
 				{
+					Action<PacketInfo> action;
+					if (!this.requestCallback.TryGetValue(rpcId, out action))
+					{
+						return;
+					}
+					this.requestCallback.Remove(rpcId);
+
+					action(packetInfo);
 					return;
 				}
-				this.requestCallback.Remove(rpcId);
-				action(packetInfo);
-				return;
 			}
-
+			else
+			{
+				packetInfo.RpcId = 0;
+				packetInfo.Index = Packet.RpcIdIndex;
+				packetInfo.Length = (ushort)(packet.Length - packetInfo.Index);
+			}
+			
 			this.Network.MessageDispatcher.Dispatch(this, packetInfo);
 		}
 
@@ -225,44 +238,49 @@ namespace Model
 
 		private void SendMessage(byte flag, ushort opcode, uint rpcId, byte[] bytes)
 		{
-			Header header = new Header
-			{
-				Opcode = opcode,
-				RpcId = rpcId,
-				Flag = flag
-			};
-
-			byte[] headerBytes = this.Network.MessagePacker.SerializeToByteArray(header);
-			byte[] headerLength = BitConverter.GetBytes((ushort)headerBytes.Length);
-
-			this.byteses[0] = headerLength;
-			this.byteses[1] = headerBytes;
-			this.byteses[2] = bytes;
+			this.flagBytes[0] = flag;
 
+			List<byte[]> bb;
+			if (rpcId == 0)
+			{
+				bb = this.byteses;
+				bb[0] = flagBytes;
+				bb[1] = BitConverter.GetBytes(opcode);
+				bb[2] = bytes;
+			}
+			else
+			{
+				bb = this.rpcByteses;
+				bb[0] = flagBytes;
+				bb[1] = BitConverter.GetBytes(opcode);
+				bb[2] = BitConverter.GetBytes(rpcId);
+				bb[3] = bytes;
+			}
+			
 #if SERVER
 			// 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈
 			if (this.Network.AppType == AppType.AllServer)
 			{
 				Session session = this.Network.Entity.GetComponent<NetInnerComponent>().Get(this.RemoteAddress);
-				this.packet.Length = 0;
+				this.pkt.Length = 0;
 				ushort index = 0;
-				foreach (var byts in this.byteses)
+				foreach (var byts in bb)
 				{
-					Array.Copy(byts, 0, this.packet.Bytes, index, byts.Length);
+					Array.Copy(byts, 0, this.pkt.Bytes, index, byts.Length);
 					index += (ushort)byts.Length;
 				}
 
-				this.packet.Length = index;
-				session.Run(packet);
+				this.pkt.Length = index;
+				session.Run(this.pkt);
 				return;
 			}
 #endif
 
-			channel.Send(this.byteses);
+			channel.Send(bb);
 		}
 
 #if SERVER
-		private Packet packet = new Packet(ushort.MaxValue);
+		private Packet pkt = new Packet(ushort.MaxValue);
 #endif
 	}
 }

+ 2 - 2
Unity/Assets/Scripts/Helper/SessionHelper.cs

@@ -12,7 +12,7 @@ namespace Model
 			ushort opcode = opcodeTypeComponent.GetOpcode(request.GetType());
 			byte[] bytes = session.Network.MessagePacker.SerializeToByteArray(request);
 			PacketInfo packetInfo = await session.Call(opcode, bytes);
-			Type responseType = opcodeTypeComponent.GetType(packetInfo.Header.Opcode);
+			Type responseType = opcodeTypeComponent.GetType(packetInfo.Opcode);
 			object message = session.Network.MessagePacker.DeserializeFrom(responseType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 			IResponse response = (IResponse) message;
 			if (response.Error > 100)
@@ -29,7 +29,7 @@ namespace Model
 			ushort opcode = opcodeTypeComponent.GetOpcode(request.GetType());
 			byte[] bytes = session.Network.MessagePacker.SerializeToByteArray(request);
 			PacketInfo packetInfo = await session.Call(opcode, bytes, cancellationToken);
-			Type responseType = opcodeTypeComponent.GetType(packetInfo.Header.Opcode);
+			Type responseType = opcodeTypeComponent.GetType(packetInfo.Opcode);
 			object message = session.Network.MessagePacker.DeserializeFrom(responseType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 			IResponse response = (IResponse)message;
 			if (response.Error > 100)

+ 19 - 0
Unity/Hotfix/Module/HotfixMessage/RecvHotfixMessageEvent_HandleHotfixMessage.cs

@@ -0,0 +1,19 @@
+using System;
+using Model;
+
+namespace Hotfix
+{
+	// 分发数值监听
+	[Event((int)Model.EventIdType.RecvHotfixMessage)]
+	public class RecvHotfixMessageEvent_HandleHotfixMessage: IEvent<Session, PacketInfo>
+	{
+		public void Run(Session session, PacketInfo packetInfo)
+		{
+			ushort opcode = packetInfo.Opcode;
+			Type t = SessionHelper.GetMessageType(opcode);
+			object aa = ProtobufHelper.FromBytes(t, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
+			IMessage message = (IMessage)aa;
+			Hotfix.Scene.GetComponent<MessageDispatherComponent>().Handle(session, opcode, message);
+		}
+	}
+}

+ 3 - 3
Unity/Hotfix/Module/HotfixMessage/SessionHelper.cs

@@ -18,8 +18,8 @@ namespace Hotfix
 			byte[] bytes = ProtobufHelper.ToBytes(request);
 			ushort opcode = GetOpcode(request.GetType());
 			PacketInfo packetInfo = await session.Call(opcode, bytes);
-			ushort responseOpcode = packetInfo.Header.Opcode;
-			Type t = GetType(responseOpcode);
+			ushort responseOpcode = packetInfo.Opcode;
+			Type t = GetMessageType(responseOpcode);
 			object aa = ProtobufHelper.FromBytes(t, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);
 			IResponse response = (IResponse)aa;
 			return response;
@@ -34,7 +34,7 @@ namespace Hotfix
 #endif
 		}
 
-		public static Type GetType(ushort opcode)
+		public static Type GetMessageType(ushort opcode)
 		{
 #if ILRuntime
 			return Hotfix.Scene.GetComponent<OpcodeTypeComponent>().GetType(opcode);

+ 1 - 0
Unity/Hotfix/Unity.Hotfix.csproj

@@ -54,6 +54,7 @@
     <Compile Include="Base\Helper\ArrayHelper.cs" />
     <Compile Include="Base\Helper\AssetBundleHelper.cs" />
     <Compile Include="Base\Helper\ExceptionHelper.cs" />
+    <Compile Include="Module\HotfixMessage\RecvHotfixMessageEvent_HandleHotfixMessage.cs" />
     <Compile Include="Module\HotfixMessage\IMessage.cs" />
     <Compile Include="Module\HotfixMessage\AMHandler.cs" />
     <Compile Include="Module\HotfixMessage\IMHandler.cs" />