netty的编解码器,以及内置的编解码器

一、编码器和解码器

1、什么是编码和解码

解码常用于入站操作,将字节转换为消息。编码用于出站,将消息转换为字节流

2、解码器ByteToMessageDecoder和ReplayingDecoder,ReplayingDecoder扩展了ByteToMessageDecoder类,使得我们不必使用readableBytes()方法,下面是两类测试代码

复制代码
public class ToIntegerDecoder extends ByteToMessageDecoder {
    @Override
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception {
        if(in.readableBytes() >=4) {
            list.add(in.readInt());
        }
    }
}

public class ToIntegerDecoder2 extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        list.add(byteBuf.readInt());
    }
}

3、编码器MessageToByteEncoder和MessageToMessageEncoder,其中T是原始消息的类型,这两个类分别代表将消息转换为字节流和将消息转换为其他类型的消息。下面代码分别为这两种的示例代码

复制代码
public class ShortToByteEncoder extends MessageToByteEncoder<Short> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Short aShort, ByteBuf byteBuf) throws Exception {
        byteBuf.writeShort(aShort);
    }
}

public class IntegerToStringEncoder extends MessageToMessageEncoder<Integer> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Integer integer, List<Object> list) throws Exception {
        list.add(String.valueOf(integer));
    }
}

4、抽象的编解码器

ByteToMessageCodec:对编码和解码的整合,可以理解为对消息解码后再次编码。

MessageToMessageCodec:原理和ByteToMessageCodec同理。

二、内置的编码器和解码器

1、通过SSL/TTL保护的netty程序,通常是将SslHandler放在ChannelPipeline的头部,示例代码如下:

复制代码
public class SslChannelInitializer extends ChannelInitializer<Channel> {

    private final SslContext sslContext;

    private final boolean startTls;

    public SslChannelInitializer(SslContext sslContext, boolean startTls) {
        this.sslContext = sslContext;
        this.startTls = startTls;//如果设置为true,第一个被写入的消息将不会被加密
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        SSLEngine sslEngine = sslContext.newEngine(channel.alloc());
        channel.pipeline().addFirst("ssl", new SslHandler(sslEngine,startTls));
    }
}

2、构建netty基于http/https协议的:客户端和服务器分别用HttpClientCodec和HttpServerCodec类即可。

复制代码
public class HttpInitializer extends ChannelInitializer<Channel> {
    private final SslContext sslContext;
    private final boolean isClient;

    public HttpInitializer(SslContext sslContext, boolean isClient) {
        this.sslContext = sslContext;
        this.isClient = isClient;
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        //如果是https协议,则需要在通道下的管道头部追加SslHandler
        ChannelPipeline pipeline = channel.pipeline();
        SSLEngine engine = sslContext.newEngine(channel.alloc());
        pipeline.addFirst("ssl", new SslHandler(engine));
        if(isClient){
            pipeline.addLast("codec", new HttpClientCodec());
        }else {
            pipeline.addLast("codec", new HttpServerCodec());
        }
    }
}

3、websocket:websocket的WebSocketFrame类型通常包括BinaryWebSocketFrame(数据帧,二进制数据)、TextWebSocketFrame(数据帧,文本数据)、CloseWebSocketFrame(控制帧,一个Close请求、关闭的状态码及关闭的原因)、PingWebSocketFrame(控制帧,请求一个PongWebSocketFrame)和PongWebSocketFrame(控制帧,对PingWebSocketFrame进行响应)。

示例代码

websocket的处理类

复制代码
public class TextFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    @Override
    public void messageReceived(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
        //处理数据
    }
}

public class WebsocketServerInitializer extends ChannelInitializer<Channel> {
    @Override
    public void initChannel(Channel ch) throws Exception {
        ch.pipeline().addLast(new HttpServerCodec(),
                new HttpObjectAggregator(65536),
                new WebSocketServerProtocolHandler("/websocket"),
                new TextFrameHandler()
        );
    }
}

4、空闲的连接和超时

IdleStateHandler:当连接空闲时间太长会触发事件。ReadTimeoutHandler读超时处理器,WriteTimeoutHandler写超时处理器

用法也是将他们的对象注册到通道里的管道中去。

复制代码
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS));
相关推荐
workflower2 小时前
单元测试-例子
java·开发语言·算法·django·个人开发·结对编程
YuanlongWang2 小时前
C# 基础——装箱和拆箱
java·开发语言·c#
摇滚侠2 小时前
Spring Boot 3零基础教程,WEB 开发 静态资源默认配置 笔记27
spring boot·笔记·后端
b78gb2 小时前
电商秒杀系统设计 Java+MySQL实现高并发库存管理与订单处理
java·开发语言·mysql
wb043072013 小时前
性能优化实战:基于方法执行监控与AI调用链分析
java·人工智能·spring boot·语言模型·性能优化
天若有情6734 小时前
Java Swing 实战:从零打造经典黄金矿工游戏
java·后端·游戏·黄金矿工·swin
lichong9515 小时前
Android studio 修改包名
android·java·前端·ide·android studio·大前端·大前端++
lichong9515 小时前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++
@yanyu6665 小时前
Tomcat安装与HTML响应实战
java·tomcat·html
Chen-Edward6 小时前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring