Преглед изворни кода

enet使用正常可以连接服务器,下阶段打算把enet api改造成async await,可以异步变同步使用

tanghai пре 13 година
родитељ
комит
ce2ab9f069

+ 49 - 11
CSharp/App/Modules/Robot/RobotViewModel.cs

@@ -1,5 +1,7 @@
 using System;
 using System.ComponentModel.Composition;
+using System.Threading;
+using System.Threading.Tasks;
 using ELog;
 using Microsoft.Practices.Prism.ViewModel;
 using ENet;
@@ -9,6 +11,7 @@ namespace Modules.Robot
 	[Export(contractType: typeof (RobotViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
 	internal class RobotViewModel : NotificationObject
 	{
+		private Host host = null;
 		private string logText = "";
 
 		public string LogText
@@ -28,21 +31,56 @@ namespace Modules.Robot
 			}
 		}
 
-		public void Start()
+		public RobotViewModel()
 		{
 			Library.Initialize();
-			var host = new Host(null, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
-			
-			var address = new Address { HostName = "192.168.10.246", Port = 8888 };
-			var peer = host.Connect(address, 1, 0);
 
-			var e = new Event();
-			while (host.CheckEvents(out e) > 0)
-			{
-				if (e.Type == EventType.Connect)
+			host = new Host(null, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
+
+			Task.Factory.StartNew(() =>
 				{
-					LogText += ("Connect OK\r\n");
-				}
+					while (host.Service(10) >= 0)
+					{
+						Event e;
+						while (host.CheckEvents(out e) > 0)
+						{
+							switch (e.Type)
+							{
+								case EventType.Receive:
+								{
+									LogText += "Receive OK\r\n";
+									Log.Debug("receive ok");
+									break;
+								}
+								case EventType.Disconnect:
+								{
+									e.Peer.Dispose();
+									break;
+								}
+							}
+						}
+					}
+				});
+
+
+		}
+
+		public async Task<Peer> StartClient()
+		{
+			return await Task.Factory.StartNew<Peer>(() =>
+				{
+					var address = new Address {Host = "192.168.10.246", Port = 8901};
+					var peer = this.host.Connect(address, 2, 1);
+					return peer;
+				});
+		}
+
+		public void Start()
+		{
+			var peer = StartClient().Result;
+			if (peer.State == PeerState.Connected)
+			{
+				Log.Debug("11111111111");
 			}
 		}
 	}

+ 1 - 0
CSharp/CSharp.sln.DotSettings

@@ -3,6 +3,7 @@
 	<s:Boolean x:Key="/Default/CodeEditing/Intellisense/LookupWindow/ShowSummary/@EntryValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/CodeEditing/Localization/CSharpLocalizationOptions/DontAnalyseVerbatimStrings/@EntryValue">False</s:Boolean>
 	<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/=ForCanBeConvertedToForeach/@EntryIndexedValue">DO_NOT_SHOW</s:String>
 	<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=InconsistentNaming/@EntryIndexedValue">DO_NOT_SHOW</s:String>

+ 2 - 0
CSharp/ThirdParty/ENet/enet.h

@@ -541,6 +541,8 @@ ENET_API void enet_range_coder_destroy (void *);
 ENET_API size_t enet_range_coder_compress (void *, const ENetBuffer *, size_t, size_t, enet_uint8 *, size_t);
 ENET_API size_t enet_range_coder_decompress (void *, const enet_uint8 *, size_t, enet_uint8 *, size_t);
 
+ENET_API void enet_enable_crc(ENetHost* host);
+
 extern size_t enet_protocol_command_size (enet_uint8);
 
 #ifdef __cplusplus

+ 5 - 0
CSharp/ThirdParty/ENet/host.c

@@ -125,6 +125,11 @@ ENetHost *enet_host_create(const ENetAddress * address, size_t peerCount, size_t
 	return host;
 }
 
+void enet_enable_crc(ENetHost* host)
+{
+	host->checksum = enet_crc32;
+}
+
 /** Destroys the host and all resources associated with it.
  @param host pointer to the host to destroy
  */

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

@@ -32,12 +32,6 @@ namespace ENet
 
 		private Native.ENetAddress address;
 
-		public Address(string hostName, ushort port) : this()
-		{
-			this.HostName = hostName;
-			this.Port = port;
-		}
-
 		public override bool Equals(object obj)
 		{
 			return obj is Address && this.Equals((Address) obj);
@@ -45,15 +39,15 @@ namespace ENet
 
 		public bool Equals(Address addr)
 		{
-			return this.Port == addr.Port && this.HostIP == addr.HostIP;
+			return this.Port == addr.Port && this.IP == addr.IP;
 		}
 
 		public override int GetHashCode()
 		{
-			return this.Port.GetHashCode() ^ this.HostIP.GetHashCode();
+			return this.Port.GetHashCode() ^ this.IP.GetHashCode();
 		}
 
-		public string HostIP
+		public string IP
 		{
 			get
 			{
@@ -69,7 +63,7 @@ namespace ENet
 			}
 		}
 
-		public string HostName
+		public string Host
 		{
 			get
 			{

+ 8 - 0
CSharp/ThirdParty/ENetCS/Event.cs

@@ -62,6 +62,10 @@ namespace ENet
 		{
 			get
 			{
+				if (this.e.packet == null)
+				{
+					return null;
+				}
 				return new Packet(this.e.packet);
 			}
 		}
@@ -70,6 +74,10 @@ namespace ENet
 		{
 			get
 			{
+				if (this.e.peer == null)
+				{
+					return null;
+				}
 				return new Peer(this.e.peer);
 			}
 		}

+ 23 - 40
CSharp/ThirdParty/ENetCS/Host.cs

@@ -32,22 +32,9 @@ namespace ENet
 		{
 		}
 
-		public Host(Address? address, uint peerLimit):
-			this(address, peerLimit, 0)
+		public Host(Address? address, uint peerLimit, uint channelLimit = 0, 
+				uint incomingBandwidth = 0, uint outgoingBandwidth = 0, bool enableCrc = true)
 		{
-		}
-
-		public Host(Address? address, uint peerLimit, uint channelLimit):
-			this(address, peerLimit, channelLimit, 0, 0)
-		{
-		}
-
-		public Host(Address? address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
-		{
-			if (this.host != null)
-			{
-				throw new InvalidOperationException("Already created.");
-			}
 			if (peerLimit > Native.ENET_PROTOCOL_MAXIMUM_PEER_ID)
 			{
 				throw new ArgumentOutOfRangeException("peerLimit");
@@ -67,67 +54,69 @@ namespace ENet
 					null, peerLimit, channelLimit, incomingBandwidth,
 					outgoingBandwidth);
 			}
+
 			if (this.host == null)
 			{
 				throw new ENetException(0, "Host creation call failed.");
 			}
+
+			if (enableCrc)
+			{
+				Native.enet_enable_crc(host);
+			}
 		}
 
 		~Host()
 		{
-			this.Dispose();
+			this.Dispose(false);
 		}
 
-		private static void CheckChannelLimit(uint channelLimit)
+		public void Dispose()
 		{
-			if (channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
-			{
-				throw new ArgumentOutOfRangeException("channelLimit");
-			}
+			this.Dispose(true);
+			GC.SuppressFinalize(this);
 		}
 
-		private void CheckCreated()
+		private void Dispose(bool disposing)
 		{
 			if (this.host == null)
 			{
-				throw new InvalidOperationException("Not created.");
+				return;
+			}
+
+			if (disposing)
+			{
+				Native.enet_host_destroy(this.host);
+				this.host = null;
 			}
 		}
 
-		public void Dispose()
+		private static void CheckChannelLimit(uint channelLimit)
 		{
-			if (this.host == null)
+			if (channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
 			{
-				return;
+				throw new ArgumentOutOfRangeException("channelLimit");
 			}
-			Native.enet_host_destroy(this.host);
-			this.host = null;
 		}
 
 		public void Broadcast(byte channelID, ref Packet packet)
 		{
-			this.CheckCreated();
-			packet.CheckCreated();
 			Native.enet_host_broadcast(this.host, channelID, packet.NativeData);
 			packet.NativeData = null; // Broadcast automatically clears this.
 		}
 
 		public void CompressWithRangeEncoder()
 		{
-			this.CheckCreated();
 			Native.enet_host_compress_with_range_encoder(this.host);
 		}
 
 		public void DoNotCompress()
 		{
-			this.CheckCreated();
 			Native.enet_host_compress(this.host, null);
 		}
 
 		public int CheckEvents(out Event e)
 		{
-			this.CheckCreated();
-
 			Native.ENetEvent nativeEvent;
 			int ret = Native.enet_host_check_events(this.host, out nativeEvent);
 			e = new Event(nativeEvent);
@@ -136,7 +125,6 @@ namespace ENet
 
 		public Peer Connect(Address address, uint channelLimit, uint data)
 		{
-			this.CheckCreated();
 			CheckChannelLimit(channelLimit);
 
 			Native.ENetAddress nativeAddress = address.NativeData;
@@ -151,7 +139,6 @@ namespace ENet
 
 		public void Flush()
 		{
-			this.CheckCreated();
 			Native.enet_host_flush(this.host);
 		}
 
@@ -161,7 +148,6 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException("timeout");
 			}
-			this.CheckCreated();
 			return Native.enet_host_service(this.host, null, (uint) timeout);
 		}
 
@@ -171,7 +157,6 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException("timeout");
 			}
-			this.CheckCreated();
 			Native.ENetEvent nativeEvent;
 
 			int ret = Native.enet_host_service(this.host, out nativeEvent, (uint) timeout);
@@ -181,14 +166,12 @@ namespace ENet
 
 		public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
 		{
-			this.CheckCreated();
 			Native.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
 		}
 
 		public void SetChannelLimit(uint channelLimit)
 		{
 			CheckChannelLimit(channelLimit);
-			this.CheckCreated();
 			Native.enet_host_channel_limit(this.host, channelLimit);
 		}
 	}

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

@@ -72,6 +72,9 @@ namespace ENet
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		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

+ 26 - 24
CSharp/ThirdParty/ENetCS/Packet.cs

@@ -24,15 +24,23 @@ using System.Runtime.InteropServices;
 
 namespace ENet
 {
-	public unsafe struct Packet : IDisposable
+	public unsafe class Packet : IDisposable
 	{
 		private Native.ENetPacket* packet;
 
 		public Packet(Native.ENetPacket* packet)
 		{
+			if (packet == null)
+			{
+				throw new InvalidOperationException("No native packet.");
+			}
 			this.packet = packet;
 		}
 
+		public Packet(): this(new byte[]{})
+		{
+		}
+
 		public Packet(byte[] data): this(data, 0, data.Length)
 		{
 		}
@@ -57,24 +65,31 @@ namespace ENet
 			}
 		}
 
+		~Packet()
+		{
+			Dispose(false);
+		}
+
 		public void Dispose()
+		{
+			Dispose(true);
+			GC.SuppressFinalize(this);
+		}
+
+		protected virtual void Dispose(bool disposing)
 		{
 			if (this.packet == null)
 			{
 				return;
 			}
-			if (this.packet->referenceCount == IntPtr.Zero)
-			{
-				Native.enet_packet_destroy(this.packet);
-			}
-			this.packet = null;
-		}
 
-		internal void CheckCreated()
-		{
-			if (this.packet == null)
+			if (disposing)
 			{
-				throw new InvalidOperationException("No native packet.");
+				if (this.packet->referenceCount == IntPtr.Zero)
+				{
+					Native.enet_packet_destroy(this.packet);
+				}
+				this.packet = null;
 			}
 		}
 
@@ -98,7 +113,6 @@ namespace ENet
 				throw new ArgumentOutOfRangeException();
 			}
 
-			this.CheckCreated();
 			if (length > this.Length - offset)
 			{
 				throw new ArgumentOutOfRangeException();
@@ -111,7 +125,6 @@ namespace ENet
 
 		public byte[] GetBytes()
 		{
-			this.CheckCreated();
 			var array = new byte[this.Length];
 			this.CopyTo(array);
 			return array;
@@ -123,7 +136,6 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException("length");
 			}
-			this.CheckCreated();
 			int ret = Native.enet_packet_resize(this.packet, (uint)length);
 			if (ret < 0)
 			{
@@ -135,7 +147,6 @@ namespace ENet
 		{
 			get
 			{
-				this.CheckCreated();
 				return this.packet->data;
 			}
 		}
@@ -144,7 +155,6 @@ namespace ENet
 		{
 			get
 			{
-				this.CheckCreated();
 				if (this.packet->dataLength.ToPointer() > (void*) int.MaxValue)
 				{
 					throw new ENetException(0, "Packet too long!");
@@ -164,13 +174,5 @@ namespace ENet
 				this.packet = value;
 			}
 		}
-
-		public bool IsSet
-		{
-			get
-			{
-				return this.packet != null;
-			}
-		}
 	}
 }

+ 26 - 37
CSharp/ThirdParty/ENetCS/Peer.cs

@@ -23,62 +23,71 @@ using System;
 
 namespace ENet
 {
-	public unsafe struct Peer
+	public unsafe class Peer : IDisposable
 	{
 		private Native.ENetPeer* peer;
 
 		public Peer(Native.ENetPeer* peer)
 		{
+			if (peer == null)
+			{
+				throw new InvalidOperationException("No native peer.");
+			}
 			this.peer = peer;
 		}
 
-		private void CheckCreated()
+		~Peer()
+		{
+			this.Dispose(false);
+		}
+
+		public void Dispose()
+		{
+			Dispose(true);
+			GC.SuppressFinalize(this);
+		}
+
+		protected void Dispose(bool disposing)
 		{
 			if (this.peer == null)
 			{
-				throw new InvalidOperationException("No native peer.");
+				return;
+			}
+
+			if (disposing)
+			{
+				Native.enet_peer_reset(this.peer);
+				this.peer = null;
 			}
 		}
 
 		public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
 		{
-			this.CheckCreated();
 			Native.enet_peer_throttle_configure(this.peer, interval, acceleration, deceleration);
 		}
 
 		public void Disconnect(uint data)
 		{
-			this.CheckCreated();
 			Native.enet_peer_disconnect(this.peer, data);
 		}
 
 		public void DisconnectLater(uint data)
 		{
-			this.CheckCreated();
 			Native.enet_peer_disconnect_later(this.peer, data);
 		}
 
 		public void DisconnectNow(uint data)
 		{
-			this.CheckCreated();
 			Native.enet_peer_disconnect_now(this.peer, data);
 		}
 
 		public void Ping()
 		{
-			this.CheckCreated();
 			Native.enet_peer_ping(this.peer);
 		}
 
-		public void Reset()
-		{
-			this.CheckCreated();
-			Native.enet_peer_reset(this.peer);
-		}
-
 		public bool Receive(out byte channelID, out Packet packet)
 		{
-			this.CheckCreated();
 			Native.ENetPacket* nativePacket = Native.enet_peer_receive(this.peer, out channelID);
 			if (nativePacket == null)
 			{
@@ -91,19 +100,11 @@ namespace ENet
 
 		public void Send(byte channelID, byte[] data)
 		{
-			if (data == null)
-			{
-				throw new ArgumentNullException("data");
-			}
 			this.Send(channelID, data, 0, data.Length);
 		}
 
 		public void Send(byte channelID, byte[] data, int offset, int length)
 		{
-			if (data == null)
-			{
-				throw new ArgumentNullException("data");
-			}
 			using (var packet = new Packet(data, offset, length))
 			{
 				this.Send(channelID, packet);
@@ -112,19 +113,9 @@ namespace ENet
 
 		public void Send(byte channelID, Packet packet)
 		{
-			this.CheckCreated();
-			packet.CheckCreated();
 			Native.enet_peer_send(this.peer, channelID, packet.NativeData);
 		}
 
-		public bool IsSet
-		{
-			get
-			{
-				return this.peer != null;
-			}
-		}
-
 		public Native.ENetPeer* NativeData
 		{
 			get
@@ -141,20 +132,18 @@ namespace ENet
 		{
 			get
 			{
-				return this.IsSet? this.peer->state : PeerState.Uninitialized;
+				return this.peer->state;
 			}
 		}
 
-		public IntPtr UserData
+		public IntPtr Data
 		{
 			get
 			{
-				this.CheckCreated();
 				return this.peer->data;
 			}
 			set
 			{
-				this.CheckCreated();
 				this.peer->data = value;
 			}
 		}