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

ENetCS库内部connect receive disconnect 使用event事件

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

+ 4 - 6
CSharp/App/Modules/Robot/RobotViewModel.cs

@@ -1,19 +1,17 @@
 using System;
 using System.ComponentModel.Composition;
-using System.Threading.Tasks;
 using System.Windows.Threading;
 using ELog;
 using Microsoft.Practices.Prism.Events;
 using Microsoft.Practices.Prism.ViewModel;
 using ENet;
-using Infrastructure;
 
 namespace Modules.Robot
 {
 	[Export(contractType: typeof (RobotViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
 	internal class RobotViewModel : NotificationObject
 	{
-		private ENetHost host;
+		private Host host;
 		private string logText = "";
 		private IEventAggregator eventAggregator = new EventAggregator();
 		private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
@@ -41,7 +39,7 @@ namespace Modules.Robot
 		public RobotViewModel()
 		{
 			Library.Initialize();
-			host = new ENetHost(8888, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
+			host = new Host(8888, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
 
 			timer.Tick += delegate { this.host.Run(); };
 			timer.Start();
@@ -51,12 +49,12 @@ namespace Modules.Robot
 		{
 			try
 			{
-				await host.ConnectAsync(new Address { Host = "192.168.10.246", Port = 8901 }, 2, 0);
+				Peer peer = await host.ConnectAsync(new Address { Host = "192.168.10.246", Port = 8901 }, 2, 0);
+				Log.Debug("peer data: " + peer.Data);
 			}
 			catch (ENetException e)
 			{
 				Log.Debug(e.Message);
-				return;
 			}
 		}
 

+ 1 - 0
CSharp/CSharp.sln.DotSettings

@@ -5,6 +5,7 @@
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckNamespace/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConvertIfStatementToReturnStatement/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConvertToAutoProperty/@EntryIndexedValue">DO_NOT_SHOW</s:String>
+	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=DelegateSubtraction/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ForCanBeConvertedToForeach/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=InconsistentNaming/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=LoopCanBeConvertedToQuery/@EntryIndexedValue">DO_NOT_SHOW</s:String>

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

@@ -43,15 +43,15 @@
     <Compile Include="Address.cs" />
     <Compile Include="AddressType.cs" />
     <Compile Include="ENetException.cs" />
-    <Compile Include="ENetEvent.cs" />
+    <Compile Include="Event.cs" />
     <Compile Include="EventType.cs" />
-    <Compile Include="ENetHost.cs" />
+    <Compile Include="Host.cs" />
     <Compile Include="Library.cs" />
     <Compile Include="Native.cs" />
     <Compile Include="Native.Structs.cs" />
-    <Compile Include="ENetPacket.cs" />
+    <Compile Include="Packet.cs" />
     <Compile Include="PacketFlags.cs" />
-    <Compile Include="ENetPeer.cs" />
+    <Compile Include="Peer.cs" />
     <Compile Include="PeerManager.cs" />
     <Compile Include="PeerState.cs" />
   </ItemGroup>

+ 6 - 6
CSharp/ThirdParty/ENetCS/ENetEvent.cs → CSharp/ThirdParty/ENetCS/Event.cs

@@ -23,12 +23,12 @@ using ELog;
 
 namespace ENet
 {
-	public unsafe struct ENetEvent
+	public unsafe struct Event
 	{
-		private readonly ENetHost host;
+		private readonly Host host;
 		private Native.ENetEvent e;
 
-		public ENetEvent(ENetHost host, Native.ENetEvent e)
+		public Event(Host host, Native.ENetEvent e)
 		{
 			this.e = e;
 			this.host = host;
@@ -62,7 +62,7 @@ namespace ENet
 			}
 		}
 
-		public ENetPacket Packet
+		public Packet Packet
 		{
 			get
 			{
@@ -70,11 +70,11 @@ namespace ENet
 				{
 					return null;
 				}
-				return new ENetPacket(this.e.packet);
+				return new Packet(this.e.packet);
 			}
 		}
 
-		public ENetPeer ENetPeer
+		public Peer Peer
 		{
 			get
 			{

+ 52 - 54
CSharp/ThirdParty/ENetCS/ENetHost.cs → CSharp/ThirdParty/ENetCS/Host.cs

@@ -26,17 +26,17 @@ using System.Threading.Tasks;
 
 namespace ENet
 {
-	public sealed unsafe class ENetHost : IDisposable
+	public sealed unsafe class Host : IDisposable
 	{
 		private Native.ENetHost* host;
-		private PeerManager peerManager = new PeerManager();
+		private readonly PeerManager peerManager = new PeerManager();
 
-		public ENetHost(ushort port, uint peerLimit):
+		public Host(ushort port, uint peerLimit):
 			this(new Address { Port = port }, peerLimit)
 		{
 		}
 
-		public ENetHost(Address? address, uint peerLimit, uint channelLimit = 0, 
+		public Host(Address? address, uint peerLimit, uint channelLimit = 0, 
 				uint incomingBandwidth = 0, uint outgoingBandwidth = 0, bool enableCrc = true)
 		{
 			if (peerLimit > Native.ENET_PROTOCOL_MAXIMUM_PEER_ID)
@@ -70,7 +70,7 @@ namespace ENet
 			}
 		}
 
-		~ENetHost()
+		~Host()
 		{
 			this.Dispose(false);
 		}
@@ -91,27 +91,58 @@ namespace ENet
 			if (disposing)
 			{
 				Native.enet_host_destroy(this.host);
-				this.host = null;
 			}
+
+			this.host = null;
 		}
 
-		public PeerManager Peers
+		private static void CheckChannelLimit(uint channelLimit)
 		{
-			get
+			if (channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
 			{
-				return peerManager;
+				throw new ArgumentOutOfRangeException("channelLimit");
 			}
 		}
 
-		private static void CheckChannelLimit(uint channelLimit)
+		private int CheckEvents(out Event e)
 		{
-			if (channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
+			Native.ENetEvent nativeEvent;
+			int ret = Native.enet_host_check_events(this.host, out nativeEvent);
+			e = new Event(this, nativeEvent);
+			return ret;
+		}
+
+		private int Service(int timeout)
+		{
+			if (timeout < 0)
 			{
-				throw new ArgumentOutOfRangeException("channelLimit");
+				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;
 			}
 		}
 
-		public void Broadcast(byte channelID, ref ENetPacket packet)
+		public void Broadcast(byte channelID, ref Packet packet)
 		{
 			Native.enet_host_broadcast(this.host, channelID, packet.NativeData);
 			packet.NativeData = null; // Broadcast automatically clears this.
@@ -127,31 +158,21 @@ namespace ENet
 			Native.enet_host_compress(this.host, null);
 		}
 
-		public int CheckEvents(out ENetEvent e)
-		{
-			Native.ENetEvent nativeEvent;
-			int ret = Native.enet_host_check_events(this.host, out nativeEvent);
-			e = new ENetEvent(this, nativeEvent);
-			return ret;
-		}
-
-		public Task<ENetPeer> ConnectAsync(
+		public Task<Peer> ConnectAsync(
 			Address address, uint channelLimit = Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 
 			uint data = 0)
 		{
 			CheckChannelLimit(channelLimit);
 
-			var tcs = new TaskCompletionSource<ENetPeer>();
+			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)
 			{
 				throw new ENetException(0, "Host connect call failed.");
 			}
-			new ENetPeer(this, p)
-			{
-				Connected = e => tcs.TrySetResult(e.ENetPeer)
-			};
+			var peer = new Peer(this, p);
+			peer.Connected += e => tcs.TrySetResult(e.Peer);
 			return tcs.Task;
 		}
 
@@ -160,28 +181,6 @@ namespace ENet
 			Native.enet_host_flush(this.host);
 		}
 
-		public int Service(int timeout)
-		{
-			if (timeout < 0)
-			{
-				throw new ArgumentOutOfRangeException("timeout");
-			}
-			return Native.enet_host_service(this.host, null, (uint) timeout);
-		}
-
-		public int Service(int timeout, out ENetEvent 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 ENetEvent(this, nativeEvent);
-			return ret;
-		}
-
 		public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
 		{
 			Native.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
@@ -200,25 +199,24 @@ namespace ENet
 				return;
 			}
 
-			ENetEvent e;
+			Event e;
 			while (this.CheckEvents(out e) > 0)
 			{
 				switch (e.Type)
 				{
 					case EventType.Connect:
 					{
-						e.ENetPeer.Connected(e);
+						e.Peer.OnConnected(e);
 						break;
 					}
 					case EventType.Receive:
 					{
-						e.ENetPeer.Received(e);
+						e.Peer.OnReceived(e);
 						break;
 					}
 					case EventType.Disconnect:
 					{
-						Log.Debug("Disconnect");
-						e.ENetPeer.Disconnect(e);
+						e.Peer.OnDisconnect(e);
 						break;
 					}
 				}

+ 8 - 7
CSharp/ThirdParty/ENetCS/ENetPacket.cs → CSharp/ThirdParty/ENetCS/Packet.cs

@@ -24,11 +24,11 @@ using System.Runtime.InteropServices;
 
 namespace ENet
 {
-	public sealed unsafe class ENetPacket : IDisposable
+	public sealed unsafe class Packet : IDisposable
 	{
 		private Native.ENetPacket* packet;
 
-		public ENetPacket(Native.ENetPacket* packet)
+		public Packet(Native.ENetPacket* packet)
 		{
 			if (packet == null)
 			{
@@ -37,15 +37,15 @@ namespace ENet
 			this.packet = packet;
 		}
 
-		public ENetPacket(): this(new byte[]{})
+		public Packet(): this(new byte[]{})
 		{
 		}
 
-		public ENetPacket(byte[] data): this(data, 0, data.Length)
+		public Packet(byte[] data): this(data, 0, data.Length)
 		{
 		}
 
-		public ENetPacket(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)
 			{
@@ -65,7 +65,7 @@ namespace ENet
 			}
 		}
 
-		~ENetPacket()
+		~Packet()
 		{
 			Dispose(false);
 		}
@@ -89,8 +89,9 @@ namespace ENet
 				{
 					Native.enet_packet_destroy(this.packet);
 				}
-				this.packet = null;
 			}
+
+			this.packet = null;
 		}
 
 		public void CopyTo(byte[] array)

+ 86 - 70
CSharp/ThirdParty/ENetCS/ENetPeer.cs → CSharp/ThirdParty/ENetCS/Peer.cs

@@ -20,19 +20,20 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #endregion
 
 using System;
+using System.Diagnostics;
 using System.Threading.Tasks;
 
 namespace ENet
 {
-	public unsafe class ENetPeer : IDisposable
+	public unsafe class Peer : IDisposable
 	{
-		private readonly ENetHost host;
+		private readonly Host host;
 		private Native.ENetPeer* peer;
-		private Action<ENetEvent> connected;
-		private Action<ENetEvent> received;
-		private Action<ENetEvent> disconnect;
+		private Action<Event> connected;
+		private Action<Event> received;
+		private Action<Event> disconnect;
 
-		public ENetPeer(ENetHost host, Native.ENetPeer* peer)
+		public Peer(Host host, Native.ENetPeer* peer)
 		{
 			if (peer == null)
 			{
@@ -43,7 +44,7 @@ namespace ENet
 			this.host.Peers.Add(this);
 		}
 
-		~ENetPeer()
+		~Peer()
 		{
 			this.Dispose(false);
 		}
@@ -69,53 +70,100 @@ namespace ENet
 			this.peer = null;
 		}
 
-		// peer连接上了调用该回调方法
-		public Action<ENetEvent> Connected
+		public Native.ENetPeer* NativeData
 		{
 			get
 			{
-				if (connected == null)
-				{
-					return e => { };
-				}
-				return connected;
+				return this.peer;
 			}
 			set
 			{
-				connected = value;
+				this.peer = value;
 			}
 		}
 
-		public Action<ENetEvent> Received
+		public PeerState State
 		{
 			get
 			{
-				if (received == null)
-				{
-					return e => { };
-				}
-				return received;
+				return this.peer->state;
+			}
+		}
+
+		public int Data
+		{
+			get
+			{
+				return (int)this.peer->data;
 			}
 			set
 			{
-				received = value;
+				this.peer->data = (IntPtr)value;
 			}
 		}
 
-		public Action<ENetEvent> Disconnect
+		// peer连接上了调用该事件
+		public event Action<Event> Connected
 		{
-			get
+			add
 			{
-				if (disconnect == null)
-				{
-					return e => { };
-				}
-				return disconnect;
+				connected += value;
 			}
-			set
+			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)
 			{
-				disconnect = value;
+				return;
 			}
+			disconnect(e);
 		}
 
 		public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
@@ -130,28 +178,28 @@ namespace ENet
 
 		public void Send(byte channelID, byte[] data, int offset, int length)
 		{
-			using (var packet = new ENetPacket(data, offset, length))
+			using (var packet = new Packet(data, offset, length))
 			{
 				this.Send(channelID, packet);
 			}
 		}
 
-		public void Send(byte channelID, ENetPacket packet)
+		public void Send(byte channelID, Packet packet)
 		{
 			Native.enet_peer_send(this.peer, channelID, packet.NativeData);
 		}
 
-		public Task<ENetPacket> ReceiveAsync()
+		public Task<Packet> ReceiveAsync()
 		{
-			var tcs = new TaskCompletionSource<ENetPacket>();
-			this.Received = e => tcs.TrySetResult(e.Packet);
+			var tcs = new TaskCompletionSource<Packet>();
+			this.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);
+			this.Disconnect += e => tcs.TrySetResult(true);
 			Native.enet_peer_disconnect(this.peer, data);
 			return tcs.Task;
 		}
@@ -159,7 +207,7 @@ namespace ENet
 		public Task<bool> DisconnectLaterAsync(uint data)
 		{
 			var tcs = new TaskCompletionSource<bool>();
-			this.Disconnect = e => tcs.TrySetResult(true);
+			this.Disconnect += e => tcs.TrySetResult(true);
 			Native.enet_peer_disconnect_later(this.peer, data);
 			return tcs.Task;
 		}
@@ -173,37 +221,5 @@ namespace ENet
 		{
 			Native.enet_peer_ping(this.peer);
 		}
-
-		public Native.ENetPeer* NativeData
-		{
-			get
-			{
-				return this.peer;
-			}
-			set
-			{
-				this.peer = value;
-			}
-		}
-
-		public PeerState State
-		{
-			get
-			{
-				return this.peer->state;
-			}
-		}
-
-		public IntPtr Data
-		{
-			get
-			{
-				return this.peer->data;
-			}
-			set
-			{
-				this.peer->data = value;
-			}
-		}
 	}
 }

+ 3 - 3
CSharp/ThirdParty/ENetCS/PeerManager.cs

@@ -6,9 +6,9 @@ namespace ENet
 	public class PeerManager
 	{
 		private int num = 0;
-		private readonly Dictionary<int, ENetPeer> peers = new Dictionary<int, ENetPeer>();
+		private readonly Dictionary<int, Peer> peers = new Dictionary<int, Peer>();
 
-		public void Add(ENetPeer peer)
+		public void Add(Peer peer)
 		{
 			++num;
 			unsafe
@@ -32,7 +32,7 @@ namespace ENet
 			return false;
 		}
 
-		public ENetPeer this[int key]
+		public Peer this[int key]
 		{
 			get
 			{