Initial commit
This commit is contained in:
commit
cdfc11344d
48 changed files with 1306 additions and 0 deletions
31
blokk-server/src/main/kotlin/space/blokk/net/FramingCodec.kt
Normal file
31
blokk-server/src/main/kotlin/space/blokk/net/FramingCodec.kt
Normal file
|
@ -0,0 +1,31 @@
|
|||
package space.blokk.net
|
||||
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.handler.codec.ByteToMessageCodec
|
||||
|
||||
class FramingCodec: ByteToMessageCodec<ByteBuf>() {
|
||||
private var currentLength: Int? = null
|
||||
|
||||
override fun encode(ctx: ChannelHandlerContext, msg: ByteBuf, out: ByteBuf) {
|
||||
out.writeVarInt(msg.readableBytes())
|
||||
msg.readerIndex(0)
|
||||
out.writeBytes(msg)
|
||||
}
|
||||
|
||||
override fun decode(ctx: ChannelHandlerContext, msg: ByteBuf, out: MutableList<Any>) {
|
||||
var length = currentLength
|
||||
if (length == null) {
|
||||
if (msg.varIntReadable()) {
|
||||
length = msg.readVarInt()
|
||||
currentLength = length
|
||||
}
|
||||
else return
|
||||
}
|
||||
|
||||
if (msg.readableBytes() >= length) {
|
||||
out.add(msg.readBytes(length))
|
||||
currentLength = null
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue