Browse Source

添加处理服务器发送信息逻辑

添加tcp逻辑错误的处理
master
sc 5 days ago
parent
commit
c6e55c166c
  1. 5
      AppShell.xaml.cs
  2. 17
      Class/SecureJsonService.cs
  3. 95
      Class/TcpServer.cs
  4. 2
      View/LogPage.xaml.cs

5
AppShell.xaml.cs

@ -23,8 +23,11 @@ namespace SunlightAggregationTerminal
}
else
{
//发送登录请求
TcpServer.Configuration(App.GlobalData.ServerID,
App.GlobalData.User, App.GlobalData.UserPassword,
Models.AppModels.GetAppUniqueId());
if (await AppModels.LogIn())
{//拒绝登陆时打开登录页面
var logpage = new View.LogPage();

17
Class/SecureJsonService.cs

@ -9,13 +9,13 @@ namespace SunlightAggregationTerminal.Class
{
public class SecureJsonService
{
private readonly byte[] _aesKey = Encoding.UTF8.GetBytes("12345678901234567890123456789012");
private static readonly byte[] _aesKey = Encoding.UTF8.GetBytes("12345678901234567890123456789012");
/// <summary>
/// 调用一:发送端处理
/// 流程:JSON序列化 -> GZip压缩 -> AES加密 -> Base64编码
/// </summary>
public string CompressEncryptAndSend(object dataObject)
public static string CompressEncryptAndSend(object dataObject)
{
try
{
@ -35,9 +35,8 @@ namespace SunlightAggregationTerminal.Class
return finalPayload;
}
catch (Exception ex)
catch (Exception)
{
Console.WriteLine($"发送端处理错误: {ex.Message}");
throw;
}
}
@ -46,7 +45,7 @@ namespace SunlightAggregationTerminal.Class
/// 调用二:接收端处理
/// 流程:Base64解码 -> AES解密 -> GZip解压 -> JSON反序列化
/// </summary>
public T? ReceiveDecryptAndDecompress<T>(string payload)
public static T? ReceiveDecryptAndDecompress<T>(string payload)
{
try
{
@ -73,7 +72,7 @@ namespace SunlightAggregationTerminal.Class
// --- 底层辅助方法 ---
private byte[] CompressBytes(byte[] data)
private static byte[] CompressBytes(byte[] data)
{
using (var outputStream = new MemoryStream())
{
@ -85,7 +84,7 @@ namespace SunlightAggregationTerminal.Class
}
}
private byte[] DecompressBytes(byte[] data)
private static byte[] DecompressBytes(byte[] data)
{
using (var inputStream = new MemoryStream(data))
{
@ -100,7 +99,7 @@ namespace SunlightAggregationTerminal.Class
}
}
private byte[] EncryptBytes(byte[] data)
private static byte[] EncryptBytes(byte[] data)
{
using (Aes aes = Aes.Create())
{
@ -126,7 +125,7 @@ namespace SunlightAggregationTerminal.Class
}
}
private byte[] DecryptBytes(byte[] data)
private static byte[] DecryptBytes(byte[] data)
{
using (Aes aes = Aes.Create())
{

95
Class/TcpServer.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using TouchSocket.Core;
using TouchSocket.Sockets;
@ -8,6 +9,15 @@ namespace SunlightAggregationTerminal.Class
{
public class TcpServer
{
public class Person
{
public required string User { get; set; }
public required string Password { get; set; }
public required string IP { get; set; }
public required string Terminal { get; set; }
public required string Dat { get; set; }
}
public static string ServerIP = "";
public static string ServerName = "";
public static string ServerPassword = "";
@ -23,18 +33,34 @@ namespace SunlightAggregationTerminal.Class
ServerTerminal = Terminal;
_= TcpClient(tcpClient, ServerIP, "7789");
_= TcpClient(tcpClient, ServerIP);
}
public static async void Query(string Dat)
public static async void TcpTransmit(string Dat)
{
var dat = new Person()
{
User = ServerName ,
Password = ServerPassword,
IP = ServerIP,
Terminal = ServerTerminal,
Dat = Dat
};
//SecureJsonService.CompressEncryptAndSend();
//发送字符串数据
await tcpClient.SendAsync("hello");
try
{
await tcpClient.SendAsync(JsonSerializer.Serialize(dat));
}
catch (Exception)
{
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "连接未启用", "是");
}
}
public static async Task TcpClient(TcpClient tcpClient, string ip, string port)
public static async Task TcpClient(TcpClient tcpClient, string ip)
{
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器
@ -45,24 +71,55 @@ namespace SunlightAggregationTerminal.Class
{
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
var mes = e.Memory.Span.ToString(Encoding.UTF8);
tcpClient.Logger.Info($"客户端接收到信息:{mes}");
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(mes);
if (data != null)
{
//处理服务器发送信息
if (data.TryGetValue("Enterprise", out var value_Enterprise))
{
if (value_Enterprise != null) { App.GlobalData.Enterprise = value_Enterprise.ToString() ?? ""; }
}
if (data.TryGetValue("Department", out var value_Department))
{
if (value_Department != null) { App.GlobalData.Department = value_Department.ToString() ?? ""; }
}
if (data.TryGetValue("Groups", out var value_Groups))
{
if (value_Groups != null) { App.GlobalData.Groups = value_Groups.ToString() ?? ""; }
}
if (data.TryGetValue("UserId", out var value_UserId))
{
if (value_Groups != null) { App.GlobalData.UserId = value_UserId.ToString() ?? ""; }
}
if (data.TryGetValue("UserName", out var value_UserName))
{
if (value_Groups != null) { App.GlobalData.Groups = value_UserName.ToString() ?? ""; }
}
}
return EasyTask.CompletedTask;
};
#endregion
await tcpClient.SetupAsync(new TouchSocketConfig()
.SetRemoteIPHost(ip + ":" + port)
.ConfigurePlugins(a =>
{
})
.ConfigureContainer(a =>
{
}
));
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
try
{
await tcpClient.SetupAsync(new TouchSocketConfig()
.SetRemoteIPHost(ip)
.ConfigurePlugins(a =>
{
a.UseReconnection<TcpClient>(options =>
{
options.PollingInterval = TimeSpan.FromSeconds(5);
});
})
.ConfigureContainer(a =>
{
}));
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
} catch (Exception)
{
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "不合法的访问地址", "是");
}
}
}
}

2
View/LogPage.xaml.cs

@ -73,6 +73,8 @@ public partial class LogPage : ContentPage
TcpServer.Configuration(SERVERIP.Text, UsernameEntry.Text, PasswordEntry.Text,
Models.AppModels.GetAppUniqueId());
TcpServer.TcpTransmit("Log");
LoadingIndicator.IsVisible = true;
if (await AppModels.LogIn())

Loading…
Cancel
Save