提交 6d169497 编写于 作者: T tanghai

1.增加一个nuget命令行解析库

2.World componet增加一个IStart接口,在World Start时调用
3.Runner改成Update
上级 885936f7
<Properties StartupItem="Exe\Profile\Profile.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Exe\Profile\Program.cs">
<Files>
<File FileName="Exe\Profile\App.config" Line="1" Column="1" />
<File FileName="Exe\Profile\Program.cs" Line="3" Column="3" />
</Files>
<Pads>
<Pad Id="MonoDevelop.NUnit.TestPad">
<State expanded="True" selected="True" />
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workbench />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
......
......@@ -7,11 +7,11 @@ using UNet;
namespace Model
{
public class NetworkComponent: Component<World>, IRunner
public class NetworkComponent: Component<World>, IUpdate, IStart
{
private IService service;
public void Run(string host, int port, NetworkProtocol protocol = NetworkProtocol.TCP)
private void Accept(string host, int port, NetworkProtocol protocol = NetworkProtocol.TCP)
{
switch (protocol)
{
......@@ -25,12 +25,17 @@ namespace Model
throw new ArgumentOutOfRangeException("protocol");
}
this.service.Add(this.AcceptChannel);
this.AcceptChannel();
}
public void Run()
public void Start()
{
this.service.Run();
this.Accept(World.Instance.Options.Host, World.Instance.Options.Port, World.Instance.Options.Protocol);
}
public void Update()
{
this.service.Update();
}
/// <summary>
......
......@@ -6,7 +6,7 @@ using MongoDB.Bson;
namespace Model
{
public class TimerComponent : Component<World>, IRunner
public class TimerComponent : Component<World>, IUpdate
{
private class Timer
{
......@@ -49,7 +49,7 @@ namespace Model
this.timeId.Remove(timer.Time, timer.Id);
}
public void Run()
public void Update()
{
long timeNow = TimeHelper.Now();
foreach (long time in this.timeId.Keys)
......
namespace Model
{
/// <summary>
/// World的Componet实现该接口后,会在World.Start时调用该Start方法
/// </summary>
interface IStart
{
void Start();
}
}
......@@ -3,8 +3,8 @@
/// <summary>
/// 实现了该接口的World Componet会每帧刷新
/// </summary>
public interface IRunner
public interface IUpdate
{
void Run();
void Update();
}
}
......@@ -30,6 +30,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommandLine">
<HintPath>..\..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Bson">
<HintPath>..\..\packages\mongocsharpdriver.1.10.0-rc1\lib\net35\MongoDB.Bson.dll</HintPath>
</Reference>
......@@ -67,8 +70,10 @@
<Compile Include="FactoryAttribute.cs" />
<Compile Include="IAssemblyLoader.cs" />
<Compile Include="IFactory.cs" />
<Compile Include="IRunner.cs" />
<Compile Include="IStart.cs" />
<Compile Include="IUpdate.cs" />
<Compile Include="MessageAttribute.cs" />
<Compile Include="Options.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Unit.cs" />
<Compile Include="UnitType.cs" />
......
using System.Text;
using CommandLine;
using Common.Network;
namespace Common.Base
{
public class Options
{
[Option('h', "host", Required = true, HelpText = "Host.")]
public string Host { get; set; }
[Option('p', "port", Required = true, HelpText = "Port.")]
public int Port { get; set; }
[Option("protocol", Required = true, HelpText = "Protocol, tcp or udp.")]
public NetworkProtocol Protocol { get; set; }
[Option('v', null, HelpText = "Print details during execution.")]
public bool Verbose { get; set; }
[HelpOption]
public string GetUsage()
{
// this without using CommandLine.Text
StringBuilder usage = new StringBuilder();
usage.AppendLine("Quickstart Application 1.0");
usage.AppendLine("Read user manual for usage instructions...");
return usage.ToString();
}
}
}
......@@ -20,7 +20,9 @@ namespace Model
}
}
private List<IRunner> iRunners;
public Options Options { get; set; }
private readonly List<IUpdate> iUpdates = new List<IUpdate>();
private bool isStop;
......@@ -31,7 +33,6 @@ namespace Model
public void Load()
{
this.assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll"));
this.iRunners = new List<IRunner>();
foreach (Component<World> component in this.GetComponents())
{
......@@ -40,23 +41,35 @@ namespace Model
{
assemblyLoader.Load(this.assembly);
}
IRunner runner = component as IRunner;
if (runner != null)
{
this.iRunners.Add(runner);
}
}
}
public void Start()
{
Load();
foreach (Component<World> component in this.GetComponents())
{
IUpdate update = component as IUpdate;
if (update != null)
{
this.iUpdates.Add(update);
}
IStart start = component as IStart;
if (start != null)
{
start.Start();
}
}
// loop
while (!isStop)
{
Thread.Sleep(1);
foreach (IRunner runner in this.iRunners)
foreach (IUpdate update in this.iUpdates)
{
runner.Run();
update.Update();
}
}
}
......
......@@ -31,19 +31,6 @@ namespace Common.Base
return timer.Id;
}
public void Update(ObjectId id, long time)
{
Timer timer;
if (!this.timers.TryGetValue(id, out timer))
{
return;
}
this.timeGuid.Remove(timer.Time, timer.Id);
timer.Time = time;
this.timeGuid.Add(timer.Time, timer.Id);
}
public void Remove(ObjectId id)
{
Timer timer;
......
......@@ -26,6 +26,6 @@ namespace Common.Network
void Remove(AChannel channel);
void Run();
void Update();
}
}
\ No newline at end of file
......@@ -6,6 +6,6 @@ namespace TNet
{
void Add(Action action);
void Run();
void Update();
}
}
\ No newline at end of file
......@@ -32,7 +32,7 @@ namespace TNet
this.StartRecv();
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (this.socket == null)
{
......
......@@ -16,7 +16,7 @@ namespace TNet
this.concurrentQueue.Enqueue(action);
}
public void Run()
public void Update()
{
while (true)
{
......
......@@ -127,9 +127,9 @@ namespace TNet
return await this.ConnectAsync(host, port);
}
public void Run()
public void Update()
{
this.poller.Run();
this.poller.Update();
this.timerManager.Refresh();
}
......
......@@ -4,7 +4,7 @@
{
public static void Initialize()
{
int ret = NativeMethods.EnetInitialize();
int ret = NativeMethods.ENetInitialize();
if (ret < 0)
{
throw new UException(string.Format("Initialization failed, ret: {0}", ret));
......@@ -13,18 +13,18 @@
public static void Deinitialize()
{
NativeMethods.EnetDeinitialize();
NativeMethods.ENetDeinitialize();
}
public static uint Time
{
get
{
return NativeMethods.EnetTimeGet();
return NativeMethods.ENetTimeGet();
}
set
{
NativeMethods.EnetTimeSet(value);
NativeMethods.ENetTimeSet(value);
}
}
}
......
......@@ -20,102 +20,102 @@ namespace UNet
public const uint ENET_HOST_BROADCAST = 0xffffffff;
[DllImport(LIB, EntryPoint = "enet_address_set_host")]
internal static extern int EnetAddressSetHost(ref ENetAddress address, string hostName);
internal static extern int ENetAddressSetHost(ref ENetAddress address, string hostName);
[DllImport(LIB, EntryPoint = "enet_address_get_host")]
internal static extern int EnetAddressGetHost(
internal static extern int ENetAddressGetHost(
ref ENetAddress address, StringBuilder hostName, uint nameLength);
[DllImport(LIB, EntryPoint = "enet_address_get_host_ip")]
internal static extern int EnetAddressGetHostIp(
internal static extern int ENetAddressGetHostIp(
ref ENetAddress address, StringBuilder hostIp, uint ipLength);
[DllImport(LIB, EntryPoint = "enet_deinitialize")]
internal static extern void EnetDeinitialize();
internal static extern void ENetDeinitialize();
[DllImport(LIB, EntryPoint = "enet_initialize")]
internal static extern int EnetInitialize();
internal static extern int ENetInitialize();
[DllImport(LIB, EntryPoint = "enet_host_create")]
internal static extern IntPtr EnetHostCreate(
internal static extern IntPtr ENetHostCreate(
ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
uint outgoingBandwidth);
[DllImport(LIB, EntryPoint = "enet_host_create")]
internal static extern IntPtr EnetHostCreate(
internal static extern IntPtr ENetHostCreate(
IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
uint outgoingBandwidth);
[DllImport(LIB, EntryPoint = "enet_host_destroy")]
internal static extern void EnetHostDestroy(IntPtr host);
internal static extern void ENetHostDestroy(IntPtr host);
[DllImport(LIB, EntryPoint = "enet_host_connect")]
internal static extern IntPtr EnetHostConnect(
internal static extern IntPtr ENetHostConnect(
IntPtr host, ref ENetAddress address, uint channelCount, uint data);
[DllImport(LIB, EntryPoint = "enet_host_broadcast")]
internal static extern void EnetHostBroadcast(IntPtr host, byte channelID, IntPtr packet);
internal static extern void ENetHostBroadcast(IntPtr host, byte channelID, IntPtr packet);
[DllImport(LIB, EntryPoint = "enet_host_compress")]
internal static extern void EnetHostCompress(IntPtr host, IntPtr compressor);
internal static extern void ENetHostCompress(IntPtr host, IntPtr compressor);
[DllImport(LIB, EntryPoint = "enet_host_compress_with_range_coder")]
internal static extern int EnetHostCompressWithRangeCoder(IntPtr host);
internal static extern int ENetHostCompressWithRangeCoder(IntPtr host);
[DllImport(LIB, EntryPoint = "enet_host_channel_limit")]
internal static extern void EnetHostChannelLimit(IntPtr host, uint channelLimit);
internal static extern void ENetHostChannelLimit(IntPtr host, uint channelLimit);
[DllImport(LIB, EntryPoint = "enet_host_bandwidth_limit")]
internal static extern void EnetHostBandwidthLimit(
internal static extern void ENetHostBandwidthLimit(
IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
[DllImport(LIB, EntryPoint = "enet_host_flush")]
internal static extern void EnetHostFlush(IntPtr host);
internal static extern void ENetHostFlush(IntPtr host);
[DllImport(LIB, EntryPoint = "enet_host_check_events")]
internal static extern int EnetHostCheckEvents(IntPtr host, ENetEvent ev);
internal static extern int ENetHostCheckEvents(IntPtr host, ENetEvent ev);
[DllImport(LIB, EntryPoint = "enet_host_service")]
internal static extern int EnetHostService(IntPtr host, ENetEvent ev, uint timeout);
internal static extern int ENetHostService(IntPtr host, ENetEvent ev, uint timeout);
[DllImport(LIB, EntryPoint = "enet_time_get")]
internal static extern uint EnetTimeGet();
internal static extern uint ENetTimeGet();
[DllImport(LIB, EntryPoint = "enet_time_set")]
internal static extern void EnetTimeSet(uint newTimeBase);
internal static extern void ENetTimeSet(uint newTimeBase);
[DllImport(LIB, EntryPoint = "enet_packet_create")]
internal static extern IntPtr EnetPacketCreate(byte[] data, uint dataLength, PacketFlags flags);
internal static extern IntPtr ENetPacketCreate(byte[] data, uint dataLength, PacketFlags flags);
[DllImport(LIB, EntryPoint = "enet_packet_destroy")]
internal static extern void EnetPacketDestroy(IntPtr packet);
internal static extern void ENetPacketDestroy(IntPtr packet);
[DllImport(LIB, EntryPoint = "enet_packet_resize")]
internal static extern int EnetPacketResize(IntPtr packet, uint dataLength);
internal static extern int ENetPacketResize(IntPtr packet, uint dataLength);
[DllImport(LIB, EntryPoint = "enet_peer_throttle_configure")]
internal static extern void EnetPeerThrottleConfigure(
internal static extern void ENetPeerThrottleConfigure(
IntPtr peer, uint interval, uint acceleration, uint deceleration);
[DllImport(LIB, EntryPoint = "enet_peer_send")]
internal static extern int EnetPeerSend(IntPtr peer, byte channelID, IntPtr packet);
internal static extern int ENetPeerSend(IntPtr peer, byte channelID, IntPtr packet);
[DllImport(LIB, EntryPoint = "enet_peer_receive")]
internal static extern IntPtr EnetPeerReceive(IntPtr peer, out byte channelID);
internal static extern IntPtr ENetPeerReceive(IntPtr peer, out byte channelID);
[DllImport(LIB, EntryPoint = "enet_peer_reset")]
internal static extern void EnetPeerReset(IntPtr peer);
internal static extern void ENetPeerReset(IntPtr peer);
[DllImport(LIB, EntryPoint = "enet_peer_ping")]
internal static extern void EnetPeerPing(IntPtr peer);
internal static extern void ENetPeerPing(IntPtr peer);
[DllImport(LIB, EntryPoint = "enet_peer_disconnect_now")]
internal static extern void EnetPeerDisconnectNow(IntPtr peer, uint data);
internal static extern void ENetPeerDisconnectNow(IntPtr peer, uint data);
[DllImport(LIB, EntryPoint = "enet_peer_disconnect")]
internal static extern void EnetPeerDisconnect(IntPtr peer, uint data);
internal static extern void ENetPeerDisconnect(IntPtr peer, uint data);
[DllImport(LIB, EntryPoint = "enet_peer_disconnect_later")]
internal static extern void EnetPeerDisconnectLater(IntPtr peer, uint data);
internal static extern void ENetPeerDisconnectLater(IntPtr peer, uint data);
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ namespace UNet
this.remoteAddress = this.socket.RemoteAddress;
}
protected void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (this.socket == null)
{
......
using System;
using System.Runtime.InteropServices;
using Common.Logger;
using Common.Network;
namespace UNet
......@@ -20,7 +19,7 @@ namespace UNet
{
throw new ArgumentNullException("data");
}
this.packet = NativeMethods.EnetPacketCreate(data, (uint) data.Length, flags);
this.packet = NativeMethods.ENetPacketCreate(data, (uint) data.Length, flags);
if (this.packet == IntPtr.Zero)
{
throw new UException("Packet creation call failed");
......@@ -45,7 +44,7 @@ namespace UNet
return;
}
NativeMethods.EnetPacketDestroy(this.packet);
NativeMethods.ENetPacketDestroy(this.packet);
this.packet = IntPtr.Zero;
}
......
......@@ -33,7 +33,7 @@ namespace UNet
{
UAddress address = new UAddress(hostName, port);
ENetAddress nativeAddress = address.Struct;
this.host = NativeMethods.EnetHostCreate(
this.host = NativeMethods.ENetHostCreate(
ref nativeAddress, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
if (this.host == IntPtr.Zero)
......@@ -41,12 +41,12 @@ namespace UNet
throw new UException("Host creation call failed.");
}
NativeMethods.EnetHostCompressWithRangeCoder(this.host);
NativeMethods.ENetHostCompressWithRangeCoder(this.host);
}
public UPoller()
{
this.host = NativeMethods.EnetHostCreate(
this.host = NativeMethods.ENetHostCreate(
IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
if (this.host == IntPtr.Zero)
......@@ -73,7 +73,7 @@ namespace UNet
return;
}
NativeMethods.EnetHostDestroy(this.host);
NativeMethods.ENetHostDestroy(this.host);
this.host = IntPtr.Zero;
}
......@@ -122,7 +122,7 @@ namespace UNet
UAddress address = new UAddress(hostName, port);
ENetAddress nativeAddress = address.Struct;
IntPtr ptr = NativeMethods.EnetHostConnect(this.host, ref nativeAddress,
IntPtr ptr = NativeMethods.ENetHostConnect(this.host, ref nativeAddress,
NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 0);
USocket socket = new USocket(ptr);
if (socket.PeerPtr == IntPtr.Zero)
......@@ -147,7 +147,7 @@ namespace UNet
{
this.eNetEventCache = new ENetEvent();
}
if (NativeMethods.EnetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
if (NativeMethods.ENetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
{
return null;
}
......@@ -158,7 +158,7 @@ namespace UNet
public void Flush()
{
NativeMethods.EnetHostFlush(this.host);
NativeMethods.ENetHostFlush(this.host);
}
public void Add(Action action)
......@@ -187,17 +187,12 @@ namespace UNet
private int Service()
{
int ret = NativeMethods.EnetHostService(this.host, null, 0);
int ret = NativeMethods.ENetHostService(this.host, null, 0);
return ret;
}
public void RunOnce(int timeout = 0)
public void Update()
{
if (timeout < 0)
{
throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
}
this.OnEvents();
if (this.Service() < 0)
......
......@@ -115,9 +115,9 @@ namespace UNet
this.channels.Remove(channel.RemoteAddress);
}
public void Run()
public void Update()
{
this.poller.RunOnce();
this.poller.Update();
}
}
}
\ No newline at end of file
......@@ -14,7 +14,6 @@ namespace UNet
public Action<ENetEvent> Connected { get; set; }
public Action<ENetEvent> Received { get; private set; }
public Action<ENetEvent> Disconnect { get; private set; }
public Action<int> Error { get; set; }
private void Dispose(bool disposing)
{
......@@ -23,7 +22,7 @@ namespace UNet
return;
}
NativeMethods.EnetPeerReset(this.peerPtr);
NativeMethods.ENetPeerReset(this.peerPtr);
this.peerPtr = IntPtr.Zero;
}
......@@ -79,18 +78,18 @@ namespace UNet
public void Ping()
{
NativeMethods.EnetPeerPing(this.peerPtr);
NativeMethods.ENetPeerPing(this.peerPtr);
}
public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
{
NativeMethods.EnetPeerThrottleConfigure(this.peerPtr, interval, acceleration, deceleration);
NativeMethods.ENetPeerThrottleConfigure(this.peerPtr, interval, acceleration, deceleration);
}
public void SendAsync(byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
{
UPacket packet = new UPacket(data, flags);
NativeMethods.EnetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
NativeMethods.ENetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
// enet_peer_send函数会自动删除packet,设置为0,防止Dispose或者析构函数再次删除
packet.PacketPtr = IntPtr.Zero;
}
......@@ -127,7 +126,7 @@ namespace UNet
public Task<bool> DisconnectAsync(uint data = 0)
{
NativeMethods.EnetPeerDisconnect(this.peerPtr, data);
NativeMethods.ENetPeerDisconnect(this.peerPtr, data);
// EnetPeerDisconnect会reset Peer,这里设置为0,防止再次Dispose
this.peerPtr = IntPtr.Zero;
var tcs = new TaskCompletionSource<bool>();
......@@ -137,7 +136,7 @@ namespace UNet
public Task<bool> DisconnectLaterAsync(uint data = 0)
{
NativeMethods.EnetPeerDisconnectLater(this.peerPtr, data);
NativeMethods.ENetPeerDisconnectLater(this.peerPtr, data);
// EnetPeerDisconnect会reset Peer,这里设置为0,防止再次Dispose
this.peerPtr = IntPtr.Zero;
var tcs = new TaskCompletionSource<bool>();
......@@ -147,7 +146,7 @@ namespace UNet
public void DisconnectNow(uint data)
{
NativeMethods.EnetPeerDisconnectNow(this.peerPtr, data);
NativeMethods.ENetPeerDisconnectNow(this.peerPtr, data);
// EnetPeerDisconnect会reset Peer,这里设置为0,防止再次Dispose
this.peerPtr = IntPtr.Zero;
}
......
......@@ -34,7 +34,7 @@
<HintPath>..\..\packages\mongocsharpdriver.1.10.0-rc1\lib\net35\MongoDB.Bson.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Driver">
<HintPath>..\..\packages\mongocsharpdriver.1.10.0-rc1\lib\net35\MongoDB.Driver.dll</HintPath>
<HintPath>..\..\packages\mongocsharpdriver.1.10.0\lib\net35\MongoDB.Driver.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
......
......@@ -29,6 +29,7 @@ namespace MongoDBTest
world.AddComponent<UnitComponent>();
world.AddComponent<FactoryComponent<Unit>>();
world.AddComponent<BehaviorTreeComponent>();
world.AddComponent<NetworkComponent>();
world.Load();
Unit player1 = world.GetComponent<FactoryComponent<Unit>>().Create(UnitType.GatePlayer, 1);
......
......@@ -56,7 +56,7 @@ namespace TNetTest
{
while (!isClientStop)
{
clientService.Run();
clientService.Update();
}
}, TaskCreationOptions.LongRunning);
......@@ -64,7 +64,7 @@ namespace TNetTest
{
while (!isServerStop)
{
serverService.Run();
serverService.Update();
}
}, TaskCreationOptions.LongRunning);
......
......@@ -55,7 +55,7 @@ namespace UNetTest
{
while (!isClientStop)
{
clientService.Run();
clientService.Update();
}
}, TaskCreationOptions.LongRunning);
......@@ -63,7 +63,7 @@ namespace UNetTest
{
while (!isServerStop)
{
serverService.Run();
serverService.Update();
}
}, TaskCreationOptions.LongRunning);
......
......@@ -32,6 +32,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommandLine">
<HintPath>..\..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Practices.Prism.Composition">
<HintPath>..\..\packages\Prism.Composition.5.0.0\lib\NET45\Microsoft.Practices.Prism.Composition.dll</HintPath>
</Reference>
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommandLineParser" version="1.9.71" targetFramework="net45" />
<package id="CommonServiceLocator" version="1.3" targetFramework="net45" />
<package id="mongocsharpdriver" version="1.10.0" targetFramework="net45" />
<package id="NLog" version="3.2.0.0" targetFramework="net45" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册