46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
using DotNetty.Transport.Channels;
|
|||
|
|
using DotNetty.Transport.Channels.Sockets;
|
||
|
|
using JSMachine.WMS.Infrastructure.Helper;
|
||
|
|
using JSMachine.WMS.Netty.NettyUdp.Transport;
|
||
|
|
using System;
|
||
|
|
using System.Text;
|
||
|
|
|
||
|
|
namespace JSMachine.WMS.Netty.NettyUdp
|
||
|
|
{
|
||
|
|
public class EchoClientChannelHandler : SimpleChannelInboundHandler<DatagramPacket>
|
||
|
|
{
|
||
|
|
public static NettyUdpTransport NettyTransport;
|
||
|
|
|
||
|
|
public override void ChannelActive(IChannelHandlerContext context)
|
||
|
|
{
|
||
|
|
NettyTransport = new NettyUdpTransport(context);
|
||
|
|
base.ChannelActive(context);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
|
||
|
|
|
||
|
|
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
|
||
|
|
{
|
||
|
|
LogHelper.Error($"JSMachine.WMS.Netty.NettyUdp.EchoClientChannelHandler.ChannelReadComplete 中发生异常,异常信息 {exception.Message}\n{exception.StackTrace}");
|
||
|
|
|
||
|
|
context.CloseAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void HandlerAdded(IChannelHandlerContext context)
|
||
|
|
{
|
||
|
|
base.HandlerAdded(context);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void HandlerRemoved(IChannelHandlerContext context)
|
||
|
|
{
|
||
|
|
Console.WriteLine($"服务端{context}下线.");
|
||
|
|
base.HandlerRemoved(context);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket msg)
|
||
|
|
{
|
||
|
|
string msgStr = msg.Content.ToString(Encoding.UTF8);
|
||
|
|
NettyTransport.ChannelReadHandle(msgStr);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|