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.

151 lines
5.6 KiB

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Http.WebSockets;
using TouchSocket.Sockets;
namespace ForwardingServer
{
public class AsyncHttpServer
{
public static HttpService httpservice = new HttpService();
public static async Task HttpMain()
{
await httpservice.SetupAsync(new TouchSocketConfig()//加载配置
.SetListenIPHosts(7791)
.ConfigureContainer(a =>
{
a.AddConsoleLogger();
})
.ConfigurePlugins(a =>
{
a.Add<MyHttpPlug1>();
//default插件应该最后添加,其作用是
//1、为找不到的路由返回404
//2、处理 header 为Option的探视跨域请求。
a.UseDefaultHttpServicePlugin();
}));
await httpservice.StartAsync();
Console.WriteLine("HttpService:Run");
}
public class MyHttpPlug1 : PluginBase, IHttpPlugin
{
public async Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e)
{
#region HttpContext生命周期
//var request = e.Context.Request;//http请求体
var response = e.Context.Response;//http响应体
#endregion
var bodyString = await e.Context.Request.GetBodyAsync();
//获取路径
var URL = e.Context.Request.URL;
try
{
string dat;
DataRow[] rows = DATA.MachineTable.Select("Enterprise='/" + URL + "'");
if (rows.Length > 0)
{
string? s_ = rows.First().Field<string?>("ID")!;
if (s_ != null)
{
// var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>(bodyString)!;
// if (deserialized.TryGetValue("Terminal", out var val) && val != null) { }
// using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await AsyncTcpServer.service.SendAsync(s_, bodyString);
DATA.Person deserializedPerson = JsonSerializer.Deserialize<DATA.Person>(bodyString)!;
DATA.UserTable.Rows.Add(client.Id, client.IP, deserializedPerson.User,
"", deserializedPerson.Terminal, "", "");
// 创建一个 秒的延时任务
Task delayTask = Task.Delay(15000);
// 创建一个监测任务,不断检查变量
Task monitorTask = Task.Run(async () =>
{
while (!(DATA.UserTable.Select("Terminal='" + deserializedPerson.Terminal + "'").
First().Field<string>("RDAT")!.Length > 1))
{
await Task.Delay(500);
}
});
Task winner = await Task.WhenAny(delayTask, monitorTask);
DataRow[] dataRows = DATA.UserTable.Select("Terminal='" + deserializedPerson.Terminal + "'");
if (winner == monitorTask)
{
dat = dataRows.First().Field<string>("RDAT")!;
}
else { dat = "970"; }
foreach (DataRow row in dataRows)
{
DATA.UserTable.Rows.Remove(row);
}
}
else { dat = "999"; }
}
else
{
dat = "980";
}
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT");
e.Context.Response.SetStatus(200, "success");
e.Context.Response.SetContent(Encoding.UTF8.GetBytes(dat.ToCharArray()));
await e.Context.Response.AnswerAsync();
}
catch (TimeoutException)
{
Console.WriteLine("等待TCP服务端响应超时");
}
catch (Exception ex)
{//错误
Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + "] Http:" + ex.ToString());
//client.SendAsync("Target instruction parsing error manager will close connection");
}
/* if (request.IsGet() && request.UrlEquals("/success"))
{
//直接响应文字
await response
.SetStatus(200, "success")
.FromText("Success")
.AnswerAsync();//直接回应
Console.WriteLine("处理/success");
return;
}
//无法处理,调用下一个插件
await e.InvokeNext();*/
}
}
}
}