first commit
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using DotNetty.Buffers;
|
||||
using DotNetty.Handlers.Timeout;
|
||||
using DotNetty.Transport.Channels;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using JSMachine.WMS.Netty.Common.Model;
|
||||
using JSMachine.WMS.Netty.NettyAsClient.Transport;
|
||||
using Prism.Events;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyAsClient
|
||||
{
|
||||
public class EchoClientChannelHandler : ChannelHandlerAdapter
|
||||
{
|
||||
private NettyClientTransport _nettyTransport;
|
||||
private TcpEngineInitParam _tcpEngineInitParam1;
|
||||
IEventAggregator _eventAggregator= ContainerLocator.Container.Resolve<IEventAggregator>();
|
||||
|
||||
public EchoClientChannelHandler() : base() { }
|
||||
|
||||
public EchoClientChannelHandler(TcpEngineInitParam tcpEngineInitParam)
|
||||
{
|
||||
_tcpEngineInitParam1= tcpEngineInitParam;
|
||||
}
|
||||
|
||||
public override async void ChannelActive(IChannelHandlerContext context)
|
||||
{
|
||||
_nettyTransport = new NettyClientTransport(context, _tcpEngineInitParam1);
|
||||
TransportManager.DicClientChannels.AddOrUpdate(_tcpEngineInitParam1.IP, p => context, (s, p) => context);
|
||||
|
||||
base.ChannelActive(context);
|
||||
|
||||
//开启喷墨打印机
|
||||
if(_tcpEngineInitParam1.DeviceType== DeviceType.PenmoPrint)
|
||||
LogHelper.Info("已成功连接服务端");
|
||||
}
|
||||
|
||||
public override void ChannelRead(IChannelHandlerContext context, object message)
|
||||
{
|
||||
//LogsTool.Default.Info("收到服务端TCP信息");
|
||||
if (message is IByteBuffer byteBuffer)
|
||||
_nettyTransport.ChannelReadHandle(byteBuffer);
|
||||
}
|
||||
|
||||
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
|
||||
|
||||
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
|
||||
{
|
||||
//LogsTool.Default.Error("Exception: " + exception);
|
||||
context.CloseAsync();
|
||||
}
|
||||
|
||||
public override void HandlerAdded(IChannelHandlerContext context)
|
||||
{
|
||||
base.HandlerAdded(context);
|
||||
}
|
||||
|
||||
public override void HandlerRemoved(IChannelHandlerContext context)
|
||||
{
|
||||
LogHelper.Warn($"跟服务端的连接断开");
|
||||
base.HandlerRemoved(context);
|
||||
}
|
||||
|
||||
public override void UserEventTriggered(IChannelHandlerContext context, object evt)
|
||||
{
|
||||
IdleStateEvent eventState = evt as IdleStateEvent;
|
||||
|
||||
//if (eventState.State == IdleState.ReaderIdle)
|
||||
//{
|
||||
// //没有任何读取
|
||||
// Reconnect();
|
||||
//}
|
||||
//else if (eventState.State == IdleState.WriterIdle)
|
||||
//{
|
||||
// //没有任何写入
|
||||
// Reconnect();
|
||||
//}
|
||||
//else
|
||||
if (eventState.State == IdleState.AllIdle)
|
||||
{
|
||||
//没有任何交互
|
||||
Reconnect();
|
||||
}
|
||||
|
||||
base.UserEventTriggered(context, evt);
|
||||
|
||||
void Reconnect()
|
||||
{
|
||||
//context.CloseAsync();
|
||||
//new NettyEngine().InitEngine(NettyTransport.RemoteIpInfo.IP, NettyTransport.RemoteIpInfo.Port);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user