Explorar el Código

修改C# ENet 库,更加符合面向对象设计

tanghai hace 13 años
padre
commit
e8f94eb912

+ 3 - 3
CSharp/Platform/Enet/Address.cs

@@ -37,9 +37,9 @@ namespace ENet
 			return obj is Address && this.Equals((Address) obj);
 		}
 
-		public bool Equals(Address address)
+		public bool Equals(Address addr)
 		{
-			return this.Port == address.Port && Native.memcmp(this.GetHostBytes(), address.GetHostBytes());
+			return this.Port == addr.Port && Native.memcmp(this.GetHostBytes(), addr.GetHostBytes());
 		}
 
 		public override int GetHashCode()
@@ -57,7 +57,7 @@ namespace ENet
 			var name = new byte[256];
 			fixed (byte* hostName = name)
 			{
-				if (Native.enet_address_get_host(ref this.address, hostName, (IntPtr) name.Length) < 0)
+				if (Native.enet_address_get_host(ref this.address, hostName, (IntPtr)name.Length) < 0)
 				{
 					return null;
 				}

+ 1 - 1
CSharp/Platform/Enet/Event.cs

@@ -23,7 +23,7 @@ namespace ENet
 {
 	public unsafe struct Event
 	{
-		internal Native.ENetEvent e;
+		private Native.ENetEvent e;
 
 		public Event(Native.ENetEvent e)
 		{

+ 34 - 68
CSharp/Platform/Enet/Host.cs

@@ -27,44 +27,22 @@ namespace ENet
 	{
 		private Native.ENetHost* host;
 
-		~Host()
-		{
-			this.Dispose(false);
-		}
-
-		private static void CheckChannelLimit(int channelLimit)
-		{
-			if (channelLimit < 0 || channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
-			{
-				throw new ArgumentOutOfRangeException("channelLimit");
-			}
-		}
-
-		private void CheckCreated()
-		{
-			if (this.host == null)
-			{
-				throw new InvalidOperationException("Not created.");
-			}
-		}
-
-		public void Create(ushort port, int peerLimit)
+		public Host(ushort port, int peerLimit):
+			this(new Address { Port = port }, peerLimit)
 		{
-			var address = new Address {Port = port};
-			Create(address, peerLimit);
 		}
 
-		public void Create(Address? address, int peerLimit)
+		public Host(Address? address, int peerLimit):
+			this(address, peerLimit, 0)
 		{
-			this.Create(address, peerLimit, 0);
 		}
 
-		public void Create(Address? address, int peerLimit, int channelLimit)
+		public Host(Address? address, int peerLimit, int channelLimit):
+			this(address, peerLimit, channelLimit, 0, 0)
 		{
-			this.Create(address, peerLimit, channelLimit, 0, 0);
 		}
 
-		public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
+		public Host(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
 		{
 			if (this.host != null)
 			{
@@ -80,14 +58,14 @@ namespace ENet
 			{
 				Native.ENetAddress nativeAddress = address.Value.NativeData;
 				this.host = Native.enet_host_create(
-				                                    ref nativeAddress, (IntPtr) peerLimit, (IntPtr) channelLimit, incomingBandwidth,
-				                                    outgoingBandwidth);
+					ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth,
+					outgoingBandwidth);
 			}
 			else
 			{
 				this.host = Native.enet_host_create(
-				                                    null, (IntPtr) peerLimit, (IntPtr) channelLimit, incomingBandwidth,
-				                                    outgoingBandwidth);
+					null, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth,
+					outgoingBandwidth);
 			}
 			if (this.host == null)
 			{
@@ -95,12 +73,28 @@ namespace ENet
 			}
 		}
 
-		public void Dispose()
+		~Host()
+		{
+			this.Dispose();
+		}
+
+		private static void CheckChannelLimit(int channelLimit)
+		{
+			if (channelLimit < 0 || channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
+			{
+				throw new ArgumentOutOfRangeException("channelLimit");
+			}
+		}
+
+		private void CheckCreated()
 		{
-			this.Dispose(true);
+			if (this.host == null)
+			{
+				throw new InvalidOperationException("Not created.");
+			}
 		}
 
-		private void Dispose(bool disposing)
+		public void Dispose()
 		{
 			if (this.host == null)
 			{
@@ -133,13 +127,9 @@ namespace ENet
 		public int CheckEvents(out Event e)
 		{
 			this.CheckCreated();
+
 			Native.ENetEvent nativeEvent;
 			int ret = Native.enet_host_check_events(this.host, out nativeEvent);
-			if (ret <= 0)
-			{
-				e = new Event();
-				return ret;
-			}
 			e = new Event(nativeEvent);
 			return ret;
 		}
@@ -150,11 +140,12 @@ namespace ENet
 			CheckChannelLimit(channelLimit);
 
 			Native.ENetAddress nativeAddress = address.NativeData;
-			var peer = new Peer(Native.enet_host_connect(this.host, ref nativeAddress, (IntPtr) channelLimit, data));
-			if (peer.NativeData == null)
+			Native.ENetPeer* p = Native.enet_host_connect(this.host, ref nativeAddress, (IntPtr) channelLimit, data);
+			if (p == null)
 			{
 				throw new ENetException(0, "Host connect call failed.");
 			}
+			var peer = new Peer(p);
 			return peer;
 		}
 
@@ -184,11 +175,6 @@ namespace ENet
 			Native.ENetEvent nativeEvent;
 
 			int ret = Native.enet_host_service(this.host, out nativeEvent, (uint) timeout);
-			if (ret <= 0)
-			{
-				e = new Event();
-				return ret;
-			}
 			e = new Event(nativeEvent);
 			return ret;
 		}
@@ -205,25 +191,5 @@ namespace ENet
 			this.CheckCreated();
 			Native.enet_host_channel_limit(this.host, (IntPtr) channelLimit);
 		}
-
-		public bool IsSet
-		{
-			get
-			{
-				return this.host != null;
-			}
-		}
-
-		public Native.ENetHost* NativeData
-		{
-			get
-			{
-				return this.host;
-			}
-			set
-			{
-				this.host = value;
-			}
-		}
 	}
 }

+ 19 - 34
CSharp/Platform/Enet/Packet.cs

@@ -33,24 +33,11 @@ namespace ENet
 			this.packet = packet;
 		}
 
-		internal void CheckCreated()
-		{
-			if (this.packet == null)
-			{
-				throw new InvalidOperationException("No native packet.");
-			}
-		}
-
-		public void Create(byte[] data)
+		public Packet(byte[] data): this(data, 0, data.Length)
 		{
-			if (data == null)
-			{
-				throw new ArgumentNullException("data");
-			}
-			Create(data, 0, data.Length);
 		}
 
-		public void Create(byte[] data, int offset, int length, PacketFlags flags = PacketFlags.None)
+		public Packet(byte[] data, int offset, int length, PacketFlags flags = PacketFlags.None)
 		{
 			if (data == null)
 			{
@@ -62,21 +49,32 @@ namespace ENet
 			}
 			fixed (byte* bytes = data)
 			{
-				Create(bytes + offset, length, flags);
+				this.packet = Native.enet_packet_create(bytes + offset, (IntPtr)length, flags);
+				if (this.packet == null)
+				{
+					throw new ENetException(0, "Packet creation call failed.");
+				}
 			}
 		}
 
-		public void Create(void* data, int length, PacketFlags flags)
+		public void Dispose()
 		{
-			if (this.packet != null)
+			if (this.packet == null)
 			{
-				throw new InvalidOperationException("Already created.");
+				return;
 			}
+			if (this.packet->referenceCount == IntPtr.Zero)
+			{
+				Native.enet_packet_destroy(this.packet);
+			}
+			this.packet = null;
+		}
 
-			this.packet = Native.enet_packet_create(data, (IntPtr) length, flags);
+		internal void CheckCreated()
+		{
 			if (this.packet == null)
 			{
-				throw new ENetException(0, "Packet creation call failed.");
+				throw new InvalidOperationException("No native packet.");
 			}
 		}
 
@@ -119,19 +117,6 @@ namespace ENet
 			return array;
 		}
 
-		public void Dispose()
-		{
-			if (this.packet == null)
-			{
-				return;
-			}
-			if (this.packet->referenceCount == IntPtr.Zero)
-			{
-				Native.enet_packet_destroy(this.packet);
-			}
-			this.packet = null;
-		}
-
 		public void Resize(int length)
 		{
 			if (length < 0)

+ 1 - 2
CSharp/Platform/Enet/Peer.cs

@@ -104,9 +104,8 @@ namespace ENet
 			{
 				throw new ArgumentNullException("data");
 			}
-			using (var packet = new Packet())
+			using (var packet = new Packet(data, offset, length))
 			{
-				packet.Create(data, offset, length);
 				this.Send(channelID, packet);
 			}
 		}