netty的启动过程是咋样的,就是那里启动了socket监听,咋处理请求的( 三 )
NioServerSocketChannel构造方法中我们看到已经创建了ServerSocketChannel,并且设置了IO模型,接下来我们关心的是ServerSocketChannel是在哪里注册监听Accept_Read事件的呢。我们从我们看fireChannelOpen(this)慢慢跟下去: public static void fireChannelOpen(Channel channel) { // Notify the parent handler. if (channel.getParent() != null) { fireChildChannelStateChanged(channel.getParent(), channel); } //封装一个UpstreamChannelStateEvent(channel, ChannelState.OPEN, Boolean.TRUE),并发送一个上线的消息 channel.getPipeline().sendUpstream( new UpstreamChannelStateEvent( channel, ChannelState.OPEN, Boolean.TRUE)); }我们看到这个方法吧open这个事件封装成一个封装一个UpstreamChannelStateEvent传递下去了,那么它传递到哪了呢? 接下来我们看SimpleChannelUpstreamHandler: public void handleUpstream( ChannelHandlerContext ctx, ChannelEvent e) throws Exception { if (e instanceof MessageEvent) { messageReceived(ctx, (MessageEvent) e); } else if (e instanceof WriteCompletionEvent) { WriteCompletionEvent evt = (WriteCompletionEvent) e; writeComplete(ctx, evt); } else if (e instanceof ChildChannelStateEvent) { ChildChannelStateEvent evt = (ChildChannelStateEvent) e; if (evt.getChildChannel().isOpen()) { childChannelOpen(ctx, evt); } else { childChannelClosed(ctx, evt); } } else if (e instanceof ChannelStateEvent) { ChannelStateEvent evt = (ChannelStateEvent) e; switch (evt.getState()) { case OPEN: if (Boolean.TRUE.equals(evt.getValue())) { //UpstreamChannelStateEvent ChannelState.OPEN进到如下分之逻辑中 channelOpen(ctx, evt); } else { channelClosed(ctx, evt); } break; case BOUND: if (evt.getValue() != null) { channelBound(ctx, evt); } else { channelUnbound(ctx, evt); } break; case CONNECTED: if (evt.getValue() != null) { channelConnected(ctx, evt); } else { channelDisconnected(ctx, evt); } break; case INTEREST_OPS: channelInterestChanged(ctx, evt); break; default: ctx.sendUpstream(e); } } else if (e instanceof ExceptionEvent) { exceptionCaught(ctx, (ExceptionEvent) e); } else { ctx.sendUpstream(e); } }我们会发现SimpleChannelUpstreamHandler类中handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) 方法主要负责处理各类的ChannelEvent,并将不同的event作不同的处理,而我们在前面传下来的UpstreamChannelStateEvent instanceof ChannelStateEvent,并且满足Boolean.TRUE.equals(evt.getValue()) 故封装成 channelOpen(ctx, evt);这个事件传递下去了。 但是这里我们可能会疑惑,为什么上面的UpstreamChannelStateEvent事件会传到SimpleChannelUpstreamHandler类里面了,它是在哪里绑定的呢? 我们回到上面的bindAsync方法中去,我们看这个Binder类,
推荐阅读
- 强寒潮来袭浙江启动低温雨雪冰冻灾害Ⅳ级应急响应
- 西藏航空一机长执飞过程中身体不适降落后送医去世
- 江苏省13市启动重污染天气黄色预警
- 兰州启动进口冷链食品监管总仓“出仓”须查验证明
- 朋友圈直播酒驾全过程?警方:当事人涉嫌酒驾接受调查
- “艺术无障碍平台”正式启动
- |共促和谐劳动关系 工会法律和集体协商技能竞赛启动
- 正点财经|
- |苏州·中国声学创新谷在常熟启动
- 大三学生准备日本留学过程中要不要准备考研
