Przeglądaj źródła

修复新TCP库的一个bug

tanghai 7 lat temu
rodzic
commit
266eda7c96

+ 0 - 21
Server/Hotfix/Handler/C2R_PingHandler.cs

@@ -6,31 +6,10 @@ namespace ETHotfix
 	[MessageHandler(AppType.AllServer)]
 	public class C2R_PingHandler : AMRpcHandler<C2R_Ping, R2C_Ping>
 	{
-		private bool isStart = false;
 		protected override void Run(Session session, C2R_Ping message, Action<R2C_Ping> reply)
 		{
 			R2C_Ping r2CPing = new R2C_Ping();
 			reply(r2CPing);
-
-			if (!this.isStart)
-			{
-				isStart = true;
-				Start(session);
-			}
-		}
-		
-		public async void Start(Session session)
-		{
-			TimerComponent timerComponent = Game.Scene.GetComponent<TimerComponent>();
-			G2C_Test g2CTest = new G2C_Test();
-			while (true)
-			{
-				await timerComponent.WaitAsync(1);
-				for (int i = 0; i < 20; ++i)
-				{
-					session.Send(g2CTest);	
-				}
-			}
 		}
 	}
 }

+ 2 - 0
Server/Model/Component/AppManagerComponent.cs

@@ -25,6 +25,8 @@ namespace ETModel
 			
 			foreach (StartConfig startConfig in startConfigs)
 			{
+				Game.Scene.GetComponent<TimerComponent>().WaitAsync(100);
+				
 				if (!ips.Contains(startConfig.ServerIP) && startConfig.ServerIP != "*")
 				{
 					continue;

+ 10 - 1
Server/Model/Module/Actor/ActorMessageSenderComponent.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 
 namespace ETModel
 {
@@ -22,6 +23,10 @@ namespace ETModel
 
 		public ActorMessageSender Get(long id)
 		{
+			if (id == 0)
+			{
+				throw new Exception($"actor id is 0");
+			}
 			if (this.ActorMessageSenders.TryGetValue(id, out ActorMessageSender actorMessageSender))
 			{
 				return actorMessageSender;
@@ -35,6 +40,10 @@ namespace ETModel
 		
 		public ActorMessageSender GetWithActorId(long actorId)
 		{
+			if (actorId == 0)
+			{
+				throw new Exception($"actor id is 0");
+			}
 			if (this.ActorMessageSenders.TryGetValue(actorId, out ActorMessageSender actorMessageSender))
 			{
 				return actorMessageSender;

+ 0 - 1
Unity/Assets/Scripts/Base/Object/ComponentWithId.cs

@@ -14,7 +14,6 @@ namespace ETModel
 
 		protected ComponentWithId()
 		{
-			this.Id = this.InstanceId;
 		}
 
 		protected ComponentWithId(long id)

+ 4 - 3
Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs

@@ -74,9 +74,8 @@ namespace ETModel
 				this.ConnectAsync(this.RemoteAddress);
 				return;
 			}
-			
-			this.StartSend();
 			this.StartRecv();
+			this.StartSend();
 		}
 
 		public override void Send(byte[] buffer, int index, int length)
@@ -158,10 +157,12 @@ namespace ETModel
 				this.OnError((int)e.SocketError);	
 				return;
 			}
+
+			e.RemoteEndPoint = null;
 			this.isConnected = true;
 			
-			this.StartSend();
 			this.StartRecv();
+			this.StartSend();
 		}
 
 		private void OnDisconnectComplete(object o)

+ 7 - 2
Unity/Assets/Scripts/Module/Message/NetworkComponent.cs

@@ -70,9 +70,14 @@ namespace ETModel
 			this.Service.Start();
 		}
 
+		public int Count
+		{
+			get { return this.sessions.Count; }
+		}
+
 		public void OnAccept(AChannel channel)
 		{
-			Session session = ComponentFactory.CreateWithParent<Session, NetworkComponent, AChannel>(this, this, channel);
+			Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
 			this.sessions.Add(session.Id, session);
 		}
 
@@ -100,7 +105,7 @@ namespace ETModel
 		public Session Create(IPEndPoint ipEndPoint)
 		{
 			AChannel channel = this.Service.ConnectChannel(ipEndPoint);
-			Session session = ComponentFactory.CreateWithParent<Session, NetworkComponent, AChannel>(this, this, channel);
+			Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
 			this.sessions.Add(session.Id, session);
 			return session;
 		}

+ 8 - 21
Unity/Assets/Scripts/Module/Message/Session.cs

@@ -9,20 +9,11 @@ using System.Threading.Tasks;
 namespace ETModel
 {
 	[ObjectSystem]
-	public class SessionAwakeSystem : AwakeSystem<Session, NetworkComponent, AChannel>
+	public class SessionAwakeSystem : AwakeSystem<Session, AChannel>
 	{
-		public override void Awake(Session self, NetworkComponent a, AChannel b)
+		public override void Awake(Session self, AChannel b)
 		{
-			self.Awake(a, b);
-		}
-	}
-
-	[ObjectSystem]
-	public class SessionStartSystem : StartSystem<Session>
-	{
-		public override void Start(Session self)
-		{
-			self.Start();
+			self.Awake(b);
 		}
 	}
 
@@ -43,7 +34,7 @@ namespace ETModel
 			}
 		}
 
-		public void Awake(NetworkComponent net, AChannel aChannel)
+		public void Awake(AChannel aChannel)
 		{
 			this.Error = 0;
 			this.channel = aChannel;
@@ -55,13 +46,9 @@ namespace ETModel
 				this.Network.Remove(this.Id); 
 			};
 			channel.ReadCallback += this.OnRead;
+			
+			this.channel.Start();
 		}
-
-		public void Start()
-		{
-			this.channel?.Start();
-		}
-
 		public override void Dispose()
 		{
 			if (this.IsDisposed)
@@ -77,7 +64,7 @@ namespace ETModel
 			{
 				action.Invoke(new ResponseMessage { Error = ErrorCode.ERR_SessionDispose });
 			}
-
+			
 			this.Error = 0;
 			this.channel.Dispose();
 			this.Network.Remove(id);
@@ -143,7 +130,7 @@ namespace ETModel
 			catch (Exception e)
 			{
 				// 出现任何消息解析异常都要断开Session,防止客户端伪造消息
-				Log.Error(e);
+				Log.Error($"opcode: {opcode} {this.Network.Count} {e} ");
 				this.Error = ErrorCode.ERR_PacketParserError;
 				this.Network.Remove(this.Id);
 				return;

+ 0 - 1
Unity/Hotfix/Base/Object/ComponentWithId.cs

@@ -14,7 +14,6 @@ namespace ETHotfix
 
 		protected ComponentWithId()
 		{
-			this.Id = this.InstanceId;
 		}
 
 		protected ComponentWithId(long id)