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

增加IRunner接口,World的Componet实现该接口会每帧调用Run方法

tanghai 11 лет назад
Родитель
Сommit
885936f7dd

+ 0 - 3
CSharp/CSharp.sln

@@ -136,7 +136,4 @@ Global
 		{1888D319-0495-43F3-BA8D-163EC20D9437} = {F13D0B3D-5B4F-452A-9378-0FD39555371D}
 		{47A7404D-F501-43C5-8183-4B4E9E8C24B2} = {F13D0B3D-5B4F-452A-9378-0FD39555371D}
 	EndGlobalSection
-	GlobalSection(Performance) = preSolution
-		HasPerformanceSessions = true
-	EndGlobalSection
 EndGlobal

+ 1 - 1
CSharp/Game/Controller/Factory/UnitFactory.cs

@@ -9,7 +9,7 @@ namespace Controller
 		{
 			Unit player = new Unit(configId);
 			player.AddComponent<BuffComponent>();
-			player.AddComponent<ActorComponent>().Run();
+			player.AddComponent<ActorComponent>();
 			World.Instance.GetComponent<UnitComponent>().Add(player);
 			return player;
 		}

+ 8 - 3
CSharp/Game/Model/Component/ActorComponent.cs

@@ -11,11 +11,16 @@ namespace Model
 	{
 		private readonly Queue<Env> msgEnvQueue = new Queue<Env>();
 
-		public Action msgAction = () => {};
+		private Action msgAction = () => {};
 
-		public Env Env { get; private set; }
+		private Env Env { get; set; }
 
-		public async void Run()
+		public ActorComponent()
+		{
+			Start();
+		}
+
+		private async void Start()
 		{
 			while (true)
 			{

+ 5 - 2
CSharp/Game/Model/Component/NetworkComponent.cs

@@ -7,7 +7,7 @@ using UNet;
 
 namespace Model
 {
-	public class NetworkComponent: Component<World>
+	public class NetworkComponent: Component<World>, IRunner
 	{
 		private IService service;
 
@@ -26,8 +26,11 @@ namespace Model
 			}
 
 			this.service.Add(this.AcceptChannel);
+		}
 
-			this.service.Start();
+		public void Run()
+		{
+			this.service.Run();
 		}
 
 		/// <summary>

+ 2 - 2
CSharp/Game/Model/Component/TimerComponent.cs

@@ -6,7 +6,7 @@ using MongoDB.Bson;
 
 namespace Model
 {
-	public class TimerComponent: Component<World>
+	public class TimerComponent : Component<World>, IRunner
 	{
 		private class Timer
 		{
@@ -49,7 +49,7 @@ namespace Model
 			this.timeId.Remove(timer.Time, timer.Id);
 		}
 
-		public void Update()
+		public void Run()
 		{
 			long timeNow = TimeHelper.Now();
 			foreach (long time in this.timeId.Keys)

+ 3 - 0
CSharp/Game/Model/IAssemblyLoader.cs

@@ -2,6 +2,9 @@
 
 namespace Model
 {
+	/// <summary>
+	/// World的Componet实现该接口,World.Load会调用Load方法
+	/// </summary>
 	public interface IAssemblyLoader
 	{
 		void Load(Assembly assembly);

+ 10 - 0
CSharp/Game/Model/IRunner.cs

@@ -0,0 +1,10 @@
+namespace Model
+{
+	/// <summary>
+	/// 实现了该接口的World Componet会每帧刷新
+	/// </summary>
+	public interface IRunner
+	{
+		void Run();
+	}
+}

+ 1 - 0
CSharp/Game/Model/Model.csproj

@@ -67,6 +67,7 @@
     <Compile Include="FactoryAttribute.cs" />
     <Compile Include="IAssemblyLoader.cs" />
     <Compile Include="IFactory.cs" />
+    <Compile Include="IRunner.cs" />
     <Compile Include="MessageAttribute.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Unit.cs" />

+ 35 - 6
CSharp/Game/Model/World.cs

@@ -1,5 +1,7 @@
-using System.IO;
+using System.Collections.Generic;
+using System.IO;
 using System.Reflection;
+using System.Threading;
 using Common.Base;
 
 namespace Model
@@ -8,7 +10,7 @@ namespace Model
 	{
 		private static readonly World instance = new World();
 
-		public Assembly Assembly { get; set; }
+		private Assembly assembly;
 
 		public static World Instance
 		{
@@ -18,23 +20,50 @@ namespace Model
 			}
 		}
 
+		private List<IRunner> iRunners;
+
+		private bool isStop;
+
 		private World()
 		{
 		}
 
 		public void Load()
 		{
-			this.Assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll"));
+			this.assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll"));
+			this.iRunners = new List<IRunner>();
 
 			foreach (Component<World> component in this.GetComponents())
 			{
 				IAssemblyLoader assemblyLoader = component as IAssemblyLoader;
-				if (assemblyLoader == null)
+				if (assemblyLoader != null)
+				{
+					assemblyLoader.Load(this.assembly);
+				}
+				
+				IRunner runner = component as IRunner;
+				if (runner != null)
+				{
+					this.iRunners.Add(runner);
+				}
+			}
+		}
+
+		public void Start()
+		{
+			while (!isStop)
+			{
+				Thread.Sleep(1);
+				foreach (IRunner runner in this.iRunners)
 				{
-					continue;
+					runner.Run();
 				}
-				assemblyLoader.Load(this.Assembly);
 			}
 		}
+
+		public void Stop()
+		{
+			isStop = true;
+		}
 	}
 }

+ 1 - 5
CSharp/Platform/Common/Network/IService.cs

@@ -26,10 +26,6 @@ namespace Common.Network
 
 		void Remove(AChannel channel);
 
-		void RunOnce(int timeout);
-
-		void Start();
-
-		void Stop();
+		void Run();
 	}
 }

+ 3 - 2
CSharp/Platform/ENet/ENet.vcxproj

@@ -73,8 +73,8 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <LinkIncremental>true</LinkIncremental>
     <IncludePath>$(SolutionDir)\Platform\;$(IncludePath)</IncludePath>
-    <OutDir>$(SolutionDir)\Bin\Debug\</OutDir>
-    <IntDir>$(SolutionDir)\Temp\Debug\</IntDir>
+    <OutDir>$(SolutionDir)\Bin\$(Configuration)\</OutDir>
+    <IntDir>$(SolutionDir)\Temp\$(Configuration)\</IntDir>
     <TargetExt>.dll</TargetExt>
     <TargetName>$(ProjectName)</TargetName>
   </PropertyGroup>
@@ -84,6 +84,7 @@
     <LinkIncremental>true</LinkIncremental>
     <IncludePath>$(SolutionDir)\Platform\;$(IncludePath)</IncludePath>
     <OutDir>$(SolutionDir)\Bin\$(Configuration)\</OutDir>
+    <IntDir>$(SolutionDir)\Temp\$(Configuration)\</IntDir>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     <LinkIncremental>false</LinkIncremental>

+ 1 - 1
CSharp/Platform/TNet/IPoller.cs

@@ -6,6 +6,6 @@ namespace TNet
 	{
 		void Add(Action action);
 
-		void Run(int timeout);
+		void Run();
 	}
 }

+ 1 - 1
CSharp/Platform/TNet/TPoller.cs

@@ -16,7 +16,7 @@ namespace TNet
 			this.concurrentQueue.Enqueue(action);
 		}
 
-		public void Run(int timeout)
+		public void Run()
 		{
 			while (true)
 			{

+ 5 - 21
CSharp/Platform/TNet/TService.cs

@@ -8,7 +8,7 @@ using MongoDB.Bson;
 
 namespace TNet
 {
-	public class TService: IService
+	public sealed class TService: IService
 	{
 		private readonly IPoller poller = new TPoller();
 		private TSocket acceptor;
@@ -19,8 +19,6 @@ namespace TNet
 
 		private readonly TimerManager timerManager = new TimerManager();
 
-		private bool isStop;
-
 		/// <summary>
 		/// 即可做client也可做server
 		/// </summary>
@@ -40,7 +38,7 @@ namespace TNet
 		{
 		}
 
-		protected virtual void Dispose(bool disposing)
+		private void Dispose(bool disposing)
 		{
 			if (this.acceptor == null)
 			{
@@ -57,7 +55,6 @@ namespace TNet
 				this.acceptor.Dispose();
 			}
 
-			isStop = true;
 			this.acceptor = null;
 		}
 
@@ -130,23 +127,10 @@ namespace TNet
 			return await this.ConnectAsync(host, port);
 		}
 
-		public void RunOnce(int timeout)
-		{
-			this.poller.Run(timeout);
-		}
-
-		public void Start()
-		{
-			while (!isStop)
-			{
-				this.RunOnce(0);
-				this.timerManager.Refresh();
-			}
-		}
-
-		public void Stop()
+		public void Run()
 		{
-			this.isStop = true;
+			this.poller.Run();
+			this.timerManager.Refresh();
 		}
 
 		internal TimerManager Timer

+ 2 - 17
CSharp/Platform/UNet/UService.cs

@@ -15,8 +15,6 @@ namespace UNet
 
 		private readonly Dictionary<ObjectId, UChannel> idChannels = new Dictionary<ObjectId, UChannel>();
 
-		private bool isStop;
-
 		/// <summary>
 		/// 即可做client也可做server
 		/// </summary>
@@ -117,22 +115,9 @@ namespace UNet
 			this.channels.Remove(channel.RemoteAddress);
 		}
 
-		public void RunOnce(int timeout)
-		{
-			this.poller.RunOnce(timeout);
-		}
-
-		public void Start()
-		{
-			while (!isStop)
-			{
-				this.poller.RunOnce();
-			}
-		}
-
-		public void Stop()
+		public void Run()
 		{
-			this.isStop = true;
+			this.poller.RunOnce();
 		}
 	}
 }

+ 20 - 4
CSharp/Test/TNetTest/TServiceTest.cs

@@ -14,6 +14,9 @@ namespace TNetTest
 		private const int echoTimes = 10000;
 		private readonly Barrier barrier = new Barrier(3);
 
+		private bool isClientStop;
+		private bool isServerStop;
+
 		private async void ClientEvent(IService service, string hostName, ushort port)
 		{
 			AChannel channel = await service.GetChannel(hostName, port);
@@ -49,8 +52,21 @@ namespace TNetTest
 			using(IService clientService = new TService())
 			using (IService serverService = new TService(hostName, 8889))
 			{
-				Task task1 = Task.Factory.StartNew(() => clientService.Start(), TaskCreationOptions.LongRunning);
-				Task task2 = Task.Factory.StartNew(() => serverService.Start(), TaskCreationOptions.LongRunning);
+				Task task1 = Task.Factory.StartNew(() =>
+				{
+					while (!isClientStop)
+					{
+						clientService.Run();
+					}
+				}, TaskCreationOptions.LongRunning);
+
+				Task task2 = Task.Factory.StartNew(() =>
+				{
+					while (!isServerStop)
+					{
+						serverService.Run();
+					}
+				}, TaskCreationOptions.LongRunning);
 
 				// 往server host线程增加事件,accept
 				serverService.Add(() => this.ServerEvent(serverService));
@@ -62,8 +78,8 @@ namespace TNetTest
 
 				this.barrier.SignalAndWait();
 
-				serverService.Add(serverService.Stop);
-				clientService.Add(clientService.Stop);
+				serverService.Add(() => { isServerStop = true; });
+				clientService.Add(() => { isClientStop = true; });
 				Task.WaitAll(task1, task2);
 			}
 		}

+ 20 - 4
CSharp/Test/UNetTest/UServiceTest.cs

@@ -15,6 +15,9 @@ namespace UNetTest
 		private const int echoTimes = 10000;
 		private readonly Barrier barrier = new Barrier(2);
 
+		private bool isClientStop;
+		private bool isServerStop;
+
 		private async void ClientEvent(IService clientService, string hostName, ushort port)
 		{
 			AChannel channel = await clientService.GetChannel(hostName, port);
@@ -48,8 +51,21 @@ namespace UNetTest
 			using (IService clientService = new UService(hostName, 8888))
 			using (IService serverService = new UService(hostName, 8889))
 			{	
-				Task task1 = Task.Factory.StartNew(() => clientService.Start(), TaskCreationOptions.LongRunning);
-				Task task2 = Task.Factory.StartNew(() => serverService.Start(), TaskCreationOptions.LongRunning);
+				Task task1 = Task.Factory.StartNew(() =>
+				{
+					while (!isClientStop)
+					{
+						clientService.Run();
+					}
+				}, TaskCreationOptions.LongRunning);
+
+				Task task2 = Task.Factory.StartNew(() =>
+				{
+					while (!isServerStop)
+					{
+						serverService.Run();
+					}
+				}, TaskCreationOptions.LongRunning);
 
 				// 往server host线程增加事件,accept
 				serverService.Add(() => this.ServerEvent(serverService));
@@ -60,8 +76,8 @@ namespace UNetTest
 				clientService.Add(() => this.ClientEvent(clientService, hostName, port));
 				barrier.SignalAndWait();
 
-				serverService.Add(serverService.Stop);
-				clientService.Add(clientService.Stop);
+				serverService.Add(() => { isServerStop = true; });
+				clientService.Add(() => { isClientStop = true; });
 				Task.WaitAll(task1, task2);
 			}
 		}