From 885936f7dd98d92983b85334c6acb57cbc94d635 Mon Sep 17 00:00:00 2001 From: tanghai Date: Sat, 28 Feb 2015 17:58:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0IRunner=E6=8E=A5=E5=8F=A3,Wor?= =?UTF-8?q?ld=E7=9A=84Componet=E5=AE=9E=E7=8E=B0=E8=AF=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BC=9A=E6=AF=8F=E5=B8=A7=E8=B0=83=E7=94=A8Run?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSharp/CSharp.sln | 3 -- CSharp/Game/Controller/Factory/UnitFactory.cs | 2 +- CSharp/Game/Model/Component/ActorComponent.cs | 11 +++-- .../Game/Model/Component/NetworkComponent.cs | 7 +++- CSharp/Game/Model/Component/TimerComponent.cs | 4 +- CSharp/Game/Model/IAssemblyLoader.cs | 3 ++ CSharp/Game/Model/IRunner.cs | 10 +++++ CSharp/Game/Model/Model.csproj | 1 + CSharp/Game/Model/World.cs | 41 ++++++++++++++++--- CSharp/Platform/Common/Network/IService.cs | 6 +-- CSharp/Platform/ENet/ENet.vcxproj | 5 ++- CSharp/Platform/TNet/IPoller.cs | 2 +- CSharp/Platform/TNet/TPoller.cs | 2 +- CSharp/Platform/TNet/TService.cs | 26 +++--------- CSharp/Platform/UNet/UService.cs | 19 +-------- CSharp/Test/TNetTest/TServiceTest.cs | 24 +++++++++-- CSharp/Test/UNetTest/UServiceTest.cs | 24 +++++++++-- 17 files changed, 118 insertions(+), 72 deletions(-) create mode 100644 CSharp/Game/Model/IRunner.cs diff --git a/CSharp/CSharp.sln b/CSharp/CSharp.sln index 5fdb01bf..30aba0b7 100644 --- a/CSharp/CSharp.sln +++ b/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 diff --git a/CSharp/Game/Controller/Factory/UnitFactory.cs b/CSharp/Game/Controller/Factory/UnitFactory.cs index b7657147..ae15f05a 100644 --- a/CSharp/Game/Controller/Factory/UnitFactory.cs +++ b/CSharp/Game/Controller/Factory/UnitFactory.cs @@ -9,7 +9,7 @@ namespace Controller { Unit player = new Unit(configId); player.AddComponent(); - player.AddComponent().Run(); + player.AddComponent(); World.Instance.GetComponent().Add(player); return player; } diff --git a/CSharp/Game/Model/Component/ActorComponent.cs b/CSharp/Game/Model/Component/ActorComponent.cs index 92147970..031dd41f 100644 --- a/CSharp/Game/Model/Component/ActorComponent.cs +++ b/CSharp/Game/Model/Component/ActorComponent.cs @@ -11,11 +11,16 @@ namespace Model { private readonly Queue msgEnvQueue = new Queue(); - 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) { diff --git a/CSharp/Game/Model/Component/NetworkComponent.cs b/CSharp/Game/Model/Component/NetworkComponent.cs index b29d7903..19ac313a 100644 --- a/CSharp/Game/Model/Component/NetworkComponent.cs +++ b/CSharp/Game/Model/Component/NetworkComponent.cs @@ -7,7 +7,7 @@ using UNet; namespace Model { - public class NetworkComponent: Component + public class NetworkComponent: Component, IRunner { private IService service; @@ -26,8 +26,11 @@ namespace Model } this.service.Add(this.AcceptChannel); + } - this.service.Start(); + public void Run() + { + this.service.Run(); } /// diff --git a/CSharp/Game/Model/Component/TimerComponent.cs b/CSharp/Game/Model/Component/TimerComponent.cs index 7a4ecc0d..67b1ec3c 100644 --- a/CSharp/Game/Model/Component/TimerComponent.cs +++ b/CSharp/Game/Model/Component/TimerComponent.cs @@ -6,7 +6,7 @@ using MongoDB.Bson; namespace Model { - public class TimerComponent: Component + public class TimerComponent : Component, 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) diff --git a/CSharp/Game/Model/IAssemblyLoader.cs b/CSharp/Game/Model/IAssemblyLoader.cs index 75e2800f..12cab00e 100644 --- a/CSharp/Game/Model/IAssemblyLoader.cs +++ b/CSharp/Game/Model/IAssemblyLoader.cs @@ -2,6 +2,9 @@ namespace Model { + /// + /// World的Componet实现该接口,World.Load会调用Load方法 + /// public interface IAssemblyLoader { void Load(Assembly assembly); diff --git a/CSharp/Game/Model/IRunner.cs b/CSharp/Game/Model/IRunner.cs new file mode 100644 index 00000000..fb2b12ad --- /dev/null +++ b/CSharp/Game/Model/IRunner.cs @@ -0,0 +1,10 @@ +namespace Model +{ + /// + /// 实现了该接口的World Componet会每帧刷新 + /// + public interface IRunner + { + void Run(); + } +} diff --git a/CSharp/Game/Model/Model.csproj b/CSharp/Game/Model/Model.csproj index c14df520..398d12db 100644 --- a/CSharp/Game/Model/Model.csproj +++ b/CSharp/Game/Model/Model.csproj @@ -67,6 +67,7 @@ + diff --git a/CSharp/Game/Model/World.cs b/CSharp/Game/Model/World.cs index c2b51d67..1872d9da 100644 --- a/CSharp/Game/Model/World.cs +++ b/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 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(); foreach (Component 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; + } } } \ No newline at end of file diff --git a/CSharp/Platform/Common/Network/IService.cs b/CSharp/Platform/Common/Network/IService.cs index 27546c23..5b27ad56 100644 --- a/CSharp/Platform/Common/Network/IService.cs +++ b/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(); } } \ No newline at end of file diff --git a/CSharp/Platform/ENet/ENet.vcxproj b/CSharp/Platform/ENet/ENet.vcxproj index 63d07ee4..cf0ddd6a 100644 --- a/CSharp/Platform/ENet/ENet.vcxproj +++ b/CSharp/Platform/ENet/ENet.vcxproj @@ -73,8 +73,8 @@ true $(SolutionDir)\Platform\;$(IncludePath) - $(SolutionDir)\Bin\Debug\ - $(SolutionDir)\Temp\Debug\ + $(SolutionDir)\Bin\$(Configuration)\ + $(SolutionDir)\Temp\$(Configuration)\ .dll $(ProjectName) @@ -84,6 +84,7 @@ true $(SolutionDir)\Platform\;$(IncludePath) $(SolutionDir)\Bin\$(Configuration)\ + $(SolutionDir)\Temp\$(Configuration)\ false diff --git a/CSharp/Platform/TNet/IPoller.cs b/CSharp/Platform/TNet/IPoller.cs index 8b885ebd..02dc443e 100644 --- a/CSharp/Platform/TNet/IPoller.cs +++ b/CSharp/Platform/TNet/IPoller.cs @@ -6,6 +6,6 @@ namespace TNet { void Add(Action action); - void Run(int timeout); + void Run(); } } \ No newline at end of file diff --git a/CSharp/Platform/TNet/TPoller.cs b/CSharp/Platform/TNet/TPoller.cs index 49c8c110..3bf379ac 100644 --- a/CSharp/Platform/TNet/TPoller.cs +++ b/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) { diff --git a/CSharp/Platform/TNet/TService.cs b/CSharp/Platform/TNet/TService.cs index 182dc2e0..508ec2d2 100644 --- a/CSharp/Platform/TNet/TService.cs +++ b/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; - /// /// 即可做client也可做server /// @@ -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 diff --git a/CSharp/Platform/UNet/UService.cs b/CSharp/Platform/UNet/UService.cs index f9e3e7f0..5adc887e 100644 --- a/CSharp/Platform/UNet/UService.cs +++ b/CSharp/Platform/UNet/UService.cs @@ -15,8 +15,6 @@ namespace UNet private readonly Dictionary idChannels = new Dictionary(); - private bool isStop; - /// /// 即可做client也可做server /// @@ -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(); } } } \ No newline at end of file diff --git a/CSharp/Test/TNetTest/TServiceTest.cs b/CSharp/Test/TNetTest/TServiceTest.cs index 77c33614..553376c7 100644 --- a/CSharp/Test/TNetTest/TServiceTest.cs +++ b/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); } } diff --git a/CSharp/Test/UNetTest/UServiceTest.cs b/CSharp/Test/UNetTest/UServiceTest.cs index deb325e8..d092e962 100644 --- a/CSharp/Test/UNetTest/UServiceTest.cs +++ b/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); } } -- GitLab