日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Netty--ChannelHandler和ChannelPipeline

發布時間:2024/4/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Netty--ChannelHandler和ChannelPipeline 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ChannelHandler

類圖

ChannelHandler

public interface ChannelHandler {void handlerAdded(ChannelHandlerContext ctx) throws Exception;void handlerRemoved(ChannelHandlerContext ctx) throws Exception; }

ChannelInboundHandler

inbound事件:從I/O線程流向用戶業務handler的事件。

public interface ChannelInboundHandler extends ChannelHandler {void channelRegistered(ChannelHandlerContext ctx) throws Exception;void channelUnregistered(ChannelHandlerContext ctx) throws Exception;void channelActive(ChannelHandlerContext ctx) throws Exception;void channelInactive(ChannelHandlerContext ctx) throws Exception;void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception;void channelReadComplete(ChannelHandlerContext ctx) throws Exception;void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception;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; }

ChannelOutboundHandler

outbound事件:由用戶或代碼發起的I/O操作事件。

public interface ChannelOutboundHandler extends ChannelHandler {void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception;void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,SocketAddress localAddress, ChannelPromise promise) throws Exception;void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;void read(ChannelHandlerContext ctx) throws Exception;void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception;void flush(ChannelHandlerContext ctx) throws Exception; }

ByteToMessageDecoder

用于把讀取到的字節數組或字節緩沖區解析為業務POJO。

MessageToMessageDecoder

MessageToMessageDecoder為二次解碼器,用于把一個對象二次解析為另一個對象。

LineBasedFrameDecoder

通過長度處理半包,粘包問題

ChannelPipeline

Netty將Channel的數據管道抽象為ChannelPipeline,消息在ChannelPipeline中流動和傳遞。ChannelPipeline持有I/O事件攔截器ChannelHandler的鏈表,由ChannelHandler對I/O事件進行處理。

在ChannelPipeline 中ChannelHandler由ChannelHandlerContext進行了封裝,包含next,prev指針。

volatile AbstractChannelHandlerContext next;volatile AbstractChannelHandlerContext prev;

調用事件時,調用next或者prev的同名事件。

static void invokeChannelRegistered(final AbstractChannelHandlerContext next) {EventExecutor executor = next.executor();if (executor.inEventLoop()) {next.invokeChannelRegistered();} else {executor.execute(new Runnable() {@Overridepublic void run() {next.invokeChannelRegistered();}});}}

ChannelPipeline 包含2個屬性:head,tail,當fireXXX()時,調用head或tail的相關方法。

final AbstractChannelHandlerContext head;final AbstractChannelHandlerContext tail; public final ChannelPipeline fireChannelUnregistered() {AbstractChannelHandlerContext.invokeChannelUnregistered(head);return this;}

?

總結

以上是生活随笔為你收集整理的Netty--ChannelHandler和ChannelPipeline的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。