Giter VIP home page Giter VIP logo

nettypro's Introduction

netty的学习

关于select、selectionKey、channel的关系理解

img.png

每一个selectionKey都会记录 自己对应哪个selector 而selectionKey会有一个interest set 记录这个channel感兴趣的event类型有哪些 (四种类型 ) SelectionKey.OP_CONNECT 一个channel成功连接到另一个服务器称为”连接就绪“ SelectionKey.OP_ACCEPT 服务器开启 表示接收客户端的连接事件 SelectionKey.OP_READ 读就绪 SelectionKey.OP_WRITE 写就绪

注意 一个channel可能不只是只注册到一个selector 所以每个channel有一个selectionKey数组(标记对应的selector) 通过这个selectionKey数组(因为每个selectionKey都会记录对应的selector) 当要channel.register(selector,one of SelectionKy.InterestSet) 第二个参数就是上面的四种类型之一 这个方法 首先会调用channel的findKey(selector) 来找到对应的selectionKey 如果没找到 说明这个channel是第一次注册到该selector,则新建一个selectionKey对应这个selector 后续 channel如果要要注册新的interesting事件(上面的四种之一),就会找到这个selectionKey,往里面的interest set这个集合添加新的感兴趣的事情

一句话selectionKey对应一个selector 一个selector不一定只有一个selectionKey,channel通过selectionKey和对应的selector绑定 参考链接

netty的一些理解

img_1.png EventLoop就是一个线程 而EventLoop就是一个线程池 EventLoop对应多个channel,所以channel通过方法eventloop()就会获取管理它的那个线程 每个eventloop都有一个selector(底层就是NIO) 每个NioChannel都有一个唯一的ChannelPipeLine 参考

如何理解channelPipeLine

比如生活中对一个事物的加工,放在流水线上经历多次加工才算生产成功。 对于一个事件,(channel要干嘛),这个事件会被放在ChannelPipeLine,ChannelPipeLine可以绑定多个Handler,按顺序执行这些Handler,最后完成事件处理

添加一个Handler在流水线的最后一步 socketChannel.pipeline().addLast(new NettyServerHandler()); 参考

nettypro's People

Contributors

yhwsostupid avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.