Have (Epoll|KQueue)RecvByteAllocatorHandle extend DelegatingHandle (#9060)

Motivation

These implementations delegate most of their methods to an existing Handle and previously extended RecvByteBufAllocator.DelegatingHandle. This was reverted in #6322 with the introduction of ExtendedHandle but it's not clear to me why it needed to be - the code looks a lot cleaner.

Modifications

Have (Epoll|KQueue)RecvByteAllocatorHandle extend DelegatingHandle again, while still implementing ExtendedHandle.

Result

Less code.
This commit is contained in:
Nick Hill 2019-04-16 00:14:09 -07:00 committed by Norman Maurer
parent 0523c781e8
commit 7f79182c82
2 changed files with 19 additions and 88 deletions

View File

@ -15,25 +15,22 @@
*/
package io.netty.channel.epoll;
import static java.util.Objects.requireNonNull;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.RecvByteBufAllocator.DelegatingHandle;
import io.netty.channel.RecvByteBufAllocator.ExtendedHandle;
import io.netty.channel.unix.PreferredDirectByteBufAllocator;
import io.netty.util.UncheckedBooleanSupplier;
class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandle {
class EpollRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle {
private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
new PreferredDirectByteBufAllocator();
private final RecvByteBufAllocator.ExtendedHandle delegate;
private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead;
private boolean isEdgeTriggered;
private boolean receivedRdHup;
EpollRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) {
delegate = requireNonNull(handle, "handle");
EpollRecvByteAllocatorHandle(ExtendedHandle handle) {
super(handle);
}
final void receivedRdHup() {
@ -70,57 +67,17 @@ class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandl
public final ByteBuf allocate(ByteBufAllocator alloc) {
// We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
preferredDirectByteBufAllocator.updateAllocator(alloc);
return delegate.allocate(preferredDirectByteBufAllocator);
}
@Override
public final int guess() {
return delegate.guess();
}
@Override
public final void reset(ChannelConfig config) {
delegate.reset(config);
}
@Override
public final void incMessagesRead(int numMessages) {
delegate.incMessagesRead(numMessages);
}
@Override
public final void lastBytesRead(int bytes) {
delegate.lastBytesRead(bytes);
}
@Override
public final int lastBytesRead() {
return delegate.lastBytesRead();
}
@Override
public final int attemptedBytesRead() {
return delegate.attemptedBytesRead();
}
@Override
public final void attemptedBytesRead(int bytes) {
delegate.attemptedBytesRead(bytes);
}
@Override
public final void readComplete() {
delegate.readComplete();
return delegate().allocate(preferredDirectByteBufAllocator);
}
@Override
public final boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
return delegate.continueReading(maybeMoreDataSupplier);
return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier);
}
@Override
public final boolean continueReading() {
// We must override the supplier which determines if there maybe more data to read.
return delegate.continueReading(defaultMaybeMoreDataSupplier);
return continueReading(defaultMaybeMoreDataSupplier);
}
}

View File

@ -18,42 +18,36 @@ package io.netty.channel.kqueue;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.RecvByteBufAllocator.DelegatingHandle;
import io.netty.channel.RecvByteBufAllocator.ExtendedHandle;
import io.netty.channel.unix.PreferredDirectByteBufAllocator;
import io.netty.util.UncheckedBooleanSupplier;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
final class KQueueRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandle {
final class KQueueRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle {
private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
new PreferredDirectByteBufAllocator();
private final RecvByteBufAllocator.ExtendedHandle delegate;
private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead;
private boolean overrideGuess;
private boolean readEOF;
private long numberBytesPending;
KQueueRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) {
delegate = requireNonNull(handle, "handle");
KQueueRecvByteAllocatorHandle(ExtendedHandle handle) {
super(handle);
}
@Override
public int guess() {
return overrideGuess ? guess0() : delegate.guess();
return overrideGuess ? guess0() : delegate().guess();
}
@Override
public void reset(ChannelConfig config) {
overrideGuess = ((KQueueChannelConfig) config).getRcvAllocTransportProvidesGuess();
delegate.reset(config);
}
@Override
public void incMessagesRead(int numMessages) {
delegate.incMessagesRead(numMessages);
delegate().reset(config);
}
@Override
@ -61,44 +55,24 @@ final class KQueueRecvByteAllocatorHandle implements RecvByteBufAllocator.Extend
// We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
preferredDirectByteBufAllocator.updateAllocator(alloc);
return overrideGuess ? preferredDirectByteBufAllocator.ioBuffer(guess0()) :
delegate.allocate(preferredDirectByteBufAllocator);
delegate().allocate(preferredDirectByteBufAllocator);
}
@Override
public void lastBytesRead(int bytes) {
numberBytesPending = bytes < 0 ? 0 : max(0, numberBytesPending - bytes);
delegate.lastBytesRead(bytes);
}
@Override
public int lastBytesRead() {
return delegate.lastBytesRead();
}
@Override
public void attemptedBytesRead(int bytes) {
delegate.attemptedBytesRead(bytes);
}
@Override
public int attemptedBytesRead() {
return delegate.attemptedBytesRead();
}
@Override
public void readComplete() {
delegate.readComplete();
delegate().lastBytesRead(bytes);
}
@Override
public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
return delegate.continueReading(maybeMoreDataSupplier);
return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier);
}
@Override
public boolean continueReading() {
// We must override the supplier which determines if there maybe more data to read.
return delegate.continueReading(defaultMaybeMoreDataSupplier);
return continueReading(defaultMaybeMoreDataSupplier);
}
void readEOF() {