1.前后端增加复位功能
2.tcpclient心跳设置 3.优化后端启动速度 4.增加后端出库日志
This commit is contained in:
53
WCS.BLL/Tool/DependencyExtensions.cs
Normal file
53
WCS.BLL/Tool/DependencyExtensions.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Sockets;
|
||||
|
||||
namespace WCS.BLL.Tool
|
||||
{
|
||||
/// <summary>
|
||||
/// 一个心跳计数器扩展。
|
||||
/// </summary>
|
||||
internal static class DependencyExtensions
|
||||
{
|
||||
public static readonly DependencyProperty<Timer> HeartbeatTimerProperty =
|
||||
DependencyProperty<Timer>.Register("HeartbeatTimer", null);
|
||||
|
||||
public static bool Ping<TClient>(this TClient client) where TClient : ITcpClientBase
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = new byte[] { 0x05, 0x00, 0x00, 0x06, 0x78, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x00, 0x00 };
|
||||
client.Send(data);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.Write($"【{client.IP}:{client.Port}】发送心跳中发现异常:" + ex.Message, LogsType.Info);
|
||||
if (ex is NotConnectedException)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool Pong<TClient>(this TClient client) where TClient : ITcpClientBase
|
||||
{
|
||||
try
|
||||
{
|
||||
client.Send(new MyRequestInfo() { DataType = DataType.Pong }.PackageAsBytes());
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
client.Logger.Exception(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
67
WCS.BLL/Tool/HeartbeatAndReceivePlugin.cs
Normal file
67
WCS.BLL/Tool/HeartbeatAndReceivePlugin.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Sockets;
|
||||
|
||||
namespace WCS.BLL.Tool
|
||||
{
|
||||
internal class HeartbeatAndReceivePlugin : PluginBase, ITcpConnectedPlugin<ITcpClientBase>, ITcpDisconnectedPlugin<ITcpClientBase>, ITcpReceivedPlugin<ITcpClientBase>
|
||||
{
|
||||
private readonly int m_timeTick;
|
||||
private readonly ILog logger;
|
||||
|
||||
[DependencyInject("20000")]
|
||||
public HeartbeatAndReceivePlugin(int timeTick, ILog logger)
|
||||
{
|
||||
this.m_timeTick = 20000;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async Task OnTcpConnected(ITcpClientBase client, ConnectedEventArgs e)
|
||||
{
|
||||
if (client is ISocketClient)
|
||||
{
|
||||
return;//此处可判断,如果为服务器,则不用使用心跳。
|
||||
}
|
||||
|
||||
if (client.GetValue(DependencyExtensions.HeartbeatTimerProperty) is Timer timer)
|
||||
{
|
||||
timer.Dispose();
|
||||
}
|
||||
|
||||
client.SetValue(DependencyExtensions.HeartbeatTimerProperty, new Timer((o) =>
|
||||
{
|
||||
client.Ping();
|
||||
}, null, 0, this.m_timeTick));
|
||||
await e.InvokeNext();
|
||||
}
|
||||
|
||||
public async Task OnTcpDisconnected(ITcpClientBase client, DisconnectEventArgs e)
|
||||
{
|
||||
if (client.GetValue(DependencyExtensions.HeartbeatTimerProperty) is Timer timer)
|
||||
{
|
||||
timer.Dispose();
|
||||
client.SetValue(DependencyExtensions.HeartbeatTimerProperty, null);
|
||||
}
|
||||
|
||||
await e.InvokeNext();
|
||||
}
|
||||
|
||||
public async Task OnTcpReceived(ITcpClientBase client, ReceivedDataEventArgs e)
|
||||
{
|
||||
if (e.RequestInfo is MyRequestInfo myRequest)
|
||||
{
|
||||
this.logger.Info(myRequest.ToString());
|
||||
if (myRequest.DataType == DataType.Ping)
|
||||
{
|
||||
client.Pong();
|
||||
}
|
||||
}
|
||||
await e.InvokeNext();
|
||||
}
|
||||
}
|
||||
}
|
@ -36,7 +36,11 @@ namespace WCS.BLL
|
||||
/// <summary>
|
||||
/// 指令重发
|
||||
/// </summary>
|
||||
InstructionResend
|
||||
InstructionResend,
|
||||
/// <summary>
|
||||
/// 出库流程日志
|
||||
/// </summary>
|
||||
Outstore,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
85
WCS.BLL/Tool/MyFixedHeaderDataHandlingAdapter .cs
Normal file
85
WCS.BLL/Tool/MyFixedHeaderDataHandlingAdapter .cs
Normal file
@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TouchSocket.Core;
|
||||
|
||||
namespace WCS.BLL.Tool
|
||||
{
|
||||
|
||||
internal class MyFixedHeaderDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter<MyRequestInfo>
|
||||
{
|
||||
public override int HeaderLength => 3;
|
||||
|
||||
public override bool CanSendRequestInfo => false;
|
||||
|
||||
protected override MyRequestInfo GetInstance()
|
||||
{
|
||||
return new MyRequestInfo();
|
||||
}
|
||||
|
||||
protected override void PreviewSend(IRequestInfo requestInfo)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
internal class MyRequestInfo : IFixedHeaderRequestInfo
|
||||
{
|
||||
public DataType DataType { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public int BodyLength { get; private set; }
|
||||
|
||||
public bool OnParsingBody(byte[] body)
|
||||
{
|
||||
if (body.Length == this.BodyLength)
|
||||
{
|
||||
this.Data = body;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool OnParsingHeader(byte[] header)
|
||||
{
|
||||
if (header.Length == 3)
|
||||
{
|
||||
this.BodyLength = TouchSocketBitConverter.Default.ToUInt16(header, 0) - 1;
|
||||
this.DataType = (DataType)header[2];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Package(ByteBlock byteBlock)
|
||||
{
|
||||
byteBlock.Write((ushort)((this.Data == null ? 0 : this.Data.Length) + 1));
|
||||
byteBlock.Write((byte)this.DataType);
|
||||
if (this.Data != null)
|
||||
{
|
||||
byteBlock.Write(this.Data);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] PackageAsBytes()
|
||||
{
|
||||
using var byteBlock = new ByteBlock();
|
||||
this.Package(byteBlock);
|
||||
return byteBlock.ToArray();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"数据类型={this.DataType},数据={(this.Data == null ? "null" : Encoding.UTF8.GetString(this.Data))}";
|
||||
}
|
||||
}
|
||||
|
||||
internal enum DataType : byte
|
||||
{
|
||||
Ping,
|
||||
Pong,
|
||||
Data
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using System.Text;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Sockets;
|
||||
using WCS.BLL.Tool;
|
||||
|
||||
|
||||
namespace WCS.BLL
|
||||
@ -68,6 +69,7 @@ namespace WCS.BLL
|
||||
{
|
||||
//配置断线重连
|
||||
a.UseReconnection(-1, true, 1000);
|
||||
a.Add<HeartbeatAndReceivePlugin>();
|
||||
})
|
||||
.ConfigureContainer(a =>
|
||||
{
|
||||
|
Reference in New Issue
Block a user