【Netty】高阶使用:自定义编解码器

当你通过 Netty 发送或者接受一个消息的时候,就将会发生一次数据转换

  • 入站时 – 消息被解码(Decode):从字节(二进制)转换为我们能读懂并操作的格式(int、String、Java对象…)
  • 出站时 – 消息被编码(Encode):从我们能读懂并操作的格式的形式(int、String、Java对象…)转换为字节形式(二进制)

Netty 提供了一系列实用的编码解码器,比如编解码字符串的 StringEncoder 和 StringDecoder,编解码对象的 ObjectEncoder 和ObjectDecoder 等,他们都实现了 ChannelInboundHadnler 或者 ChannelOutcoundHandler 接口。

在这里插入图片描述

在这些类中, channelRead 方法已经被重写了。以入站为例,对于每个从入站 Channel 读取的消息,这个方法会被调用。随后,它将调用由已知解码器所提供的 decode() 方法进行解码,并将已经解码的字节转发给 ChannelPipeline 中的下一个 ChannelInboundHandler。

除了 Netty 已经实现的,我们也可以通过集成 MessageToByteEncoder,ByteToMessageDecoder 自定义编解码器。

/*
* 自定义编码器
* 实现 MessageToByteEncoder接口,重写 encode 方法
* 注:通过泛型指定可以编码数据类型
*/
public class LongToByteEncoder extends MessageToByteEncoder<Long> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Long msg, ByteBuf out) throws Exception {
        System.out.println("LongToByteEncoder encode被调用");
        System.out.println("msg=" + msg);
        // 关键!!将编码结果通过 pipeline 继续向下传递,继续接受处理
        out.writeLong(msg);
    }
}
/*
* 自定义解码器
* 实现 ByteToMessageDecoder 接口,重写 decode 方法
*/
public class ByteToLongDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("ByteToLongDecoder decode 被调用");
        // 注:因为 long 8个字节, 需要判断有8个字节,才能读取一个long
        if(in.readableBytes() >= 8) {
        	// 关键!!将解码结果通过 pipeline 继续向下传递,继续接受处理
            out.add(in.readLong());
        }
    }
}

然后再将我们的自定义编解码器加入到 ChannelPipeline 中

public class NettyServerInitailizer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) {

        ChannelPipeline pipeline = socketChannel.pipeline();
		// 入站-Decode
		pipeline.addLast(new ByteToLongDecoder());
		// 出站-Encode(后执行)
		pipeline.addLast(new LongToByteEncoder());
		// 出站-自定义的 Handler(先执行)
		pipeline.addLast(new NettyServerHandler());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A minor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值