Netty——Unsafe容貌之揭

       上一篇 末尾简单提到了JDK的Unsafe类,其实是调用了一些底层的Native的一些方法实现,不希望用户调用的。而Netty中的Unsafe类实际上也是Channel的辅助类,不应该被用户直接调用的。实际的IO的读写操作都是有Unsafe完成的。好,我们简单总结一下。

      一,AIP接口功能列表:

Unsafe-API功能
序号 接口 注释
1,
RecvByteBufAllocator.Handle recvBufAllocHandle();
Return the assigned {@link RecvByteBufAllocator.Handle} which will be used to allocate {@link ByteBuf}'s when receiving data.
2,
ChannelHandlerInvoker invoker();
Returns the {@link ChannelHandlerInvoker} which is used by default unless specified by a user.
3,
SocketAddress localAddress();
 Return the {@link SocketAddress} to which is bound local or {@code null} if none.
4,
SocketAddress remoteAddress();
Return the {@link SocketAddress} to which is bound remote or {@code null} if none is bound yet.
5,
void register(EventLoop eventLoop, ChannelPromise promise);
Register the {@link Channel} of the {@link ChannelPromise} and notify the {@link ChannelFuture} once the registration was complete.
It's only safe to submit a new task to the {@link EventLoop} from within a {@link ChannelHandler} once the {@link ChannelPromise} succeeded. Otherwise the task may or may not be rejected.
6,
void bind(SocketAddress localAddress, ChannelPromise promise);
Bind the {@link SocketAddress} to the {@link Channel} of the {@link ChannelPromise} and notify it once its done.
7,
void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise);
Connect the {@link Channel} of the given {@link ChannelFuture} with the given remote {@link SocketAddress}.
If a specific local {@link SocketAddress} should be used it need to be given as argument. Otherwise just pass {@code null} to it.
The {@link ChannelPromise} will get notified once the connect operation was complete.
8,
void disconnect(ChannelPromise promise);
Disconnect the {@link Channel} of the {@link ChannelFuture} and notify the {@link ChannelPromise} once the operation was complete.
9,
void close(ChannelPromise promise);
Close the {@link Channel} of the {@link ChannelPromise} and notify the {@link ChannelPromise} once the operation was complete.
10,
void closeForcibly();
Closes the {@link Channel} immediately without firing any events.  Probably only useful when registration attempt failed.
11,
void deregister(ChannelPromise promise);
Deregister the {@link Channel} of the {@link ChannelPromise} from {@link EventLoop} and notify the {@link ChannelPromise} once the operation was complete.
12,
void beginRead();
Schedules a read operation that fills the inbound buffer of the first {@link ChannelHandler} in the {@link ChannelPipeline}.  If there's already a pending read operation, this method does nothing.
13,
void write(Object msg, ChannelPromise promise);
Schedules a write operation.
14,
void flush();
Flush out all write operations scheduled via {@link #write(Object, ChannelPromise)}.
15,
ChannelPromise voidPromise();
Return a special ChannelPromise which can be reused and passed to the operations in {@link Unsafe}. It will never be notified of a success or error and so is only a placeholder for operations that take a {@link ChannelPromise} as argument but for which you not want to get notified.
16,
ChannelOutboundBuffer outboundBuffer();
Returns the {@link ChannelOutboundBuffer} of the {@link Channel} where the pending write requests are stored.

      二,Unsafe的关系结构图:

     三,阅读源码:

       由于Channel的很多实现都是通过子类Unsafe来完成的,所以,这篇我们学习总结了一下Unsafe,这种方式是不是值得我们学习一下,不让希望外部直接调用的情况下,来帮助父类完成更多底层的东西。

       PS:Unsafe总结思维导图

猜你喜欢

转载自blog.csdn.net/liujiahan629629/article/details/84195024