diff --git a/CSharp/CSharp.sln b/CSharp/CSharp.sln index 5fdb01bfecd4f3b53d19bb651764cecefa01a6a6..30aba0b70e324c0051e1d00efe33f1627cbf9c73 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 b7657147bb402c2f9104ca14f63f36c00b8197da..ae15f05a2130400af99661f2125a58452367c668 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 92147970acd62d4c40c8148a7839eabf6f1fff1a..031dd41faa6b841bc537306a255f12314203f8d7 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 b29d79035223d2834ad56f20932f7b0cd3489150..19ac313ac54c29661984298cc1051bad4e09ebc6 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 7a4ecc0d2fa736c7e57b1e9d6a51a9269b50f2fd..67b1ec3c98c8209f0d50c302702da7e8a9e72dd2 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 75e2800fcc5f1835b5df0f40c46f7a9b79675ecc..12cab00e96005196274a0e4b29e268afe7b9075c 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 0000000000000000000000000000000000000000..fb2b12ad0f02503b195e228d464c7d1e07d3d475 --- /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 c14df52022ab3d15c94e9f2a5003966c4aa31fe4..398d12db7d77186435eba883a125ee93ce8ba268 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 c2b51d6774e31abb846742daaf328e5be522c9cb..1872d9da6250194d1f3583b35c1a81d844a7aa81 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 27546c235d75315ec27b63715bd8000dd4168df8..5b27ad5669d94b26354dfab3a084573a66e1df6e 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 63d07ee4f64c027bdf24dda1d4919653d9580d79..cf0ddd6a4e329063f670540a41d070cc6d81afe3 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 8b885ebd1c3b5a2d08b5226e2aaf4cedc6f2e1ff..02dc443ee49ec10fe059c5677a1733ab58702552 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 49c8c110b85b097b91760d83df4af2948919359d..3bf379ac917708f2dc5ad214fdca7effa951cd9a 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 182dc2e04621576a9854d595ae60b98f174e8f8a..508ec2d2460da905be49766ac3115f7c8ee0a9e9 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 f9e3e7f0da72b05ce660ff47647870647673278b..5adc887e19ede1e5ffcc85a724bceb34d73e9945 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 77c33614d037e90aaf8c27af9e500cfe85fb906f..553376c722e1463f569f5513043786b705bacfd8 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 deb325e8f497d7ecde34397093a2657f556cf6b4..d092e962a65f672d95383319a891bc486e25ef32 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); } }