Netty5.0使用简单介绍

xiaoxiao2021-02-28  103

 NioEventLoopGroup Netty内部都是通过线程在处理各种数据, NioEventLoopGroup就是用来管理调度他们的,注册Channel,管理他们的生命周期

ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就封装了一个标准IOServerSocket.

 

ObjectEncoder把对象序列化后,转为ChannelBuffer并返回,但是在返回的ChannelBuffer的最前面4个字节(int),这个数字,代表了该对象转为字节流后,字节流的长度 只要实现方法: encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception 此处执行往客户端返回序列化后的数据   ObjectDecoder 反序列化后根据headerlength + content的格式,取得content 继承了FixedLengthFrameDecoder定长解码器可以帮助我们轻松实现定长解码报文 只要实现方法: decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception //解析接收到的序列化的数据        extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) :重写了父类的这个方法,避免了memory copy。本方法被decode方法调用  

ChannelPipeline可以理解成一个消息( 或消息事件,ChanelEvent)流转的通道,在这个通道中可以被附上许多用来处理消息的handler,当消息在这个通道中流转的时候,如果有与这个消息类型相对应的handler,就会触发这个handler去执行相应的动作。

主要使用提供的方法: addLast(ChannelHandler... var1);

此处实现了ChannelHandlerAdapterà I/O事件的处理类

handlerAdded(ChannelHandlerContext ctx) throws Exception { exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 所有的客户端通讯操作都再此处开始被处理
转载请注明原文地址: https://www.6miu.com/read-20433.html

最新回复(0)