Skip to content

Commit

Permalink
Fix visibility and make it possible to decorate IoRegistration sub-cl…
Browse files Browse the repository at this point in the history
…asses
  • Loading branch information
normanmaurer committed Apr 27, 2024
1 parent d1db017 commit aa7c143
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ final long doWriteOrSendBytes(ByteBuf data, InetSocketAddress remoteAddress, boo
}

if (data.nioBufferCount() > 1) {
IovArray array = ((EpollInternalIoRegistration) registration()).cleanIovArray();
IovArray array = registration().ioHandler().cleanIovArray();
array.add(data, data.readerIndex(), data.readableBytes());
int cnt = array.count();
assert cnt != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ protected int doWriteSingle(ChannelOutboundBuffer in) throws Exception {
*/
private int doWriteMultiple(ChannelOutboundBuffer in) throws Exception {
final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite();
IovArray array = ((EpollInternalIoRegistration) registration()).cleanIovArray();
IovArray array = registration().ioHandler().cleanIovArray();
array.maxBytes(maxBytesPerGatheringWrite);
in.forEachFlushedMessage(array);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,6 @@ private boolean scatteringRead(EpollRecvByteAllocatorHandle allocHandle, NativeD
}

private NativeDatagramPacketArray cleanDatagramPacketArray() {
return ((EpollInternalIoRegistration) registration()).cleanDatagramPacketArray();
return registration().ioHandler().cleanDatagramPacketArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private boolean doWriteMessage(Object msg) throws Exception {
remoteAddress.path().getBytes(CharsetUtil.UTF_8));
}
} else if (data.nioBufferCount() > 1) {
IovArray array = ((EpollInternalIoRegistration) registration()).cleanIovArray();
IovArray array = registration().ioHandler().cleanIovArray();
array.add(data, data.readerIndex(), data.readableBytes());
int cnt = array.count();
assert cnt != 0;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.netty.channel.IoHandler;
import io.netty.channel.IoHandlerFactory;
import io.netty.channel.IoOpt;
import io.netty.channel.IoRegistration;
import io.netty.channel.SelectStrategy;
import io.netty.channel.SelectStrategyFactory;
import io.netty.channel.unix.FileDescriptor;
Expand Down Expand Up @@ -257,7 +256,7 @@ public void destroy() {
}
}

private final class DefaultEpollIoRegistration extends AtomicBoolean implements EpollInternalIoRegistration {
private final class DefaultEpollIoRegistration extends AtomicBoolean implements EpollIoRegistration {
private final IoEventLoop eventLoop;
final EpollIoHandle handle;

Expand All @@ -269,18 +268,6 @@ private final class DefaultEpollIoRegistration extends AtomicBoolean implements
this.currentOpt = initialOpt;
}

@Override
public IovArray cleanIovArray() {
assert eventLoop.inEventLoop();
return EpollIoHandler.this.cleanIovArray();
}

@Override
public NativeDatagramPacketArray cleanDatagramPacketArray() {
assert eventLoop.inEventLoop();
return EpollIoHandler.this.cleanDatagramPacketArray();
}

@Override
public void updateInterestOpt(EpollIoOpt opt) throws IOException {
currentOpt = opt;
Expand All @@ -301,6 +288,11 @@ public EpollIoOpt interestOpt() {
return currentOpt;
}

@Override
public EpollIoHandler ioHandler() {
return EpollIoHandler.this;
}

@Override
public boolean isValid() {
return !get();
Expand Down Expand Up @@ -355,8 +347,8 @@ void handle(long ev) {
}

@Override
public IoRegistration register(IoEventLoop eventLoop, IoHandle handle,
IoOpt initialOpt)
public EpollIoRegistration register(IoEventLoop eventLoop, IoHandle handle,
IoOpt initialOpt)
throws Exception {
final EpollIoHandle epollHandle = cast(handle);
EpollIoOpt opt = cast(initialOpt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ public interface EpollIoRegistration extends IoRegistration {

@Override
void cancel() throws IOException;

@Override
EpollIoHandler ioHandler();
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ protected int doWriteSingle(ChannelOutboundBuffer in) throws Exception {
*/
private int doWriteMultiple(ChannelOutboundBuffer in) throws Exception {
final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite();
IovArray array = ((KQueueInternalIoRegistration) registration()).cleanArray();
IovArray array = registration().ioHandler().cleanArray();
array.maxBytes(maxBytesPerGatheringWrite);
in.forEachFlushedMessage(array);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ protected boolean doWriteMessage(Object msg) throws Exception {
remoteAddress.getAddress(), remoteAddress.getPort());
}
} else if (data.nioBufferCount() > 1) {
IovArray array = ((KQueueInternalIoRegistration) registration()).cleanArray();
IovArray array = registration().ioHandler().cleanArray();
array.add(data, data.readerIndex(), data.readableBytes());
int cnt = array.count();
assert cnt != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected boolean doWriteMessage(Object msg) throws Exception {
remoteAddress.path().getBytes(CharsetUtil.UTF_8));
}
} else if (data.nioBufferCount() > 1) {
IovArray array = ((KQueueInternalIoRegistration) registration()).cleanArray();
IovArray array = registration().ioHandler().cleanArray();
array.add(data, data.readerIndex(), data.readableBytes());
int cnt = array.count();
assert cnt != 0;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
import static java.lang.Math.min;

/**
* {@link EventLoop} which uses kqueue under the covers. Only works on BSD!
* {@link IoHandler} which uses kqueue under the covers. Only works on BSD!
*/
final class KQueueIoHandler implements IoHandler {
public final class KQueueIoHandler implements IoHandler {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(KQueueIoHandler.class);
private static final AtomicIntegerFieldUpdater<KQueueIoHandler> WAKEN_UP_UPDATER =
AtomicIntegerFieldUpdater.newUpdater(KQueueIoHandler.class, "wakenUp");
Expand Down Expand Up @@ -125,7 +125,7 @@ private KQueueIoHandler(int maxEvents, SelectStrategy strategy) {
/**
* Return a cleared {@link IovArray} that can be used for writes in this {@link EventLoop}.
*/
private IovArray cleanArray() {
IovArray cleanArray() {
iovArray.clear();
return iovArray;
}
Expand Down Expand Up @@ -325,8 +325,7 @@ public void destroy() {
}

@Override
public IoRegistration register(IoEventLoop eventLoop, IoHandle handle,
IoOpt initialOpts) {
public KQueueIoRegistration register(IoEventLoop eventLoop, IoHandle handle, IoOpt initialOpts) {
final KQueueIoHandle kqueueHandle = cast(handle);
if (kqueueHandle.ident() == KQUEUE_WAKE_UP_IDENT) {
throw new IllegalArgumentException("ident " + KQUEUE_WAKE_UP_IDENT + " is reserved for internal usage");
Expand Down Expand Up @@ -367,8 +366,8 @@ public boolean isCompatible(Class<? extends IoHandle> handleType) {
return KQueueIoHandle.class.isAssignableFrom(handleType);
}

private final class DefaultKqueueIoRegistration extends AtomicBoolean implements KQueueInternalIoRegistration {
private final KQueueEventIoOpt readyEventIoOpt = new KQueueEventIoOpt();;
private final class DefaultKqueueIoRegistration extends AtomicBoolean implements KQueueIoRegistration {
private final KQueueEventIoOpt readyEventIoOpt = new KQueueEventIoOpt();

final KQueueIoHandle handle;

Expand All @@ -395,8 +394,8 @@ public void addOpt(KQueueEventIoOpt opt) {
}

@Override
public IovArray cleanArray() {
return KQueueIoHandler.this.cleanArray();
public KQueueIoHandler ioHandler() {
return KQueueIoHandler.this;
}

void handle(int ident, short filter, short flags, int fflags, long data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ public interface KQueueIoRegistration extends IoRegistration {
* @param opt the {@link KQueueEventIoOpt} to use.
*/
void addOpt(KQueueEventIoOpt opt);

@Override
KQueueIoHandler ioHandler();
}
7 changes: 7 additions & 0 deletions transport/src/main/java/io/netty/channel/IoRegistration.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public interface IoRegistration {
* Cancel the registration.
*/
void cancel() throws Exception;

/**
* The {@link IoHandler} to which this {@link IoRegistration} belongs too.
*
* @return ioHandler.
*/
IoHandler ioHandler();
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,10 @@ private void cancel0() {
handle.deregisterNow();
}
}

@Override
public IoHandler ioHandler() {
return LocalIoHandler.this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.netty.channel.IoHandler;
import io.netty.channel.IoHandlerFactory;
import io.netty.channel.IoOpt;
import io.netty.channel.IoRegistration;
import io.netty.channel.SelectStrategy;
import io.netty.channel.SelectStrategyFactory;
import io.netty.util.IntSupplier;
Expand Down Expand Up @@ -377,10 +376,15 @@ void close() {
void handle(int ready) {
handle.handle(this, NioIoOpt.valueOf(ready));
}

@Override
public NioIoHandler ioHandler() {
return NioIoHandler.this;
}
}

@Override
public IoRegistration register(IoEventLoop eventLoop, IoHandle handle, IoOpt initialOpt)
public NioIoRegistration register(IoEventLoop eventLoop, IoHandle handle, IoOpt initialOpt)
throws Exception {
NioHandle nioHandle = nioHandle(handle);
NioIoOpt opt = cast(initialOpt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ public interface NioIoRegistration extends IoRegistration {

@Override
void cancel();

@Override
NioIoHandler ioHandler();
}

0 comments on commit aa7c143

Please sign in to comment.