From a0d65bb3c3629c460262038fd7bd5a6b96dd43cf Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 18 Jun 2024 16:57:15 +0200 Subject: [PATCH 01/16] Improve pipeline This simplifies all pipeline code and ensures some listeners like the sizer are always present. The code already assumed that the sizer is always there and thus causes issues. The sizer can be deactivated still now and has pretty much no performance losses from this. The profit from this PR is that there is less logic with modifying the PR and thus developers interacting with the channel can assume specific things about the order and placements of elements in the pipeline. This will be useful once ViaVersion is supported, and it is expected that certain elements always are in the pipeline and don't change. My plan is to also always have an encryption and compression handler in the pipeline that is controlled via AttributeKeys from netty, but for that first #828 needs to be merged. So this PR only completes the goal partially, but that's fine. PR is ready for review like it is right now. --- .../mcprotocollib/network/BuiltinFlags.java | 6 +- .../mcprotocollib/network/Session.java | 46 +---- .../network/tcp/TcpClientSession.java | 170 +++++++++--------- .../network/tcp/TcpPacketSizer.java | 46 +++-- .../mcprotocollib/network/tcp/TcpServer.java | 91 ++++------ .../mcprotocollib/network/tcp/TcpSession.java | 77 -------- 6 files changed, 150 insertions(+), 286 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java index 7a060dd46..40a9a4ca7 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java @@ -6,8 +6,6 @@ * Built-in PacketLib session flags. */ public class BuiltinFlags { - public static final Flag ENABLE_CLIENT_PROXY_PROTOCOL = new Flag<>("enable-client-proxy-protocol", Boolean.class); - public static final Flag CLIENT_PROXIED_ADDRESS = new Flag<>("client-proxied-address", InetSocketAddress.class); /** @@ -20,6 +18,10 @@ public class BuiltinFlags { */ public static final Flag TCP_FAST_OPEN = new Flag<>("tcp-fast-open", Boolean.class); + public static final Flag CLIENT_CONNECT_TIMEOUT = new Flag<>("client-connect-timeout", Integer.class); + public static final Flag READ_TIMEOUT = new Flag<>("read-timeout", Integer.class); + public static final Flag WRITE_TIMEOUT = new Flag<>("write-timeout", Integer.class); + private BuiltinFlags() { } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java index 30bfe8126..2a0eca9b6 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java @@ -36,7 +36,7 @@ public interface Session { * @param wait Whether to wait for the connection to be established before returning. * @param transferring Whether the session is a client being transferred. */ - public void connect(boolean wait, boolean transferring); + void connect(boolean wait, boolean transferring); /** * Gets the host the session is connected to. @@ -137,7 +137,7 @@ public interface Session { * * @param flags Collection of flags */ - public void setFlags(Map flags); + void setFlags(Map flags); /** * Gets the listeners listening on this session. @@ -203,48 +203,6 @@ public interface Session { */ void enableEncryption(PacketEncryption encryption); - /** - * Gets the connect timeout for this session in seconds. - * - * @return The session's connect timeout. - */ - int getConnectTimeout(); - - /** - * Sets the connect timeout for this session in seconds. - * - * @param timeout Connect timeout to set. - */ - void setConnectTimeout(int timeout); - - /** - * Gets the read timeout for this session in seconds. - * - * @return The session's read timeout. - */ - int getReadTimeout(); - - /** - * Sets the read timeout for this session in seconds. - * - * @param timeout Read timeout to set. - */ - void setReadTimeout(int timeout); - - /** - * Gets the write timeout for this session in seconds. - * - * @return The session's write timeout. - */ - int getWriteTimeout(); - - /** - * Sets the write timeout for this session in seconds. - * - * @param timeout Write timeout to set. - */ - void setWriteTimeout(int timeout); - /** * Returns true if the session is connected. * diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java index 6c1019c72..3053d097d 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java @@ -5,8 +5,6 @@ import io.netty.channel.AddressedEnvelope; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; @@ -25,11 +23,13 @@ import io.netty.handler.proxy.HttpProxyHandler; import io.netty.handler.proxy.Socks4ProxyHandler; import io.netty.handler.proxy.Socks5ProxyHandler; +import io.netty.handler.timeout.ReadTimeoutHandler; +import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.resolver.dns.DnsNameResolver; import io.netty.resolver.dns.DnsNameResolverBuilder; import io.netty.util.concurrent.DefaultThreadFactory; -import org.geysermc.mcprotocollib.network.ProxyInfo; import org.geysermc.mcprotocollib.network.BuiltinFlags; +import org.geysermc.mcprotocollib.network.ProxyInfo; import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper; import org.geysermc.mcprotocollib.network.helper.TransportHelper; import org.geysermc.mcprotocollib.network.packet.PacketProtocol; @@ -90,56 +90,48 @@ public void connect(boolean wait, boolean transferring) { createTcpEventLoopGroup(); } - try { - final Bootstrap bootstrap = new Bootstrap() - .channelFactory(TRANSPORT_TYPE.socketChannelFactory()) - .option(ChannelOption.TCP_NODELAY, true) - .option(ChannelOption.IP_TOS, 0x18) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout() * 1000) - .group(EVENT_LOOP_GROUP) - .remoteAddress(resolveAddress()) - .localAddress(bindAddress, bindPort) - .handler(new ChannelInitializer<>() { - @Override - public void initChannel(Channel channel) { - PacketProtocol protocol = getPacketProtocol(); - protocol.newClientSession(TcpClientSession.this, transferring); - - ChannelPipeline pipeline = channel.pipeline(); - - refreshReadTimeoutHandler(channel); - refreshWriteTimeoutHandler(channel); - - addProxy(pipeline); - - int size = protocol.getPacketHeader().getLengthSize(); - if (size > 0) { - pipeline.addLast("sizer", new TcpPacketSizer(TcpClientSession.this, size)); - } - - pipeline.addLast("codec", new TcpPacketCodec(TcpClientSession.this, true)); - pipeline.addLast("manager", TcpClientSession.this); - - addHAProxySupport(pipeline); - } - }); + final Bootstrap bootstrap = new Bootstrap() + .channelFactory(TRANSPORT_TYPE.socketChannelFactory()) + .option(ChannelOption.TCP_NODELAY, true) + .option(ChannelOption.IP_TOS, 0x18) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getFlag(BuiltinFlags.CLIENT_CONNECT_TIMEOUT, 30) * 1000) + .group(EVENT_LOOP_GROUP) + .remoteAddress(resolveAddress()) + .localAddress(bindAddress, bindPort) + .handler(new ChannelInitializer<>() { + @Override + public void initChannel(Channel channel) { + PacketProtocol protocol = getPacketProtocol(); + protocol.newClientSession(TcpClientSession.this, transferring); - if (getFlag(BuiltinFlags.TCP_FAST_OPEN, false) && TRANSPORT_TYPE.supportsTcpFastOpenClient()) { - bootstrap.option(ChannelOption.TCP_FASTOPEN_CONNECT, true); - } + ChannelPipeline pipeline = channel.pipeline(); - ChannelFuture future = bootstrap.connect(); - if (wait) { - future.sync(); - } + addProxy(pipeline); + + addHAProxySupport(channel); + + pipeline.addLast("read-timeout", new ReadTimeoutHandler(getFlag(BuiltinFlags.READ_TIMEOUT, 30))); + pipeline.addLast("write-timeout", new WriteTimeoutHandler(getFlag(BuiltinFlags.WRITE_TIMEOUT, 0))); - future.addListener((futureListener) -> { - if (!futureListener.isSuccess()) { - exceptionCaught(null, futureListener.cause()); + pipeline.addLast("sizer", new TcpPacketSizer(protocol.getPacketHeader(), getCodecHelper())); + + pipeline.addLast("codec", new TcpPacketCodec(TcpClientSession.this, true)); + pipeline.addLast("manager", TcpClientSession.this); } }); - } catch (Throwable t) { - exceptionCaught(null, t); + + if (getFlag(BuiltinFlags.TCP_FAST_OPEN, false) && TRANSPORT_TYPE.supportsTcpFastOpenClient()) { + bootstrap.option(ChannelOption.TCP_FASTOPEN_CONNECT, true); + } + + ChannelFuture future = bootstrap.connect().addListener((futureListener) -> { + if (!futureListener.isSuccess()) { + exceptionCaught(null, futureListener.cause()); + } + }); + + if (wait) { + future.syncUninterruptibly(); } } @@ -155,8 +147,8 @@ private InetSocketAddress resolveAddress() { if (getFlag(BuiltinFlags.ATTEMPT_SRV_RESOLVE, true) && (!this.host.matches(IP_REGEX) && !this.host.equalsIgnoreCase("localhost"))) { AddressedEnvelope envelope = null; try (DnsNameResolver resolver = new DnsNameResolverBuilder(EVENT_LOOP_GROUP.next()) - .channelFactory(TRANSPORT_TYPE.datagramChannelFactory()) - .build()) { + .channelFactory(TRANSPORT_TYPE.datagramChannelFactory()) + .build()) { envelope = resolver.query(new DefaultDnsQuestion(name, DnsRecordType.SRV)).get(); DnsResponse response = envelope.content(); @@ -206,54 +198,52 @@ private InetSocketAddress resolveAddress() { } private void addProxy(ChannelPipeline pipeline) { - if (proxy != null) { - switch (proxy.type()) { - case HTTP -> { - if (proxy.username() != null && proxy.password() != null) { - pipeline.addFirst("proxy", new HttpProxyHandler(proxy.address(), proxy.username(), proxy.password())); - } else { - pipeline.addFirst("proxy", new HttpProxyHandler(proxy.address())); - } + if (proxy == null) { + return; + } + + switch (proxy.type()) { + case HTTP -> { + if (proxy.username() != null && proxy.password() != null) { + pipeline.addLast("proxy", new HttpProxyHandler(proxy.address(), proxy.username(), proxy.password())); + } else { + pipeline.addLast("proxy", new HttpProxyHandler(proxy.address())); } - case SOCKS4 -> { - if (proxy.username() != null) { - pipeline.addFirst("proxy", new Socks4ProxyHandler(proxy.address(), proxy.username())); - } else { - pipeline.addFirst("proxy", new Socks4ProxyHandler(proxy.address())); - } + } + case SOCKS4 -> { + if (proxy.username() != null) { + pipeline.addLast("proxy", new Socks4ProxyHandler(proxy.address(), proxy.username())); + } else { + pipeline.addLast("proxy", new Socks4ProxyHandler(proxy.address())); } - case SOCKS5 -> { - if (proxy.username() != null && proxy.password() != null) { - pipeline.addFirst("proxy", new Socks5ProxyHandler(proxy.address(), proxy.username(), proxy.password())); - } else { - pipeline.addFirst("proxy", new Socks5ProxyHandler(proxy.address())); - } + } + case SOCKS5 -> { + if (proxy.username() != null && proxy.password() != null) { + pipeline.addLast("proxy", new Socks5ProxyHandler(proxy.address(), proxy.username(), proxy.password())); + } else { + pipeline.addLast("proxy", new Socks5ProxyHandler(proxy.address())); } - default -> throw new UnsupportedOperationException("Unsupported proxy type: " + proxy.type()); } + default -> throw new UnsupportedOperationException("Unsupported proxy type: " + proxy.type()); } } - private void addHAProxySupport(ChannelPipeline pipeline) { + private void addHAProxySupport(Channel channel) { InetSocketAddress clientAddress = getFlag(BuiltinFlags.CLIENT_PROXIED_ADDRESS); - if (getFlag(BuiltinFlags.ENABLE_CLIENT_PROXY_PROTOCOL, false) && clientAddress != null) { - pipeline.addFirst("proxy-protocol-packet-sender", new ChannelInboundHandlerAdapter() { - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - HAProxyProxiedProtocol proxiedProtocol = clientAddress.getAddress() instanceof Inet4Address ? HAProxyProxiedProtocol.TCP4 : HAProxyProxiedProtocol.TCP6; - InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); - ctx.channel().writeAndFlush(new HAProxyMessage( - HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, proxiedProtocol, - clientAddress.getAddress().getHostAddress(), remoteAddress.getAddress().getHostAddress(), - clientAddress.getPort(), remoteAddress.getPort() - )); - ctx.pipeline().remove(this); - ctx.pipeline().remove("proxy-protocol-encoder"); - super.channelActive(ctx); - } - }); - pipeline.addFirst("proxy-protocol-encoder", HAProxyMessageEncoder.INSTANCE); + if (clientAddress == null) { + return; } + + channel.pipeline().addLast("proxy-protocol-encoder", HAProxyMessageEncoder.INSTANCE); + HAProxyProxiedProtocol proxiedProtocol = clientAddress.getAddress() instanceof Inet4Address ? HAProxyProxiedProtocol.TCP4 : HAProxyProxiedProtocol.TCP6; + InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress(); + channel.writeAndFlush(new HAProxyMessage( + HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, proxiedProtocol, + clientAddress.getAddress().getHostAddress(), remoteAddress.getAddress().getHostAddress(), + clientAddress.getPort(), remoteAddress.getPort() + )).addListener(future -> { + channel.pipeline().remove("proxy-protocol-encoder"); + }); } @Override @@ -269,7 +259,7 @@ private static void createTcpEventLoopGroup() { EVENT_LOOP_GROUP = TRANSPORT_TYPE.eventLoopGroupFactory().apply(newThreadFactory()); Runtime.getRuntime().addShutdownHook(new Thread( - () -> EVENT_LOOP_GROUP.shutdownGracefully(SHUTDOWN_QUIET_PERIOD_MS, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS))); + () -> EVENT_LOOP_GROUP.shutdownGracefully(SHUTDOWN_QUIET_PERIOD_MS, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS))); } protected static ThreadFactory newThreadFactory() { diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java index f6d5e9b70..3025105cc 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java @@ -3,31 +3,45 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageCodec; import io.netty.handler.codec.CorruptedFrameException; -import org.geysermc.mcprotocollib.network.Session; +import io.netty.handler.codec.MessageToMessageCodec; +import lombok.RequiredArgsConstructor; +import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper; +import org.geysermc.mcprotocollib.network.packet.PacketHeader; import java.util.List; -public class TcpPacketSizer extends ByteToMessageCodec { - private final Session session; - private final int size; - - public TcpPacketSizer(Session session, int size) { - this.session = session; - this.size = size; - } +@RequiredArgsConstructor +public class TcpPacketSizer extends MessageToMessageCodec { + private final PacketHeader header; + private final PacketCodecHelper codecHelper; @Override - public void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) { + public void encode(ChannelHandlerContext ctx, ByteBuf in, List out) { + int size = header.getLengthSize(); + if (size == 0) { + out.add(in.retain()); + return; + } + int length = in.readableBytes(); - out.ensureWritable(this.session.getPacketProtocol().getPacketHeader().getLengthSize(length) + length); - this.session.getPacketProtocol().getPacketHeader().writeLength(out, this.session.getCodecHelper(), length); - out.writeBytes(in); + int targetLength = header.getLengthSize(length) + length; + ByteBuf resultBuf = ctx.alloc().buffer(targetLength); + + header.writeLength(resultBuf, codecHelper, length); + resultBuf.writeBytes(in); + + out.add(resultBuf); } @Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List out) { + int size = header.getLengthSize(); + if (size == 0) { + out.add(buf.retain()); + return; + } + buf.markReaderIndex(); byte[] lengthBytes = new byte[size]; for (int index = 0; index < lengthBytes.length; index++) { @@ -37,8 +51,8 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List out) } lengthBytes[index] = buf.readByte(); - if ((this.session.getPacketProtocol().getPacketHeader().isLengthVariable() && lengthBytes[index] >= 0) || index == size - 1) { - int length = this.session.getPacketProtocol().getPacketHeader().readLength(Unpooled.wrappedBuffer(lengthBytes), this.session.getCodecHelper(), buf.readableBytes()); + if ((header.isLengthVariable() && lengthBytes[index] >= 0) || index == size - 1) { + int length = header.readLength(Unpooled.wrappedBuffer(lengthBytes), codecHelper, buf.readableBytes()); if (buf.readableBytes() < length) { buf.resetReaderIndex(); return; diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index b3298fd1a..7bd825e05 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -8,6 +8,8 @@ import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoopGroup; +import io.netty.handler.timeout.ReadTimeoutHandler; +import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.util.concurrent.Future; import org.geysermc.mcprotocollib.network.AbstractServer; import org.geysermc.mcprotocollib.network.BuiltinFlags; @@ -37,10 +39,6 @@ public boolean isListening() { @Override public void bindImpl(boolean wait, final Runnable callback) { - if (this.group != null || this.channel != null) { - return; - } - this.group = TRANSPORT_TYPE.eventLoopGroupFactory().apply(null); ServerBootstrap bootstrap = new ServerBootstrap() @@ -60,13 +58,10 @@ public void initChannel(Channel channel) { ChannelPipeline pipeline = channel.pipeline(); - session.refreshReadTimeoutHandler(channel); - session.refreshWriteTimeoutHandler(channel); + pipeline.addLast("read-timeout", new ReadTimeoutHandler(session.getFlag(BuiltinFlags.READ_TIMEOUT, 30))); + pipeline.addLast("write-timeout", new WriteTimeoutHandler(session.getFlag(BuiltinFlags.WRITE_TIMEOUT, 0))); - int size = protocol.getPacketHeader().getLengthSize(); - if (size > 0) { - pipeline.addLast("sizer", new TcpPacketSizer(session, size)); - } + pipeline.addLast("sizer", new TcpPacketSizer(protocol.getPacketHeader(), session.getCodecHelper())); pipeline.addLast("codec", new TcpPacketCodec(session, false)); pipeline.addLast("manager", session); @@ -77,29 +72,19 @@ public void initChannel(Channel channel) { bootstrap.option(ChannelOption.TCP_FASTOPEN, 3); } - ChannelFuture future = bootstrap.bind(); - - if (wait) { - try { - future.sync(); - } catch (InterruptedException e) { + ChannelFuture future = bootstrap.bind().addListener((ChannelFutureListener) future1 -> { + if (future1.isSuccess()) { + channel = future1.channel(); + if (callback != null) { + callback.run(); + } + } else { + log.error("Failed to bind connection listener.", future1.cause()); } + });; - channel = future.channel(); - if (callback != null) { - callback.run(); - } - } else { - future.addListener((ChannelFutureListener) future1 -> { - if (future1.isSuccess()) { - channel = future1.channel(); - if (callback != null) { - callback.run(); - } - } else { - log.error("Failed to asynchronously bind connection listener.", future1.cause()); - } - }); + if (wait) { + future.syncUninterruptibly(); } } @@ -107,26 +92,22 @@ public void initChannel(Channel channel) { public void closeImpl(boolean wait, final Runnable callback) { if (this.channel != null) { if (this.channel.isOpen()) { - ChannelFuture future = this.channel.close(); - if (wait) { - try { - future.sync(); - } catch (InterruptedException e) { + ChannelFuture future = this.channel.close().addListener((ChannelFutureListener) future1 -> { + if (future1.isSuccess()) { + if (callback != null) { + callback.run(); + } + } else { + log.error("Failed to close connection listener.", future1.cause()); } + }); + + if (wait) { + future.syncUninterruptibly(); if (callback != null) { callback.run(); } - } else { - future.addListener((ChannelFutureListener) future1 -> { - if (future1.isSuccess()) { - if (callback != null) { - callback.run(); - } - } else { - log.error("Failed to asynchronously close connection listener.", future1.cause()); - } - }); } } @@ -134,18 +115,14 @@ public void closeImpl(boolean wait, final Runnable callback) { } if (this.group != null) { - Future future = this.group.shutdownGracefully(); - if (wait) { - try { - future.sync(); - } catch (InterruptedException e) { + Future future = this.group.shutdownGracefully().addListener(future1 -> { + if (!future1.isSuccess()) { + log.debug("Failed to close connection listener.", future1.cause()); } - } else { - future.addListener(future1 -> { - if (!future1.isSuccess()) { - log.debug("Failed to asynchronously close connection listener.", future1.cause()); - } - }); + }); + + if (wait) { + future.syncUninterruptibly(); } this.group = null; diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java index f3a70e72a..4f6ce9694 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java @@ -9,9 +9,7 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.ReadTimeoutException; -import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutException; -import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.util.concurrent.DefaultThreadFactory; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; @@ -51,9 +49,6 @@ public abstract class TcpSession extends SimpleChannelInboundHandler imp private final EventLoop eventLoop = createEventLoop(); private int compressionThreshold = -1; - private int connectTimeout = 30; - private int readTimeout = 30; - private int writeTimeout = 0; private final Map flags = new HashMap<>(); private final List listeners = new CopyOnWriteArrayList<>(); @@ -220,38 +215,6 @@ public void enableEncryption(PacketEncryption encryption) { channel.pipeline().addBefore("sizer", "encryption", new TcpPacketEncryptor(encryption)); } - @Override - public int getConnectTimeout() { - return this.connectTimeout; - } - - @Override - public void setConnectTimeout(int timeout) { - this.connectTimeout = timeout; - } - - @Override - public int getReadTimeout() { - return this.readTimeout; - } - - @Override - public void setReadTimeout(int timeout) { - this.readTimeout = timeout; - this.refreshReadTimeoutHandler(); - } - - @Override - public int getWriteTimeout() { - return this.writeTimeout; - } - - @Override - public void setWriteTimeout(int timeout) { - this.writeTimeout = timeout; - this.refreshWriteTimeoutHandler(); - } - @Override public boolean isConnected() { return this.channel != null && this.channel.isOpen() && !this.disconnected; @@ -330,46 +293,6 @@ public Channel getChannel() { return this.channel; } - protected void refreshReadTimeoutHandler() { - this.refreshReadTimeoutHandler(this.channel); - } - - protected void refreshReadTimeoutHandler(Channel channel) { - if (channel != null) { - if (this.readTimeout <= 0) { - if (channel.pipeline().get("readTimeout") != null) { - channel.pipeline().remove("readTimeout"); - } - } else { - if (channel.pipeline().get("readTimeout") == null) { - channel.pipeline().addFirst("readTimeout", new ReadTimeoutHandler(this.readTimeout)); - } else { - channel.pipeline().replace("readTimeout", "readTimeout", new ReadTimeoutHandler(this.readTimeout)); - } - } - } - } - - protected void refreshWriteTimeoutHandler() { - this.refreshWriteTimeoutHandler(this.channel); - } - - protected void refreshWriteTimeoutHandler(Channel channel) { - if (channel != null) { - if (this.writeTimeout <= 0) { - if (channel.pipeline().get("writeTimeout") != null) { - channel.pipeline().remove("writeTimeout"); - } - } else { - if (channel.pipeline().get("writeTimeout") == null) { - channel.pipeline().addFirst("writeTimeout", new WriteTimeoutHandler(this.writeTimeout)); - } else { - channel.pipeline().replace("writeTimeout", "writeTimeout", new WriteTimeoutHandler(this.writeTimeout)); - } - } - } - } - @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { if (this.disconnected || this.channel != null) { From bd821698411f3749b6c5e610b062cd8c97441574 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 18 Jun 2024 17:37:00 +0200 Subject: [PATCH 02/16] Revert some stuff --- .../network/tcp/TcpClientSession.java | 12 +++++++----- .../mcprotocollib/network/tcp/TcpServer.java | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java index 3053d097d..26885acab 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java @@ -124,15 +124,17 @@ public void initChannel(Channel channel) { bootstrap.option(ChannelOption.TCP_FASTOPEN_CONNECT, true); } - ChannelFuture future = bootstrap.connect().addListener((futureListener) -> { - if (!futureListener.isSuccess()) { - exceptionCaught(null, futureListener.cause()); - } - }); + ChannelFuture future = bootstrap.connect(); if (wait) { future.syncUninterruptibly(); } + + future.addListener((futureListener) -> { + if (!futureListener.isSuccess()) { + exceptionCaught(null, futureListener.cause()); + } + }); } @Override diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index 7bd825e05..f9d0eeff8 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -39,6 +39,10 @@ public boolean isListening() { @Override public void bindImpl(boolean wait, final Runnable callback) { + if (this.group != null || this.channel != null) { + return; + } + this.group = TRANSPORT_TYPE.eventLoopGroupFactory().apply(null); ServerBootstrap bootstrap = new ServerBootstrap() @@ -72,7 +76,13 @@ public void initChannel(Channel channel) { bootstrap.option(ChannelOption.TCP_FASTOPEN, 3); } - ChannelFuture future = bootstrap.bind().addListener((ChannelFutureListener) future1 -> { + ChannelFuture future = bootstrap.bind(); + + if (wait) { + future.syncUninterruptibly(); + } + + future.addListener((ChannelFutureListener) future1 -> { if (future1.isSuccess()) { channel = future1.channel(); if (callback != null) { @@ -81,11 +91,7 @@ public void initChannel(Channel channel) { } else { log.error("Failed to bind connection listener.", future1.cause()); } - });; - - if (wait) { - future.syncUninterruptibly(); - } + }); } @Override From ba164ec8a1d19a1dc683cd1794649cf75a321de1 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 18 Jun 2024 18:00:28 +0200 Subject: [PATCH 03/16] Fix channel race condition --- .../network/tcp/TcpClientSession.java | 12 +++++------- .../mcprotocollib/network/tcp/TcpServer.java | 16 +++++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java index 26885acab..3053d097d 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java @@ -124,17 +124,15 @@ public void initChannel(Channel channel) { bootstrap.option(ChannelOption.TCP_FASTOPEN_CONNECT, true); } - ChannelFuture future = bootstrap.connect(); - - if (wait) { - future.syncUninterruptibly(); - } - - future.addListener((futureListener) -> { + ChannelFuture future = bootstrap.connect().addListener((futureListener) -> { if (!futureListener.isSuccess()) { exceptionCaught(null, futureListener.cause()); } }); + + if (wait) { + future.syncUninterruptibly(); + } } @Override diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index f9d0eeff8..cd693c74d 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; +import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; public class TcpServer extends AbstractServer { @@ -76,13 +77,8 @@ public void initChannel(Channel channel) { bootstrap.option(ChannelOption.TCP_FASTOPEN, 3); } - ChannelFuture future = bootstrap.bind(); - - if (wait) { - future.syncUninterruptibly(); - } - - future.addListener((ChannelFutureListener) future1 -> { + CompletableFuture handleFuture = new CompletableFuture<>(); + bootstrap.bind().addListener((ChannelFutureListener) future1 -> { if (future1.isSuccess()) { channel = future1.channel(); if (callback != null) { @@ -91,7 +87,13 @@ public void initChannel(Channel channel) { } else { log.error("Failed to bind connection listener.", future1.cause()); } + + handleFuture.complete(null); }); + + if (wait) { + handleFuture.join(); + } } @Override From b38fc442522cc28776f1ffbf31b82db375ef04f8 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 18 Jun 2024 18:24:10 +0200 Subject: [PATCH 04/16] Fix closing race condition --- .../mcprotocollib/network/tcp/TcpServer.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index cd693c74d..b46805617 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -2,7 +2,6 @@ import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; @@ -10,7 +9,6 @@ import io.netty.channel.EventLoopGroup; import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler; -import io.netty.util.concurrent.Future; import org.geysermc.mcprotocollib.network.AbstractServer; import org.geysermc.mcprotocollib.network.BuiltinFlags; import org.geysermc.mcprotocollib.network.helper.TransportHelper; @@ -100,7 +98,8 @@ public void initChannel(Channel channel) { public void closeImpl(boolean wait, final Runnable callback) { if (this.channel != null) { if (this.channel.isOpen()) { - ChannelFuture future = this.channel.close().addListener((ChannelFutureListener) future1 -> { + CompletableFuture handleFuture = new CompletableFuture<>(); + this.channel.close().addListener((ChannelFutureListener) future1 -> { if (future1.isSuccess()) { if (callback != null) { callback.run(); @@ -108,14 +107,12 @@ public void closeImpl(boolean wait, final Runnable callback) { } else { log.error("Failed to close connection listener.", future1.cause()); } + + handleFuture.complete(null); }); if (wait) { - future.syncUninterruptibly(); - - if (callback != null) { - callback.run(); - } + handleFuture.join(); } } @@ -123,14 +120,17 @@ public void closeImpl(boolean wait, final Runnable callback) { } if (this.group != null) { - Future future = this.group.shutdownGracefully().addListener(future1 -> { + CompletableFuture handleFuture = new CompletableFuture<>(); + this.group.shutdownGracefully().addListener(future1 -> { if (!future1.isSuccess()) { log.debug("Failed to close connection listener.", future1.cause()); } + + handleFuture.complete(null); }); if (wait) { - future.syncUninterruptibly(); + handleFuture.join(); } this.group = null; From 2bc5475c521df5452617f838c655d80456aadf05 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 18 Jun 2024 18:37:34 +0200 Subject: [PATCH 05/16] Prevent client race conditions. --- .../mcprotocollib/network/tcp/TcpClientSession.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java index 3053d097d..a10ae0cab 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java @@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.AddressedEnvelope; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; @@ -40,6 +39,7 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; @@ -124,14 +124,17 @@ public void initChannel(Channel channel) { bootstrap.option(ChannelOption.TCP_FASTOPEN_CONNECT, true); } - ChannelFuture future = bootstrap.connect().addListener((futureListener) -> { + CompletableFuture handleFuture = new CompletableFuture<>(); + bootstrap.connect().addListener((futureListener) -> { if (!futureListener.isSuccess()) { exceptionCaught(null, futureListener.cause()); } + + handleFuture.complete(null); }); if (wait) { - future.syncUninterruptibly(); + handleFuture.join(); } } From ef6a7b686a8da62fb449f726e0da53bbe39abbec Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 18 Jun 2024 19:24:34 +0200 Subject: [PATCH 06/16] Fix test failure, idk how, idk why, but it works now --- .../network/tcp/TcpPacketSizer.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java index 3025105cc..decb5069b 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpPacketSizer.java @@ -3,8 +3,8 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageCodec; import io.netty.handler.codec.CorruptedFrameException; -import io.netty.handler.codec.MessageToMessageCodec; import lombok.RequiredArgsConstructor; import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper; import org.geysermc.mcprotocollib.network.packet.PacketHeader; @@ -12,26 +12,22 @@ import java.util.List; @RequiredArgsConstructor -public class TcpPacketSizer extends MessageToMessageCodec { +public class TcpPacketSizer extends ByteToMessageCodec { private final PacketHeader header; private final PacketCodecHelper codecHelper; @Override - public void encode(ChannelHandlerContext ctx, ByteBuf in, List out) { + public void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) { int size = header.getLengthSize(); if (size == 0) { - out.add(in.retain()); + out.writeBytes(in); return; } int length = in.readableBytes(); - int targetLength = header.getLengthSize(length) + length; - ByteBuf resultBuf = ctx.alloc().buffer(targetLength); - - header.writeLength(resultBuf, codecHelper, length); - resultBuf.writeBytes(in); - - out.add(resultBuf); + out.ensureWritable(header.getLengthSize(length) + length); + header.writeLength(out, codecHelper, length); + out.writeBytes(in); } @Override From d358c40dd301ddff0e44359d4d26e58fc347d089 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Sat, 13 Jul 2024 19:15:15 +0200 Subject: [PATCH 07/16] Address review --- .../mcprotocollib/network/BuiltinFlags.java | 16 ++++++++++++++++ .../network/tcp/TcpClientSession.java | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java index 40a9a4ca7..b59eea6b1 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java @@ -6,6 +6,10 @@ * Built-in PacketLib session flags. */ public class BuiltinFlags { + /** + * Enables HAProxy protocol support. + * When this value is not null it means the ip and port the client claims the connection is from. + */ public static final Flag CLIENT_PROXIED_ADDRESS = new Flag<>("client-proxied-address", InetSocketAddress.class); /** @@ -18,8 +22,20 @@ public class BuiltinFlags { */ public static final Flag TCP_FAST_OPEN = new Flag<>("tcp-fast-open", Boolean.class); + /** + * Connection timeout in seconds. + * Only used by the client. + */ public static final Flag CLIENT_CONNECT_TIMEOUT = new Flag<>("client-connect-timeout", Integer.class); + /** + * Read timeout in seconds. + * Used by both server and client. + */ public static final Flag READ_TIMEOUT = new Flag<>("read-timeout", Integer.class); + /** + * Write timeout in seconds. + * Used by both server and client. + */ public static final Flag WRITE_TIMEOUT = new Flag<>("write-timeout", Integer.class); private BuiltinFlags() { diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java index d2c99f6dc..a04d0d89d 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpClientSession.java @@ -108,7 +108,7 @@ public void initChannel(Channel channel) { addProxy(pipeline); - addHAProxySupport(channel); + initializeHAProxySupport(channel); pipeline.addLast("read-timeout", new ReadTimeoutHandler(getFlag(BuiltinFlags.READ_TIMEOUT, 30))); pipeline.addLast("write-timeout", new WriteTimeoutHandler(getFlag(BuiltinFlags.WRITE_TIMEOUT, 0))); @@ -231,7 +231,7 @@ private void addProxy(ChannelPipeline pipeline) { } } - private void addHAProxySupport(Channel channel) { + private void initializeHAProxySupport(Channel channel) { InetSocketAddress clientAddress = getFlag(BuiltinFlags.CLIENT_PROXIED_ADDRESS); if (clientAddress == null) { return; From 150f7ff61d32ece744c547ae57ff26ae9b14f168 Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Sun, 28 Jul 2024 08:03:02 +0200 Subject: [PATCH 08/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java Co-authored-by: Konicai <71294714+Konicai@users.noreply.github.com> --- .../java/org/geysermc/mcprotocollib/network/BuiltinFlags.java | 1 + 1 file changed, 1 insertion(+) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java index b59eea6b1..4a2e9c018 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java @@ -6,6 +6,7 @@ * Built-in PacketLib session flags. */ public class BuiltinFlags { + /** * Enables HAProxy protocol support. * When this value is not null it means the ip and port the client claims the connection is from. From c8632180b76a6a33267f98a959db635cd6876d51 Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Sun, 28 Jul 2024 08:03:11 +0200 Subject: [PATCH 09/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java Co-authored-by: Konicai <71294714+Konicai@users.noreply.github.com> --- .../java/org/geysermc/mcprotocollib/network/BuiltinFlags.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java index 4a2e9c018..8300c5a51 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java @@ -28,11 +28,13 @@ public class BuiltinFlags { * Only used by the client. */ public static final Flag CLIENT_CONNECT_TIMEOUT = new Flag<>("client-connect-timeout", Integer.class); + /** * Read timeout in seconds. * Used by both server and client. */ public static final Flag READ_TIMEOUT = new Flag<>("read-timeout", Integer.class); + /** * Write timeout in seconds. * Used by both server and client. From a464d5ff189898421d1a2406f120aae1383fdb86 Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 10 Sep 2024 07:50:41 +0200 Subject: [PATCH 10/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java Co-authored-by: chris --- .../java/org/geysermc/mcprotocollib/network/BuiltinFlags.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java index 8300c5a51..9febac8ca 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java @@ -37,7 +37,7 @@ public class BuiltinFlags { /** * Write timeout in seconds. - * Used by both server and client. + * Used by both the server and client. */ public static final Flag WRITE_TIMEOUT = new Flag<>("write-timeout", Integer.class); From 1f882d2dc283ea8bc88bb269b358ce35c4aa44bc Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 10 Sep 2024 07:50:49 +0200 Subject: [PATCH 11/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java Co-authored-by: chris --- .../org/geysermc/mcprotocollib/network/tcp/TcpServer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index b46805617..ce7a185e5 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -99,8 +99,8 @@ public void closeImpl(boolean wait, final Runnable callback) { if (this.channel != null) { if (this.channel.isOpen()) { CompletableFuture handleFuture = new CompletableFuture<>(); - this.channel.close().addListener((ChannelFutureListener) future1 -> { - if (future1.isSuccess()) { + this.channel.close().addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { if (callback != null) { callback.run(); } From 118c324b45668ceb8ee824357a69d9f18a5f0f1d Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 10 Sep 2024 07:50:59 +0200 Subject: [PATCH 12/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java Co-authored-by: chris --- .../java/org/geysermc/mcprotocollib/network/BuiltinFlags.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java index 9febac8ca..921411183 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java @@ -9,7 +9,7 @@ public class BuiltinFlags { /** * Enables HAProxy protocol support. - * When this value is not null it means the ip and port the client claims the connection is from. + * When this value is not null it represents the ip and port the client claims the connection is from. */ public static final Flag CLIENT_PROXIED_ADDRESS = new Flag<>("client-proxied-address", InetSocketAddress.class); From bbe066e853d09420d877ecd90fce0929d33a941b Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 10 Sep 2024 07:51:04 +0200 Subject: [PATCH 13/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java Co-authored-by: chris --- .../java/org/geysermc/mcprotocollib/network/BuiltinFlags.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java index 921411183..d602cb904 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/BuiltinFlags.java @@ -31,7 +31,7 @@ public class BuiltinFlags { /** * Read timeout in seconds. - * Used by both server and client. + * Used by both the server and client. */ public static final Flag READ_TIMEOUT = new Flag<>("read-timeout", Integer.class); From 912241d61e19535a267a3a5c5c9090abcddf8eaf Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 10 Sep 2024 07:51:12 +0200 Subject: [PATCH 14/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java Co-authored-by: chris --- .../java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index ce7a185e5..f83321b66 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -105,7 +105,7 @@ public void closeImpl(boolean wait, final Runnable callback) { callback.run(); } } else { - log.error("Failed to close connection listener.", future1.cause()); + log.error("Failed to close connection listener.", future.cause()); } handleFuture.complete(null); From e26fa85459417eae7f72473b2787faf5ff8208fd Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 10 Sep 2024 07:51:19 +0200 Subject: [PATCH 15/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java Co-authored-by: chris --- .../org/geysermc/mcprotocollib/network/tcp/TcpServer.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index f83321b66..470b08797 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -76,14 +76,14 @@ public void initChannel(Channel channel) { } CompletableFuture handleFuture = new CompletableFuture<>(); - bootstrap.bind().addListener((ChannelFutureListener) future1 -> { - if (future1.isSuccess()) { - channel = future1.channel(); + bootstrap.bind().addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { + channel = future.channel(); if (callback != null) { callback.run(); } } else { - log.error("Failed to bind connection listener.", future1.cause()); + log.error("Failed to bind connection listener.", future.cause()); } handleFuture.complete(null); From 35efb17cc876af65f1851131dbcd6f1a4accdb68 Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Tue, 10 Sep 2024 07:51:29 +0200 Subject: [PATCH 16/16] Update protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java Co-authored-by: chris --- .../org/geysermc/mcprotocollib/network/tcp/TcpServer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java index 470b08797..4d35e9666 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpServer.java @@ -121,9 +121,9 @@ public void closeImpl(boolean wait, final Runnable callback) { if (this.group != null) { CompletableFuture handleFuture = new CompletableFuture<>(); - this.group.shutdownGracefully().addListener(future1 -> { - if (!future1.isSuccess()) { - log.debug("Failed to close connection listener.", future1.cause()); + this.group.shutdownGracefully().addListener(future -> { + if (!future.isSuccess()) { + log.debug("Failed to close connection listener.", future.cause()); } handleFuture.complete(null);