【1】实现粘包和拆包
如果要自己进行上行消息解码,通常需要两个 ChannelHandler 来实现,第一个 handler 则实现一个 ByteToMessageDecoder 子类将原始数据转为自定义格式(需要解决粘包和拆包)。再将自定义格式传给第二个 handler,实现一个 SimpleChannelInboundHandler<I> 进行业务处理,其中 <I> 就是由第一个 handler 转化能得到的类型。例如很多公司使用 json/string+分隔符 或者定长 进行消息透传,则通常下面几个即可实现:
LineBasedFrameDecoder:通过换行符进行拆包粘包处理,\n或者\r\n。
DelimiterBasedFrameDecoder:通过指定分隔符进行粘包拆包处理。
FixedLengthFrameDecoder:通过固定长度进行粘包拆包处理。
SimpleChannelInboundHandler<Clazz>:由Decoder进行基础帧解码后合并为一个自定义完整包进行后续处理。
【2】标准解码器
也就是说实现粘包、拆包需要实现一个 ByteToMessageDecoder 子类将 tcp 传递的流式数据正确拆分为若干个自定义包对象,实际上 netty 解码器已经实现了一系列标准解码器可供选择:
io.netty.handler.codec.http.websocketx.WebSocket00FrameDecoder
io.netty.handler.codec.memcache.AbstractMemcacheObjectDecoder
io.netty.handler.codec.mqtt.MqttDecoder
io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder
io.netty.handler.codec.socksx.v4.Socks4ClientDecoder
io.netty.handler.codec.LineBasedFrameDecoder
io.netty.handler.codec.http.websocketx.WebSocket07FrameDecoder
io.netty.handler.codec.rtsp.RtspDecoder
io.netty.handler.codec.smtp.SmtpResponseDecoder
io.netty.handler.codec.rtsp.RtspObjectDecoder
io.netty.handler.codec.DelimiterBasedFrameDecoder
io.netty.handler.codec.rtsp.RtspResponseDecoder
io.netty.handler.codec.LengthFieldBasedFrameDecoder
io.netty.handler.codec.marshalling.MarshallingDecoder
io.netty.handler.codec.http2.Http2FrameCodec
io.netty.handler.codec.rtsp.RtspRequestDecoder
io.netty.handler.codec.compression.Bzip2Decoder
io.netty.handler.codec.socksx.v5.Socks5CommandResponseDecoder
io.netty.handler.codec.socks.SocksCmdResponseDecoder
io.netty.handler.codec.compression.LzfDecoder
io.netty.handler.codec.socksx.SocksPortUnificationServerHandler
io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler
io.netty.handler.codec.http.HttpObjectDecoder
io.netty.handler.codec.marshalling.CompatibleMarshallingDecoder
io.netty.handler.codec.compression.JdkZlibDecoder
io.netty.handler.codec.memcache.binary.BinaryMemcacheClientCodec$Decoder
io.netty.handler.codec.socks.SocksCmdRequestDecoder
io.netty.handler.codec.http2.HttpToHttp2ConnectionHandler
io.netty.handler.codec.xml.XmlFrameDecoder
io.netty.handler.codec.compression.ZlibDecoder
io.netty.handler.codec.socksx.v4.Socks4ServerDecoder
io.netty.handler.codec.json.JsonObjectDecoder
io.netty.handler.codec.memcache.binary.AbstractBinaryMemcacheDecoder
io.netty.handler.codec.socksx.v5.Socks5InitialResponseDecoder
io.netty.handler.codec.http2.Http2ConnectionHandler
io.netty.handler.codec.memcache.binary.BinaryMemcacheResponseDecoder
io.netty.handler.codec.memcache.binary.BinaryMemcacheRequestDecoder
io.netty.handler.codec.http.HttpServerCodec$HttpServerRequestDecoder
io.netty.handler.codec.socksx.v5.Socks5PasswordAuthResponseDecoder
io.netty.handler.codec.compression.BrotliDecoder
io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder
io.netty.handler.codec.compression.FastLzFrameDecoder
io.netty.handler.codec.socks.SocksAuthRequestDecoder
io.netty.handler.codec.redis.RedisDecoder
io.netty.handler.codec.compression.SnappyFrameDecoder
io.netty.handler.codec.stomp.StompSubframeDecoder
io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder
io.netty.handler.codec.http.HttpClientCodec$Decoder
io.netty.handler.codec.http.HttpRequestDecoder
io.netty.handler.codec.socks.SocksAuthResponseDecoder
io.netty.handler.codec.spdy.SpdyFrameCodec
io.netty.handler.codec.dns.TcpDnsResponseDecoder
io.netty.handler.codec.dns.TcpDnsQueryDecoder
io.netty.handler.codec.socks.SocksInitRequestDecoder
io.netty.handler.codec.haproxy.HAProxyMessageDecoder
io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder
io.netty.handler.codec.http2.Http2MultiplexCodec
io.netty.handler.codec.serialization.ObjectDecoder
io.netty.handler.codec.compression.JZlibDecoder
io.netty.handler.codec.compression.Lz4FrameDecoder
io.netty.handler.codec.FixedLengthFrameDecoder
io.netty.handler.codec.compression.SnappyFramedDecoder
io.netty.handler.codec.xml.XmlDecoder
io.netty.handler.codec.ReplayingDecoder
io.netty.handler.codec.socksx.v5.Socks5PasswordAuthRequestDecoder
io.netty.handler.codec.http.websocketx.WebSocket13FrameDecoder
io.netty.handler.codec.socks.SocksInitResponseDecoder
io.netty.handler.codec.ByteToMessageCodec$1
io.netty.handler.codec.http.HttpResponseDecoder
【3】源码分析:io.netty.handler.codec.mqtt.MqttDecoder
使用样例:
源码分析:
【3】源码分析:io.netty.handler.codec.LineBasedFrameDecoder
源码分析: