You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
5.2 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography.Pkcs;
using System.Text;
using System.Text.Json;
using System.Windows.Markup;
using System.Xml.Linq;
using TouchSocket.Core;
using TouchSocket.Sockets;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace ForwardingServer
{
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; }
}
public static TcpService service = new TcpService();
public static async Task TcpMain()
{
//TcpService service = new TcpService();
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) => {
//离线记录
DataRow[] dat = DATA.MachineTable.Select("ID='"+client.Id+"'");
foreach (DataRow row in dat)
{
DATA.MachineTable.Rows.Remove(row);
}
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + "] NOT ID " + client.Id + " IP " + client.IP);
return EasyTask.CompletedTask;
};//有客户端断开连接
service.Received = (client, e) =>{
string DAT = e.Memory.Span.ToString(Encoding.UTF8);
try
{
if (DAT.Length > 5)
{
1 month ago
DataRow[] r_ = DATA.MachineTable.Select("ID='" + client.Id + "'");
if (r_.Length > 0)
{
1 month ago
int? s_ = r_.First().Field<int?>("Start");
if (s_ == 30)
{
1 month ago
if (DAT.StartsWith("{") || DAT.StartsWith("["))
{
1 month ago
DATA.Person deserializedPerson = JsonSerializer.Deserialize<DATA.Person>(DAT)!;
//转发逻辑
var dr = DATA.UserTable.Select("Terminal='" + deserializedPerson.Terminal + "'");
if (dr.Length > 0)
{
DataRow drEmployee = dr.First();
drEmployee.BeginEdit();
drEmployee["RDAT"] = DAT;
drEmployee.EndEdit();
drEmployee.AcceptChanges();
}
1 month ago
LogGing.LogGingDATA("[LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT);
}
1 month ago
}
}
1 month ago
else
{
Personm deserializedPerson = JsonSerializer.Deserialize<Personm>(DAT)!;
DATA.MachineTable.Rows.Add(client.Id, client.IP, 30, "/" + deserializedPerson.SERVEREnterprise,
deserializedPerson.SERVERMachineName, deserializedPerson.MachineVer);
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + " [LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT);
1 month ago
LogGing.LogGingDATA("[LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT);
1 month ago
}
}
}
catch (Exception ex)
{//错误
1 month ago
Console.WriteLine("Tcp:" + ex.ToString());
1 month ago
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");
}
}
}