first commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using DotNetty.Buffers;
|
||||
using DotNetty.Handlers.Timeout;
|
||||
using DotNetty.Transport.Channels;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using JSMachine.WMS.Netty.Common.Util;
|
||||
using JSMachine.WMS.Netty.NettyAsServer.Transport;
|
||||
using System;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyAsServer
|
||||
{
|
||||
public class EchoServerChannelHandler : ChannelHandlerAdapter
|
||||
{
|
||||
private NettyServerTransport NettyTransport;
|
||||
|
||||
public override void ChannelActive(IChannelHandlerContext context)
|
||||
{
|
||||
LogHelper.Info($"客户端 {context.ToIPInfo(AddressOnwer.Remote).IP} 已连接");
|
||||
NettyTransport = new NettyServerTransport(context);
|
||||
|
||||
base.ChannelActive(context);
|
||||
}
|
||||
|
||||
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);
|
||||
TransportManager.RemoveInActiveChannel(context);
|
||||
context.CloseAsync();
|
||||
}
|
||||
|
||||
public override void HandlerAdded(IChannelHandlerContext context)
|
||||
{
|
||||
base.HandlerAdded(context);
|
||||
}
|
||||
|
||||
public override void HandlerRemoved(IChannelHandlerContext context)
|
||||
{
|
||||
LogHelper.Warn($"客户端 {context.ToIPInfo(AddressOnwer.Remote).IP} 下线.");
|
||||
TransportManager.RemoveInActiveChannel(context);
|
||||
base.HandlerRemoved(context);
|
||||
}
|
||||
|
||||
public override void UserEventTriggered(IChannelHandlerContext context, object evt)
|
||||
{
|
||||
IdleStateEvent eventState = evt as IdleStateEvent;
|
||||
|
||||
if (eventState.State == IdleState.ReaderIdle)
|
||||
{
|
||||
//没有任何读取
|
||||
RemoveInactiveChannel();
|
||||
}
|
||||
else if (eventState.State == IdleState.WriterIdle)
|
||||
{
|
||||
//没有任何写入
|
||||
RemoveInactiveChannel();
|
||||
}
|
||||
else
|
||||
if (eventState.State == IdleState.AllIdle)
|
||||
{
|
||||
//没有任何交互
|
||||
RemoveInactiveChannel();
|
||||
}
|
||||
|
||||
base.UserEventTriggered(context, evt);
|
||||
|
||||
void RemoveInactiveChannel()
|
||||
{
|
||||
context.CloseAsync();
|
||||
TransportManager.RemoveInActiveChannel(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user