200 lines
9.2 KiB
C#
200 lines
9.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Security.Cryptography.Pkcs;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Windows;
|
|
using System.Windows.Markup;
|
|
using System.Xml.Linq;
|
|
using TouchSocket.Core;
|
|
using TouchSocket.Sockets;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace ForwardingServerUI.Class
|
|
{
|
|
public class AsyncTcpServer
|
|
{
|
|
public class Personm
|
|
{
|
|
public required string SERVEREnterprise { get; set; }
|
|
public required string SERVERMachineName { get; set; }
|
|
public required string SERVERNAME { get; set; }
|
|
public required string SERVERUSER { get; set; }
|
|
public required string SERRVERPASWORD { get; set; }
|
|
public required string MachineVer { get; set; }
|
|
}
|
|
|
|
private static Timer? _timer;
|
|
public static TcpService service = new TcpService();
|
|
|
|
public static async Task TcpMain()
|
|
{
|
|
service.Connecting = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在连接
|
|
service.Connected = (client, e) => { return EasyTask.CompletedTask;};//有客户端成功连接
|
|
service.Closing = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在断开连接,只有当主动断开时才有效。
|
|
service.Closed = (client, e) => {
|
|
//离线记录
|
|
|
|
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "] NOT ID " +
|
|
client.Id + " IP " + client.IP);
|
|
|
|
DataRow[] dat = MainWindowViewModel._machineTable.Select("ID='"+client.Id+"'");
|
|
foreach (DataRow row in dat)
|
|
{
|
|
Application.Current.Dispatcher.Invoke(() => {
|
|
MainWindowViewModel._machineTable.Rows.Remove(row);
|
|
});
|
|
|
|
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "] NOT LINK ID = " +
|
|
row["ID"] + " IP = " + row["IP"] + " Enterprise = " + row["Enterprise"] +
|
|
" MachineName = " + row["MachineName"]);
|
|
}
|
|
return EasyTask.CompletedTask;
|
|
};//有客户端断开连接
|
|
service.Received = (client, e) =>{
|
|
string DAT = e.Memory.Span.ToString(Encoding.UTF8);
|
|
try
|
|
{
|
|
if (DAT.Length > 5)
|
|
{
|
|
DataRow[] r_ = MainWindowViewModel._machineTable.Select("ID='" + client.Id + "'");
|
|
if (r_.Length > 0)
|
|
{
|
|
int? s_ = r_.First().Field<int?>("Start");
|
|
|
|
if (s_ == 30)
|
|
{
|
|
string trimmedDat = DAT.Trim();
|
|
if (!string.IsNullOrEmpty(trimmedDat) &&
|
|
(trimmedDat[0] == '{' || trimmedDat[0] == '['))
|
|
{
|
|
MainWindowViewModel.Person deserializedPerson = JsonSerializer.Deserialize<MainWindowViewModel.Person>(trimmedDat)!;
|
|
|
|
//转发逻辑
|
|
var dr = MainWindowViewModel._userTable.Select("Terminal='" + deserializedPerson.Terminal + "'");
|
|
if (dr.Length > 0)
|
|
{
|
|
DataRow drEmployee = dr.First();
|
|
drEmployee.BeginEdit();
|
|
drEmployee["RDAT"] = trimmedDat;
|
|
drEmployee.EndEdit();
|
|
drEmployee.AcceptChanges();
|
|
}
|
|
}
|
|
|
|
}
|
|
else if (s_ == 90)
|
|
{ }
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
DataRow drEmployee = r_.First();
|
|
drEmployee.BeginEdit();
|
|
drEmployee["HeartbeatTime"] = DateTime.Now.ToString();
|
|
drEmployee.EndEdit();
|
|
drEmployee.AcceptChanges();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
Personm deserializedPerson = JsonSerializer.Deserialize<Personm>(DAT)!;
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
var mach = MainWindowViewModel._machineTable.Select("Enterprise='" + deserializedPerson.SERVEREnterprise +
|
|
"'and MachineName='" + deserializedPerson.SERVERMachineName + "'");
|
|
if (mach.Length > 0)
|
|
{
|
|
//刷新设备信息表
|
|
DataRow dataRow = mach.First();
|
|
dataRow.BeginEdit();
|
|
dataRow["ID"] = client.Id;
|
|
dataRow["IP"] = client.IP;
|
|
dataRow["Start"] = 30;
|
|
dataRow["Enterprise"] = "/" + deserializedPerson.SERVEREnterprise;
|
|
dataRow["MachineName"] = deserializedPerson.SERVERMachineName;
|
|
dataRow["MachineVer"] = deserializedPerson.MachineVer;
|
|
dataRow.EndEdit();
|
|
dataRow.AcceptChanges();
|
|
}
|
|
else
|
|
{
|
|
//添加设备信息表
|
|
MainWindowViewModel._machineTable.Rows.Add(client.Id, client.IP, 30, "/" + deserializedPerson.SERVEREnterprise,
|
|
deserializedPerson.SERVERMachineName, DateTime.Now.ToString(), deserializedPerson.MachineVer);
|
|
}
|
|
});
|
|
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT);
|
|
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{//错误
|
|
Console.WriteLine("Tcp:" + ex.ToString());
|
|
LogGing.ERRDATA(ex);
|
|
}
|
|
|
|
return EasyTask.CompletedTask;
|
|
};
|
|
|
|
|
|
await service.SetupAsync(new TouchSocketConfig()//载入配置
|
|
// .SetMaxBufferSize(1024 * 1024 * 100)
|
|
//.SetMinBufferSize(1024 * 1024)
|
|
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
|
|
.SetListenIPHosts(new IPHost[] { new IPHost(7794), new IPHost(7795) })//同时监听两个地址
|
|
.ConfigureContainer(a =>//容器的配置顺序应该在最前面
|
|
{
|
|
})
|
|
.ConfigurePlugins(a =>
|
|
{
|
|
})
|
|
);
|
|
|
|
await service.StartAsync();//启动
|
|
|
|
Console.WriteLine("TcpServer:Run");
|
|
|
|
//启动检查循环定时器
|
|
_timer = new Timer(TimerCallback, null, TimeSpan.FromSeconds(90), TimeSpan.FromSeconds(30));
|
|
}
|
|
|
|
private static async void TimerCallback(object? state)
|
|
{
|
|
try
|
|
{
|
|
int rows = MainWindowViewModel._machineTable.Rows.Count;
|
|
for (int i = rows; i > 0; i--)
|
|
{
|
|
DataRow dataRow = MainWindowViewModel._machineTable.Rows[i-1];
|
|
DateTime dateTime = DateTime.Parse(dataRow["HeartbeatTime"].ToString()!);
|
|
if (DateTime.Now > dateTime.AddSeconds(180))
|
|
{
|
|
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "] NOT LINK ID = " +
|
|
dataRow["ID"] + " IP = " + dataRow["IP"] + " Enterprise = " + dataRow["Enterprise"] +
|
|
" MachineName = " + dataRow["MachineName"] );
|
|
|
|
//关闭无回应连接
|
|
foreach (var item in service.Clients)
|
|
{
|
|
if (item.Id == dataRow["ID"].ToString())
|
|
{
|
|
await item.CloseAsync();
|
|
|
|
//在断开连接后,释放对象。
|
|
item.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
LogGing.ERRDATA(ex);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|