package de.fiereu.openmmo.protocols import io.github.oshai.kotlinlogging.KotlinLogging import io.netty.buffer.ByteBuf import kotlin.reflect.KClass /** Value class wrapping a UByte to represent an opcode. */ @JvmInline value class Opcode(val value: UByte) { override fun toString(): String = "0x${value.toString(16).padStart(2, '0').uppercase()}" } /** * Packet direction enum representing the direction of packet flow. This is needed because depending * on the current side (client or server), the same opcode can represent different packets. For * example, opcode 0x01 might represent a "LoginRequest" packet on the client side, but a * "LoginResponse" packet on the server side. */ enum class PacketDirection { CLIENT_TO_SERVER, SERVER_TO_CLIENT } interface PacketSerializer { fun serializeObject(packet: Any, buffer: ByteBuf) { @Suppress("UNCHECKED_CAST") serialize(packet as T, buffer) } fun serialize(packet: T, buffer: ByteBuf) } interface PacketDeserializer { fun deserialize(buffer: ByteBuf): T } /** * The base class for all protocols. A protocol is a collection of packets that can be sent and * received. * * We cant combine Server and Client protocols because they have different dependencies. For * example, the server protocol needs to know about the server's root private key, while the client * protocol should not have any knowledge about the server's private key. */ abstract class Protocol { private val log = KotlinLogging.logger {} private val incomingPacketRegistry: MutableMap> = mutableMapOf() private val outgoingPacketRegistry: MutableMap> = mutableMapOf() private val dataClassToOpcode: MutableMap, Opcode> = mutableMapOf() abstract val async: Boolean abstract val compressed: Boolean fun getOpcode(dataClass: KClass<*>): Opcode? = dataClassToOpcode[dataClass] fun registerIncoming(opcode: Opcode, deserializer: PacketDeserializer) { log.debug { "Registering incoming packet: opcode=$opcode, deserializer=${deserializer::class.simpleName}" } incomingPacketRegistry[opcode] = deserializer } fun registerOutgoing( opcode: Opcode, serializer: PacketSerializer, dataClass: KClass, ) { log.debug { "Registering outgoing packet: opcode=$opcode, serializer=${serializer::class.simpleName}" } if (dataClassToOpcode.containsKey(dataClass)) { // Better to fail fast here than to have hard to debug issues later error("Data class ${dataClass.simpleName} is already registered with an opcode!") } dataClassToOpcode[dataClass] = opcode outgoingPacketRegistry[opcode] = serializer } fun getSerializer(opcode: Opcode): PacketSerializer<*>? { return outgoingPacketRegistry[opcode] } fun getDeserializer(opcode: Opcode): PacketDeserializer<*>? { return incomingPacketRegistry[opcode] } } fun Protocol.getDeserializer(opcode: UByte): PacketDeserializer<*>? { return this.getDeserializer(Opcode(opcode)) } fun Protocol.incomingPacket(opcode: UByte, deserializer: PacketDeserializer) { this.registerIncoming(Opcode(opcode), deserializer) } inline fun Protocol.outgoingPacket( opcode: UByte, serializer: PacketSerializer ) { this.registerOutgoing(Opcode(opcode), serializer, T::class) }