Open-source multiplayer game server compatible with the RuneScape client
https://www.openrs2.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
791 B
28 lines
791 B
package org.openrs2.protocol
|
|
|
|
import jakarta.inject.Inject
|
|
import jakarta.inject.Singleton
|
|
|
|
@Singleton
|
|
public class Protocol @Inject constructor(codecs: Set<PacketCodec<*>>) {
|
|
public constructor(vararg codecs: PacketCodec<*>) : this(codecs.toSet())
|
|
|
|
private val decoders = arrayOfNulls<PacketCodec<*>>(256)
|
|
private val encoders = codecs.associateBy(PacketCodec<*>::type)
|
|
|
|
init {
|
|
for (codec in codecs) {
|
|
decoders[codec.opcode] = codec
|
|
}
|
|
}
|
|
|
|
public fun getDecoder(opcode: Int): PacketCodec<*>? {
|
|
require(opcode in decoders.indices)
|
|
return decoders[opcode]
|
|
}
|
|
|
|
@Suppress("UNCHECKED_CAST")
|
|
public fun <T : Packet> getEncoder(type: Class<T>): PacketCodec<T>? {
|
|
return encoders[type] as PacketCodec<T>?
|
|
}
|
|
}
|
|
|