From f381c0ea51d4ec6d83f51e72e0559765f75656cd Mon Sep 17 00:00:00 2001 From: xiongkunlun <18681768004@163.com> Date: Wed, 9 Sep 2020 10:01:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/netty/buffer/AbstractByteBuf.java | 23 +++++++++++++++++-- .../main/java/io/netty/buffer/ByteBuf.java | 3 ++- .../java/io/netty/util/ReferenceCounted.java | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 9ce8ccc..6281eb1 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -283,10 +283,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + /** + * 确定可写容量+要写容量 < 最大容量 + * @param minWritableBytes + */ final void ensureWritable0(int minWritableBytes) { final int writerIndex = writerIndex(); final int targetCapacity = writerIndex + minWritableBytes; if (targetCapacity <= capacity()) { + //检查buf释放情况,不抛出异常则正常返回 ensureAccessible(); return; } @@ -1069,8 +1074,17 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + /** + * 将byte数组写入到buf中 + * @param src + * @param srcIndex the first index of the source + * @param length the number of bytes to transfer + * + * @return + */ @Override public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + //确定容量 ensureWritable(length); setBytes(writerIndex, src, srcIndex, length); writerIndex += length; @@ -1387,12 +1401,17 @@ public abstract class AbstractByteBuf extends ByteBuf { return ByteBufUtil.compare(this, that); } + /** + * 重写toString,如果引用计数=0,则表示将被释放 + * 展示 readIndex,writeIndex,capacity容量 + * @return + */ @Override public String toString() { if (refCnt() == 0) { return StringUtil.simpleClassName(this) + "(freed)"; } - + // 返回相关容量信息 StringBuilder buf = new StringBuilder() .append(StringUtil.simpleClassName(this)) .append("(ridx: ").append(readerIndex) @@ -1401,7 +1420,7 @@ public abstract class AbstractByteBuf extends ByteBuf { if (maxCapacity != Integer.MAX_VALUE) { buf.append('/').append(maxCapacity); } - + //返回buf的根本类型 ByteBuf unwrapped = unwrap(); if (unwrapped != null) { buf.append(", unwrapped: ").append(unwrapped); diff --git a/buffer/src/main/java/io/netty/buffer/ByteBuf.java b/buffer/src/main/java/io/netty/buffer/ByteBuf.java index 850c85a..ea39d6e 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBuf.java @@ -17,6 +17,7 @@ package io.netty.buffer; import io.netty.util.ByteProcessor; import io.netty.util.ReferenceCounted; +import sun.misc.Unsafe; import java.io.IOException; import java.io.InputStream; @@ -2354,7 +2355,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { /** * Returns the backing byte array of this buffer. - * + * 堆内buf会返回数组对象,直接内存buf将抛出异常 * @throws UnsupportedOperationException * if there no accessible backing byte array */ diff --git a/common/src/main/java/io/netty/util/ReferenceCounted.java b/common/src/main/java/io/netty/util/ReferenceCounted.java index 2e090cf..f348bec 100644 --- a/common/src/main/java/io/netty/util/ReferenceCounted.java +++ b/common/src/main/java/io/netty/util/ReferenceCounted.java @@ -31,11 +31,13 @@ package io.netty.util; */ public interface ReferenceCounted { /** + * 返回对象的引用计数, 0=已经被释放了 * Returns the reference count of this object. If {@code 0}, it means this object has been deallocated. */ int refCnt(); /** + * 引用计数+1 * Increases the reference count by {@code 1}. */ ReferenceCounted retain(); -- Gitee From 7dc004dab636bfd8f9aec7c484cda42da6f63b6e Mon Sep 17 00:00:00 2001 From: xiongkunlun <18681768004@163.com> Date: Wed, 9 Sep 2020 11:12:30 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0channel=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/netty/channel/Channel.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/Channel.java b/transport/src/main/java/io/netty/channel/Channel.java index 6acb59d..8132f79 100644 --- a/transport/src/main/java/io/netty/channel/Channel.java +++ b/transport/src/main/java/io/netty/channel/Channel.java @@ -78,48 +78,56 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl /** * Returns the globally unique identifier of this {@link Channel}. + * 返回全局唯一的channelId对象, */ ChannelId id(); /** * Return the {@link EventLoop} this {@link Channel} was registered to. + * 返回channel被注册于哪个eventloop */ EventLoop eventLoop(); /** * Returns the parent of this channel. - * + *返回父channel,如果没有则为null * @return the parent channel. * {@code null} if this channel does not have a parent channel. */ Channel parent(); /** + * 返回配置 * Returns the configuration of this channel. */ ChannelConfig config(); /** + * 返回channel是否open状态,稍后将处于活跃状态 * Returns {@code true} if the {@link Channel} is open and may get active later */ boolean isOpen(); /** + * 返回是否已注册到eventloop * Returns {@code true} if the {@link Channel} is registered with an {@link EventLoop}. */ boolean isRegistered(); /** + * channel如果处于活跃状态,因此肯定是已连接(的状态) * Return {@code true} if the {@link Channel} is active and so connected. */ boolean isActive(); /** + * 返回channel的元数据 * Return the {@link ChannelMetadata} of the {@link Channel} which describe the nature of the {@link Channel}. */ ChannelMetadata metadata(); /** + * 返回channel绑定的本地地址 * Returns the local address where this channel is bound to. The returned * {@link SocketAddress} is supposed to be down-cast into more concrete * type such as {@link InetSocketAddress} to retrieve the detailed @@ -131,6 +139,7 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl SocketAddress localAddress(); /** + * 返回channle所连接的对方地址,如果连接关闭,则返回null,一些不借助不用连接连也可以传输数据的情况下,也返回null,比如 udp协议 * Returns the remote address where this channel is connected to. The * returned {@link SocketAddress} is supposed to be down-cast into more * concrete type such as {@link InetSocketAddress} to retrieve the detailed @@ -147,12 +156,14 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl SocketAddress remoteAddress(); /** + * 返回channel关闭时将会被通知的futrue对象 * Returns the {@link ChannelFuture} which will be notified when this * channel is closed. This method always returns the same future instance. */ ChannelFuture closeFuture(); /** + * 当且仅当io线程可以立即执行写操作时,返回ture。当返回false时,所有的写操作都会进入队列,直到io线程又开始处理写操作时。 * Returns {@code true} if and only if the I/O thread will perform the * requested write operation immediately. Any write requests made when * this method returns {@code false} are queued until the I/O thread is @@ -161,6 +172,7 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl boolean isWritable(); /** + * 当 isWritable()返回false时,返回可以写入多少个bytes。 * Get how many bytes can be written until {@link #isWritable()} returns {@code false}. * This quantity will always be non-negative. If {@link #isWritable()} is {@code false} then 0. */ @@ -209,6 +221,7 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl interface Unsafe { /** + * 返回,RecvByteBufAllocator对象,这个对象用来接受到数据后分配bytebuf * Return the assigned {@link RecvByteBufAllocator.Handle} which will be used to allocate {@link ByteBuf}'s when * receiving data. */ @@ -227,18 +240,23 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl SocketAddress remoteAddress(); /** + * 将 channelPromise中的channel注册到evnetloop上,并在注册完成后通知 channelFuture(promise是futrue的子类)。 * Register the {@link Channel} of the {@link ChannelPromise} and notify * the {@link ChannelFuture} once the registration was complete. */ void register(EventLoop eventLoop, ChannelPromise promise); /** + * 将socketaddress绑定到channel上,并在完成后通知promise * Bind the {@link SocketAddress} to the {@link Channel} of the {@link ChannelPromise} and notify * it once its done. */ void bind(SocketAddress localAddress, ChannelPromise promise); /** + * 将入参 对方地址和给定的 promise中的channel连接起来,并和特定的本地socket地址绑定,不指定本地socket地址的话, + * localAddress可以传null + * 连接操作完成后,会通知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. @@ -248,46 +266,55 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise); /** + * 断开连接,并在完成后通知promise * Disconnect the {@link Channel} of the {@link ChannelFuture} and notify the {@link ChannelPromise} once the * operation was complete. */ void disconnect(ChannelPromise promise); /** + * 关闭channel,并通知promise * Close the {@link Channel} of the {@link ChannelPromise} and notify the {@link ChannelPromise} once the * operation was complete. */ void close(ChannelPromise promise); /** + * 强制关闭channel,且不触发任意事件,一般用于注册失败的时候 * Closes the {@link Channel} immediately without firing any events. Probably only useful * when registration attempt failed. */ void closeForcibly(); /** + * 从eventloop中取消注册channel,并通知promise * Deregister the {@link Channel} of the {@link ChannelPromise} from {@link EventLoop} and notify the * {@link ChannelPromise} once the operation was complete. */ void deregister(ChannelPromise promise); /** - * Schedules a read operation that fills the inbound buffer of the first {@link ChannelInboundHandler} in the + * 准备一次读操作,该读操作会把管道中第一个ChannelInboundHandler的 inbound buffer 缓冲块填满,如果已有读操作了,这个方法将不会生效 + * Schedules安排,预定 a read operation that fills the inbound buffer of the first {@link ChannelInboundHandler} in the * {@link ChannelPipeline}. If there's already a pending read operation, this method does nothing. */ void beginRead(); /** + * 计划一次写操作 * Schedules a write operation. */ void write(Object msg, ChannelPromise promise); /** + * 对所有写操作执行flush * Flush out all write operations scheduled via {@link #write(Object, ChannelPromise)}. */ void flush(); /** + * 返回一个特殊的promise对象,并且可以在unsafe的操作中重复使用和传递。这个特殊的promise + * 仅仅作为操作的载体,但不接收任何成功或失败的通知 * 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. @@ -295,6 +322,7 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparabl ChannelPromise voidPromise(); /** + * 返回即将发生写操作的channel中的ChannelOutboundBuffer * Returns the {@link ChannelOutboundBuffer} of the {@link Channel} where the pending write requests are stored. */ ChannelOutboundBuffer outboundBuffer(); -- Gitee From df219e8badde45498b4cd48e06a16464710175f9 Mon Sep 17 00:00:00 2001 From: xiongkunlun <18681768004@163.com> Date: Wed, 9 Sep 2020 18:02:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0boots=EF=BC=8Chandler?= =?UTF-8?q?=EF=BC=8Cinbond=EF=BC=8Coutbond=E6=8E=A5=E5=8F=A3=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/netty/bootstrap/ServerBootstrap.java | 5 +++++ .../src/main/java/io/netty/channel/ChannelHandler.java | 3 +++ .../java/io/netty/channel/ChannelHandlerAdapter.java | 1 + .../java/io/netty/channel/ChannelHandlerContext.java | 5 +++++ .../java/io/netty/channel/ChannelInboundHandler.java | 10 ++++++++++ .../io/netty/channel/ChannelInboundHandlerAdapter.java | 1 + 6 files changed, 25 insertions(+) diff --git a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java index 084c42f..76e2a54 100644 --- a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java @@ -67,6 +67,7 @@ public class ServerBootstrap extends AbstractBootstrap