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

1.enetcs peer host event packet在C#层建立联系
2.Log库代码优化,NLoggerAdapter类继承与ALogDecorater

tanghai 13 лет назад
Родитель
Сommit
b4aeaef244

+ 0 - 13
CSharp/App/Infrastructure/ENetUpdateEvent.cs

@@ -1,13 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using Microsoft.Practices.Prism.Events;
-
-namespace Infrastructure
-{
-	public class ENetUpdateEvent : CompositePresentationEvent<Action>
-	{
-	}
-}

+ 0 - 1
CSharp/App/Infrastructure/Infrastructure.csproj

@@ -47,7 +47,6 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AutoPopulateExportedViewsBehavior.cs" />
-    <Compile Include="ENetUpdateEvent.cs" />
     <Compile Include="IViewRegionRegistration.cs" />
     <Compile Include="ViewExportAttribute.cs" />
   </ItemGroup>

+ 4 - 3
CSharp/App/Modules/Robot/RobotView.xaml.cs

@@ -1,5 +1,6 @@
 using System.ComponentModel.Composition;
 using System.Windows;
+using System.Windows.Controls;
 using Infrastructure;
 
 namespace Modules.Robot
@@ -30,12 +31,12 @@ namespace Modules.Robot
 
 		private void btnStart_Click(object sender, RoutedEventArgs e)
 		{
-			ViewModel.Start();
+			this.ViewModel.Start();
 		}
 
-		private void tbLog_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
+		private void tbLog_TextChanged(object sender, TextChangedEventArgs e)
 		{
-			tbLog.ScrollToEnd();
+			this.tbLog.ScrollToEnd();
 		}
 	}
 }

+ 16 - 16
CSharp/App/Modules/Robot/RobotViewModel.cs

@@ -1,10 +1,9 @@
 using System;
 using System.ComponentModel.Composition;
 using System.Windows.Threading;
+using ENet;
 using Log;
-using Microsoft.Practices.Prism.Events;
 using Microsoft.Practices.Prism.ViewModel;
-using ENet;
 
 namespace Modules.Robot
 {
@@ -13,10 +12,9 @@ namespace Modules.Robot
 	{
 		private readonly Host host;
 		private string logText = "";
+
 		private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
-		{
-			Interval = new TimeSpan(0, 0, 0, 0, 50)
-		};
+		{Interval = new TimeSpan(0, 0, 0, 0, 50)};
 
 		public string LogText
 		{
@@ -38,26 +36,28 @@ namespace Modules.Robot
 		public RobotViewModel()
 		{
 			Library.Initialize();
-			host = new Host();
+			this.host = new Host();
 
-			timer.Tick += delegate { this.host.Run(); };
-			timer.Start();
+			this.timer.Tick += delegate { this.host.Run(); };
+			this.timer.Start();
 		}
 
 		public async void StartClient()
 		{
 			try
 			{
-				using (Peer peer = await host.ConnectAsync(
-					new Address { Host = "192.168.10.246", Port = 8901 }))
+				var address = new Address {Host = "192.168.10.246", Port = 8901};
+				using (Peer peer = await this.host.ConnectAsync(address))
 				{
-					Packet packet = await peer.ReceiveAsync();
-					Logger.Debug(packet.Length + " " + packet.Data);
-					
-					await peer.DisconnectLaterAsync();
+					using (Packet packet = await peer.ReceiveAsync())
+					{
+						Logger.Debug(packet.Length + " " + packet.Data);
+
+						await peer.DisconnectLaterAsync();
+					}
 				}
 			}
-			catch (ENetException e)
+			catch (Exception e)
 			{
 				Logger.Debug(e.Message);
 			}
@@ -67,7 +67,7 @@ namespace Modules.Robot
 		{
 			for (int i = 0; i < 4095; ++i)
 			{
-				StartClient();
+				this.StartClient();
 			}
 		}
 	}

+ 1 - 1
CSharp/App/Modules/WaiGua/WaiGuaModule.cs

@@ -3,7 +3,7 @@ using Microsoft.Practices.Prism.Modularity;
 
 namespace Modules.WaiGua
 {
-	[ModuleExport(moduleType: typeof(WaiGuaModule))]
+	[ModuleExport(moduleType: typeof (WaiGuaModule))]
 	public class WaiGuaModule : IModule
 	{
 		public void Initialize()

+ 1 - 1
CSharp/App/Modules/WaiGua/WaiGuaViewModel.cs

@@ -4,7 +4,7 @@ using Microsoft.Practices.Prism.ViewModel;
 
 namespace Modules.WaiGua
 {
-	[Export(contractType: typeof(WaiGuaViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
+	[Export(contractType: typeof (WaiGuaViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
 	internal class WaiGuaViewModel : NotificationObject
 	{
 		private string logText = "";

+ 2 - 0
CSharp/CSharp.sln

@@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "App\Infra
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Log", "Platform\Log\Log.csproj", "{72E16572-FC1F-4A9E-BC96-035417239298}"
 EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{FD5F443E-CBEE-443E-821D-C47C86E09534}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU

+ 4 - 4
CSharp/Platform/Hooks/RecvHook.cs

@@ -19,8 +19,8 @@ namespace Hooks
 		{
 			try
 			{
-				localHook = LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "recv"), new DRecv(dRecv), this);
-				localHook.ThreadACL.SetInclusiveACL(new[] { 0 });
+				this.localHook = LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "recv"), new DRecv(dRecv), this);
+				this.localHook.ThreadACL.SetInclusiveACL(new[] {0});
 			}
 			catch (Exception)
 			{
@@ -31,7 +31,7 @@ namespace Hooks
 
 		public void Dispose()
 		{
-			localHook.Dispose();
+			this.localHook.Dispose();
 		}
 
 		//static int RecvHooked(IntPtr socketHandle, IntPtr buf, int count, int socketFlags)
@@ -50,4 +50,4 @@ namespace Hooks
 		//	return bytesCount;
 		//}
 	}
-}
+}

+ 5 - 5
CSharp/Platform/Hooks/SendHook.cs

@@ -1,7 +1,7 @@
 using System;
 using System.Runtime.InteropServices;
-using Log;
 using EasyHook;
+using Log;
 
 namespace Hooks
 {
@@ -19,8 +19,8 @@ namespace Hooks
 		{
 			try
 			{
-				localHook = LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "send"), new DSend(dSend), this);
-				localHook.ThreadACL.SetInclusiveACL(new[] {0});
+				this.localHook = LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "send"), new DSend(dSend), this);
+				this.localHook.ThreadACL.SetInclusiveACL(new[] {0});
 			}
 			catch (Exception)
 			{
@@ -31,7 +31,7 @@ namespace Hooks
 
 		public void Dispose()
 		{
-			localHook.Dispose();
+			this.localHook.Dispose();
 		}
 	}
-}
+}

+ 22 - 9
CSharp/Platform/Log/ALogDecorater.cs

@@ -1,13 +1,18 @@
-
-namespace Log
+namespace Log
 {
 	public abstract class ALogDecorater
 	{
+		protected const string SEP = " ";
 		private int level;
+		protected readonly ALogDecorater decorater;
 
-		protected ALogDecorater decorater;
+		protected ALogDecorater(ALogDecorater decorater = null)
+		{
+			this.decorater = decorater;
+			this.Level = 0;
+		}
 
-		public int Level
+		protected int Level
 		{
 			get
 			{
@@ -15,13 +20,21 @@ namespace Log
 			}
 			set
 			{
-				if (decorater != null)
+				this.level = value;
+				if (this.decorater != null)
 				{
-					decorater.Level = value + 1;
+					this.decorater.Level = value + 1;
 				}
-				this.level = value;
 			}
 		}
-		public abstract string Decorate(string message);
+
+		public virtual string Decorate(string message)
+		{
+			if (this.decorater == null)
+			{
+				return message;
+			}
+			return this.decorater.Decorate(message);
+		}
 	}
-}
+}

+ 5 - 6
CSharp/Platform/Log/ILogger.cs

@@ -1,9 +1,8 @@
-
-namespace Log
+namespace Log
 {
-    public interface ILogger
+	public interface ILogger
 	{
 		void Trace(string message);
-	    void Debug(string message);
-    }
-}
+		void Debug(string message);
+	}
+}

+ 2 - 3
CSharp/Platform/Log/Logger.cs

@@ -1,5 +1,4 @@
-
-namespace Log
+namespace Log
 {
 	public static class Logger
 	{
@@ -23,4 +22,4 @@ namespace Log
 			globalLogger.Debug(message);
 		}
 	}
-}
+}

+ 5 - 23
CSharp/Platform/Log/NLoggerAdapter.cs

@@ -2,40 +2,22 @@
 
 namespace Log
 {
-	public class NLoggerAdapter : ILogger
+	public class NLoggerAdapter : ALogDecorater, ILogger
 	{
-		private const string SEP = " ";
-
-		private readonly ALogDecorater decorater;
-
 		private readonly NLog.Logger logger = LogManager.GetCurrentClassLogger();
 
-		public NLoggerAdapter(ALogDecorater decorater = null)
-		{
-			this.decorater = decorater;
-			if (this.decorater != null)
-			{
-				this.decorater.Level = 0;
-			}
-		}
-
-		public string Decorate(string message)
+		public NLoggerAdapter(ALogDecorater decorater = null) : base(decorater)
 		{
-			if (decorater == null)
-			{
-				return message;
-			}
-			return decorater.Decorate(message);
 		}
 
 		public void Trace(string message)
 		{
-			logger.Trace(Decorate(SEP + message));
+			this.logger.Trace(this.Decorate(SEP + message));
 		}
 
 		public void Debug(string message)
 		{
-			logger.Debug(Decorate(SEP + message));
+			this.logger.Debug(this.Decorate(SEP + message));
 		}
 	}
-}
+}

+ 8 - 10
CSharp/Platform/Log/StackInfoDecorater.cs

@@ -3,12 +3,10 @@ using System.IO;
 
 namespace Log
 {
-	class StackInfoDecorater: ALogDecorater
+	internal class StackInfoDecorater : ALogDecorater
 	{
-
-		public StackInfoDecorater(ALogDecorater decorater = null)
+		public StackInfoDecorater(ALogDecorater decorater = null) : base(decorater)
 		{
-			this.decorater = decorater;
 			this.FileName = true;
 			this.FileLineNumber = true;
 		}
@@ -27,9 +25,9 @@ namespace Log
 
 		public override string Decorate(string message)
 		{
-			if (decorater != null)
+			if (this.decorater != null)
 			{
-				message = decorater.Decorate(message);
+				message = this.decorater.Decorate(message);
 			}
 
 			if (!this.FileLineNumber && !this.FileName)
@@ -39,14 +37,14 @@ namespace Log
 
 			string extraInfo = "";
 			var stackTrace = new StackTrace(true);
-			var frame = stackTrace.GetFrame(this.Level + 4);
+			var frame = stackTrace.GetFrame(this.Level + 3);
 
-			if (FileName)
+			if (this.FileName)
 			{
 				var fileName = Path.GetFileName(frame.GetFileName());
 				extraInfo += fileName + " ";
 			}
-			if (FileLineNumber)
+			if (this.FileLineNumber)
 			{
 				var fileLineNumber = frame.GetFileLineNumber();
 				extraInfo += fileLineNumber + " ";
@@ -54,4 +52,4 @@ namespace Log
 			return extraInfo + message;
 		}
 	}
-}
+}

+ 3 - 4
CSharp/ThirdParty/ENetCS/Address.cs

@@ -10,8 +10,7 @@ namespace ENet
 		public bool Equals(Address addr)
 		{
 			ENetAddress enetAddr = addr.Struct;
-			return this.address.host == enetAddr.host &&
-				this.address.port == enetAddr.port;
+			return this.address.host == enetAddr.host && this.address.port == enetAddr.port;
 		}
 
 		public string IP
@@ -19,7 +18,7 @@ namespace ENet
 			get
 			{
 				var hostIP = new StringBuilder(256);
-				Native.enet_address_get_host_ip(ref this.address, hostIP, (uint)hostIP.Length);
+				Native.enet_address_get_host_ip(ref this.address, hostIP, (uint) hostIP.Length);
 				return hostIP.ToString();
 			}
 		}
@@ -29,7 +28,7 @@ namespace ENet
 			get
 			{
 				var hostName = new StringBuilder(256);
-				Native.enet_address_get_host(ref this.address, hostName, (uint)hostName.Length);
+				Native.enet_address_get_host(ref this.address, hostName, (uint) hostName.Length);
 				return hostName.ToString();
 			}
 			set

+ 1 - 1
CSharp/ThirdParty/ENetCS/ENetCS.csproj

@@ -46,10 +46,10 @@
     <Compile Include="ENetException.cs" />
     <Compile Include="Event.cs" />
     <Compile Include="Host.cs" />
-    <Compile Include="PeerEventsManager.cs" />
     <Compile Include="Library.cs" />
     <Compile Include="Native.cs" />
     <Compile Include="PeerEvent.cs" />
+    <Compile Include="PeersManager.cs" />
     <Compile Include="Structs.cs" />
     <Compile Include="Packet.cs" />
     <Compile Include="Peer.cs" />

+ 7 - 7
CSharp/ThirdParty/ENetCS/Event.cs

@@ -1,14 +1,13 @@
-using System;
-using System.Runtime.InteropServices;
-
-namespace ENet
+namespace ENet
 {
 	public class Event
 	{
+		private readonly Host host;
 		private readonly ENetEvent ev;
 
-		public Event(ENetEvent ev)
+		public Event(Host host, ENetEvent ev)
 		{
+			this.host = host;
 			this.ev = ev;
 		}
 
@@ -24,7 +23,7 @@ namespace ENet
 		{
 			get
 			{
-				return new Packet(this.Ev.packet);
+				return new Packet(this.host, this.Ev.packet);
 			}
 		}
 
@@ -32,7 +31,8 @@ namespace ENet
 		{
 			get
 			{
-				return new Peer(this.Ev.peer);
+				Peer peer = this.host.PeersManager[this.Ev.peer];
+				return peer;
 			}
 		}
 

+ 46 - 35
CSharp/ThirdParty/ENetCS/Host.cs

@@ -5,13 +5,23 @@ namespace ENet
 {
 	public sealed class Host : IDisposable
 	{
+		private readonly PeersManager peersManager = new PeersManager();
+
+		public PeersManager PeersManager
+		{
+			get
+			{
+				return this.peersManager;
+			}
+		}
+
 		private IntPtr host;
 		private readonly object eventsLock = new object();
 		private Action events;
 
-		public Host(Address address, uint peerLimit = Native.ENET_PROTOCOL_MAXIMUM_PEER_ID, 
-			uint channelLimit = 0, uint incomingBandwidth = 0,
-			uint outgoingBandwidth = 0, bool enableCrc = true)
+		public Host(
+				Address address, uint peerLimit = Native.ENET_PROTOCOL_MAXIMUM_PEER_ID, uint channelLimit = 0,
+				uint incomingBandwidth = 0, uint outgoingBandwidth = 0, bool enableCrc = true)
 		{
 			if (peerLimit > Native.ENET_PROTOCOL_MAXIMUM_PEER_ID)
 			{
@@ -20,9 +30,7 @@ namespace ENet
 			CheckChannelLimit(channelLimit);
 
 			ENetAddress nativeAddress = address.Struct;
-			this.host = Native.enet_host_create(
-				ref nativeAddress, peerLimit, channelLimit, incomingBandwidth,
-				outgoingBandwidth);
+			this.host = Native.enet_host_create(ref nativeAddress, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
 
 			if (this.host == IntPtr.Zero)
 			{
@@ -31,12 +39,13 @@ namespace ENet
 
 			if (enableCrc)
 			{
-				Native.enet_enable_crc(host);
+				Native.enet_enable_crc(this.host);
 			}
 		}
 
-		public Host(uint peerLimit = Native.ENET_PROTOCOL_MAXIMUM_PEER_ID, uint channelLimit = 0,
-				uint incomingBandwidth = 0, uint outgoingBandwidth = 0, bool enableCrc = true)
+		public Host(
+				uint peerLimit = Native.ENET_PROTOCOL_MAXIMUM_PEER_ID, uint channelLimit = 0, uint incomingBandwidth = 0,
+				uint outgoingBandwidth = 0, bool enableCrc = true)
 		{
 			if (peerLimit > Native.ENET_PROTOCOL_MAXIMUM_PEER_ID)
 			{
@@ -44,9 +53,7 @@ namespace ENet
 			}
 			CheckChannelLimit(channelLimit);
 
-			this.host = Native.enet_host_create(
-				IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth,
-				outgoingBandwidth);
+			this.host = Native.enet_host_create(IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
 
 			if (this.host == IntPtr.Zero)
 			{
@@ -55,7 +62,7 @@ namespace ENet
 
 			if (enableCrc)
 			{
-				Native.enet_enable_crc(host);
+				Native.enet_enable_crc(this.host);
 			}
 		}
 
@@ -76,7 +83,12 @@ namespace ENet
 			{
 				return;
 			}
-			Native.enet_host_destroy(this.host);
+
+			if (disposing)
+			{
+				Native.enet_host_destroy(this.host);
+			}
+
 			this.host = IntPtr.Zero;
 		}
 
@@ -92,7 +104,7 @@ namespace ENet
 		{
 			var enetEv = new ENetEvent();
 			int ret = Native.enet_host_check_events(this.host, enetEv);
-			e = new Event(enetEv);
+			e = new Event(this, enetEv);
 			return ret;
 		}
 
@@ -102,7 +114,7 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException("timeout");
 			}
-			return Native.enet_host_service(this.host, null, (uint)timeout);
+			return Native.enet_host_service(this.host, null, (uint) timeout);
 		}
 
 		public void Broadcast(byte channelID, ref Packet packet)
@@ -121,8 +133,7 @@ namespace ENet
 		}
 
 		public Task<Peer> ConnectAsync(
-			Address address, uint channelLimit = Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 
-			uint data = 0)
+				Address address, uint channelLimit = Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, uint data = 0)
 		{
 			CheckChannelLimit(channelLimit);
 
@@ -133,8 +144,8 @@ namespace ENet
 			{
 				throw new ENetException(0, "Host connect call failed.");
 			}
-			var peer = new Peer(p);
-			Peer.PeerEventsManager[p].Connected += e => tcs.TrySetResult(peer);
+			var peer = new Peer(this, p);
+			this.PeersManager[p].PeerEvent.Connected += e => tcs.TrySetResult(peer);
 			return tcs.Task;
 		}
 
@@ -158,16 +169,16 @@ namespace ENet
 		{
 			add
 			{
-				lock (eventsLock)
+				lock (this.eventsLock)
 				{
-					events += value;
+					this.events += value;
 				}
 			}
 			remove
 			{
-				lock (eventsLock)
+				lock (this.eventsLock)
 				{
-					events -= value;
+					this.events -= value;
 				}
 			}
 		}
@@ -175,14 +186,14 @@ namespace ENet
 		private void OnExecuteEvents()
 		{
 			Action local = null;
-			lock (eventsLock)
+			lock (this.eventsLock)
 			{
-				if (events == null)
+				if (this.events == null)
 				{
 					return;
 				}
-				local = events;
-				events = null;
+				local = this.events;
+				this.events = null;
 			}
 			local();
 		}
@@ -190,31 +201,31 @@ namespace ENet
 		public void Run()
 		{
 			// 处理其它线程扔过来的事件
-			OnExecuteEvents();
+			this.OnExecuteEvents();
 
 			if (this.Service(0) < 0)
 			{
 				return;
 			}
 
-			Event e;
-			while (this.CheckEvents(out e) > 0)
+			Event ev;
+			while (this.CheckEvents(out ev) > 0)
 			{
-				switch (e.Type)
+				switch (ev.Type)
 				{
 					case EventType.Connect:
 					{
-						Peer.PeerEventsManager.OnConnected(e.Ev.peer, e);
+						ev.Peer.PeerEvent.OnConnected(ev);
 						break;
 					}
 					case EventType.Receive:
 					{
-						Peer.PeerEventsManager.OnReceived(e.Ev.peer, e);
+						ev.Peer.PeerEvent.OnReceived(ev);
 						break;
 					}
 					case EventType.Disconnect:
 					{
-						Peer.PeerEventsManager.OnDisconnect(e.Ev.peer, e);
+						ev.Peer.PeerEvent.OnDisconnect(ev);
 						break;
 					}
 				}

+ 3 - 6
CSharp/ThirdParty/ENetCS/Native.cs

@@ -45,20 +45,17 @@ namespace ENet
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern IntPtr enet_host_create(
-				ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth, 
-				uint outgoingBandwidth);
+				ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern IntPtr enet_host_create(
-				IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
-				uint outgoingBandwidth);
+				IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern void enet_host_destroy(IntPtr host);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern IntPtr enet_host_connect(
-				IntPtr host, ref ENetAddress address, uint channelCount, uint data);
+		public static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, uint channelCount, uint data);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);

+ 15 - 8
CSharp/ThirdParty/ENetCS/Packet.cs

@@ -5,10 +5,12 @@ namespace ENet
 {
 	public sealed class Packet : IDisposable
 	{
+		private Host host;
 		private IntPtr packet;
 
-		public Packet(IntPtr packet)
+		public Packet(Host host, IntPtr packet)
 		{
+			this.host = host;
 			this.packet = packet;
 		}
 
@@ -18,7 +20,7 @@ namespace ENet
 			{
 				throw new ArgumentNullException("data");
 			}
-			this.packet = Native.enet_packet_create(data, (uint)data.Length, flags);
+			this.packet = Native.enet_packet_create(data, (uint) data.Length, flags);
 			if (this.packet == IntPtr.Zero)
 			{
 				throw new ENetException(0, "Packet creation call failed.");
@@ -27,12 +29,12 @@ namespace ENet
 
 		~Packet()
 		{
-			Dispose(false);
+			this.Dispose(false);
 		}
 
 		public void Dispose()
 		{
-			Dispose(true);
+			this.Dispose(true);
 			GC.SuppressFinalize(this);
 		}
 
@@ -42,15 +44,20 @@ namespace ENet
 			{
 				return;
 			}
-			Native.enet_packet_destroy(this.packet);
+
+			if (disposing)
+			{
+				Native.enet_packet_destroy(this.packet);
+			}
+
 			this.packet = IntPtr.Zero;
 		}
 
-		public ENetPacket Struct
+		private ENetPacket Struct
 		{
 			get
 			{
-				return (ENetPacket)Marshal.PtrToStructure(this.packet, typeof(ENetPacket));
+				return (ENetPacket) Marshal.PtrToStructure(this.packet, typeof (ENetPacket));
 			}
 			set
 			{
@@ -91,7 +98,7 @@ namespace ENet
 				{
 					return "";
 				}
-				return Marshal.PtrToStringAuto(pkt.data, (int)pkt.dataLength);
+				return Marshal.PtrToStringAuto(pkt.data, (int) pkt.dataLength);
 			}
 		}
 	}

+ 19 - 23
CSharp/ThirdParty/ENetCS/Peer.cs

@@ -6,22 +6,15 @@ namespace ENet
 {
 	public class Peer : IDisposable
 	{
-		private static readonly PeerEventsManager peerEventsManager = new PeerEventsManager();
-
-		public static PeerEventsManager PeerEventsManager
-		{
-			get
-			{
-				return peerEventsManager;
-			}
-		}
-
+		private readonly Host host;
+		private readonly PeerEvent peerEvent = new PeerEvent();
 		private IntPtr peer;
 
-		public Peer(IntPtr peer)
+		public Peer(Host host, IntPtr peer)
 		{
+			this.host = host;
 			this.peer = peer;
-			PeerEventsManager.Add(peer);
+			host.PeersManager.Add(peer, this);
 		}
 
 		~Peer()
@@ -31,7 +24,7 @@ namespace ENet
 
 		public void Dispose()
 		{
-			Dispose(true);
+			this.Dispose(true);
 			GC.SuppressFinalize(this);
 		}
 
@@ -42,12 +35,15 @@ namespace ENet
 				return;
 			}
 
-			PeerEventsManager.Remove(this.peer);
-			Native.enet_peer_reset(this.peer);
+			if (disposing)
+			{
+				this.host.PeersManager.Remove(this.peer);
+				Native.enet_peer_reset(this.peer);
+			}
 			this.peer = IntPtr.Zero;
 		}
 
-		public ENetPeer Struct
+		private ENetPeer Struct
 		{
 			get
 			{
@@ -55,7 +51,7 @@ namespace ENet
 				{
 					return new ENetPeer();
 				}
-				return (ENetPeer)Marshal.PtrToStructure(this.peer, typeof(ENetPeer));
+				return (ENetPeer) Marshal.PtrToStructure(this.peer, typeof (ENetPeer));
 			}
 			set
 			{
@@ -63,11 +59,11 @@ namespace ENet
 			}
 		}
 
-		public IntPtr NativePtr
+		public PeerEvent PeerEvent
 		{
 			get
 			{
-				return this.peer;
+				return this.peerEvent;
 			}
 		}
 
@@ -79,7 +75,7 @@ namespace ENet
 				{
 					return PeerState.Uninitialized;
 				}
-				return Struct.state;
+				return this.Struct.state;
 			}
 		}
 
@@ -109,14 +105,14 @@ namespace ENet
 		public Task<Packet> ReceiveAsync()
 		{
 			var tcs = new TaskCompletionSource<Packet>();
-			PeerEventsManager[this.peer].Received += e => tcs.TrySetResult(e.Packet);
+			this.PeerEvent.Received += e => tcs.TrySetResult(e.Packet);
 			return tcs.Task;
 		}
 
 		public Task<bool> DisconnectAsync(uint data = 0)
 		{
 			var tcs = new TaskCompletionSource<bool>();
-			PeerEventsManager[this.peer].Disconnect += e => tcs.TrySetResult(true);
+			this.PeerEvent.Disconnect += e => tcs.TrySetResult(true);
 			Native.enet_peer_disconnect(this.peer, data);
 			return tcs.Task;
 		}
@@ -124,7 +120,7 @@ namespace ENet
 		public Task<bool> DisconnectLaterAsync(uint data = 0)
 		{
 			var tcs = new TaskCompletionSource<bool>();
-			PeerEventsManager[this.peer].Disconnect += e => tcs.TrySetResult(true);
+			this.PeerEvent.Disconnect += e => tcs.TrySetResult(true);
 			Native.enet_peer_disconnect_later(this.peer, data);
 			return tcs.Task;
 		}

+ 13 - 13
CSharp/ThirdParty/ENetCS/PeerEvent.cs

@@ -12,11 +12,11 @@ namespace ENet
 		{
 			add
 			{
-				connected += value;
+				this.connected += value;
 			}
 			remove
 			{
-				connected -= value;
+				this.connected -= value;
 			}
 		}
 
@@ -24,11 +24,11 @@ namespace ENet
 		{
 			add
 			{
-				received += value;
+				this.received += value;
 			}
 			remove
 			{
-				received -= value;
+				this.received -= value;
 			}
 		}
 
@@ -36,39 +36,39 @@ namespace ENet
 		{
 			add
 			{
-				disconnect += value;
+				this.disconnect += value;
 			}
 			remove
 			{
-				disconnect -= value;
+				this.disconnect -= value;
 			}
 		}
 
 		internal void OnConnected(Event e)
 		{
-			if (connected == null)
+			if (this.connected == null)
 			{
 				return;
 			}
-			connected(e);
+			this.connected(e);
 		}
 
 		internal void OnReceived(Event e)
 		{
-			if (received == null)
+			if (this.received == null)
 			{
 				return;
 			}
-			received(e);
+			this.received(e);
 		}
 
 		internal void OnDisconnect(Event e)
 		{
-			if (disconnect == null)
+			if (this.disconnect == null)
 			{
 				return;
 			}
-			disconnect(e);
+			this.disconnect(e);
 		}
 	}
-}
+}

+ 0 - 62
CSharp/ThirdParty/ENetCS/PeerEventsManager.cs

@@ -1,62 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace ENet
-{
-	public class PeerEventsManager
-	{
-		private readonly Dictionary<IntPtr, PeerEvent> peerEvents = new Dictionary<IntPtr, PeerEvent>();
-
-		public void Add(IntPtr peer)
-		{
-			this.peerEvents.Add(peer, new PeerEvent());
-		}
-
-		public void Remove(IntPtr peer)
-		{
-			this.peerEvents.Remove(peer);
-		}
-
-		public PeerEvent this[IntPtr peer]
-		{
-			get
-			{
-				if (!peerEvents.ContainsKey(peer))
-				{
-					throw new KeyNotFoundException("No Peer Key");
-				}
-				return peerEvents[peer];
-			}
-		}
-
-		internal void OnConnected(IntPtr peer, Event e)
-		{
-			var peerEvent = peerEvents[peer];
-			if (peerEvent == null)
-			{
-				return;
-			}
-			peerEvent.OnConnected(e);
-		}
-
-		internal void OnReceived(IntPtr peer, Event e)
-		{
-			var peerEvent = peerEvents[peer];
-			if (peerEvent == null)
-			{
-				return;
-			}
-			peerEvent.OnReceived(e);
-		}
-
-		internal void OnDisconnect(IntPtr peer, Event e)
-		{
-			var peerEvent = peerEvents[peer];
-			if (peerEvent == null)
-			{
-				return;
-			}
-			peerEvent.OnDisconnect(e);
-		}
-	}
-}

+ 32 - 0
CSharp/ThirdParty/ENetCS/PeersManager.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+
+namespace ENet
+{
+	public class PeersManager
+	{
+		private readonly Dictionary<IntPtr, Peer> peersManager = new Dictionary<IntPtr, Peer>();
+
+		public void Add(IntPtr peerPtr, Peer peer)
+		{
+			this.peersManager.Add(peerPtr, peer);
+		}
+
+		public void Remove(IntPtr peerPtr)
+		{
+			this.peersManager.Remove(peerPtr);
+		}
+
+		public Peer this[IntPtr peerPtr]
+		{
+			get
+			{
+				if (!this.peersManager.ContainsKey(peerPtr))
+				{
+					throw new KeyNotFoundException("No Peer Key");
+				}
+				return this.peersManager[peerPtr];
+			}
+		}
+	}
+}

+ 2 - 3
CSharp/ThirdParty/ENetCS/Structs.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Runtime.InteropServices;
-using System.Text;
 
 namespace ENet
 {
@@ -97,10 +96,10 @@ namespace ENet
 		public ENetListNode previous;
 	}
 
-	[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
+	[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
 	public delegate void ENetPacketFreeCallback(ref ENetPacket param0);
 
-	[StructLayoutAttribute(LayoutKind.Sequential)]
+	[StructLayout(LayoutKind.Sequential)]
 	public struct ENetPacket
 	{
 		public uint referenceCount;