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

ENetCS Address类直接放到C#层,不需要再包装ENetAddress

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

+ 1 - 1
CSharp/App/Editor/Editor.csproj

@@ -119,7 +119,7 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
-    <None Include="app.config" />
+    <None Include="App.config" />
     <None Include="NLog.config">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
       <SubType>Designer</SubType>

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

@@ -67,18 +67,12 @@ namespace Modules.Robot
 		{
 			try
 			{
-				var address = new Address { Host = "192.168.10.246", Port = 8901 };
+				var address = new Address { HostName = "192.168.10.246", Port = 8901 };
 				using (Peer peer = await this.host.ConnectAsync(address))
 				{
 					using (Packet packet = await peer.ReceiveAsync())
 					{
-						var builder = new StringBuilder();
 						var bytes = packet.Bytes;
-						for (int i = 0; i < bytes.Length; ++i)
-						{
-							var b = bytes[i];
-							builder.Append(b.ToString("X2"));
-						}
 						var packetStream = new MemoryStream(bytes, 4, bytes.Length - 4);
 						var smsg = Serializer.Deserialize<SMSG_Auth_Challenge>(packetStream);
 						Logger.Debug(string.Format(

+ 28 - 26
CSharp/ThirdParty/ENetCS/Address.cs

@@ -1,59 +1,61 @@
 using System;
-using System.Text;
+using System.Net;
 
 namespace ENet
 {
-	public struct Address: IEquatable<Address>
+	public struct Address
 	{
-		private ENetAddress address;
+		private uint ip;
+		private ushort port;
 
-		public bool Equals(Address addr)
-		{
-			ENetAddress enetAddr = addr.Struct;
-			return this.address.host == enetAddr.host && this.address.port == enetAddr.port;
-		}
-
-		public string IP
+		public uint Ip
 		{
 			get
 			{
-				var hostIP = new StringBuilder(256);
-				NativeMethods.enet_address_get_host_ip(ref this.address, hostIP, (uint) hostIP.Length);
-				return hostIP.ToString();
+				return this.ip;
+			}
+			set
+			{
+				this.ip = value;
 			}
 		}
 
-		public string Host
+		public ushort Port
 		{
 			get
 			{
-				var hostName = new StringBuilder(256);
-				NativeMethods.enet_address_get_host(ref this.address, hostName, (uint) hostName.Length);
-				return hostName.ToString();
+				return this.port;
 			}
 			set
 			{
-				NativeMethods.enet_address_set_host(ref this.address, value);
+				this.port = value;
 			}
 		}
 
-		public ENetAddress Struct
+		public string HostName
 		{
 			get
 			{
-				return this.address;
+				var hostInfo = Dns.GetHostEntry(new IPAddress(this.ip));
+				return hostInfo.HostName;
+			}
+			set
+			{
+				var nameToIpAddress = Dns.GetHostEntry(value);
+				foreach (IPAddress address in nameToIpAddress.AddressList)
+				{
+					this.ip = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
+					return;
+				}
 			}
 		}
 
-		public ushort Port
+		public ENetAddress Struct
 		{
 			get
 			{
-				return this.address.port;
-			}
-			set
-			{
-				this.address.port = value;
+				var address = new ENetAddress { host = this.ip, port = this.port };
+				return address;
 			}
 		}
 	}