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));
}
}
}
我很平凡
没有过人的天分
没有命运的恩宠
世界不公平。
但我知道
有一个倔强的我
不甘平庸
渴望自由
潜能无限
我相信
只要执着和努力
总有一天
让世界的不公平
在我面前低头!

浙公网安备 33010602011771号