From 1bb244b7f78b7c35336c681d8979ce6bea7cf473 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 24 Jul 2022 23:42:26 +0100 Subject: [PATCH] Move length encoding/decoding from Rs2{Decoder,Encoder} to PacketCodec There are now three additional abstract PacketCodec classes: FixedPacketCodec, VariableBytePacketCodec and VariableShortPacketCodec. The PacketLength class has been removed, as it is no longer required. The main reason for this change is that the create suggested names packet is a bit of an oddball: its length field measures the size of the packet in longs, not bytes. The codec for this packet will be able to inherit from PacketCodec directly to implement the custom length logic. Signed-off-by: Graham --- .../cache/nxt/InitJs5RemoteConnectionCodec.kt | 10 ++-- .../openrs2/archive/cache/nxt/Js5OkCodec.kt | 8 ++-- .../org/openrs2/protocol/EmptyPacketCodec.kt | 2 +- .../org/openrs2/protocol/FixedPacketCodec.kt | 37 +++++++++++++++ .../org/openrs2/protocol/PacketCodec.kt | 33 ++++--------- .../org/openrs2/protocol/PacketLength.kt | 6 --- .../kotlin/org/openrs2/protocol/Rs2Decoder.kt | 20 ++------ .../kotlin/org/openrs2/protocol/Rs2Encoder.kt | 29 +----------- .../protocol/VariableBytePacketCodec.kt | 47 +++++++++++++++++++ .../protocol/VariableShortPacketCodec.kt | 47 +++++++++++++++++++ .../upstream/CheckWorldSuitabilityCodec.kt | 8 ++-- .../CreateCheckDateOfBirthCountryCodec.kt | 4 +- .../login/upstream/CreateCheckNameCodec.kt | 4 +- .../login/upstream/InitGameConnectionCodec.kt | 8 ++-- .../upstream/InitJs5RemoteConnectionCodec.kt | 4 +- .../login/upstream/RequestWorldListCodec.kt | 4 +- .../downstream/WorldListResponseCodec.kt | 8 ++-- .../protocol/LengthMismatchPacketCodec.kt | 2 +- .../org/openrs2/protocol/Rs2DecoderTest.kt | 16 +++---- .../org/openrs2/protocol/Rs2EncoderTest.kt | 14 +++--- ...PacketCodec.kt => TestFixedPacketCodec.kt} | 2 +- ...odec.kt => TestVariableBytePacketCodec.kt} | 5 +- ...dec.kt => TestVariableShortPacketCodec.kt} | 5 +- .../VariableByteOptimisedPacketCodec.kt | 5 +- .../VariableShortOptimisedPacketCodec.kt | 5 +- 25 files changed, 197 insertions(+), 136 deletions(-) create mode 100644 protocol/src/main/kotlin/org/openrs2/protocol/FixedPacketCodec.kt delete mode 100644 protocol/src/main/kotlin/org/openrs2/protocol/PacketLength.kt create mode 100644 protocol/src/main/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt create mode 100644 protocol/src/main/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt rename protocol/src/test/kotlin/org/openrs2/protocol/{FixedPacketCodec.kt => TestFixedPacketCodec.kt} (86%) rename protocol/src/test/kotlin/org/openrs2/protocol/{VariableBytePacketCodec.kt => TestVariableBytePacketCodec.kt} (79%) rename protocol/src/test/kotlin/org/openrs2/protocol/{VariableShortPacketCodec.kt => TestVariableShortPacketCodec.kt} (79%) diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/InitJs5RemoteConnectionCodec.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/InitJs5RemoteConnectionCodec.kt index f384ffc2..14fed974 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/InitJs5RemoteConnectionCodec.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/InitJs5RemoteConnectionCodec.kt @@ -4,13 +4,11 @@ import io.netty.buffer.ByteBuf import org.openrs2.buffer.readString import org.openrs2.buffer.writeString import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec -import org.openrs2.protocol.PacketLength +import org.openrs2.protocol.VariableBytePacketCodec -public object InitJs5RemoteConnectionCodec : PacketCodec( - length = PacketLength.VARIABLE_BYTE, - opcode = 15, - type = InitJs5RemoteConnection::class.java +public object InitJs5RemoteConnectionCodec : VariableBytePacketCodec( + type = InitJs5RemoteConnection::class.java, + opcode = 15 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): InitJs5RemoteConnection { val buildMajor = input.readInt() diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/Js5OkCodec.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/Js5OkCodec.kt index 6648b467..bd26525b 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/Js5OkCodec.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/nxt/Js5OkCodec.kt @@ -2,12 +2,12 @@ package org.openrs2.archive.cache.nxt import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec +import org.openrs2.protocol.FixedPacketCodec -public object Js5OkCodec : PacketCodec( +public object Js5OkCodec : FixedPacketCodec( + type = LoginResponse.Js5Ok::class.java, opcode = 0, - length = LoginResponse.Js5Ok.LOADING_REQUIREMENTS * 4, - type = LoginResponse.Js5Ok::class.java + length = LoginResponse.Js5Ok.LOADING_REQUIREMENTS * 4 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): LoginResponse.Js5Ok { val loadingRequirements = mutableListOf() diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt index 1413967b..3237bfa2 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt @@ -6,7 +6,7 @@ import org.openrs2.crypto.StreamCipher public abstract class EmptyPacketCodec( private val packet: T, opcode: Int -) : PacketCodec(packet.javaClass, opcode, length = 0) { +) : FixedPacketCodec(packet.javaClass, opcode, length = 0) { override fun decode(input: ByteBuf, cipher: StreamCipher): T { return packet } diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/FixedPacketCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/FixedPacketCodec.kt new file mode 100644 index 00000000..2c0c3d5c --- /dev/null +++ b/protocol/src/main/kotlin/org/openrs2/protocol/FixedPacketCodec.kt @@ -0,0 +1,37 @@ +package org.openrs2.protocol + +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufAllocator +import io.netty.handler.codec.EncoderException + +public abstract class FixedPacketCodec( + type: Class, + opcode: Int, + public val length: Int +) : PacketCodec(type, opcode) { + override fun isLengthReadable(input: ByteBuf): Boolean { + return true + } + + override fun readLength(input: ByteBuf): Int { + return length + } + + override fun writeLengthPlaceholder(output: ByteBuf) { + // empty + } + + override fun setLength(output: ByteBuf, index: Int, written: Int) { + if (written != length) { + throw EncoderException("Fixed payload length mismatch (expected $length bytes, got $written bytes)") + } + } + + override fun allocateBuffer(alloc: ByteBufAllocator, input: T, preferDirect: Boolean): ByteBuf { + return if (preferDirect) { + alloc.ioBuffer(1 + length, 1 + length) + } else { + alloc.heapBuffer(1 + length, 1 + length) + } + } +} diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt index df1e894b..2162c29f 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt @@ -6,43 +6,26 @@ import org.openrs2.crypto.StreamCipher public abstract class PacketCodec( public val type: Class, - public val opcode: Int, - public val length: Int + public val opcode: Int ) { init { require(opcode in 0 until 256) - require(length >= PacketLength.VARIABLE_SHORT) } public abstract fun decode(input: ByteBuf, cipher: StreamCipher): T public abstract fun encode(input: T, output: ByteBuf, cipher: StreamCipher) - public open fun getLength(input: T): Int { - return length - } - - public fun allocateBuffer(alloc: ByteBufAllocator, input: T, preferDirect: Boolean): ByteBuf { - val payloadLen = getLength(input) - if (payloadLen < 0) { - return if (preferDirect) { - alloc.ioBuffer() - } else { - alloc.heapBuffer() - } - } - - val headerLen = when (length) { - PacketLength.VARIABLE_BYTE -> 2 - PacketLength.VARIABLE_SHORT -> 3 - else -> 1 - } + public abstract fun isLengthReadable(input: ByteBuf): Boolean + public abstract fun readLength(input: ByteBuf): Int - val totalLen = headerLen + payloadLen + public abstract fun writeLengthPlaceholder(output: ByteBuf) + public abstract fun setLength(output: ByteBuf, index: Int, written: Int) + public open fun allocateBuffer(alloc: ByteBufAllocator, input: T, preferDirect: Boolean): ByteBuf { return if (preferDirect) { - alloc.ioBuffer(totalLen, totalLen) + alloc.ioBuffer() } else { - alloc.heapBuffer(totalLen, totalLen) + alloc.heapBuffer() } } } diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/PacketLength.kt b/protocol/src/main/kotlin/org/openrs2/protocol/PacketLength.kt deleted file mode 100644 index b04d6a10..00000000 --- a/protocol/src/main/kotlin/org/openrs2/protocol/PacketLength.kt +++ /dev/null @@ -1,6 +0,0 @@ -package org.openrs2.protocol - -public object PacketLength { - public const val VARIABLE_SHORT: Int = -2 - public const val VARIABLE_BYTE: Int = -1 -} diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt index 77838584..be39a045 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt @@ -32,29 +32,17 @@ public class Rs2Decoder(public var protocol: Protocol) : ByteToMessageDecoder() val opcode = (input.readUnsignedByte().toInt() - cipher.nextInt()) and 0xFF decoder = protocol.getDecoder(opcode) ?: throw DecoderException("Unsupported opcode: $opcode") - length = decoder.length state = State.READ_LENGTH } if (state == State.READ_LENGTH) { - when (length) { - PacketLength.VARIABLE_BYTE -> { - if (!input.isReadable) { - return - } - - length = input.readUnsignedByte().toInt() - } - PacketLength.VARIABLE_SHORT -> { - if (input.readableBytes() < 2) { - return - } - - length = input.readUnsignedShort() - } + if (!decoder.isLengthReadable(input)) { + return } + length = decoder.readLength(input) + state = State.READ_PAYLOAD } diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt index 806ad257..cdc63c2f 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt @@ -16,39 +16,14 @@ public class Rs2Encoder(public var protocol: Protocol) : MessageToByteEncoder out.writeZero(1) - PacketLength.VARIABLE_SHORT -> out.writeZero(2) - } + encoder.writeLengthPlaceholder(out) val payloadIndex = out.writerIndex() encoder.encode(msg, out, cipher) val written = out.writerIndex() - payloadIndex - - when (len) { - PacketLength.VARIABLE_BYTE -> { - if (written >= 256) { - throw EncoderException("Variable byte payload too long: $written bytes") - } - - out.setByte(lenIndex, written) - } - PacketLength.VARIABLE_SHORT -> { - if (written >= 65536) { - throw EncoderException("Variable short payload too long: $written bytes") - } - - out.setShort(lenIndex, written) - } - else -> { - if (written != len) { - throw EncoderException("Fixed payload length mismatch (expected $len bytes, got $written bytes)") - } - } - } + encoder.setLength(out, lenIndex, written) } override fun allocateBuffer(ctx: ChannelHandlerContext, msg: Packet, preferDirect: Boolean): ByteBuf { diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt new file mode 100644 index 00000000..3dc58a84 --- /dev/null +++ b/protocol/src/main/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt @@ -0,0 +1,47 @@ +package org.openrs2.protocol + +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufAllocator +import io.netty.handler.codec.EncoderException + +public abstract class VariableBytePacketCodec( + type: Class, + opcode: Int +) : PacketCodec(type, opcode) { + override fun isLengthReadable(input: ByteBuf): Boolean { + return input.isReadable + } + + override fun readLength(input: ByteBuf): Int { + return input.readUnsignedByte().toInt() + } + + override fun writeLengthPlaceholder(output: ByteBuf) { + output.writeZero(1) + } + + override fun setLength(output: ByteBuf, index: Int, written: Int) { + if (written >= 256) { + throw EncoderException("Variable byte payload too long: $written bytes") + } + + output.setByte(index, written) + } + + public open fun getLength(input: T): Int { + return -1 + } + + override fun allocateBuffer(alloc: ByteBufAllocator, input: T, preferDirect: Boolean): ByteBuf { + val length = getLength(input) + if (length < 0) { + return super.allocateBuffer(alloc, input, preferDirect) + } + + return if (preferDirect) { + alloc.ioBuffer(2 + length, 2 + length) + } else { + alloc.heapBuffer(2 + length, 2 + length) + } + } +} diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt new file mode 100644 index 00000000..11be1ec5 --- /dev/null +++ b/protocol/src/main/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt @@ -0,0 +1,47 @@ +package org.openrs2.protocol + +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufAllocator +import io.netty.handler.codec.EncoderException + +public abstract class VariableShortPacketCodec( + type: Class, + opcode: Int +) : PacketCodec(type, opcode) { + override fun isLengthReadable(input: ByteBuf): Boolean { + return input.readableBytes() >= 2 + } + + override fun readLength(input: ByteBuf): Int { + return input.readUnsignedShort() + } + + override fun writeLengthPlaceholder(output: ByteBuf) { + output.writeZero(2) + } + + override fun setLength(output: ByteBuf, index: Int, written: Int) { + if (written >= 65536) { + throw EncoderException("Variable short payload too long: $written bytes") + } + + output.setShort(index, written) + } + + public open fun getLength(input: T): Int { + return -2 + } + + override fun allocateBuffer(alloc: ByteBufAllocator, input: T, preferDirect: Boolean): ByteBuf { + val length = getLength(input) + if (length < 0) { + return super.allocateBuffer(alloc, input, preferDirect) + } + + return if (preferDirect) { + alloc.ioBuffer(3 + length, 3 + length) + } else { + alloc.heapBuffer(3 + length, 3 + length) + } + } +} diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CheckWorldSuitabilityCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CheckWorldSuitabilityCodec.kt index dcc353ef..26d7b365 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CheckWorldSuitabilityCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CheckWorldSuitabilityCodec.kt @@ -9,8 +9,7 @@ import org.openrs2.crypto.Rsa import org.openrs2.crypto.StreamCipher import org.openrs2.crypto.rsa import org.openrs2.crypto.secureRandom -import org.openrs2.protocol.PacketCodec -import org.openrs2.protocol.PacketLength +import org.openrs2.protocol.VariableBytePacketCodec import org.openrs2.util.Base37 import javax.inject.Inject import javax.inject.Singleton @@ -18,10 +17,9 @@ import javax.inject.Singleton @Singleton public class CheckWorldSuitabilityCodec @Inject constructor( private val key: RSAPrivateCrtKeyParameters -) : PacketCodec( +) : VariableBytePacketCodec( type = LoginRequest.CheckWorldSuitability::class.java, - opcode = 24, - length = PacketLength.VARIABLE_BYTE + opcode = 24 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): LoginRequest.CheckWorldSuitability { val build = input.readShort().toInt() diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckDateOfBirthCountryCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckDateOfBirthCountryCodec.kt index 579ea321..08eac516 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckDateOfBirthCountryCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckDateOfBirthCountryCodec.kt @@ -2,12 +2,12 @@ package org.openrs2.protocol.login.upstream import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec +import org.openrs2.protocol.FixedPacketCodec import java.time.LocalDate import javax.inject.Singleton @Singleton -public class CreateCheckDateOfBirthCountryCodec : PacketCodec( +public class CreateCheckDateOfBirthCountryCodec : FixedPacketCodec( type = LoginRequest.CreateCheckDateOfBirthCountry::class.java, opcode = 20, length = 6 diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckNameCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckNameCodec.kt index c3bac271..eecd8f08 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckNameCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateCheckNameCodec.kt @@ -2,12 +2,12 @@ package org.openrs2.protocol.login.upstream import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec +import org.openrs2.protocol.FixedPacketCodec import org.openrs2.util.Base37 import javax.inject.Singleton @Singleton -public class CreateCheckNameCodec : PacketCodec( +public class CreateCheckNameCodec : FixedPacketCodec( type = LoginRequest.CreateCheckName::class.java, opcode = 21, length = 8 diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitGameConnectionCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitGameConnectionCodec.kt index 28a00c23..9a0584f7 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitGameConnectionCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitGameConnectionCodec.kt @@ -2,14 +2,14 @@ package org.openrs2.protocol.login.upstream import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec +import org.openrs2.protocol.FixedPacketCodec import javax.inject.Singleton @Singleton -public class InitGameConnectionCodec : PacketCodec( +public class InitGameConnectionCodec : FixedPacketCodec( + type = LoginRequest.InitGameConnection::class.java, opcode = 14, - length = 1, - type = LoginRequest.InitGameConnection::class.java + length = 1 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): LoginRequest.InitGameConnection { val usernameHash = input.readUnsignedByte().toInt() diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitJs5RemoteConnectionCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitJs5RemoteConnectionCodec.kt index 17d063fa..01607c2c 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitJs5RemoteConnectionCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/InitJs5RemoteConnectionCodec.kt @@ -2,11 +2,11 @@ package org.openrs2.protocol.login.upstream import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec +import org.openrs2.protocol.FixedPacketCodec import javax.inject.Singleton @Singleton -public class InitJs5RemoteConnectionCodec : PacketCodec( +public class InitJs5RemoteConnectionCodec : FixedPacketCodec( type = LoginRequest.InitJs5RemoteConnection::class.java, opcode = 15, length = 4 diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/RequestWorldListCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/RequestWorldListCodec.kt index b8e32b28..0c243824 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/RequestWorldListCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/RequestWorldListCodec.kt @@ -2,11 +2,11 @@ package org.openrs2.protocol.login.upstream import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec +import org.openrs2.protocol.FixedPacketCodec import javax.inject.Singleton @Singleton -public class RequestWorldListCodec : PacketCodec( +public class RequestWorldListCodec : FixedPacketCodec( type = LoginRequest.RequestWorldList::class.java, opcode = 23, length = 4 diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/world/downstream/WorldListResponseCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/world/downstream/WorldListResponseCodec.kt index a0dba381..e0bb86a2 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/world/downstream/WorldListResponseCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/world/downstream/WorldListResponseCodec.kt @@ -6,15 +6,13 @@ import org.openrs2.buffer.readVersionedString import org.openrs2.buffer.writeUnsignedShortSmart import org.openrs2.buffer.writeVersionedString import org.openrs2.crypto.StreamCipher -import org.openrs2.protocol.PacketCodec -import org.openrs2.protocol.PacketLength +import org.openrs2.protocol.VariableShortPacketCodec import javax.inject.Singleton @Singleton -public class WorldListResponseCodec : PacketCodec( +public class WorldListResponseCodec : VariableShortPacketCodec( type = WorldListResponse::class.java, - opcode = 0, - length = PacketLength.VARIABLE_SHORT + opcode = 0 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): WorldListResponse { val version = input.readUnsignedByte().toInt() diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt index f89d5873..30f44057 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt @@ -3,7 +3,7 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -internal object LengthMismatchPacketCodec : PacketCodec( +internal object LengthMismatchPacketCodec : FixedPacketCodec( type = FixedPacket::class.java, opcode = 0, length = 5 diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/Rs2DecoderTest.kt b/protocol/src/test/kotlin/org/openrs2/protocol/Rs2DecoderTest.kt index e24deb86..5af05d12 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/Rs2DecoderTest.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/Rs2DecoderTest.kt @@ -35,7 +35,7 @@ class Rs2DecoderTest { @Test fun testEncryptedOpcode() { - val decoder = Rs2Decoder(Protocol(FixedPacketCodec)) + val decoder = Rs2Decoder(Protocol(TestFixedPacketCodec)) decoder.cipher = TestStreamCipher val channel = EmbeddedChannel(decoder) @@ -47,7 +47,7 @@ class Rs2DecoderTest { @Test fun testSwitchProtocol() { - val decoder = Rs2Decoder(Protocol(FixedPacketCodec)) + val decoder = Rs2Decoder(Protocol(TestFixedPacketCodec)) val channel = EmbeddedChannel(decoder) channel.writeInbound(wrappedBuffer(0, 0x11, 0x22, 0x33, 0x44)) @@ -73,9 +73,9 @@ class Rs2DecoderTest { val channel = EmbeddedChannel( Rs2Decoder( Protocol( - FixedPacketCodec, - VariableBytePacketCodec, - VariableShortPacketCodec, + TestFixedPacketCodec, + TestVariableBytePacketCodec, + TestVariableShortPacketCodec, VariableByteOptimisedPacketCodec, VariableShortOptimisedPacketCodec, TestEmptyPacketCodec @@ -92,9 +92,9 @@ class Rs2DecoderTest { val channel = EmbeddedChannel( Rs2Decoder( Protocol( - FixedPacketCodec, - VariableBytePacketCodec, - VariableShortPacketCodec, + TestFixedPacketCodec, + TestVariableBytePacketCodec, + TestVariableShortPacketCodec, VariableByteOptimisedPacketCodec, VariableShortOptimisedPacketCodec ) diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/Rs2EncoderTest.kt b/protocol/src/test/kotlin/org/openrs2/protocol/Rs2EncoderTest.kt index 2914fb9b..4ded297a 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/Rs2EncoderTest.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/Rs2EncoderTest.kt @@ -24,8 +24,8 @@ class Rs2EncoderTest { val channel = EmbeddedChannel( Rs2Encoder( Protocol( - VariableBytePacketCodec, - VariableShortPacketCodec + TestVariableBytePacketCodec, + TestVariableShortPacketCodec ) ) ) @@ -71,7 +71,7 @@ class Rs2EncoderTest { @Test fun testEncryptedOpcode() { - val encoder = Rs2Encoder(Protocol(FixedPacketCodec)) + val encoder = Rs2Encoder(Protocol(TestFixedPacketCodec)) encoder.cipher = TestStreamCipher val channel = EmbeddedChannel(encoder) @@ -86,7 +86,7 @@ class Rs2EncoderTest { @Test fun testSwitchProtocol() { - val encoder = Rs2Encoder(Protocol(FixedPacketCodec)) + val encoder = Rs2Encoder(Protocol(TestFixedPacketCodec)) val channel = EmbeddedChannel(encoder) channel.writeOutbound(FixedPacket(0x11223344)) @@ -115,9 +115,9 @@ class Rs2EncoderTest { val channel = EmbeddedChannel( Rs2Encoder( Protocol( - FixedPacketCodec, - VariableBytePacketCodec, - VariableShortPacketCodec, + TestFixedPacketCodec, + TestVariableBytePacketCodec, + TestVariableShortPacketCodec, VariableByteOptimisedPacketCodec, VariableShortOptimisedPacketCodec, TestEmptyPacketCodec diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/TestFixedPacketCodec.kt similarity index 86% rename from protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt rename to protocol/src/test/kotlin/org/openrs2/protocol/TestFixedPacketCodec.kt index 3d9452fd..36102736 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/TestFixedPacketCodec.kt @@ -3,7 +3,7 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -internal object FixedPacketCodec : PacketCodec( +internal object TestFixedPacketCodec : FixedPacketCodec( type = FixedPacket::class.java, opcode = 0, length = 4 diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/TestVariableBytePacketCodec.kt similarity index 79% rename from protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt rename to protocol/src/test/kotlin/org/openrs2/protocol/TestVariableBytePacketCodec.kt index 89de8166..81fe4919 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/TestVariableBytePacketCodec.kt @@ -3,10 +3,9 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -internal object VariableBytePacketCodec : PacketCodec( +internal object TestVariableBytePacketCodec : VariableBytePacketCodec( type = VariableBytePacket::class.java, - opcode = 1, - length = PacketLength.VARIABLE_BYTE + opcode = 1 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableBytePacket { val value = ByteArray(input.readableBytes()) diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/TestVariableShortPacketCodec.kt similarity index 79% rename from protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt rename to protocol/src/test/kotlin/org/openrs2/protocol/TestVariableShortPacketCodec.kt index 814163d7..5cee6c1f 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/TestVariableShortPacketCodec.kt @@ -3,10 +3,9 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -internal object VariableShortPacketCodec : PacketCodec( +internal object TestVariableShortPacketCodec : VariableShortPacketCodec( type = VariableShortPacket::class.java, - opcode = 2, - length = PacketLength.VARIABLE_SHORT + opcode = 2 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableShortPacket { val value = ByteArray(input.readableBytes()) diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt index 7ac4085b..95f7c074 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt @@ -3,10 +3,9 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -internal object VariableByteOptimisedPacketCodec : PacketCodec( +internal object VariableByteOptimisedPacketCodec : VariableBytePacketCodec( type = VariableByteOptimisedPacket::class.java, - opcode = 3, - length = PacketLength.VARIABLE_BYTE + opcode = 3 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableByteOptimisedPacket { val value = ByteArray(input.readableBytes()) diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt index acb3d5b2..0fc84715 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt @@ -3,10 +3,9 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf import org.openrs2.crypto.StreamCipher -internal object VariableShortOptimisedPacketCodec : PacketCodec( +internal object VariableShortOptimisedPacketCodec : VariableShortPacketCodec( type = VariableShortOptimisedPacket::class.java, - opcode = 4, - length = PacketLength.VARIABLE_SHORT + opcode = 4 ) { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableShortOptimisedPacket { val value = ByteArray(input.readableBytes())