常常我们需要开一个服务单,对接不同的客户端,编码器、解码器等都不同,需要针对不同IP添加不同的处理器。
public class CustomInitializer :Channellnitializer<lSocketChannel>
{
Action<string,string>_dealMsgAction; lServer_server;
public CustomInitializer(Action<string,string>dealMsgAction,IServer server)
{
dealMsgAction = dealMsgAction;
_server = server;
}
protected override void InitChannel(lSocketChannel channel)
{
var ip = (channel.RemoteAddress as IPEndPoint).IPEndPointToString();
IChannelPipeline pipeline =channel.Pipeline;
if (ip== SystemSettingViewModel.Instance.SystemSetting.PLCIP)
{
pipeline.AddLast("modbus", new ModbusEncoder();
}
else
{
pipeline.AddLast(new LoggingHandler("SRV-CONN"));
pipeline.AddLast(new IdleStateHandler(10,10,30));//加心跳
pipeline.AddLast("encoder", new TIvEncoder();
pipeline.AddLast("decoder", new LengthFieldBasedFrameDecoder(DotNetty.Buffers.ByteOrder.LittleEndian, int.MaxValue, 4,4, 0,8,true));
pipeline.AddLast("handler", new SocketServerHandler(_dealMsgAction,_server));
}
}
}
标签:初始化,pipeline,自定义,AddLast,dealMsgAction,server,new,Dotnetty,channel
From: https://www.cnblogs.com/kafeibuhuizui/p/18401948