提交 885936f7 编写于 作者: T tanghai

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

上级 28af0327
......@@ -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
......@@ -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;
}
......
......@@ -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)
{
......
......@@ -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>
......
......@@ -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)
......
......@@ -2,6 +2,9 @@
namespace Model
{
/// <summary>
/// World的Componet实现该接口,World.Load会调用Load方法
/// </summary>
public interface IAssemblyLoader
{
void Load(Assembly assembly);
......
namespace Model
{
/// <summary>
/// 实现了该接口的World Componet会每帧刷新
/// </summary>
public interface IRunner
{
void Run();
}
}
......@@ -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" />
......
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)
{
continue;
this.iRunners.Add(runner);
}
}
assemblyLoader.Load(this.Assembly);
}
public void Start()
{
while (!isStop)
{
Thread.Sleep(1);
foreach (IRunner runner in this.iRunners)
{
runner.Run();
}
}
}
public void Stop()
{
isStop = true;
}
}
}
\ No newline at end of file
......@@ -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
......@@ -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>
......
......@@ -6,6 +6,6 @@ namespace TNet
{
void Add(Action action);
void Run(int timeout);
void Run();
}
}
\ No newline at end of file
......@@ -16,7 +16,7 @@ namespace TNet
this.concurrentQueue.Enqueue(action);
}
public void Run(int timeout)
public void Run()
{
while (true)
{
......
......@@ -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,24 +127,11 @@ namespace TNet
return await this.ConnectAsync(host, port);
}
public void RunOnce(int timeout)
{
this.poller.Run(timeout);
}
public void Start()
{
while (!isStop)
public void Run()
{
this.RunOnce(0);
this.poller.Run();
this.timerManager.Refresh();
}
}
public void Stop()
{
this.isStop = true;
}
internal TimerManager Timer
{
......
......@@ -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)
public void Run()
{
this.poller.RunOnce();
}
}
public void Stop()
{
this.isStop = true;
}
}
}
\ No newline at end of file
......@@ -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);
}
}
......
......@@ -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);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册