Jelajahi Sumber

全异步登录realm,使用c#新语法async await

tanghai 14 tahun lalu
induk
melakukan
6e9beb25fc

+ 4 - 0
CSharp/Editor/Editor.csproj

@@ -35,6 +35,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="AsyncCtpLibrary">
+      <HintPath>..\Packages\AsynCtp\AsyncCtpLibrary.dll</HintPath>
+    </Reference>
     <Reference Include="GalaSoft.MvvmLight.Extras.WPF4, Version=3.0.0.32852, Culture=neutral, PublicKeyToken=1673db7d5906b0ad, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\packages\MvvmLight.3.0.3\lib\net40\GalaSoft.MvvmLight.Extras.WPF4.dll</HintPath>
@@ -79,6 +82,7 @@
       <SubType>Designer</SubType>
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </None>
+    <Compile Include="Logical\LoginRealm.cs" />
     <Compile Include="Model\Main.cs" />
     <Compile Include="ViewModel\MainViewModel.cs" />
     <Compile Include="ViewModel\ViewModelLocator.cs" />

+ 71 - 0
CSharp/Editor/Logical/LoginRealm.cs

@@ -0,0 +1,71 @@
+using System;
+using System.Net.Sockets;
+using GalaSoft.MvvmLight.Threading;
+using System.Security.Cryptography;
+using System.Threading.Tasks;
+using System.Text;
+
+namespace Egametang
+{
+	enum RealmOpcode
+	{
+		REALM_AUTH_LOGON_CHALLENGE = 0x0000,
+		REALM_AUTH_LOGON_PROOF = 0x0001,
+		AUTH_RECONNECT_CHALLENGE = 0x02,
+		AUTH_RECONNECT_PROOF = 0x03,
+		AUTH_LOGON_PERMIT = 0x04,
+		REALM_LIST = 0x10,
+		SURVEY = 48,
+	}
+
+	class LoginRealm
+	{
+		private MainViewModel mainViewModel = null;
+
+		public LoginRealm(MainViewModel mainViewModel)
+		{
+			this.mainViewModel = mainViewModel;
+		}
+
+		public async void Login()
+		{
+			using (TcpClient tcpClient = new TcpClient())
+			{
+				// 异步连接
+				await tcpClient.ConnectAsync("192.168.10.246", 19000);
+				var stream = tcpClient.GetStream();
+
+				// 验证通行证
+				await LoginPermit(stream);
+			}
+		}
+
+		public async Task LoginPermit(NetworkStream stream)
+		{
+			byte[] opcodeBuffer = System.BitConverter.GetBytes((Int32)RealmOpcode.AUTH_LOGON_PERMIT);
+			await stream.WriteAsync(opcodeBuffer, 0, opcodeBuffer.Length);
+
+			string username = "egametang@163.com";
+			username += new string(' ', 128 - username.Length);
+			byte[] usernameBuffer = System.Text.Encoding.Default.GetBytes(username);
+			await stream.WriteAsync(usernameBuffer, 0, usernameBuffer.Length);
+
+			MD5 md5 = new MD5CryptoServiceProvider();
+			byte[] password = System.Text.Encoding.Default.GetBytes("163bio1");
+			byte[] passMD5Buffer = md5.ComputeHash(password);
+
+			string passMD5 = BitConverter.ToString(passMD5Buffer);
+			passMD5 = passMD5.Replace("-", "");
+			passMD5 = passMD5.ToLower();
+			App.Logger.Debug("passMD5: " + passMD5);
+
+			passMD5Buffer = System.Text.Encoding.Default.GetBytes(passMD5);
+			await stream.WriteAsync(passMD5Buffer, 0, passMD5Buffer.Length);
+
+			await DispatcherHelper.UIDispatcher.InvokeAsync(new Action(() =>
+			{
+				mainViewModel.LoginResult = "Login Permit!";
+			}));
+		}
+	}
+}

+ 6 - 19
CSharp/Editor/ViewModel/MainViewModel.cs

@@ -1,9 +1,5 @@
-using System;
-using System.Threading.Tasks;
-using System.Windows.Threading;
-using GalaSoft.MvvmLight;
+using GalaSoft.MvvmLight;
 using GalaSoft.MvvmLight.Command;
-using GalaSoft.MvvmLight.Threading;
 
 namespace Egametang
 {
@@ -11,10 +7,12 @@ namespace Egametang
 	{
 		private Main main = new Main();
 		private RelayCommand loginCmd = null;
+		private LoginRealm loginRealm = null;
 
 		public MainViewModel()
 		{
-			loginCmd = new RelayCommand(AsyncLogin);
+			loginCmd = new RelayCommand(Login);
+			loginRealm = new LoginRealm(this);
 		}
 
 		public string LoginResult
@@ -42,20 +40,9 @@ namespace Egametang
 			}
 		}
 
-		private void AsyncLogin()
+		private void Login()
 		{
-			var task = new Task(() =>
-			{
-			});
-			task.ContinueWith(_ =>
-			{
-				DispatcherHelper.UIDispatcher.BeginInvoke(DispatcherPriority.Normal,
-					new Action(() =>
-					{
-						LoginResult = "Login OK!";
-					}));
-			});
-			task.Start(App.OrderedTaskScheduler);
+			loginRealm.Login();
 		}
 
 		public override void Cleanup()