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

发现可用TaskCompletionSource实现 enet async await异步方法,不过仍需要做大量修改

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

+ 3 - 30
CSharp/App/Modules/Robot/RobotViewModel.cs

@@ -11,7 +11,7 @@ namespace Modules.Robot
 	[Export(contractType: typeof (RobotViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
 	internal class RobotViewModel : NotificationObject
 	{
-		private Host host = null;
+		private Host host;
 		private string logText = "";
 
 		public string LogText
@@ -35,31 +35,9 @@ namespace Modules.Robot
 		{
 			Library.Initialize();
 
-			host = new Host(null, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
-
 			Task.Factory.StartNew(() =>
 				{
-					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;
-								}
-							}
-						}
-					}
+				
 				});
 
 
@@ -67,12 +45,7 @@ namespace Modules.Robot
 
 		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()

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

@@ -43,6 +43,7 @@
     <Compile Include="Address.cs" />
     <Compile Include="AddressType.cs" />
     <Compile Include="ENetException.cs" />
+    <Compile Include="ENetClient.cs" />
     <Compile Include="Event.cs" />
     <Compile Include="EventType.cs" />
     <Compile Include="Host.cs" />

+ 60 - 0
CSharp/ThirdParty/ENetCS/ENetClient.cs

@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ENet
+{
+	public class ENetClient
+	{
+		private readonly Host host;
+
+		private readonly Dictionary<uint, TaskCompletionSource<Peer>> peerTcs = new Dictionary<uint, TaskCompletionSource<Peer>>();
+
+		public ENetClient(uint peerLimit)
+		{
+			host = new Host(null, peerLimit);
+		}
+
+		public void Poll(int timeout)
+		{
+			while (host.Service(timeout) >= 0)
+			{
+				Event e;
+				while (host.CheckEvents(out e) > 0)
+				{
+					switch (e.Type)
+					{
+						case EventType.Connect:
+						{
+							var tcs = peerTcs[e.Peer.ConnectID];
+							tcs.TrySetResult(e.Peer);
+							peerTcs.Remove(e.Peer.ConnectID);
+							break;
+						}
+						case EventType.Receive:
+						{
+							break;
+						}
+						case EventType.Disconnect:
+						{
+							break;
+						}
+					}
+				}
+			}
+		}
+
+		public Task<Peer> ConnectAsync(string hostName, ushort port)
+		{
+			var tcs = new TaskCompletionSource<Peer>();
+			var address = new Address { Host = "192.168.10.246", Port = 8901 };
+			var peer = this.host.Connect(address, 2, 1);
+			uint id = peer.ConnectID;
+			peerTcs[id] = tcs;
+			var t = tcs.Task;
+			return t;
+		}
+	}
+}

+ 9 - 0
CSharp/ThirdParty/ENetCS/Peer.cs

@@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #endregion
 
 using System;
+using System.Threading.Tasks;
 
 namespace ENet
 {
@@ -147,5 +148,13 @@ namespace ENet
 				this.peer->data = value;
 			}
 		}
+
+		public uint ConnectID
+		{
+			get
+			{
+				return this.peer->connectID;
+			}
+		}
 	}
 }