From db198f7bf5234c4853c3a84521450d8c533c40aa Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Tue, 16 Jan 2024 10:26:22 +0100 Subject: [PATCH] cleanup some netty code --- .../network/client/netty/NettyClient.kt | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/NettyClient.kt b/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/NettyClient.kt index 536c58b6c..2a69a7c1f 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/NettyClient.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/NettyClient.kt @@ -14,6 +14,7 @@ package de.bixilon.minosoft.protocol.network.network.client.netty import de.bixilon.kutil.cast.CastUtil.nullCast +import de.bixilon.kutil.exception.ExceptionUtil.catchAll import de.bixilon.kutil.observer.DataObserver.Companion.observed import de.bixilon.minosoft.config.profile.profiles.other.OtherProfileManager import de.bixilon.minosoft.protocol.address.ServerAddress @@ -82,10 +83,8 @@ class NettyClient( } override fun setupEncryption(encrypt: Cipher, decrypt: Cipher) { + if (encrypted) throw IllegalStateException("Already encrypted!") val channel = requireChannel() - if (encrypted) { - throw IllegalStateException("Already encrypted!") - } channel.pipeline().addBefore(LengthEncoder.NAME, PacketEncryptor.NAME, PacketEncryptor(encrypt)) channel.pipeline().addBefore(LengthDecoder.NAME, PacketDecryptor.NAME, PacketDecryptor(decrypt)) encrypted = true @@ -133,19 +132,17 @@ class NettyClient( override fun forceSend(packet: C2SPacket) { val channel = getChannel() ?: return - packet.log((connection.nullCast()?.profiles?.other ?: OtherProfileManager.selected).log.reducedProtocolLog) + val profile = connection.nullCast()?.profiles?.other ?: OtherProfileManager.selected + val reduced = profile.log.reducedProtocolLog + packet.log(reduced) channel.writeAndFlush(packet) } - override fun channelRead0(context: ChannelHandlerContext?, message: Any?) { - } + override fun channelRead0(context: ChannelHandlerContext?, message: Any?) = Unit override fun channelActive(context: ChannelHandlerContext) { - try { - context.channel().config().setOption(ChannelOption.TCP_NODELAY, true) - } catch (_: Throwable) { - } + catchAll { context.channel().config().setOption(ChannelOption.TCP_NODELAY, true) } context.channel().config().isAutoRead = true this.channel = context.channel() connected = true @@ -176,11 +173,7 @@ class NettyClient( } private fun requireChannel(): Channel { - val channel = this.channel - if (!connected || channel == null) { - throw IllegalStateException("Not connected!") - } - return channel + return getChannel() ?: throw IllegalStateException("Not connected!") } private fun getChannel(): Channel? {