Dotnetty学习笔记——自定义初始化处理器

常常我们需要开一个服务单,对接不同的客户端,编码器、解码器等都不同,需要针对不同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));
  }
}
}
posted @ 2024-09-07 17:35  咖啡不会醉  阅读(32)  评论(0)    收藏  举报