From 286b89933ccc0d7ace84bffc46c59853809700ca Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 6 Aug 2014 14:38:23 +0200 Subject: [PATCH] Allow to obtain RecvByteBufAllocator.Handle to allow more flexible implementations Motivation: At the moment it's only possible for a user to set the RecvByteBufAllocator for a Channel but not access the Handle once it is assigned. This makes it hard to write more flexible implementations. Modifications: Add a new method to the Channel.Unsafe to allow access the the used Handle for the Channel. The RecvByteBufAllocator.Handle is created lazily. Result: It's possible to write more flexible implementatons that allow to adjust stuff on the fly for a Handle that is used by a Channel --- .../io/netty/channel/epoll/EpollDatagramChannel.java | 6 +----- .../java/io/netty/channel/epoll/EpollSocketChannel.java | 5 +---- .../java/io/netty/channel/sctp/nio/NioSctpChannel.java | 7 +------ .../java/io/netty/channel/sctp/oio/OioSctpChannel.java | 7 +------ .../src/main/java/io/netty/channel/AbstractChannel.java | 9 +++++++++ transport/src/main/java/io/netty/channel/Channel.java | 6 ++++++ .../io/netty/channel/nio/AbstractNioByteChannel.java | 6 +----- .../io/netty/channel/oio/AbstractOioByteChannel.java | 6 +----- .../io/netty/channel/socket/nio/NioDatagramChannel.java | 7 ++----- .../io/netty/channel/socket/oio/OioDatagramChannel.java | 7 +------ 10 files changed, 24 insertions(+), 42 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java index 26cc442a73..85cc6c16f0 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java @@ -386,7 +386,6 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements } final class EpollDatagramChannelUnsafe extends AbstractEpollUnsafe { - private RecvByteBufAllocator.Handle allocHandle; @Override public void connect(SocketAddress remote, SocketAddress local, ChannelPromise channelPromise) { @@ -419,10 +418,7 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements @Override void epollInReady() { DatagramChannelConfig config = config(); - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); assert eventLoop().inEventLoop(); final ChannelPipeline pipeline = pipeline(); diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java index 008f0e94d7..4540943d6c 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java @@ -693,10 +693,7 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So final ChannelConfig config = config(); final ChannelPipeline pipeline = pipeline(); final ByteBufAllocator allocator = config.getAllocator(); - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf byteBuf = null; boolean close = false; diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java index 8542ce5fbe..e339e781b9 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java @@ -68,8 +68,6 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett private final NotificationHandler notificationHandler; - private RecvByteBufAllocator.Handle allocHandle; - private static SctpChannel newSctpChannel() { try { return SctpChannel.open(); @@ -265,10 +263,7 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett protected int doReadMessages(List buf) throws Exception { SctpChannel ch = javaChannel(); - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config().getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf buffer = allocHandle.allocate(config().getAllocator()); boolean free = true; try { diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java index e12981f7a6..c10a9bfb0d 100755 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java @@ -76,8 +76,6 @@ public class OioSctpChannel extends AbstractOioMessageChannel private final NotificationHandler notificationHandler; - private RecvByteBufAllocator.Handle allocHandle; - private static SctpChannel openChannel() { try { return SctpChannel.open(); @@ -187,10 +185,7 @@ public class OioSctpChannel extends AbstractOioMessageChannel Set reableKeys = readSelector.selectedKeys(); try { for (SelectionKey ignored : reableKeys) { - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config().getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf buffer = allocHandle.allocate(config().getAllocator()); boolean free = true; diff --git a/transport/src/main/java/io/netty/channel/AbstractChannel.java b/transport/src/main/java/io/netty/channel/AbstractChannel.java index 87e489bae1..ff9f498f9d 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannel.java @@ -397,8 +397,17 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha protected abstract class AbstractUnsafe implements Unsafe { private ChannelOutboundBuffer outboundBuffer = new ChannelOutboundBuffer(AbstractChannel.this); + private RecvByteBufAllocator.Handle recvHandle; private boolean inFlush0; + @Override + public RecvByteBufAllocator.Handle recvBufAllocHandle() { + if (recvHandle == null) { + recvHandle = config().getRecvByteBufAllocator().newHandle(); + } + return recvHandle; + } + @Override public final ChannelHandlerInvoker invoker() { return eventLoop().asInvoker(); diff --git a/transport/src/main/java/io/netty/channel/Channel.java b/transport/src/main/java/io/netty/channel/Channel.java index d3d2c48674..a41ee08448 100644 --- a/transport/src/main/java/io/netty/channel/Channel.java +++ b/transport/src/main/java/io/netty/channel/Channel.java @@ -440,6 +440,12 @@ public interface Channel extends AttributeMap, Comparable { */ interface Unsafe { + /** + * Return the assigned {@link RecvByteBufAllocator.Handle} which will be used to allocate {@link ByteBuf}'s when + * receiving data. + */ + RecvByteBufAllocator.Handle recvBufAllocHandle(); + /** * Returns the {@link ChannelHandlerInvoker} which is used by default unless specified by a user. */ diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java index 20e1727104..977614ca66 100644 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java @@ -58,7 +58,6 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { } private final class NioByteUnsafe extends AbstractNioUnsafe { - private RecvByteBufAllocator.Handle allocHandle; private void closeOnRead(ChannelPipeline pipeline) { SelectionKey key = selectionKey(); @@ -102,10 +101,7 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { final ChannelPipeline pipeline = pipeline(); final ByteBufAllocator allocator = config.getAllocator(); final int maxMessagesPerRead = config.getMaxMessagesPerRead(); - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle(); ByteBuf byteBuf = null; int messages = 0; diff --git a/transport/src/main/java/io/netty/channel/oio/AbstractOioByteChannel.java b/transport/src/main/java/io/netty/channel/oio/AbstractOioByteChannel.java index 285d853853..b65753636d 100644 --- a/transport/src/main/java/io/netty/channel/oio/AbstractOioByteChannel.java +++ b/transport/src/main/java/io/netty/channel/oio/AbstractOioByteChannel.java @@ -39,7 +39,6 @@ public abstract class AbstractOioByteChannel extends AbstractOioChannel { " (expected: " + StringUtil.simpleClassName(ByteBuf.class) + ", " + StringUtil.simpleClassName(FileRegion.class) + ')'; - private RecvByteBufAllocator.Handle allocHandle; private volatile boolean inputShutdown; /** @@ -82,10 +81,7 @@ public abstract class AbstractOioByteChannel extends AbstractOioChannel { final ChannelConfig config = config(); final ChannelPipeline pipeline = pipeline(); - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf byteBuf = allocHandle.allocate(alloc()); diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index 099b9f2b70..f43cf189f7 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -72,7 +72,6 @@ public final class NioDatagramChannel private final DatagramChannelConfig config; private Map> memberships; - private RecvByteBufAllocator.Handle allocHandle; private static DatagramChannel newSocket(SelectorProvider provider) { try { @@ -230,10 +229,8 @@ public final class NioDatagramChannel protected int doReadMessages(List buf) throws Exception { DatagramChannel ch = javaChannel(); DatagramChannelConfig config = config(); - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); + ByteBuf data = allocHandle.allocate(config.getAllocator()); boolean free = true; try { diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java index 07189de5a8..69d729eede 100644 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioDatagramChannel.java @@ -71,8 +71,6 @@ public class OioDatagramChannel extends AbstractOioMessageChannel private final DatagramChannelConfig config; private final java.net.DatagramPacket tmpPacket = new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0); - private RecvByteBufAllocator.Handle allocHandle; - private static MulticastSocket newSocket() { try { return new MulticastSocket(null); @@ -202,10 +200,7 @@ public class OioDatagramChannel extends AbstractOioMessageChannel @Override protected int doReadMessages(List buf) throws Exception { DatagramChannelConfig config = config(); - RecvByteBufAllocator.Handle allocHandle = this.allocHandle; - if (allocHandle == null) { - this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle(); - } + RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf data = config.getAllocator().heapBuffer(allocHandle.guess()); boolean free = true;