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.
112 lines
4.9 KiB
112 lines
4.9 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)
|
|
{
|
|
DataRow[] r_ = DATA.MachineTable.Select("ID='" + client.Id + "'");
|
|
if (r_.Length > 0)
|
|
{
|
|
int? s_ = r_.First().Field<int?>("Start");
|
|
|
|
if (s_ == 30)
|
|
{
|
|
if (DAT.StartsWith("{") || DAT.StartsWith("["))
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
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);
|
|
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{//错误
|
|
Console.WriteLine("Tcp:" + ex.ToString());
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
}
|
|
}
|
|
|