Netty学习——源码篇7 Pipeline的事件传播机制1 备份

netty学习(四)事件传递 1.不同的传递方法   1.1ChannelPipeline和Channel的调用   每个ChannelHandler被添加到ChannelPipeline后,都会创建一个ChannelHandlerContext并与之创建的ChannelHandler关联绑定。 ChannelHandlerContext允许ChannelHandler与其他的ChannelHandler实现进行交互,这 阅读详情

        上篇:Netty学习——源码篇6 Pipeline设计原理 已经知道AbstractChannelHandlerContext中有Inbound和Outbound两个boolean变量,分别用于识别Context所对应的Handler的类型。

        1、Inbound为true时,表示其对应的ChannelHandler是ChannelInboundHandler的子类。

        2、Outbound为true时,表示其对应的ChannelHandler是ChannelOutboundHandler的子类。

        这两个属性到底有什么作用呢?还要从ChannelPipeline的事件传播类型说起。Netty中的传播事件可以分为两种:Inbound事件和Outbound事件。以下是Netty官网针对这两个事件的说明。

         由上可以看出,Inbound和Outbound事件的流向是不一样的,Inbound事件的流向是从下至上的,而Outbound恰好相反,是从下到上。并且Inbound方法是通过调用相应的ChannelHandlerContext.fireIN_EVT()方法来传递的,而Outbound方法是通过ChannelHandlerContext的fireChannelRegister()调用会发送一个ChannelRegistered的Inbound给下一个ChannelHandlerContext,而ChannelHandlerContext的bind()方法调用时会发送一个bind的Outbound事件给下一个ChannelHandlerContext。

        Inbound事件传播方法代码如下:

public interface ChannelInboundHandler extends ChannelHandler {

    /**
     * The {@link Channel} of the {@link ChannelHandlerContext} was registered with its {@link EventLoop}
     */
    void channelRegistered(ChannelHandlerContext ctx) throws Exception;

    /**
     * The {@link Channel} of the {@link ChannelHandlerContext} was unregistered from its {@link EventLoop}
     */
    void channelUnregistered(ChannelHandlerContext ctx) throws Exception;

    /**
     * The {@link Channel} of the {@link ChannelHandlerContext} is now active
     */
    void channelActive(ChannelHandlerContext ctx) throws Exception;

    /**
     * The {@link Channel} of the {@link ChannelHandlerContext} was registered is now inactive and reached its
     * end of lifetime.
     */
    void channelInactive(ChannelHandlerContext ctx) throws Exception;

    /**
     * Invoked when the current {@link Channel} has read a message from the peer.
     */
    void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception;

    /**
     * Invoked when the last message read by the current read operation has been consumed by
     * {@link #channelRead(ChannelHandlerContext, Object)}.  If {@link ChannelOption#AUTO_READ} is off, no further
     * attempt to read an inbound data from the current {@link Channel} will be made until
     * {@link ChannelHandlerContext#read()} is called.
     */
    void channelReadComplete(ChannelHandlerContext ctx) throws Exception;

    /**
     * Gets called if an user event was triggered.
     */
    void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception;

    /**
     * Gets called once the writable state of a {@link Channel} changed. You can check the state with
     * {@link Channel#isWritable()}.
     */
    void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception;

    /**
     * Gets called if a {@link Throwable} was thrown.
     */
    @Override
    @SuppressWarnings("deprecation")
    void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception;
}

        Outbound事件传播方法的代码如下:

public interface ChannelOutboundHandler extends ChannelHandler {
    /**
     * Called once a bind operation is made.
     *
     * @param ctx           the {@link ChannelHandlerContext} for which the bind operation is made
     * @param localAddress  the {@link SocketAddress} to which it should bound
     * @param promise       the {@link ChannelPromise} to notify once the operation completes
     * @throws Exception    thrown if an error accour
     */
    void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception;

    /**
     * Called once a connect operation is made.
     *
     * @param ctx               the {@link ChannelHandlerContext} for which the connect operation is made
     * @param remoteAddress     the {@link SocketAddress} to which it should connect
     * @param localAddress      the {@link SocketAddress} which is used as source on connect
     * @param promise           the {@link ChannelPromise} to notify once the operation completes
     * @throws Exception        thrown if an error accour
     */
    void connect(
            ChannelHandlerContext ctx, SocketAddress remoteAddress,
            SocketAddress localAddress, ChannelPromise promise) throws Exception;

    /**
     * Called once a disconnect operation is made.
     *
     * @param ctx               the {@link ChannelHandlerContext} for which the disconnect operation is made
     * @param promise           the {@link ChannelPromise} to notify once the operation completes
     * @throws Exception        thrown if an error accour
     */
    void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;

    /**
     * Called once a close operation is made.
     *
     * @param ctx               the {@link ChannelHandlerContext} for which the close operation is made
     * @param promise           the {@link ChannelPromise} to notify once the operation completes
     * @throws Exception        thrown if an error accour
     */
    void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;

    /**
     * Called once a deregister operation is made from the current registered {@link EventLoop}.
     *
     * @param ctx               the {@link ChannelHandlerContext} for which the close operation is made
     * @param promise           the {@link ChannelPromise} to notify once the operation completes
     * @throws Exception        thrown if an error accour
     */
    void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;

    /**
     * Intercepts {@link ChannelHandlerContext#read()}.
     */
    void read(ChannelHandlerContext ctx) throws Exception;

    /**
    * Called once a write operation is made. The write operation will write the messages through the
     * {@link ChannelPipeline}. Those are then ready to be flushed to the actual {@link Channel} once
     * {@link Channel#flush()} is called
     *
     * @param ctx               the {@link ChannelHandlerContext} for which the write operation is made
     * @param msg               the message to write
     * @param promise           the {@link ChannelPromise} to notify once the operation completes
     * @throws Exception        thrown if an error accour
     */
    void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception;

    /**
     * Called once a flush operation is made. The flush operation will try to flush out all previous written messages
     * that are pending.
     *
     * @param ctx               the {@link ChannelHandlerContext} for which the flush operation is made
     * @throws Exception        thrown if an error accour
     */
    void flush(ChannelHandlerContext ctx) throws Exception;
}

        可以发现,Inbound类似于事件回调(响应请求的事件),而Outbound类似于主动触发(发起请求的事件)。注意,如果捕获了一个事件,并且想让这个事件继续传递下去,需要调用Context对应的fireXXX()方法。

public class MyInboundHandler extends ChannelInboundHandlerAdapter {
    
    @Override
    public void channelActive(ChannelHandlerContext ctx)  throws Exception{
        System.out.println("连接成功");
        ctx.fireChannelActive();
    }
    
}
public class MyOutboundHandler extends ChannelOutboundHandlerAdapter {
    @Override
    public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception{
        System.out.println("客户端关闭");
        ctx.close(promise);
    }
}

        如上面的代码所示,MyInboundHandler收到了一个channelActive事件,它在处理后,如果希望将事件继续传播下去,那么需要接着调用ctx.fireChannelActive()方法。

        下面用一个代码案例了解一下Pipeline的传播机制。分别编写InboundHandlerA、InboundHandlerB、InboundHandlerC和OutboundandlerA、OutboundandlerB、OutboundandlerC类。

public class InboundHandlerA extends ChannelInboundHandlerAdapter {
    public void channelRead(ChannelHandlerContext ctx,Object msg){
        System.out.println("InboundHandlerA");
        ctx.fireChannelRead(msg);
    }
}
public class InboundHandlerB extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception{
        System.out.println("InboundHandlerB");
        ctx.fireChannelRead(msg);
    }
}
public class InboundHandlerC extends ChannelInboundHandlerAdapter {
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
        System.out.println("InboundHandlerC");
        ctx.fireChannelRead(msg);
    }
}

        以上三个类都调用了ctx.fireChannelRead()方法向下传播。

public class OutboundHandlerA extends ChannelOutboundHandlerAdapter {
    
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception{
        System.out.println("OutboundHandlerA write");
        ctx.write(msg,promise);
    }
}

public class OutboundHandlerB extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception{
        System.out.println("OutboundHandlerB write");
        ctx.write(msg,promise);
    }

    @Override
    public void handlerAdded(final ChannelHandlerContext ctx) throws Exception{
        ctx.executor().schedule(new Runnable() {
            @Override
            public void run() {
                ctx.channel().write("hello");
            }
        },3, TimeUnit.SECONDS);
    }
}

public class OutboundHandlerC extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception{
        System.out.println("OutboundHandlerC write");
        ctx.write(msg,promise);
    }
}

        上面的三个类都调用了ctx.write()方法。下面编写测试代码,来了解其传播顺序。先编写服务端代码。PipelineServer类主要完成Pipeline的注册工作,代码如下:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class PipelineServer {
    public void start(int port) throws Exception{
        NioEventLoopGroup bossGroup= new NioEventLoopGroup();
        NioEventLoopGroup workerGroup= new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception{
                            //InboundHandler的执行顺序,应该是 A B C
                            ch.pipeline().addLast(new InboundHandlerA());
                            ch.pipeline().addLast(new InboundHandlerB());
                            ch.pipeline().addLast(new InboundHandlerC());
                            
                            //Outbound的执行顺序应该是C B A
                            ch.pipeline().addLast(new OutboundHandlerA());
                            ch.pipeline().addLast(new OutboundHandlerB());
                            ch.pipeline().addLast(new OutboundHandlerC());
                        }
                    }).option(ChannelOption.SO_BACKLOG,128)
                    .childOption(ChannelOption.SO_KEEPALIVE,true);
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        }finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
        
    }

    public static void main(String[] args) throws Exception{
        PipelineServer server = new PipelineServer();
        server.start(8080);
    }
}

        PipelineClient类,与服务端建立连接并向服务端发送数据,代码如下:

public class PipelineClient {
    public void connect(String host,int port) throws Exception{
        NioEventLoopGroup workGroup = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(workGroup);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE,true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception{
                    ch.pipeline().addLast(new ClientInhandler());
                }
            });
            ChannelFuture f = b.connect(host,port).sync();
            f.channel().closeFuture().sync();
        }finally {
            workGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception{
        PipelineClient client = new PipelineClient();
        client.connect("127.0.0.1",8080);
    }
}

        ClientHandler类,完成向服务端发送数据的动作,代码如下:

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ClientInhandler extends ChannelInboundHandlerAdapter {

    //读取服务端的消息
    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception{
        System.out.println("clientInHandler.channelRead");
        ByteBuf result = (ByteBuf) msg;
        byte[] result1 = new byte[result.readableBytes()];
        result.readBytes(result1);
        result.release();
        ctx.close();
        System.out.println("server infomation:" + new String(result1));
    }

    //当连接建立的时候向服务端发送消息,channelActive 事件在连接建立的时候会被触发
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception{
        System.out.println("ClientHandler.channelActive");
        String msg = "are you ok";
        ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());
        encoded.writeBytes(msg.getBytes());
        ctx.write(encoded);
        ctx.flush();
    }
}

        接下来运行测试代码,分别启动PipelineServer和PipelineClient,得到的运行结果如下图:

        从运行结果上看,Handler的传播顺序:从Inbound开始顺序执行,然后从Outbound逆序执行。 

Netty的inbound和outbound事件传播过程 参考https://www.jianshu.com/p/772f14930f83, 这里对于文章里的 ctx.fireChannelRead(msg); 从头节点HeadContext开始传播 ctx.pipeline().fireChannelRead(msg);从当前节点往下传播事件 至今不清楚ctx.pipeline().fireChannelRead(msg) 是如何用的 ... 阅读详情

相关推荐

【信息科学与工程学】计算机科学与自动化 第二百三十三篇 凤凰架构01

编号类型领域系统软件/硬件或系统集成方案及系统架构模块子模块函数名称/算法名称算法的数学分析及计算机体系架构及数据结构与算法详细设计及参数列表及数值范围设计关联知识1共识算法分布式基石状态机复制架构:Proposer/Acceptor/Learner 多节点角色模型,基于操作转移(Operation Transfer)分布式共识PaxosPaxos 协议(Prepare/Promise/Accept/Accepted 两阶段)数学基础:多数派(Quorum)投票,基于鸽巢原理保证 W+R>N 时读写集合必有

weixin_49199313的博客 296

Netty源码分析之激活服务端通道

通道绑定活跃后激活管道的处理器 public final ChannelPipeline fireChannelActive() { AbstractChannelHandlerContext.invokeChannelActive(head); return this; } static void invokeChannelActive(final AbstractChan...

962

在Ubuntu 22.04上从零搭建TrinityCore 3.3.5服务器:一份保姆级避坑指南

本文详细介绍了在Ubuntu 22.04系统上从零搭建TrinityCore 3.3.5服务器的完整流程,包括环境准备、源码编译、数据库配置、资源文件处理及性能调优等关键步骤。特别针对国内开发者和低配置用户优化,提供保姆级避坑指南,帮助用户高效搭建魔兽世界经典旧世版本私服。

weixin_33393017的博客 315

Netty事件传播机制

文章目录源码栗子拾遗 源码 栗子 ch.pipeline().addLast(new InboundHandler1()); ch.pipeline().addLast(new InboundHandler2()); ch.pipeline().addLast(new OutboundHandler1()); ch.pipeline().addLast(new OutboundH...

牧夏的个人博客 5035

PLSQL 14.0.6 下载使用教程

众所周知,PLSQL 只是一个oracle第三方开发测试工具,它依赖oracle 客户端搭配使用,但是oracle 客户端有2G,相当庞大,不建议安装。所以我们如果下载安装好PLSQL,再下载好轻量级的支持远程链接的oracle客户端,这样搭配使用可以的话就最好了,答案是可以的。 第一步: PLSQL 下载与安装 PLSQL 14.0.6 下载地址 : https://www.allroundautomations.com/registered-plsqldev/ 下载下来,除了安装路径更改一下,

qq976511508的博客 2154

不只是CPU主频:深入解析英飞凌Aurix TC3xx各外设时钟树(CAN、以太网、Flash时序如何配置)

本文深入解析英飞凌Aurix TC3xx系列微控制器的外设时钟树配置,涵盖CAN、以太网和Flash时序的优化方法。通过详细分析时钟系统架构、关键外设配置及多外设协同设计策略,帮助工程师实现高性能汽车电子控制单元(ECU)开发。特别关注系统时钟的灵活配置与EMC优化技巧,提升系统实时性和可靠性。

weixin_29189373的博客 406

Netty使用及事件传递

本文地址: https://juejin.im/post/6844903985720229902 Netty 使用 首先到官网看一下Netty Server 和 Client的demo, https://netty.io/wiki/user-guide-for-4.x.html, 我用的是4.1.xx,一般来说不是大版本变更, 变化不会很大.下面是 Netty Server 的demo,跟官网的是一样的. public class Main { // 下面是一个接收线程, 3个worker线程.

一个程序员 1079

知识图谱构建中的数据清洗:AI原生应用高质量图谱的前提

数据清洗是知识图谱构建的核心前置环节,直接决定AI原生应用(如智能问答、个性化推荐、决策支持系统)的图谱质量与应用效能。本文以第一性原理为起点,系统解析数据清洗的理论框架、技术架构与工程实践,覆盖从多源异构数据的噪声检测到高质量知识三元组(实体-关系-实体)生成的全流程。通过层次化概念映射(专家→中级→入门)、数学形式化建模及工业级案例分析,揭示数据清洗在解决实体歧义、关系冲突、结构不一致等关键问题中的技术逻辑,并展望基于大模型的智能清洗范式演化。Completeness(完整性)

移动开发前沿的博客 413

Netty学习——源码篇8 Outbound/Inbound传播方式 备份

1、Outbound 事件是请求事件(由Context发起一个请求,并最终由Unsafe处理这个请求)。2、Outbound事件 发起者是Channel。3、Outbound事件的处理者是Unsafe。4、Outbound事件在Pipeline中的传输方向是从Tail到Head。5、在ChannelHandler中处理事件时,如果这个Handler不是最后一个Handler,则需要调用ctx的方法将此事件继续传播下去。如果不这样做,那么此事件的传播会提前终止。

geminigoth的博客 1097

JavaSwing_4.2: JDialog、JOptionPane(对话框)

JDialog,对话框。使用 JDialog 类可以创建自定义有的对话框,或者调用 JOptionPane 中的多个静态方法快速创建各种标准的对话框。JOptionPane是 JavaSwing 内部已实现好的,以静态方法的形式提供调用,能够快速方便的弹出要求用户提供值或向其发出通知的标准对话框。

谢TS的博客 7万+

张力控制速度闭环控制算法

这个算法的核心思想是通过比较实际速度和期望速度的差异来调整控制信号,以达到稳定的速度控制。比例项用于直接根据误差调整输出,积分项用于积累误差并进行补偿,微分项用于预测误差的变化趋势并进行调整。张力控制是在许多工程和工业应用中非常重要的一个过程,它涉及到通过调节力的大小来实现稳定的工作状态。通过合理设置比例、积分和微分系数,并结合实际系统的特性进行调试和优化,可以实现准确稳定的张力控制。速度闭环控制算法通过比较实际速度和期望速度之间的差异,并根据差异调整系统的输出,以保持速度稳定。

LiJavascript的博客 1315

Redis 从入门到精通:一份给 Java 工程师的系统学习指南

Redis 从入门到精通 一份给 Java 工程师的系统学习指南

weixin_45823828的博客 649

【云原生系列】第一讲:什么是云计算

云计算(cloud computing):分布式:分布式计算的一种,指的是通过网络“云”将巨大的数据计算处理程序分解成无数个小程序,合并&反馈:通过多部服务器组成的系统进行处理和分析这些小程序得到结果并返回给用户。云计算早期:分布式计算:简单的分布式计算,解决任务分发,并进行计算结果的合并。因而,云计算又称为网格计算。通过分布式计算,可以在很短的时间内(几秒钟)完成对数以万计的数据的处理,从而达到强大的网络服务。现阶段:计算:分布式计算、效用计算、负载均衡、并行计算、存储。

颜淡慕潇 1万+

Netty学习(六)----Netty基础组件入场介绍

Bootstrap 、ServerBootStrap Bootstrap 意思是引导,一个netty应用通常由一个Bootstrap开始,它主要作用是配置整个Netty程序,串联各个组件。Bootstrap、ServerBootStrap 分别对应客户端启动引导类与服务启动引导类 客户端声明: Bootstrap bootstrap = new Bootstrap(); ...

Lcy 538

uniapp - 实现苹果App打包上架App Store应用商店详细教程,详解Hbuilder打包编译ipa发布上传到苹果ios软件商店全流程,提供云打包/离线本地打包ios、申请IOS苹果证书方法

uni-app,苹果ios,苹果app,打包编译,appstore应用商店,ipa发布到软件商店,uniapp的app苹果应用商店上架,上架苹果APPStore,uniApp打包的ipa文件上传到商店,上架苹果商城,审核失败,被拒绝,UniApp打包ipa文件,上架iOSAppStore流程指南,最新详细教程最全,Hbuilder打包发布iosapp,苹果云打包上线全过程,云打包IPA并上传到商店,打包iOSipa文件后怎么发布到应用商店,苹果证书申请教程,ios证书申请,mac电脑,审核很久

🏆 前端领域优质创作者 1万+

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Netty学习笔记_10(Netty核心模块组件)

1、BootStrap,ServerBootStrap 一个Netty应用通常由一个BootStrap开始,主要作用是配置整个Netty程序,串联各个组件,Netty中的BootStrap类是客户端程序的启动引导类,ServerBootStrap是服务端启动引导类 常见方法: public ServerBootstrapgroup (EventLoopGroup parentGroup, EventLoopGroup childGroup) 作用于服..

feiku_ysu的博客 479

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Netty核心原理详解第二篇 Netty核心各组件剖析

本节课我们围绕Netty的引导器,学习了如何开发最基本的网络应用程序。引导器串接了Netty的所有核心组件,通过引导器作为学习Netty的切入点有助于我们快速上手。Netty的引导器作为一个非常方便的工具,避免我们再去手动完成繁琐的Channel的创建和配置等过程,其中有很多知识点可以深挖,在后续源码章节中我们再一起探索它的实现原理。本节课我们一起学习了NettyReactor线程模型的核心处理引擎EventLoop,熟悉了EventLoop的来龙去脉。httpshttps。...

fegus的博客 872

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Netty 学习(四):ChannelHandler 的事件传播和生命周期

作者: Grey原文地址:博客园:Netty 学习(四):ChannelHandler 的事件传播和生命周期CSDN:Netty 学习(四):ChannelHandler 的事件传播和生命周期在通信客户端和服务端,处理的流程大致有如下步骤 在『根据不同的消息指令解析数据包』这个步骤中,经常需要用来判断不同的指令类型并进行解析。逻辑一旦复杂,就会让代码变的极为臃肿,难以维护。Netty 中的 Pipeline 和 ChannelHandler 就是用来解决这个问题,它通过责任链设计模式来组织代码逻辑,并且能够

Practitioner 729

Netty——事件传播机制

本文讲解了 Netty 底层的事件传播机制,并介绍了 Netty 在这个职责链中内置的头节点和尾节点的作用。

qq_61350148的博客 1377

Netty---解码器/编码器

示例代码 // 必须先添加解码器/编码器 保证数据流动的顺序正确 ch.pipeline().addLast("decoder", new ProtobufDecoder(BalanceNetProtocol.BasePackage.getDefaultInstance()));

xiaoxishuaidaile的博客 1089

Netty学习——源码篇7 Pipeline的事件传播机制1

并且Inbound方法是通过调用相应的ChannelHandlerContext.fireIN_EVT()方法来传递的,而Outbound方法是通过ChannelHandlerContext的fireChannelRegister()调用会发送一个ChannelRegistered的Inbound给下一个ChannelHandlerContext,而ChannelHandlerContext的bind()方法调用时会发送一个bind的Outbound事件给下一个ChannelHandlerContext。

geminigoth的博客 1360

Netty粘包拆包详细总结

TCP黏包拆包 TCP是一个流协议,就是没有界限的一长串二进制数据。TCP作为传输层协议并不不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际情况进行数据包的划分,所以在业务上认为是一个完整的包,可能会被TCP拆分成多个包进行发送,也有可能把多个小的包封装成一个大的数据包发送,这就是所谓的TCP粘包和拆包问题。 怎么解决? 消息定长度,传输的数据大小固定长度,例如每段的长度固定为100...

彪锅锅来啦 1078

Netty入门-pipeline 与 channelHandler

pipeline 与 channelHandler 的构成 无论是从服务端来看,还是客户端来看,在 Netty 整个框架里面,一条连接对应着一个 Channel,这条 Channel 所有的处理逻辑都在一个叫做ChannelPipeline的对象里面,ChannelPipeline是一个双向链表结构,他和 Channel 之间是一对一的关系。 ChannelPipeline里面每个节点都是一个ChannelHandlerContext对象,这个对象能够拿到和 Channel 相...

小毛贼_哪里逃 406

Netty学习笔记(六)Pipeline的传播机制

前面简单提到了下Pipeline的传播机制,这里再详细分析下 Pipeline的传播机制中有两个非常重要的属性inbound和outbound(AbstractChannelHandlerContext的属性), inbound为true表示其对应的ChannelHandler实现了ChannelInboundHandler接口 outbound为true表示其对应的ChannelHandler实...

qq_35448165的博客 1831

Netty pipeline执行机制

pipeline结构如下 ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", new MyClientDecoder()); pipeline.addLast(new MyServerHandler()); pipeline.addLast("encoder", new MyClientEncoder()); MyServerHandler.java代码如

qq_2908411009的博客 1084

Netty4入门(二)

/** * */ package com.a; /** * */ public class Constant { public final static int HEADER_FLAG = 123456789; } /** * */ package com.a; /** * 请求消息 */ public class RequestMsg { priv...

qq5132834的专栏 382
上一篇: Netty学习——源码篇7 Pipeline的事件传播机制1
下一篇: Netty学习——源码篇8 Outbound/Inbound传播方式
geminigoth
博客等级 码龄16年 2694粉丝 224原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

geminigoth

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

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

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

打赏作者

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

抵扣说明:

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

余额充值