Explorar el Código

TimerComponent组件Wait方法返回一个bool值,返回true表示正常timeout,返回false表示中途取消计时器
主要原因是为了避免类似using语句因为取消协程导致无法调用到Dispose方法的问题,所以协程即使取消也一定要往下执行
因此增加bool值来判断是否已经取消

tanghai hace 6 años
padre
commit
b9d5ec4a39

+ 0 - 0
Unity/Assets/Model/Helper/ResourcesHelper.cs → Unity/Assets/HotfixView/Helper/ResourcesHelper.cs


+ 63 - 40
Unity/Assets/Model/Entity/TimerComponent.cs

@@ -6,13 +6,13 @@ namespace ET
 {
 	public interface ITimer
 	{
-		void Run();
+		void Run(bool isTimeout);
 	}
     
     [ObjectSystem]
-	public class OnceWaitTimerAwakeSystem : AwakeSystem<OnceWaitTimer, ETTaskCompletionSource>
+	public class OnceWaitTimerAwakeSystem : AwakeSystem<OnceWaitTimer, ETTaskCompletionSource<bool>>
 	{
-		public override void Awake(OnceWaitTimer self, ETTaskCompletionSource callback)
+		public override void Awake(OnceWaitTimer self, ETTaskCompletionSource<bool> callback)
 		{
 			self.Callback = callback;
 		}
@@ -20,20 +20,20 @@ namespace ET
 	
 	public class OnceWaitTimer: Entity, ITimer
 	{
-		public ETTaskCompletionSource Callback { get; set; }
+		public ETTaskCompletionSource<bool> Callback { get; set; }
 		
-		public void Run()
+		public void Run(bool isTimeout)
 		{
-			ETTaskCompletionSource tcs = this.Callback;
+			ETTaskCompletionSource<bool> tcs = this.Callback;
 			this.GetParent<TimerComponent>().Remove(this.Id);
-			tcs.SetResult();
+			tcs.SetResult(isTimeout);
 		}
 	}
 	
 	[ObjectSystem]
-	public class OnceTimerAwakeSystem : AwakeSystem<OnceTimer, Action>
+	public class OnceTimerAwakeSystem : AwakeSystem<OnceTimer, Action<bool>>
 	{
-		public override void Awake(OnceTimer self, Action callback)
+		public override void Awake(OnceTimer self, Action<bool> callback)
 		{
 			self.Callback = callback;
 		}
@@ -41,13 +41,13 @@ namespace ET
 	
 	public class OnceTimer: Entity, ITimer
 	{
-		public Action Callback { get; set; }
+		public Action<bool> Callback { get; set; }
 		
-		public void Run()
+		public void Run(bool isTimeout)
 		{
 			try
 			{
-				this.Callback.Invoke();
+				this.Callback.Invoke(isTimeout);
 			}
 			catch (Exception e)
 			{
@@ -57,9 +57,9 @@ namespace ET
 	}
 	
 	[ObjectSystem]
-	public class RepeatedTimerAwakeSystem : AwakeSystem<RepeatedTimer, long, Action>
+	public class RepeatedTimerAwakeSystem : AwakeSystem<RepeatedTimer, long, Action<bool>>
 	{
-		public override void Awake(RepeatedTimer self, long repeatedTime, Action callback)
+		public override void Awake(RepeatedTimer self, long repeatedTime, Action<bool> callback)
 		{
 			self.Awake(repeatedTime, callback);
 		}
@@ -67,7 +67,7 @@ namespace ET
 	
 	public class RepeatedTimer: Entity, ITimer
 	{
-		public void Awake(long repeatedTime, Action callback)
+		public void Awake(long repeatedTime, Action<bool> callback)
 		{
 			this.StartTime = TimeHelper.Now();
 			this.RepeatedTime = repeatedTime;
@@ -82,9 +82,9 @@ namespace ET
 		// 下次一是第几次触发
 		private int Count { get; set; }
 		
-		public Action Callback { private get; set; }
+		public Action<bool> Callback { private get; set; }
 		
-		public void Run()
+		public void Run(bool isTimeout)
 		{
 			++this.Count;
 			TimerComponent timerComponent = this.GetParent<TimerComponent>();
@@ -93,7 +93,7 @@ namespace ET
 
 			try
 			{
-				this.Callback.Invoke();
+				this.Callback.Invoke(isTimeout);
 			}
 			catch (Exception e)
 			{
@@ -205,62 +205,85 @@ namespace ET
 					continue;
 				}
 				
-				timer.Run();
+				timer.Run(true);
 			}
 		}
 
-		public ETTask WaitTillAsync(long tillTime, ETCancellationToken cancellationToken)
+		public async ETTask<bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken)
 		{
 			if (TimeHelper.Now() > tillTime)
 			{
-				return ETTask.CompletedTask;
+				return true;
 			}
-			ETTaskCompletionSource tcs = new ETTaskCompletionSource();
-			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource>(this, tcs);
+			ETTaskCompletionSource<bool> tcs = new ETTaskCompletionSource<bool>();
+			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource<bool>>(this, tcs);
 			this.timers[timer.Id] = timer;
 			AddToTimeId(tillTime, timer.Id);
-			cancellationToken.Register(() => { this.Remove(timer.Id); });
-			return tcs.Task;
+			
+			long instanceId = timer.InstanceId;
+			cancellationToken.Register(() =>
+			{
+				if (instanceId != timer.InstanceId)
+				{
+					return;
+				}
+				
+				timer.Run(false);
+				
+				this.Remove(timer.Id);
+			});
+			return await tcs.Task;
 		}
 
-		public ETTask WaitTillAsync(long tillTime)
+		public async ETTask<bool> WaitTillAsync(long tillTime)
 		{
 			if (TimeHelper.Now() > tillTime)
 			{
-				return ETTask.CompletedTask;
+				return true;
 			}
-			ETTaskCompletionSource tcs = new ETTaskCompletionSource();
-			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource>(this, tcs);
+			ETTaskCompletionSource<bool> tcs = new ETTaskCompletionSource<bool>();
+			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource<bool>>(this, tcs);
 			this.timers[timer.Id] = timer;
 			AddToTimeId(tillTime, timer.Id);
-			return tcs.Task;
+			return await tcs.Task;
 		}
 
-		public ETTask WaitAsync(long time, ETCancellationToken cancellationToken)
+		public async ETTask<bool> WaitAsync(long time, ETCancellationToken cancellationToken)
 		{
 			long tillTime = TimeHelper.Now() + time;
 
             if (TimeHelper.Now() > tillTime)
             {
-                return ETTask.CompletedTask;
+                return true;
             }
 
-            ETTaskCompletionSource tcs = new ETTaskCompletionSource();
-			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource>(this, tcs);
+            ETTaskCompletionSource<bool> tcs = new ETTaskCompletionSource<bool>();
+			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource<bool>>(this, tcs);
 			this.timers[timer.Id] = timer;
 			AddToTimeId(tillTime, timer.Id);
-			cancellationToken.Register(() => { this.Remove(timer.Id); });
-			return tcs.Task;
+			long instanceId = timer.InstanceId;
+			cancellationToken.Register(() =>
+			{
+				if (instanceId != timer.InstanceId)
+				{
+					return;
+				}
+				
+				timer.Run(false);
+				
+				this.Remove(timer.Id);
+			});
+			return await tcs.Task;
 		}
 
-		public ETTask WaitAsync(long time)
+		public async ETTask<bool> WaitAsync(long time)
 		{
 			long tillTime = TimeHelper.Now() + time;
-			ETTaskCompletionSource tcs = new ETTaskCompletionSource();
-			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource>(this, tcs);
+			ETTaskCompletionSource<bool> tcs = new ETTaskCompletionSource<bool>();
+			OnceWaitTimer timer = EntityFactory.CreateWithParent<OnceWaitTimer, ETTaskCompletionSource<bool>>(this, tcs);
 			this.timers[timer.Id] = timer;
 			AddToTimeId(tillTime, timer.Id);
-			return tcs.Task;
+			return await tcs.Task;
 		}
 
 		/// <summary>

+ 4 - 1
Unity/Assets/Model/Module/Message/SessionIdleCheckComponent.cs

@@ -21,7 +21,10 @@
             RepeatedTimer repeatedTimer = TimerComponent.Instance.GetRepeatedTimer(self.RepeatedTimer);
             if (repeatedTimer != null)
             {
-                repeatedTimer.Callback = self.Check;
+                repeatedTimer.Callback = (isTimeout) =>
+                {
+                    self.Check();
+                };
             }
         }
     }

+ 8 - 36
Unity/Assets/Model/Other/AppType.cs

@@ -6,43 +6,15 @@ namespace ET
 	[Flags]
 	public enum AppType
 	{
-		None = 0,
 		Manager = 1,
-		Realm = 1 << 1,
-		Gate = 1 << 2,
-		Http = 1 << 3,
-		DB = 1 << 4,
-		Location = 1 << 5,
-		Map = 1 << 6,
+		Realm = 2,
+		Gate = 3,
+		Http = 4,
+		DB = 5,
+		Location = 6,
+		Map = 7,
 
-		BenchmarkWebsocketServer = 1 << 26,
-		BenchmarkWebsocketClient = 1 << 27,
-		Robot = 1 << 28,
-		Benchmark = 1 << 29,
-		// 客户端Hotfix层
-		ClientH = 1 << 30,
-		// 客户端Model层
-		ClientM = 1 << 31,
-
-		// 7
-		AllServer = Manager | Realm | Gate | Http | DB | Location | Map | BenchmarkWebsocketServer
-	}
-
-	public static class AppTypeHelper
-	{
-		public static List<AppType> GetServerTypes()
-		{
-			List<AppType> appTypes = new List<AppType> { AppType.Manager, AppType.Realm, AppType.Gate };
-			return appTypes;
-		}
-
-		public static bool Is(this AppType a, AppType b)
-		{
-			if ((a & b) != 0)
-			{
-				return true;
-			}
-			return false;
-		}
+		Robot = 20,
+		Benchmark = 21,
 	}
 }

+ 1 - 0
Unity/Unity.HotfixView.csproj

@@ -74,6 +74,7 @@
      <Compile Include="Assets\HotfixView\Demo\Unit\AfterUnitCreate_CreateUnitView.cs" />
      <Compile Include="Assets\HotfixView\Helper\ActionHelper.cs" />
      <Compile Include="Assets\HotfixView\Helper\GameObjectHelper.cs" />
+     <Compile Include="Assets\HotfixView\Helper\ResourcesHelper.cs" />
      <Compile Include="Assets\HotfixView\Init.cs" />
      <None Include="Assets\HotfixView\Unity.HotfixView.asmdef" />
  <Reference Include="UnityEditor.UI">

+ 0 - 1
Unity/Unity.Model.csproj

@@ -161,7 +161,6 @@
      <Compile Include="Assets\Model\Helper\BundleHelper.cs" />
      <Compile Include="Assets\Model\Helper\PathHelper.cs" />
      <Compile Include="Assets\Model\Helper\PositionHelper.cs" />
-     <Compile Include="Assets\Model\Helper\ResourcesHelper.cs" />
      <Compile Include="Assets\Model\Module\Actor\IActorMessage.cs" />
      <Compile Include="Assets\Model\Module\ActorLocation\IActorLocationMessage.cs" />
      <Compile Include="Assets\Model\Module\AssetsBundle\AssetsBundleLoaderAsync.cs" />

+ 3 - 0
Unity/Unity.ModelView.csproj

@@ -668,6 +668,9 @@
       <Name>Unity.ThirdParty</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Assets\ModelView\Helper" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.