JiShe.CollectBus/JiShe.CollectBus.Core/Plugins/TcpServiceReceivedPlugin.cs

73 lines
2.2 KiB
C#
Raw Normal View History

2024-09-30 17:10:43 +08:00
using JiShe.CollectBus.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Plugins
{
public class TcpServiceReceivedPlugin : PluginBase, ITcpReceivedPlugin, ITcpConnectingPlugin, ITcpConnectedPlugin, ITcpClosedPlugin
{
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
//TODO:根据指令区别是376还是645协议
var protocolType = "";
switch (protocolType)
{
case "376":
//todo登录拿到设备信息根据设备信息使用不同的协议解析服务
break;
case "645":
//todo: 直接拿设备信息,根据设备信息使用不同的协议解析服务
break;
default:
break;
}
//从客户端收到信息
var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
client.Logger.Info($"[TCP] 已从{client.GetIPPort()}接收到信息:{messageHexString}");
await e.InvokeNext();
}
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e)
{
if (client is ITcpSessionClient sessionClient)
{
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}正在连接中...");
}
await e.InvokeNext();
}
public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e)
{
if (client is ITcpSessionClient sessionClient)
{
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}已连接");
}
await e.InvokeNext();
}
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e)
{
if (client is ITcpSessionClient sessionClient)
{
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}已关闭连接");
}
await e.InvokeNext();
}
}
}