|
|
|
using DyeingComputer.UserClass;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Data;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using TouchSocket.Core;
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
using DyeingComputer.ViewModel;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace DyeingComputer.UserClass
|
|
|
|
{/// <summary>
|
|
|
|
/// 异步TCP服务器
|
|
|
|
/// </summary>
|
|
|
|
public class AsyncTcpServer
|
|
|
|
{
|
|
|
|
public static async Task Main()
|
|
|
|
{
|
|
|
|
TcpService service = new TcpService();
|
|
|
|
service.Received = (client, e) =>
|
|
|
|
{
|
|
|
|
LogGing.LogSQLDATA("800", "TcpServer", "API:"+e.ByteBlock.Span.ToString(Encoding.ASCII));
|
|
|
|
if (e.ByteBlock.Span.ToString(Encoding.ASCII) == "SC800") client.SendAsync(MainWindowViewModel.S01);
|
|
|
|
if (e.ByteBlock.Span.ToString(Encoding.ASCII) == "SC801") client.SendAsync(MainWindowViewModel.S16.ToString());
|
|
|
|
if (e.ByteBlock.Span.ToString(Encoding.ASCII) == "SC802") client.SendAsync(MainWindowViewModel.S03);
|
|
|
|
if (e.ByteBlock.Span.ToString(Encoding.ASCII) == "SC803") client.SendAsync(MainWindowViewModel.S05);
|
|
|
|
if (e.ByteBlock.Span.ToString(Encoding.ASCII) == "SC804") client.SendAsync(MainWindowViewModel.S06);
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};
|
|
|
|
|
|
|
|
await service.SetupAsync(new TouchSocketConfig()//载入配置
|
|
|
|
.SetListenIPHosts(new IPHost[] { new IPHost("tcp://127.0.0.1:7789"), new IPHost(7790) })//同时监听两个地址
|
|
|
|
.ConfigureContainer(a =>//容器的配置顺序应该在最前面
|
|
|
|
{
|
|
|
|
//a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用)
|
|
|
|
})
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
{
|
|
|
|
//a.Add<DifferentProtocolPlugin>();
|
|
|
|
}));
|
|
|
|
await service.StartAsync();//启动
|
|
|
|
|
|
|
|
LogGing.LogGingDATA("800SREVER:START");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyTcpService : TcpService<MyTcpSessionClient>
|
|
|
|
{
|
|
|
|
protected override MyTcpSessionClient NewClient()
|
|
|
|
{
|
|
|
|
return new MyTcpSessionClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyTcpSessionClient : TcpSessionClient
|
|
|
|
{
|
|
|
|
internal void SetDataHandlingAdapter(SingleStreamDataHandlingAdapter adapter)
|
|
|
|
{
|
|
|
|
base.SetAdapter(adapter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 此插件实现,按照不同端口,使用不同适配器。
|
|
|
|
/// <list type="bullet">
|
|
|
|
/// <item>7789端口:使用"**"结尾的数据</item>
|
|
|
|
/// <item>7790端口:使用"##"结尾的数据</item>
|
|
|
|
/// </list>
|
|
|
|
/// </summary>
|
|
|
|
internal class DifferentProtocolPlugin : PluginBase, ITcpConnectingPlugin, ITcpReceivedPlugin
|
|
|
|
{
|
|
|
|
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e)
|
|
|
|
{
|
|
|
|
if (client is MyTcpSessionClient sessionClient)
|
|
|
|
{
|
|
|
|
if (sessionClient.ServicePort == 7789)
|
|
|
|
{
|
|
|
|
sessionClient.SetDataHandlingAdapter(new TerminatorPackageAdapter("**"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sessionClient.SetDataHandlingAdapter(new TerminatorPackageAdapter("##"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
|
|
|
|
{
|
|
|
|
//如果是自定义适配器,此处解析时,可以判断e.RequestInfo的类型
|
|
|
|
|
|
|
|
if (client is ITcpSessionClient sessionClient)
|
|
|
|
{
|
|
|
|
sessionClient.Logger.Info($"{sessionClient.GetIPPort()}收到数据,服务器端口:{sessionClient.ServicePort},数据:{e.ByteBlock}");
|
|
|
|
}
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|