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

+ 1 - 0
CSharp/App/Modules/Robot/Packages.config

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
+  <package id="BouncyCastle" version="1.7.0" targetFramework="net45" />
   <package id="CommonServiceLocator" version="1.0" targetFramework="net45" />
   <package id="Prism" version="4.1.0.0" targetFramework="net45" />
   <package id="Prism.MEFExtensions" version="4.1.0.0" targetFramework="net45" />

+ 11 - 0
CSharp/App/Modules/Robot/Robot.csproj

@@ -32,6 +32,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="BouncyCastle.Crypto">
+      <HintPath>..\..\..\packages\BouncyCastle.1.7.0\lib\Net20\BouncyCastle.Crypto.dll</HintPath>
+    </Reference>
     <Reference Include="Microsoft.Practices.Prism">
       <HintPath>..\..\..\packages\Prism.4.1.0.0\lib\NET40\Microsoft.Practices.Prism.dll</HintPath>
     </Reference>
@@ -62,10 +65,18 @@
     </Page>
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\..\..\Platform\ELog\ELog.csproj">
+      <Project>{72e16572-fc1f-4a9e-bc96-035417239298}</Project>
+      <Name>ELog</Name>
+    </ProjectReference>
     <ProjectReference Include="..\..\..\Platform\Infrastructure\Infrastructure.csproj">
       <Project>{48A2E149-0DAC-41B4-BB54-DFBCCD6D42B3}</Project>
       <Name>Infrastructure</Name>
     </ProjectReference>
+    <ProjectReference Include="..\..\..\ThirdParty\ENetSharp\ENetSharp.csproj">
+      <Project>{d0b4cfac-a368-4742-9863-68776cfa9938}</Project>
+      <Name>ENetSharp</Name>
+    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />

+ 1 - 1
CSharp/App/Modules/Robot/RobotView.xaml

@@ -9,7 +9,7 @@
 		<StackPanel>
 			<Button Name="btnStart" Content="开始" Width="80" Height="25" Click="btnStart_Click" />
 			<TextBox Name="tbLog" Text="{Binding LogText, Mode=OneWay}" Height="700"
-					VerticalScrollBarVisibility="Visible" />
+					VerticalScrollBarVisibility="Visible" TextChanged="tbLog_TextChanged" />
 		</StackPanel>
 	</Grid>
 </UserControl>

+ 6 - 2
CSharp/App/Modules/Robot/RobotView.xaml.cs

@@ -30,8 +30,12 @@ namespace Modules.Robot
 
 		private void btnStart_Click(object sender, RoutedEventArgs e)
 		{
-			this.ViewModel.Start();
-			this.tbLog.ScrollToEnd();
+			ViewModel.Start();
+		}
+
+		private void tbLog_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
+		{
+			tbLog.ScrollToEnd();
 		}
 	}
 }

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

@@ -1,6 +1,8 @@
 using System;
 using System.ComponentModel.Composition;
+using ELog;
 using Microsoft.Practices.Prism.ViewModel;
+using ENet;
 
 namespace Modules.Robot
 {
@@ -28,7 +30,17 @@ namespace Modules.Robot
 
 		public void Start()
 		{
-			this.LogText += "11111111111" + Environment.NewLine;
+			var address = new Address {HostName = "192.168.10.246", Port = 8888};
+			var host = new Host(address, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
+			var e = new Event();
+			var peer = host.Connect(address, 255, 0);
+			while (host.CheckEvents(out e) > 0)
+			{
+				if (e.Type == EventType.Connect)
+				{
+					LogText += ("Connect OK\r\n");
+				}
+			}
 		}
 	}
 }

+ 1 - 1
CSharp/ThirdParty/ENet/ENet.vcxproj

@@ -14,7 +14,7 @@
     <ProjectGuid>{C9992B7C-313E-4C9F-A954-640D01EDFB58}</ProjectGuid>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <Keyword>ManagedCProj</Keyword>
-    <RootNamespace>Enet</RootNamespace>
+    <RootNamespace>ENet</RootNamespace>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

+ 28 - 62
CSharp/ThirdParty/ENetSharp/Address.cs

@@ -32,6 +32,12 @@ 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);
@@ -39,63 +45,47 @@ namespace ENet
 
 		public bool Equals(Address addr)
 		{
-			return this.Port == addr.Port && Native.memcmp(this.GetHostBytes(), addr.GetHostBytes());
+			return this.Port == addr.Port && this.HostIP == addr.HostIP;
 		}
 
 		public override int GetHashCode()
 		{
-			return Type.GetHashCode() ^ this.Port.GetHashCode() ^ this.IPv4Host.GetHashCode();
-		}
-
-		public byte[] GetHostBytes()
-		{
-			return BitConverter.GetBytes(IPAddress.NetworkToHostOrder((int) this.IPv4Host));
+			return this.Port.GetHashCode() ^ this.HostIP.GetHashCode();
 		}
 
-		public string GetHostName()
+		public string HostIP
 		{
-			var name = new byte[256];
-			fixed (byte* hostName = name)
+			get
 			{
-				if (Native.enet_address_get_host(ref this.address, hostName, (IntPtr)name.Length) < 0)
+				var ip = new byte[256];
+				fixed (byte* hostIP = ip)
 				{
-					return null;
+					if (Native.enet_address_get_host_ip(ref this.address, hostIP, (IntPtr) ip.Length) < 0)
+					{
+						return null;
+					}
 				}
+				return Encoding.ASCII.GetString(ip, 0, Native.strlen(ip));
 			}
-			return BytesToString(name);
 		}
 
-		public string GetHostIP()
+		public string HostName
 		{
-			var ip = new byte[256];
-			fixed (byte* hostIP = ip)
+			get
 			{
-				if (Native.enet_address_get_host_ip(ref this.address, hostIP, (IntPtr) ip.Length) < 0)
+				var name = new byte[256];
+				fixed (byte* hostName = name)
 				{
-					return null;
+					if (Native.enet_address_get_host(ref this.address, hostName, (IntPtr)name.Length) < 0)
+					{
+						return null;
+					}
 				}
+				return Encoding.ASCII.GetString(name, 0, Native.strlen(name));
 			}
-			return BytesToString(ip);
-		}
-
-		public bool SetHost(string hostName)
-		{
-			if (hostName == null)
-			{
-				throw new ArgumentNullException("hostName");
-			}
-			return Native.enet_address_set_host(ref this.address, Encoding.ASCII.GetBytes(hostName)) == 0;
-		}
-
-		private static string BytesToString(byte[] bytes)
-		{
-			try
-			{
-				return Encoding.ASCII.GetString(bytes, 0, Native.strlen(bytes));
-			}
-			catch
+			set
 			{
-				return null;
+				Native.enet_address_set_host(ref this.address, Encoding.ASCII.GetBytes(value));
 			}
 		}
 
@@ -105,22 +95,6 @@ namespace ENet
 			{
 				return this.address;
 			}
-			set
-			{
-				this.address = value;
-			}
-		}
-
-		public uint IPv4Host
-		{
-			get
-			{
-				return this.address.host;
-			}
-			set
-			{
-				this.address.host = value;
-			}
 		}
 
 		public ushort Port
@@ -134,13 +108,5 @@ namespace ENet
 				this.address.port = value;
 			}
 		}
-
-		public static AddressType Type
-		{
-			get
-			{
-				return AddressType.IPv4;
-			}
-		}
 	}
 }