Ver Fonte

单进程开发,网络消息不走网络,直接交给remote session,这样可以看到整个调用堆栈

tanghai há 8 anos atrás
pai
commit
5458ee77a7

+ 2 - 0
Server/Hotfix/System/NetInnerComponentSystem.cs

@@ -29,6 +29,7 @@ namespace Hotfix
 			self.Awake(NetworkProtocol.TCP);
 			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
+			self.AppType = self.GetComponent<StartConfigComponent>().StartConfig.AppType;
 		}
 
 		public static void Awake(this NetInnerComponent self, IPEndPoint ipEndPoint)
@@ -36,6 +37,7 @@ namespace Hotfix
 			self.Awake(NetworkProtocol.TCP, ipEndPoint);
 			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
+			self.AppType = self.GetComponent<StartConfigComponent>().StartConfig.AppType;
 		}
 
 		public static void Update(this NetInnerComponent self)

+ 2 - 0
Unity/Assets/Scripts/Component/NetworkComponent.cs

@@ -10,6 +10,8 @@ namespace Model
 	{
 		private AService Service;
 
+		public AppType AppType;
+
 		private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
 
 		public IMessagePacker MessagePacker { get; set; }

+ 25 - 31
Unity/Assets/Scripts/Entity/Session.cs

@@ -13,11 +13,12 @@ namespace Model
 		private readonly Dictionary<uint, Action<object>> requestCallback = new Dictionary<uint, Action<object>>();
 		private readonly AChannel channel;
 		private readonly List<byte[]> byteses = new List<byte[]>() {new byte[0], new byte[0]};
-
+		
 		public Session(NetworkComponent network, AChannel channel)
 		{
 			this.network = network;
 			this.channel = channel;
+			
 			this.StartRecv();
 		}
 
@@ -82,20 +83,7 @@ namespace Model
 
 		private void Run(ushort opcode, byte[] messageBytes)
 		{
-			int offset;
-			// opcode最高位表示是否压缩
-			bool isCompressed = (opcode & 0x8000) > 0;
-			if (isCompressed) // 最高位为1,表示有压缩,需要解压缩
-			{
-				messageBytes = ZipHelper.Decompress(messageBytes, 2, messageBytes.Length - 2);
-				offset = 0;
-			}
-			else
-			{
-				offset = 2;
-			}
-			opcode &= 0x7fff;
-			this.RunDecompressedBytes(opcode, messageBytes, offset);
+			this.RunDecompressedBytes(opcode, messageBytes, 2);
 		}
 
 		private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
@@ -142,7 +130,6 @@ namespace Model
 		public void CallWithAction(ARequest request, Action<AResponse> action)
 		{
 			request.RpcId = ++RpcId;
-			this.SendMessage(request);
 
 			this.requestCallback[RpcId] = (message) =>
 			{
@@ -156,6 +143,8 @@ namespace Model
 					Log.Error(e.ToString());
 				}
 			};
+			
+			this.SendMessage(request);
 		}
 
 		/// <summary>
@@ -164,7 +153,6 @@ namespace Model
 		public Task<AResponse> Call(ARequest request, bool isHotfix)
 		{
 			request.RpcId = ++RpcId;
-			this.SendMessage(request);
 
 			var tcs = new TaskCompletionSource<AResponse>();
 			this.requestCallback[RpcId] = (message) =>
@@ -186,6 +174,7 @@ namespace Model
 				}
 			};
 
+			this.SendMessage(request);
 			return tcs.Task;
 		}
 
@@ -195,8 +184,7 @@ namespace Model
 		public Task<AResponse> Call(ARequest request, bool isHotfix, CancellationToken cancellationToken)
 		{
 			request.RpcId = ++RpcId;
-			this.SendMessage(request);
-
+			
 			var tcs = new TaskCompletionSource<AResponse>();
 
 			this.requestCallback[RpcId] = (message) =>
@@ -220,6 +208,8 @@ namespace Model
 
 			cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
 
+			this.SendMessage(request);
+
 			return tcs.Task;
 		}
 
@@ -229,8 +219,7 @@ namespace Model
 		public Task<Response> Call<Response>(ARequest request) where Response : AResponse
 		{
 			request.RpcId = ++RpcId;
-			this.SendMessage(request);
-
+			
 			var tcs = new TaskCompletionSource<Response>();
 			this.requestCallback[RpcId] = (message) =>
 			{
@@ -251,6 +240,8 @@ namespace Model
 				}
 			};
 
+			this.SendMessage(request);
+
 			return tcs.Task;
 		}
 
@@ -261,8 +252,7 @@ namespace Model
 			where Response : AResponse
 		{
 			request.RpcId = ++RpcId;
-			this.SendMessage(request);
-
+			
 			var tcs = new TaskCompletionSource<Response>();
 
 			this.requestCallback[RpcId] = (message) =>
@@ -286,6 +276,8 @@ namespace Model
 
 			cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
 
+			this.SendMessage(request);
+
 			return tcs.Task;
 		}
 
@@ -310,23 +302,25 @@ namespace Model
 		private void SendMessage(object message)
 		{
 			//Log.Debug($"send: {MongoHelper.ToJson(message)}");
-			Opcode opcode = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
+			Opcode opcode = this.network.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
 			ushort op = (ushort)opcode;
 			byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
-			if (messageBytes.Length > 100)
+
+#if SERVER
+			// 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈
+			if (this.network.AppType == AppType.AllServer)
 			{
-				byte[] newMessageBytes = ZipHelper.Compress(messageBytes);
-				if (newMessageBytes.Length < messageBytes.Length)
-				{
-					messageBytes = newMessageBytes;
-					op |= 0x8000;
-				}
+				Session session = this.network.GetComponent<NetInnerComponent>().Get(this.RemoteAddress.ToString());
+				session.RunDecompressedBytes(op, messageBytes, 0);
+				return;
 			}
+#endif
 
 			byte[] opcodeBytes = BitConverter.GetBytes(op);
 			
 			this.byteses[0] = opcodeBytes;
 			this.byteses[1] = messageBytes;
+
 			channel.Send(this.byteses);
 		}