added JoinResponsePacket.kt + Builders + ext
added game protocol compression
This commit is contained in:
1 parent
f5e18d8540
commit
88e888dfc9
16 files changed
+272
-16
No files matched your search
@@ -0,0 +1,55 @@
|
||||
package de.fiereu.openmmo.server.netty
|
||||
|
||||
import de.fiereu.openmmo.server.config.ServerConfig
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.handler.codec.MessageToMessageEncoder
|
||||
import java.util.zip.Deflater
|
||||
|
||||
class Compressor(
|
||||
serverConfig: ServerConfig,
|
||||
) {
|
||||
private val deflater = Deflater(Deflater.DEFAULT_COMPRESSION, true)
|
||||
/**
|
||||
* At which packet size we need to compress the packet.
|
||||
*/
|
||||
private val threshold: Int = (serverConfig.sendBufferSize * 0.9).toInt()
|
||||
|
||||
fun compress(
|
||||
msg: ByteBuf
|
||||
) : ByteBuf {
|
||||
val dataLength = msg.readableBytes()
|
||||
if (dataLength < threshold) {
|
||||
val outputBuffer = Unpooled
|
||||
.buffer(dataLength + 1)
|
||||
.writeBoolean(false)
|
||||
.writeBytes(msg)
|
||||
|
||||
return outputBuffer
|
||||
}
|
||||
|
||||
// Maybe use pre-allocated buffer?
|
||||
val inputBuffer = Unpooled.copiedBuffer(msg)
|
||||
.writeShortLE(0xFFFF)
|
||||
|
||||
deflater.setInput(
|
||||
inputBuffer.array(),
|
||||
inputBuffer.arrayOffset() + inputBuffer.readerIndex(),
|
||||
inputBuffer.readableBytes()
|
||||
)
|
||||
val outputBuffer = Unpooled
|
||||
.buffer(dataLength + 1)
|
||||
.writeBoolean(true)
|
||||
|
||||
val buffer = ByteArray(Short.MAX_VALUE.toInt())
|
||||
do {
|
||||
val read = deflater.deflate(buffer)
|
||||
if (read == 0) break
|
||||
|
||||
outputBuffer.writeBytes(buffer, 0, read)
|
||||
} while (true)
|
||||
|
||||
return outputBuffer
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -52,7 +52,7 @@ class DefaultChannelHandlerProvider(
|
||||
pipeline.addLast("tls-encryption", TlsEncryptionHandler(tlsContext))
|
||||
|
||||
pipeline.addLast("protocol-logger", LoggingHandler("protocol-logger", LogLevel.TRACE))
|
||||
pipeline.addLast("protocol", TlsProtocolHandler(tlsProtocol, tlsConfig, this))
|
||||
pipeline.addLast("protocol", TlsProtocolHandler(tlsProtocol, serverConfig, tlsConfig, this))
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,9 +3,12 @@ package de.fiereu.openmmo.server.netty.handlers
|
||||
import de.fiereu.openmmo.protocols.PacketDirection
|
||||
import de.fiereu.openmmo.protocols.Protocol
|
||||
import de.fiereu.openmmo.protocols.getDeserializer
|
||||
import de.fiereu.openmmo.server.config.ServerConfig
|
||||
import de.fiereu.openmmo.server.netty.Compressor
|
||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.channel.ChannelDuplexHandler
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.channel.ChannelPromise
|
||||
@@ -13,9 +16,12 @@ import io.netty.channel.ChannelPromise
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
abstract class ProtocolHandler(
|
||||
private val protocol: Protocol
|
||||
val protocol: Protocol,
|
||||
serverConfig: ServerConfig
|
||||
) : ChannelDuplexHandler() {
|
||||
|
||||
private val compressor = Compressor(serverConfig)
|
||||
|
||||
override fun channelRead(ctx: ChannelHandlerContext, msg: Any) {
|
||||
if (msg !is ByteBuf) {
|
||||
log.warn { "Received non-ByteBuf message: ${msg::class.simpleName}" }
|
||||
@@ -38,11 +44,11 @@ abstract class ProtocolHandler(
|
||||
val packetData = deserializer.deserialize(msg)
|
||||
|
||||
/*
|
||||
We cant call the onPacketReceived function asynchronously directly here,
|
||||
because the TLS protocol expects a synchronous flow. Meaning that the client already sends the next packet
|
||||
after the ClientReady packet, so if we would process the ClientReady packet asynchronously,
|
||||
the next packet would be processed before the ClientReady packet is fully processed.
|
||||
-> It would be processed by the TLS protocol before and not by the next protocol (e.g. login protocol).
|
||||
* We cant call the onPacketReceived function asynchronously directly here,
|
||||
* because the TLS protocol expects a synchronous flow. Meaning that the client already sends the next packet
|
||||
* after the ClientReady packet, so if we would process the ClientReady packet asynchronously,
|
||||
* the next packet would be processed before the ClientReady packet is fully processed.
|
||||
* -> It would be processed by the TLS protocol before and not by the next protocol (e.g. login protocol).
|
||||
*/
|
||||
onPacketReceived(PacketEvent(ctx, packetData))
|
||||
} catch (e: Exception) {
|
||||
@@ -71,7 +77,20 @@ abstract class ProtocolHandler(
|
||||
|
||||
val buffer = ctx.alloc().buffer()
|
||||
buffer.writeByte(opcode.value.toInt())
|
||||
serializer.serializeObject(msg, buffer)
|
||||
|
||||
/*
|
||||
* If the protocol requires compression (e.g. Game protocol),
|
||||
* we need to compress the serialized packet before adding the opcode.
|
||||
*/
|
||||
if (!protocol.compressed) {
|
||||
serializer.serializeObject(msg, buffer)
|
||||
} else {
|
||||
val uncompressedBuffer = Unpooled.buffer()
|
||||
serializer.serializeObject(msg, uncompressedBuffer)
|
||||
buffer.writeBytes(
|
||||
compressor.compress(uncompressedBuffer)
|
||||
)
|
||||
}
|
||||
|
||||
ctx.write(buffer, promise)
|
||||
} catch (e: Exception) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import de.fiereu.openmmo.protocols.Protocol
|
||||
import de.fiereu.openmmo.protocols.tls.packets.ClientHelloPacket
|
||||
import de.fiereu.openmmo.protocols.tls.packets.ClientReadyPacket
|
||||
import de.fiereu.openmmo.protocols.tls.packets.ServerHelloPacket
|
||||
import de.fiereu.openmmo.server.config.ServerConfig
|
||||
import de.fiereu.openmmo.server.config.TlsConfig
|
||||
import de.fiereu.openmmo.server.netty.DefaultChannelHandlerProvider
|
||||
import de.fiereu.openmmo.server.netty.handlers.ProtocolHandler
|
||||
@@ -23,9 +24,10 @@ private val log = KotlinLogging.logger {}
|
||||
|
||||
class TlsProtocolHandler(
|
||||
protocol: Protocol,
|
||||
serverConfig: ServerConfig,
|
||||
private val tlsConfig: TlsConfig,
|
||||
private val channelHandlerProvider: DefaultChannelHandlerProvider
|
||||
) : ProtocolHandler(protocol) {
|
||||
) : ProtocolHandler(protocol, serverConfig) {
|
||||
|
||||
private val packetBuffer: MutableList<ByteBuf> = mutableListOf()
|
||||
private val connectionKeyPair: Pair<ECPublicKey, ECPrivateKey>
|
||||
|
||||
Reference in new issue
Block a user