Parcourir la source

使用完全的P/Invoke调用,指针是IntPtr

tanghai il y a 13 ans
Parent
commit
94fcf014fb

+ 1 - 2
CSharp/App/Modules/Robot/RobotViewModel.cs

@@ -48,8 +48,7 @@ namespace Modules.Robot
 		{
 			try
 			{
-				Peer peer = await host.ConnectAsync(new Address { Host = "192.168.10.246", Port = 8901 }, 2);
-				LogText += string.Format("peer data: {0}\r\n", peer.Data);
+				Peer peer = await host.ConnectAsync(new Address { Host = "192.168.10.246", Port = 8901 });
 			}
 			catch (ENetException e)
 			{

+ 3 - 0
CSharp/Platform/Hooks/Hooks.csproj

@@ -11,6 +11,8 @@
     <AssemblyName>Hooks</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <RestorePackages>true</RestorePackages>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -51,6 +53,7 @@
     </ProjectReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Import Project="$(SolutionDir)\.nuget\nuget.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="BeforeBuild">

+ 14 - 59
CSharp/ThirdParty/ENetCS/Address.cs

@@ -1,65 +1,26 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using System;
-using System.Net;
+using System;
 using System.Text;
 
 namespace ENet
 {
-	public unsafe struct Address : IEquatable<Address>
+	public struct Address : IEquatable<Address>
 	{
-		public const uint IPv4HostAny = Native.ENET_HOST_ANY;
-		public const uint IPv4HostBroadcast = Native.ENET_HOST_BROADCAST;
-
-		private Native.ENetAddress address;
-
-		public override bool Equals(object obj)
-		{
-			return obj is Address && this.Equals((Address) obj);
-		}
+		private ENetAddress address;
 
 		public bool Equals(Address addr)
 		{
-			return this.Port == addr.Port && this.IP == addr.IP;
-		}
-
-		public override int GetHashCode()
-		{
-			return this.Port.GetHashCode() ^ this.IP.GetHashCode();
+			ENetAddress enetAddr = addr.Struct;
+			return this.address.host == enetAddr.host &&
+				this.address.port == enetAddr.port;
 		}
 
 		public string IP
 		{
 			get
 			{
-				var ip = new byte[256];
-				fixed (byte* hostIP = ip)
-				{
-					if (Native.enet_address_get_host_ip(ref this.address, hostIP, (uint)ip.Length) < 0)
-					{
-						return null;
-					}
-				}
-				return Encoding.ASCII.GetString(ip, 0, Native.strlen(ip));
+				var hostIP = new StringBuilder(256);
+				Native.enet_address_get_host_ip(ref this.address, hostIP, (uint)hostIP.Length);
+				return hostIP.ToString();
 			}
 		}
 
@@ -67,23 +28,17 @@ namespace ENet
 		{
 			get
 			{
-				var name = new byte[256];
-				fixed (byte* hostName = name)
-				{
-					if (Native.enet_address_get_host(ref this.address, hostName, (uint)name.Length) < 0)
-					{
-						return null;
-					}
-				}
-				return Encoding.ASCII.GetString(name, 0, Native.strlen(name));
+				var hostName = new StringBuilder(256);
+				Native.enet_address_get_host(ref this.address, hostName, (uint)hostName.Length);
+				return hostName.ToString();
 			}
 			set
 			{
-				Native.enet_address_set_host(ref this.address, Encoding.ASCII.GetBytes(value));
+				Native.enet_address_set_host(ref this.address, value);
 			}
 		}
 
-		public Native.ENetAddress NativeData
+		public ENetAddress Struct
 		{
 			get
 			{

+ 0 - 28
CSharp/ThirdParty/ENetCS/AddressType.cs

@@ -1,28 +0,0 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-namespace ENet
-{
-	public enum AddressType
-	{
-		IPv4 = 0
-	}
-}

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

@@ -11,6 +11,8 @@
     <AssemblyName>ENetCS</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <RestorePackages>true</RestorePackages>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -41,19 +43,16 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Address.cs" />
-    <Compile Include="AddressType.cs" />
     <Compile Include="ENetException.cs" />
     <Compile Include="Event.cs" />
-    <Compile Include="EventType.cs" />
     <Compile Include="Host.cs" />
+    <Compile Include="PeerEventsManager.cs" />
     <Compile Include="Library.cs" />
     <Compile Include="Native.cs" />
-    <Compile Include="Native.Structs.cs" />
+    <Compile Include="PeerEvent.cs" />
+    <Compile Include="Structs.cs" />
     <Compile Include="Packet.cs" />
-    <Compile Include="PacketFlags.cs" />
     <Compile Include="Peer.cs" />
-    <Compile Include="PeerManager.cs" />
-    <Compile Include="PeerState.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />
@@ -65,6 +64,7 @@
     </ProjectReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Import Project="$(SolutionDir)\.nuget\nuget.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="BeforeBuild">

+ 1 - 22
CSharp/ThirdParty/ENetCS/ENetException.cs

@@ -1,25 +1,4 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using System;
+using System;
 
 namespace ENet
 {

+ 14 - 57
CSharp/ThirdParty/ENetCS/Event.cs

@@ -1,64 +1,34 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using Log;
+using System;
+using System.Runtime.InteropServices;
 
 namespace ENet
 {
-	public unsafe struct Event
+	public struct Event
 	{
-		private readonly Host host;
-		private Native.ENetEvent e;
+		private readonly IntPtr e;
 
-		public Event(Host host, Native.ENetEvent e)
+		public Event(IntPtr e)
 		{
 			this.e = e;
-			this.host = host;
 		}
 
-		public byte ChannelID
+		public ENetEvent Struct
 		{
 			get
 			{
-				return this.e.channelID;
+				return (ENetEvent)Marshal.PtrToStructure(this.e, typeof(ENetEvent));
 			}
-		}
-
-		public uint Data
-		{
-			get
+			set
 			{
-				return this.e.data;
+				Marshal.StructureToPtr(value, this.e, false);
 			}
 		}
 
-		public Native.ENetEvent NativeData
+		public IntPtr NativePtr
 		{
 			get
 			{
-				return this.e;
-			}
-			set
-			{
-				this.e = value;
+				return e;
 			}
 		}
 
@@ -66,11 +36,7 @@ namespace ENet
 		{
 			get
 			{
-				if (this.e.packet == null)
-				{
-					return null;
-				}
-				return new Packet(this.e.packet);
+				return new Packet(this.Struct.packet);
 			}
 		}
 
@@ -78,16 +44,7 @@ namespace ENet
 		{
 			get
 			{
-				if (this.e.peer == null)
-				{
-					return null;
-				}
-				var data = (int)e.peer->data;
-				if (!this.host.Peers.ContainsKey(data))
-				{
-					return null;
-				}
-				return this.host.Peers[data];
+				return new Peer(this.Struct.peer);
 			}
 		}
 
@@ -95,7 +52,7 @@ namespace ENet
 		{
 			get
 			{
-				return this.e.type;
+				return this.Struct.type;
 			}
 		}
 	}

+ 0 - 31
CSharp/ThirdParty/ENetCS/EventType.cs

@@ -1,31 +0,0 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-namespace ENet
-{
-	public enum EventType
-	{
-		None = 0,
-		Connect = 1,
-		Disconnect = 2,
-		Receive = 3
-	}
-}

+ 48 - 82
CSharp/ThirdParty/ENetCS/Host.cs

@@ -1,35 +1,11 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using Log;
-using System;
-using System.Collections.Generic;
+using System;
 using System.Threading.Tasks;
 
 namespace ENet
 {
-	public sealed unsafe class Host : IDisposable
+	public sealed class Host : IDisposable
 	{
-		private Native.ENetHost* host;
-		private readonly PeerManager peerManager = new PeerManager();
+		private IntPtr host;
 		private readonly object eventsLock = new object();
 		private Action events;
 
@@ -38,8 +14,9 @@ namespace ENet
 		{
 		}
 
-		public Host(Address? address, uint peerLimit, 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)
 			{
@@ -47,21 +24,36 @@ namespace ENet
 			}
 			CheckChannelLimit(channelLimit);
 
-			if (address != null)
+			ENetAddress nativeAddress = address.Struct;
+			this.host = Native.enet_host_create(
+				ref nativeAddress, peerLimit, channelLimit, incomingBandwidth,
+				outgoingBandwidth);
+
+			if (this.host == IntPtr.Zero)
 			{
-				Native.ENetAddress nativeAddress = address.Value.NativeData;
-				this.host = Native.enet_host_create(
-					ref nativeAddress, peerLimit, channelLimit, incomingBandwidth,
-					outgoingBandwidth);
+				throw new ENetException(0, "Host creation call failed.");
 			}
-			else
+
+			if (enableCrc)
 			{
-				this.host = Native.enet_host_create(
-					null, peerLimit, channelLimit, incomingBandwidth,
-					outgoingBandwidth);
+				Native.enet_enable_crc(host);
 			}
+		}
 
-			if (this.host == null)
+		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)
+			{
+				throw new ArgumentOutOfRangeException("peerLimit");
+			}
+			CheckChannelLimit(channelLimit);
+
+			this.host = Native.enet_host_create(
+				IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth,
+				outgoingBandwidth);
+
+			if (this.host == IntPtr.Zero)
 			{
 				throw new ENetException(0, "Host creation call failed.");
 			}
@@ -85,17 +77,12 @@ namespace ENet
 
 		private void Dispose(bool disposing)
 		{
-			if (this.host == null)
+			if (this.host == IntPtr.Zero)
 			{
 				return;
 			}
-
-			if (disposing)
-			{
-				Native.enet_host_destroy(this.host);
-			}
-
-			this.host = null;
+			Native.enet_host_destroy(this.host);
+			this.host = IntPtr.Zero;
 		}
 
 		private static void CheckChannelLimit(uint channelLimit)
@@ -108,9 +95,9 @@ namespace ENet
 
 		private int CheckEvents(out Event e)
 		{
-			Native.ENetEvent nativeEvent;
+			IntPtr nativeEvent;
 			int ret = Native.enet_host_check_events(this.host, out nativeEvent);
-			e = new Event(this, nativeEvent);
+			e = new Event(nativeEvent);
 			return ret;
 		}
 
@@ -120,34 +107,13 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException("timeout");
 			}
-			return Native.enet_host_service(this.host, null, (uint)timeout);
-		}
-
-		private int Service(int timeout, out Event e)
-		{
-			if (timeout < 0)
-			{
-				throw new ArgumentOutOfRangeException("timeout");
-			}
-			Native.ENetEvent nativeEvent;
-
-			int ret = Native.enet_host_service(this.host, out nativeEvent, (uint)timeout);
-			e = new Event(this, nativeEvent);
-			return ret;
-		}
-
-		public PeerManager Peers
-		{
-			get
-			{
-				return peerManager;
-			}
+			IntPtr e = IntPtr.Zero;
+			return Native.enet_host_service(this.host, ref e, (uint)timeout);
 		}
 
 		public void Broadcast(byte channelID, ref Packet packet)
 		{
-			Native.enet_host_broadcast(this.host, channelID, packet.NativeData);
-			packet.NativeData = null; // Broadcast automatically clears this.
+			Native.enet_host_broadcast(this.host, channelID, packet.NativePtr);
 		}
 
 		public void CompressWithRangeEncoder()
@@ -157,7 +123,7 @@ namespace ENet
 
 		public void DoNotCompress()
 		{
-			Native.enet_host_compress(this.host, null);
+			Native.enet_host_compress(this.host, IntPtr.Zero);
 		}
 
 		public Task<Peer> ConnectAsync(
@@ -167,14 +133,14 @@ namespace ENet
 			CheckChannelLimit(channelLimit);
 
 			var tcs = new TaskCompletionSource<Peer>();
-			Native.ENetAddress nativeAddress = address.NativeData;
-			Native.ENetPeer* p = Native.enet_host_connect(this.host, ref nativeAddress, channelLimit, data);
-			if (p == null)
+			ENetAddress nativeAddress = address.Struct;
+			IntPtr p = Native.enet_host_connect(this.host, ref nativeAddress, channelLimit, data);
+			if (p == IntPtr.Zero)
 			{
 				throw new ENetException(0, "Host connect call failed.");
 			}
-			var peer = new Peer(this, p);
-			peer.Connected += e => tcs.TrySetResult(e.Peer);
+			var peer = new Peer(p);
+			Peer.PeerEventsManager[p].Connected += e => tcs.TrySetResult(peer);
 			return tcs.Task;
 		}
 
@@ -244,17 +210,17 @@ namespace ENet
 				{
 					case EventType.Connect:
 					{
-						e.Peer.OnConnected(e);
+						Peer.PeerEventsManager.OnConnected(e.Struct.peer, e);
 						break;
 					}
 					case EventType.Receive:
 					{
-						e.Peer.OnReceived(e);
+						Peer.PeerEventsManager.OnReceived(e.Struct.peer, e);
 						break;
 					}
 					case EventType.Disconnect:
 					{
-						e.Peer.OnDisconnect(e);
+						Peer.PeerEventsManager.OnDisconnect(e.Struct.peer, e);
 						break;
 					}
 				}

+ 2 - 23
CSharp/ThirdParty/ENetCS/Library.cs

@@ -1,31 +1,10 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-namespace ENet
+namespace ENet
 {
 	public static class Library
 	{
 		public static void Initialize()
 		{
-			var inits = new Native.ENetCallbacks();
+			var inits = new ENetCallbacks();
 			int ret = Native.enet_initialize_with_callbacks(Native.ENET_VERSION, ref inits);
 			if (ret < 0)
 			{

+ 0 - 118
CSharp/ThirdParty/ENetCS/Native.Structs.cs

@@ -1,118 +0,0 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using System;
-using System.Runtime.InteropServices;
-
-namespace ENet
-{
-	public static unsafe partial class Native
-	{
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetAddress
-		{
-			public uint host;
-			public ushort port;
-		}
-
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetCallbacks
-		{
-			[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-			public delegate IntPtr malloc_cb(IntPtr size);
-
-			[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-			public delegate void free_cb(IntPtr memory);
-
-			[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-			public delegate void no_memory_cb();
-
-			public IntPtr malloc, free, no_memory;
-		}
-
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetCompressor
-		{
-			[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-			public delegate void compress_cb(
-					IntPtr context, IntPtr inBuffers, IntPtr inBufferCount, IntPtr inLimit, IntPtr outData, IntPtr outLimit);
-
-			[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-			public delegate void decompress_cb(IntPtr context, IntPtr inData, IntPtr inLimit, IntPtr outData, IntPtr outLimit);
-
-			[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-			public delegate void destroy_cb(IntPtr context);
-
-			public IntPtr context;
-			public IntPtr compress, decompress, destroy;
-		}
-
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetEvent
-		{
-			public readonly EventType type;
-			public readonly ENetPeer* peer;
-			public readonly byte channelID;
-			public readonly uint data;
-			public readonly ENetPacket* packet;
-		}
-
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetHost
-		{
-		}
-
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetListNode
-		{
-			public readonly ENetListNode* next;
-			public readonly ENetListNode* previous;
-		}
-
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetPacket
-		{
-			[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-			public delegate void freeCallback_cb(IntPtr packet);
-
-			public IntPtr referenceCount;
-			public readonly PacketFlags flags;
-			public readonly void* data;
-			public IntPtr dataLength;
-			public IntPtr freeCallback;
-		}
-
-		[StructLayout(LayoutKind.Sequential)]
-		public struct ENetPeer
-		{
-			public ENetListNode dispatchList;
-			public readonly ENetHost* host;
-			public readonly ushort outgoingPeerID;
-			public readonly ushort incomingPeerID;
-			public readonly uint connectID;
-			public readonly byte outgoingSessionID;
-			public readonly byte incomingSessionID;
-			public ENetAddress address;
-			public IntPtr data;
-			public readonly PeerState state;
-		}
-	}
-}

+ 35 - 93
CSharp/ThirdParty/ENetCS/Native.cs

@@ -1,30 +1,10 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using System;
+using System;
 using System.Runtime.InteropServices;
+using System.Text;
 
 namespace ENet
 {
-	public static unsafe partial class Native
+	public static class Native
 	{
 		private const string LIB = "ENet.dll";
 
@@ -39,29 +19,14 @@ namespace ENet
 		public const uint ENET_HOST_ANY = 0;
 		public const uint ENET_HOST_BROADCAST = 0xffffffff;
 
-		#region Address Functions
-
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_address_set_host(ref ENetAddress address, byte* hostName);
-
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
-
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_address_get_host(ref ENetAddress address, byte* hostName, uint nameLength);
+		public static extern int enet_address_set_host(ref ENetAddress address, string hostName);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_address_get_host(ref ENetAddress address, byte[] hostName, uint nameLength);
+		public static extern int enet_address_get_host(ref ENetAddress address, StringBuilder hostName, uint nameLength);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_address_get_host_ip(ref ENetAddress address, byte* hostIP, uint ipLength);
-
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_address_get_host_ip(ref ENetAddress address, byte[] hostIP, uint ipLength);
-
-		#endregion
-
-		#region Global Functions
+		public static extern int enet_address_get_host_ip(ref ENetAddress address, StringBuilder hostIp, uint ipLength);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern void enet_deinitialize();
@@ -73,57 +38,48 @@ namespace ENet
 		public static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_enable_crc(ENetHost* host);
-
-		#endregion
-
-		#region Host Functions
+		public static extern void enet_enable_crc(IntPtr host);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_host_compress_with_range_encoder(ENetHost* host);
+		public static extern int enet_host_compress_with_range_encoder(IntPtr host);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern ENetHost* enet_host_create(
-				ENetAddress* address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
+		public static extern IntPtr enet_host_create(
+				ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth, 
+				uint outgoingBandwidth);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern ENetHost* enet_host_create(
-				ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
+		public static extern IntPtr enet_host_create(
+				IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
+				uint outgoingBandwidth);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_host_destroy(ENetHost* host);
+		public static extern void enet_host_destroy(IntPtr host);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern ENetPeer* enet_host_connect(
-				ENetHost* 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(ENetHost* host, byte channelID, ENetPacket* packet);
+		public static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_host_compress(ENetHost* host, ENetCompressor* compressor);
+		public static extern void enet_host_compress(IntPtr host, IntPtr compressor);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_host_channel_limit(ENetHost* host, uint channelLimit);
+		public static extern void enet_host_channel_limit(IntPtr host, uint channelLimit);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_host_bandwidth_limit(ENetHost* host, uint incomingBandwidth, uint outgoingBandwidth);
+		public static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_host_flush(ENetHost* host);
+		public static extern void enet_host_flush(IntPtr host);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_host_check_events(ENetHost* host, out ENetEvent e);
+		public static extern int enet_host_check_events(IntPtr host, out IntPtr e);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_host_service(ENetHost* host, ENetEvent* e, uint timeout);
-
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_host_service(ENetHost* host, out ENetEvent e, uint timeout);
-
-		#endregion
-
-		#region Miscellaneous Functions
+		public static extern int enet_host_service(IntPtr host, ref IntPtr e, uint timeout);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern uint enet_time_get();
@@ -131,51 +87,39 @@ namespace ENet
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern void enet_time_set(uint newTimeBase);
 
-		#endregion
-
-		#region Packet Functions
-
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern ENetPacket* enet_packet_create(void* data, uint dataLength, PacketFlags flags);
+		public static extern IntPtr enet_packet_create(string data, uint dataLength, PacketFlags flags);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_packet_destroy(ENetPacket* packet);
+		public static extern void enet_packet_destroy(IntPtr packet);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_packet_resize(ENetPacket* packet, uint dataLength);
-
-		#endregion
-
-		#region Peer Functions
+		public static extern int enet_packet_resize(IntPtr packet, uint dataLength);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern void enet_peer_throttle_configure(
-				ENetPeer* peer, uint interval, uint acceleration, uint deceleration);
+				IntPtr peer, uint interval, uint acceleration, uint deceleration);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_peer_send(ENetPeer* peer, byte channelID, ENetPacket* packet);
+		public static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern ENetPacket* enet_peer_receive(ENetPeer* peer, out byte channelID);
+		public static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_peer_reset(ENetPeer* peer);
+		public static extern void enet_peer_reset(IntPtr peer);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_peer_ping(ENetPeer* peer);
+		public static extern void enet_peer_ping(IntPtr peer);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_peer_disconnect_now(ENetPeer* peer, uint data);
+		public static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_peer_disconnect(ENetPeer* peer, uint data);
+		public static extern void enet_peer_disconnect(IntPtr peer, uint data);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern void enet_peer_disconnect_later(ENetPeer* peer, uint data);
-
-		#endregion
-
-		#region C# Utility
+		public static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
 
 		public static bool memcmp(byte[] s1, byte[] s2)
 		{
@@ -212,7 +156,5 @@ namespace ENet
 			}
 			return i;
 		}
-
-		#endregion
 	}
 }

+ 17 - 123
CSharp/ThirdParty/ENetCS/Packet.cs

@@ -1,67 +1,31 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using System;
+using System;
 using System.Runtime.InteropServices;
 
 namespace ENet
 {
-	public sealed unsafe class Packet : IDisposable
+	public sealed class Packet : IDisposable
 	{
-		private Native.ENetPacket* packet;
+		private IntPtr packet;
 
-		public Packet(Native.ENetPacket* packet)
+		public Packet(IntPtr packet)
 		{
-			if (packet == null)
+			if (packet == IntPtr.Zero)
 			{
 				throw new InvalidOperationException("No native packet.");
 			}
 			this.packet = packet;
 		}
 
-		public Packet(): this(new byte[]{})
-		{
-		}
-
-		public Packet(byte[] data): this(data, 0, data.Length)
-		{
-		}
-
-		public Packet(byte[] data, int offset, int length, PacketFlags flags = PacketFlags.None)
+		public Packet(string data, PacketFlags flags = PacketFlags.None)
 		{
 			if (data == null)
 			{
 				throw new ArgumentNullException("data");
 			}
-			if (offset < 0 || length < 0 || length > data.Length - offset)
-			{
-				throw new ArgumentOutOfRangeException();
-			}
-			fixed (byte* bytes = data)
+			this.packet = Native.enet_packet_create(data, (uint)data.Length, flags);
+			if (this.packet == IntPtr.Zero)
 			{
-				this.packet = Native.enet_packet_create(bytes + offset, (uint)length, flags);
-				if (this.packet == null)
-				{
-					throw new ENetException(0, "Packet creation call failed.");
-				}
+				throw new ENetException(0, "Packet creation call failed.");
 			}
 		}
 
@@ -78,102 +42,32 @@ namespace ENet
 
 		private void Dispose(bool disposing)
 		{
-			if (this.packet == null)
+			if (this.packet == IntPtr.Zero)
 			{
 				return;
 			}
-
-			if (disposing)
-			{
-				if (this.packet->referenceCount == IntPtr.Zero)
-				{
-					Native.enet_packet_destroy(this.packet);
-				}
-			}
-
-			this.packet = null;
-		}
-
-		public void CopyTo(byte[] array)
-		{
-			if (array == null)
-			{
-				throw new ArgumentNullException("array");
-			}
-			this.CopyTo(array, 0, array.Length);
-		}
-
-		public void CopyTo(byte[] array, int offset, int length)
-		{
-			if (array == null)
-			{
-				throw new ArgumentNullException("array");
-			}
-			if (offset < 0 || length < 0 || length > array.Length - offset)
-			{
-				throw new ArgumentOutOfRangeException();
-			}
-
-			if (length > this.Length - offset)
-			{
-				throw new ArgumentOutOfRangeException();
-			}
-			if (length > 0)
-			{
-				Marshal.Copy((IntPtr) ((byte*) this.Data + offset), array, offset, length);
-			}
-		}
-
-		public byte[] GetBytes()
-		{
-			var array = new byte[this.Length];
-			this.CopyTo(array);
-			return array;
-		}
-
-		public void Resize(int length)
-		{
-			if (length < 0)
-			{
-				throw new ArgumentOutOfRangeException("length");
-			}
-			int ret = Native.enet_packet_resize(this.packet, (uint)length);
-			if (ret < 0)
-			{
-				throw new ENetException(ret, "Packet resize call failed.");
-			}
+			Native.enet_packet_destroy(this.packet);
+			this.packet = IntPtr.Zero;
 		}
 
-		public void* Data
+		public ENetPacket Struct
 		{
 			get
 			{
-				return this.packet->data;
+				return (ENetPacket)Marshal.PtrToStructure(this.packet, typeof(ENetPacket));
 			}
-		}
-
-		public int Length
-		{
-			get
+			set
 			{
-				if (this.packet->dataLength.ToPointer() > (void*) int.MaxValue)
-				{
-					throw new ENetException(0, "Packet too long!");
-				}
-				return (int) this.packet->dataLength;
+				Marshal.StructureToPtr(value, this.packet, false);
 			}
 		}
 
-		public Native.ENetPacket* NativeData
+		public IntPtr NativePtr
 		{
 			get
 			{
 				return this.packet;
 			}
-			set
-			{
-				this.packet = value;
-			}
 		}
 	}
 }

+ 0 - 34
CSharp/ThirdParty/ENetCS/PacketFlags.cs

@@ -1,34 +0,0 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using System;
-
-namespace ENet
-{
-	[Flags]
-	public enum PacketFlags
-	{
-		None = 0,
-		Reliable = 1 << 0,
-		Unsequenced = 1 << 1,
-		NoAllocate = 1 << 2
-	}
-}

+ 34 - 126
CSharp/ThirdParty/ENetCS/Peer.cs

@@ -1,47 +1,31 @@
-#region License
-
-/*
-ENet for C#
-Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-
-#endregion
-
-using System;
-using System.Diagnostics;
+using System;
+using System.Runtime.InteropServices;
 using System.Threading.Tasks;
 
 namespace ENet
 {
-	public unsafe class Peer : IDisposable
+	public class Peer : IDisposable
 	{
-		private readonly Host host;
-		private Native.ENetPeer* peer;
-		private Action<Event> connected;
-		private Action<Event> received;
-		private Action<Event> disconnect;
+		private static readonly PeerEventsManager peerEventsManager = new PeerEventsManager();
+
+		public static PeerEventsManager PeerEventsManager
+		{
+			get
+			{
+				return peerEventsManager;
+			}
+		}
+
+		private IntPtr peer;
 
-		public Peer(Host host, Native.ENetPeer* peer)
+		public Peer(IntPtr peer)
 		{
-			if (peer == null)
+			if (peer == IntPtr.Zero)
 			{
 				throw new InvalidOperationException("No native peer.");
 			}
 			this.peer = peer;
-			this.host = host;
-			this.host.Peers.Add(this);
+			PeerEventsManager.Add(peer);
 		}
 
 		~Peer()
@@ -57,128 +41,52 @@ namespace ENet
 
 		protected void Dispose(bool disposing)
 		{
-			if (this.peer == null)
+			if (this.peer == IntPtr.Zero)
 			{
 				return;
 			}
 
-			if (disposing)
-			{
-				Native.enet_peer_reset(this.peer);
-			}
-			this.host.Peers.Remove((int)this.peer->data);
-			this.peer = null;
+			PeerEventsManager.Remove(this.peer);
+			Native.enet_peer_reset(this.peer);
+			this.peer = IntPtr.Zero;
 		}
 
-		public Native.ENetPeer* NativeData
+		public ENetPeer Struct
 		{
 			get
 			{
-				return this.peer;
+				return (ENetPeer)Marshal.PtrToStructure(this.peer, typeof(ENetPeer));
 			}
 			set
 			{
-				this.peer = value;
+				Marshal.StructureToPtr(value, this.peer, false);
 			}
 		}
 
-		public PeerState State
+		public IntPtr NativePtr
 		{
 			get
 			{
-				return this.peer->state;
+				return this.peer;
 			}
 		}
 
-		public int Data
+		public PeerState State
 		{
 			get
 			{
-				return (int)this.peer->data;
-			}
-			set
-			{
-				this.peer->data = (IntPtr)value;
-			}
-		}
-
-		// peer连接上了调用该事件
-		public event Action<Event> Connected
-		{
-			add
-			{
-				connected += value;
-			}
-			remove
-			{
-				connected -= value;
+				return Struct.state;
 			}
 		}
 
-		public event Action<Event> Received
-		{
-			add
-			{
-				received += value;
-			}
-			remove
-			{
-				received -= value;
-			}
-		}
-
-		public event Action<Event> Disconnect
-		{
-			add
-			{
-				disconnect += value;
-			}
-			remove
-			{
-				disconnect -= value;
-			}
-		}
-
-		internal void OnConnected(Event e)
-		{
-			if (connected == null)
-			{
-				return;
-			}
-			connected(e);
-		}
-
-		internal void OnReceived(Event e)
-		{
-			if (received == null)
-			{
-				return;
-			}
-			received(e);
-		}
-
-		internal void OnDisconnect(Event e)
-		{
-			if (disconnect == null)
-			{
-				return;
-			}
-			disconnect(e);
-		}
-
 		public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
 		{
 			Native.enet_peer_throttle_configure(this.peer, interval, acceleration, deceleration);
 		}
 
-		public void Send(byte channelID, byte[] data)
-		{
-			this.Send(channelID, data, 0, data.Length);
-		}
-
-		public void Send(byte channelID, byte[] data, int offset, int length)
+		public void Send(byte channelID, string data)
 		{
-			using (var packet = new Packet(data, offset, length))
+			using (var packet = new Packet(data))
 			{
 				this.Send(channelID, packet);
 			}
@@ -186,20 +94,20 @@ namespace ENet
 
 		public void Send(byte channelID, Packet packet)
 		{
-			Native.enet_peer_send(this.peer, channelID, packet.NativeData);
+			Native.enet_peer_send(this.peer, channelID, packet.NativePtr);
 		}
 
 		public Task<Packet> ReceiveAsync()
 		{
 			var tcs = new TaskCompletionSource<Packet>();
-			this.Received += e => tcs.TrySetResult(e.Packet);
+			PeerEventsManager[this.peer].Received += e => tcs.TrySetResult(e.Packet);
 			return tcs.Task;
 		}
 
 		public Task<bool> DisconnectAsync(uint data)
 		{
 			var tcs = new TaskCompletionSource<bool>();
-			this.Disconnect += e => tcs.TrySetResult(true);
+			PeerEventsManager[this.peer].Disconnect += e => tcs.TrySetResult(true);
 			Native.enet_peer_disconnect(this.peer, data);
 			return tcs.Task;
 		}
@@ -207,7 +115,7 @@ namespace ENet
 		public Task<bool> DisconnectLaterAsync(uint data)
 		{
 			var tcs = new TaskCompletionSource<bool>();
-			this.Disconnect += e => tcs.TrySetResult(true);
+			PeerEventsManager[this.peer].Disconnect += e => tcs.TrySetResult(true);
 			Native.enet_peer_disconnect_later(this.peer, data);
 			return tcs.Task;
 		}

+ 74 - 0
CSharp/ThirdParty/ENetCS/PeerEvent.cs

@@ -0,0 +1,74 @@
+using System;
+
+namespace ENet
+{
+	public class PeerEvent
+	{
+		private Action<Event> connected;
+		private Action<Event> received;
+		private Action<Event> disconnect;
+
+		public event Action<Event> Connected
+		{
+			add
+			{
+				connected += value;
+			}
+			remove
+			{
+				connected -= value;
+			}
+		}
+
+		public event Action<Event> Received
+		{
+			add
+			{
+				received += value;
+			}
+			remove
+			{
+				received -= value;
+			}
+		}
+
+		public event Action<Event> Disconnect
+		{
+			add
+			{
+				disconnect += value;
+			}
+			remove
+			{
+				disconnect -= value;
+			}
+		}
+
+		internal void OnConnected(Event e)
+		{
+			if (connected == null)
+			{
+				return;
+			}
+			connected(e);
+		}
+
+		internal void OnReceived(Event e)
+		{
+			if (received == null)
+			{
+				return;
+			}
+			received(e);
+		}
+
+		internal void OnDisconnect(Event e)
+		{
+			if (disconnect == null)
+			{
+				return;
+			}
+			disconnect(e);
+		}
+	}
+}

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

@@ -0,0 +1,62 @@
+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);
+		}
+	}
+}

+ 0 - 47
CSharp/ThirdParty/ENetCS/PeerManager.cs

@@ -1,47 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace ENet
-{
-	public class PeerManager
-	{
-		private int num = 0;
-		private readonly Dictionary<int, Peer> peers = new Dictionary<int, Peer>();
-
-		public void Add(Peer peer)
-		{
-			++num;
-			unsafe
-			{
-				peer.NativeData->data = (IntPtr)num;
-			}
-			peers[num] = peer;
-		}
-
-		public void Remove(int key)
-		{
-			peers.Remove(key);
-		}
-
-		public bool ContainsKey(int key)
-		{
-			if (peers.ContainsKey(key))
-			{
-				return true;
-			}
-			return false;
-		}
-
-		public Peer this[int key]
-		{
-			get
-			{
-				return this.peers[key];
-			}
-			set
-			{
-				this.peers[key] = value;
-			}
-		}
-	}
-}

+ 0 - 17
CSharp/ThirdParty/ENetCS/PeerState.cs

@@ -1,17 +0,0 @@
-namespace ENet
-{
-	public enum PeerState
-	{
-		Uninitialized = -1,
-		Disconnected = 0,
-		Connecting = 1,
-		AcknowledgingConnect = 2,
-		ConnectionPending = 3,
-		ConnectionSucceeded = 4,
-		Connected = 5,
-		DisconnectLater = 6,
-		Disconnecting = 7,
-		AcknowledgingDisconnect = 8,
-		Zombie = 9
-	}
-}

+ 133 - 0
CSharp/ThirdParty/ENetCS/Structs.cs

@@ -0,0 +1,133 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Text;
+
+namespace ENet
+{
+	public enum EventType
+	{
+		None = 0,
+		Connect = 1,
+		Disconnect = 2,
+		Receive = 3
+	}
+
+	public enum PeerState
+	{
+		Uninitialized = -1,
+		Disconnected = 0,
+		Connecting = 1,
+		AcknowledgingConnect = 2,
+		ConnectionPending = 3,
+		ConnectionSucceeded = 4,
+		Connected = 5,
+		DisconnectLater = 6,
+		Disconnecting = 7,
+		AcknowledgingDisconnect = 8,
+		Zombie = 9
+	}
+
+	public enum AddressType
+	{
+		IPv4 = 0
+	}
+
+	[Flags]
+	public enum PacketFlags
+	{
+		None = 0,
+		Reliable = 1 << 0,
+		Unsequenced = 1 << 1,
+		NoAllocate = 1 << 2
+	}
+
+	[StructLayout(LayoutKind.Sequential)]
+	public struct ENetAddress
+	{
+		public uint host;
+		public ushort port;
+	}
+
+	[StructLayout(LayoutKind.Sequential)]
+	public struct ENetCallbacks
+	{
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+		public delegate IntPtr malloc_cb(IntPtr size);
+
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+		public delegate void free_cb(IntPtr memory);
+
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+		public delegate void no_memory_cb();
+
+		public IntPtr malloc, free, no_memory;
+	}
+
+	[StructLayout(LayoutKind.Sequential)]
+	public struct ENetCompressor
+	{
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+		public delegate void compress_cb(
+				IntPtr context, IntPtr inBuffers, IntPtr inBufferCount, IntPtr inLimit, IntPtr outData, IntPtr outLimit);
+
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+		public delegate void decompress_cb(IntPtr context, IntPtr inData, IntPtr inLimit, IntPtr outData, IntPtr outLimit);
+
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+		public delegate void destroy_cb(IntPtr context);
+
+		public IntPtr context;
+		public IntPtr compress, decompress, destroy;
+	}
+
+	[StructLayout(LayoutKind.Sequential)]
+	public struct ENetEvent
+	{
+		public readonly EventType type;
+		public readonly IntPtr peer;
+		public readonly byte channelID;
+		public readonly uint data;
+		public readonly IntPtr packet;
+	}
+
+	[StructLayout(LayoutKind.Sequential)]
+	public struct ENetHost
+	{
+	}
+
+	[StructLayout(LayoutKind.Sequential)]
+	public class ENetListNode
+	{
+		public ENetListNode next;
+		public ENetListNode previous;
+	}
+
+	[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
+	public delegate void ENetPacketFreeCallback(ref ENetPacket param0);
+
+	[StructLayoutAttribute(LayoutKind.Sequential)]
+	public struct ENetPacket
+	{
+		public uint referenceCount;
+		public uint flags;
+		[MarshalAsAttribute(UnmanagedType.LPStr)]
+		public StringBuilder data;
+		public uint dataLength;
+		public ENetPacketFreeCallback freeCallback;
+	}
+
+	[StructLayout(LayoutKind.Sequential)]
+	public struct ENetPeer
+	{
+		public ENetListNode dispatchList;
+		public readonly IntPtr host;
+		public readonly ushort outgoingPeerID;
+		public readonly ushort incomingPeerID;
+		public readonly uint connectID;
+		public readonly byte outgoingSessionID;
+		public readonly byte incomingSessionID;
+		public ENetAddress address;
+		public IntPtr data;
+		public readonly PeerState state;
+	}
+}