handler链调用机制示例
https://www.bilibili.com/video/BV1jK4y1s7GV?p=79
https://www.bilibili.com/video/BV1jK4y1s7GV?p=80
https://www.bilibili.com/video/BV1jK4y1s7GV?p=81
socket --> channel -->入栈
channel --> socket --> 出栈
(server)解码器–ProtobufDecoder : MessageToMessageDecoder : ChannelInboundHandlerAdapter
(client)编码器–ProtobufEncoder : MessageToMessageEncoder : ChannelOutboundHandlerAdapter : ChannelHandlerAdapter : ChannelHandler (I)
ByteToMessageDecoder
ByteToMessageDecoder 会对入栈数据进行缓冲,直到它准备好处理。
服务端的channelpipeline接收到客户端通过socket传过来的数据,解析
ToIntegerDecoder
当没有更多元素添加到List的时候,它的内容将会被发送给下一个ChannelInBoundHandler.
读数据(encoder)的顺序
socket channelPipeline
二进制数据 <-- ToIntegerDecoder --> 业务处理器 ChannelInboundHandlerAdapter —>---|
二进制数据 <-- ToIntegerEncoder <-- 业务处理器 ChannelOutboundHandlerAdapter —<---|
==========================客户端代码 ======================================
package com.atguigu.inboundhandlerandoutboundhandler.client;
import com.sun.corba.se.impl.orbutil.concurrent.Sync;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* Author: tz_wl
* Date: 2020/9/28 15:50
* Content:
*/
public class MyClient {
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new MyClientInitializer());
ChannelFuture channelFuture = bootstrap.connect("localhost", 8087).sync();