From d65507d4d55e70054a9699919673ca0a9e362df2 Mon Sep 17 00:00:00 2001 From: xDark <19853368+xxDark@users.noreply.github.com> Date: Thu, 30 Jul 2020 17:34:37 +0300 Subject: [PATCH 1/3] Use channel factories to create new channels --- .../proxy/network/ConnectionManager.java | 7 ++- .../proxy/network/TransportType.java | 45 +++++++++++++++---- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java index aacde7c3a..c5408fc60 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java @@ -26,7 +26,6 @@ import org.asynchttpclient.AsyncHttpClient; import org.asynchttpclient.RequestBuilder; import org.asynchttpclient.filter.FilterContext; import org.asynchttpclient.filter.FilterContext.FilterContextBuilder; -import org.asynchttpclient.filter.FilterException; import org.asynchttpclient.filter.RequestFilter; import org.checkerframework.checker.nullness.qual.Nullable; @@ -93,7 +92,7 @@ public final class ConnectionManager { */ public void bind(final InetSocketAddress address) { final ServerBootstrap bootstrap = new ServerBootstrap() - .channel(this.transportType.serverSocketChannelClass) + .channelFactory(this.transportType.serverSocketChannelFactory) .group(this.bossGroup, this.workerGroup) .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, SERVER_WRITE_MARK) .childHandler(this.serverChannelInitializer.get()) @@ -126,7 +125,7 @@ public final class ConnectionManager { public void queryBind(final String hostname, final int port) { InetSocketAddress address = new InetSocketAddress(hostname, port); final Bootstrap bootstrap = new Bootstrap() - .channel(this.transportType.datagramChannelClass) + .channelFactory(this.transportType.datagramChannelFactory) .group(this.workerGroup) .handler(new GS4QueryHandler(this.server)) .localAddress(address); @@ -151,7 +150,7 @@ public final class ConnectionManager { */ public Bootstrap createWorker(@Nullable EventLoopGroup group) { Bootstrap bootstrap = new Bootstrap() - .channel(this.transportType.socketChannelClass) + .channelFactory(this.transportType.socketChannelFactory) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.server.getConfiguration().getConnectTimeout()) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java index b5829f375..a2135f117 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java @@ -1,7 +1,9 @@ package com.velocitypowered.proxy.network; import com.velocitypowered.proxy.util.concurrent.VelocityNettyThreadFactory; +import io.netty.channel.ChannelFactory; import io.netty.channel.EventLoopGroup; +import io.netty.channel.ReflectiveChannelFactory; import io.netty.channel.epoll.Epoll; import io.netty.channel.epoll.EpollDatagramChannel; import io.netty.channel.epoll.EpollEventLoopGroup; @@ -18,28 +20,55 @@ import java.util.concurrent.ThreadFactory; import java.util.function.BiFunction; enum TransportType { - NIO("NIO", NioServerSocketChannel.class, NioSocketChannel.class, NioDatagramChannel.class, + NIO("NIO", NioServerSocketChannel::new, NioServerSocketChannel.class, + NioSocketChannel::new, NioSocketChannel.class, + NioDatagramChannel::new, NioDatagramChannel.class, (name, type) -> new NioEventLoopGroup(0, createThreadFactory(name, type))), - EPOLL("epoll", EpollServerSocketChannel.class, EpollSocketChannel.class, - EpollDatagramChannel.class, + EPOLL("epoll", EpollServerSocketChannel::new, EpollServerSocketChannel.class, + EpollSocketChannel::new, EpollSocketChannel.class, + EpollDatagramChannel::new, EpollDatagramChannel.class, (name, type) -> new EpollEventLoopGroup(0, createThreadFactory(name, type))); final String name; + @Deprecated final Class serverSocketChannelClass; + @Deprecated final Class socketChannelClass; + @Deprecated final Class datagramChannelClass; + final ChannelFactory serverSocketChannelFactory; + final ChannelFactory socketChannelFactory; + final ChannelFactory datagramChannelFactory; final BiFunction eventLoopGroupFactory; + TransportType(final String name, + final ChannelFactory serverSocketChannelFactory, + final Class serverSocketChannelClass, + final ChannelFactory socketChannelFactory, + final Class socketChannelClass, + final ChannelFactory datagramChannelFactory, + final Class datagramChannelClass, + final BiFunction eventLoopGroupFactory) { + this.name = name; + this.serverSocketChannelClass = serverSocketChannelClass; + this.socketChannelClass = socketChannelClass; + this.datagramChannelClass = datagramChannelClass; + this.serverSocketChannelFactory = serverSocketChannelFactory; + this.socketChannelFactory = socketChannelFactory; + this.datagramChannelFactory = datagramChannelFactory; + this.eventLoopGroupFactory = eventLoopGroupFactory; + } + + @Deprecated TransportType(final String name, final Class serverSocketChannelClass, final Class socketChannelClass, final Class datagramChannelClass, final BiFunction eventLoopGroupFactory) { - this.name = name; - this.serverSocketChannelClass = serverSocketChannelClass; - this.socketChannelClass = socketChannelClass; - this.datagramChannelClass = datagramChannelClass; - this.eventLoopGroupFactory = eventLoopGroupFactory; + this(name, new ReflectiveChannelFactory<>(serverSocketChannelClass), serverSocketChannelClass, + new ReflectiveChannelFactory<>(socketChannelClass), socketChannelClass, + new ReflectiveChannelFactory<>(datagramChannelClass), datagramChannelClass, + eventLoopGroupFactory); } @Override From 6379936f324ac0cfefe84b1a41e213a250d936d6 Mon Sep 17 00:00:00 2001 From: xDark <19853368+xxDark@users.noreply.github.com> Date: Thu, 30 Jul 2020 17:36:57 +0300 Subject: [PATCH 2/3] Fix code style --- .../proxy/network/TransportType.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java index a2135f117..63c9c5eec 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java @@ -42,13 +42,13 @@ enum TransportType { final BiFunction eventLoopGroupFactory; TransportType(final String name, - final ChannelFactory serverSocketChannelFactory, - final Class serverSocketChannelClass, - final ChannelFactory socketChannelFactory, - final Class socketChannelClass, - final ChannelFactory datagramChannelFactory, - final Class datagramChannelClass, - final BiFunction eventLoopGroupFactory) { + final ChannelFactory serverSocketChannelFactory, + final Class serverSocketChannelClass, + final ChannelFactory socketChannelFactory, + final Class socketChannelClass, + final ChannelFactory datagramChannelFactory, + final Class datagramChannelClass, + final BiFunction eventLoopGroupFactory) { this.name = name; this.serverSocketChannelClass = serverSocketChannelClass; this.socketChannelClass = socketChannelClass; From 76bef1337763db5350e6dd13a563673c99ba9338 Mon Sep 17 00:00:00 2001 From: xDark <19853368+xxDark@users.noreply.github.com> Date: Fri, 31 Jul 2020 14:17:31 +0300 Subject: [PATCH 3/3] Remove deprecated members --- .../proxy/network/TransportType.java | 37 +++---------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java index 63c9c5eec..d241267b9 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java @@ -3,7 +3,6 @@ package com.velocitypowered.proxy.network; import com.velocitypowered.proxy.util.concurrent.VelocityNettyThreadFactory; import io.netty.channel.ChannelFactory; import io.netty.channel.EventLoopGroup; -import io.netty.channel.ReflectiveChannelFactory; import io.netty.channel.epoll.Epoll; import io.netty.channel.epoll.EpollDatagramChannel; import io.netty.channel.epoll.EpollEventLoopGroup; @@ -20,22 +19,16 @@ import java.util.concurrent.ThreadFactory; import java.util.function.BiFunction; enum TransportType { - NIO("NIO", NioServerSocketChannel::new, NioServerSocketChannel.class, - NioSocketChannel::new, NioSocketChannel.class, - NioDatagramChannel::new, NioDatagramChannel.class, + NIO("NIO", NioServerSocketChannel::new, + NioSocketChannel::new, + NioDatagramChannel::new, (name, type) -> new NioEventLoopGroup(0, createThreadFactory(name, type))), - EPOLL("epoll", EpollServerSocketChannel::new, EpollServerSocketChannel.class, - EpollSocketChannel::new, EpollSocketChannel.class, - EpollDatagramChannel::new, EpollDatagramChannel.class, + EPOLL("epoll", EpollServerSocketChannel::new, + EpollSocketChannel::new, + EpollDatagramChannel::new, (name, type) -> new EpollEventLoopGroup(0, createThreadFactory(name, type))); final String name; - @Deprecated - final Class serverSocketChannelClass; - @Deprecated - final Class socketChannelClass; - @Deprecated - final Class datagramChannelClass; final ChannelFactory serverSocketChannelFactory; final ChannelFactory socketChannelFactory; final ChannelFactory datagramChannelFactory; @@ -43,34 +36,16 @@ enum TransportType { TransportType(final String name, final ChannelFactory serverSocketChannelFactory, - final Class serverSocketChannelClass, final ChannelFactory socketChannelFactory, - final Class socketChannelClass, final ChannelFactory datagramChannelFactory, - final Class datagramChannelClass, final BiFunction eventLoopGroupFactory) { this.name = name; - this.serverSocketChannelClass = serverSocketChannelClass; - this.socketChannelClass = socketChannelClass; - this.datagramChannelClass = datagramChannelClass; this.serverSocketChannelFactory = serverSocketChannelFactory; this.socketChannelFactory = socketChannelFactory; this.datagramChannelFactory = datagramChannelFactory; this.eventLoopGroupFactory = eventLoopGroupFactory; } - @Deprecated - TransportType(final String name, - final Class serverSocketChannelClass, - final Class socketChannelClass, - final Class datagramChannelClass, - final BiFunction eventLoopGroupFactory) { - this(name, new ReflectiveChannelFactory<>(serverSocketChannelClass), serverSocketChannelClass, - new ReflectiveChannelFactory<>(socketChannelClass), socketChannelClass, - new ReflectiveChannelFactory<>(datagramChannelClass), datagramChannelClass, - eventLoopGroupFactory); - } - @Override public String toString() { return this.name;