Skip to content

Instantly share code, notes, and snippets.

@autronix
Created November 3, 2015 16:00
Show Gist options
  • Save autronix/fe1a90f2de6e9c2806df to your computer and use it in GitHub Desktop.
Save autronix/fe1a90f2de6e9c2806df to your computer and use it in GitHub Desktop.
package com.autronix.qmfeedclient
import io.netty.handler.codec.ByteToMessageDecoder
import io.netty.handler.codec.ReplayingDecoder
import io.netty.channel.ChannelHandlerContext
import io.netty.buffer.ByteBuf
import java.util.List
import io.netty.channel.ChannelHandler.Sharable
@Sharable
class RawPacketDecoder extends ReplayingDecoder[Int] {
val READ_HEADER = 0
val READ_CONTENT = 1
state(READ_HEADER)
var blockSize:Int = 0
var blockSeq:Int = 0
var blockTime: Long = 0
def decode(ctx: ChannelHandlerContext,in: ByteBuf,out: List[AnyRef]): Unit = {
var received_size = in.readableBytes()
//println("R size: "+received_size)
if(state() == READ_HEADER){
blockTime = in.readLong()
blockSeq = in.readInt()
blockSize = in.readInt()
//println("Time: "+blockTime+" ("+blockTime.toHexString+") - Sequence: "+ blockSeq+"("+blockSeq.toHexString+") - Size: "+ blockSize+"("+blockSize.toHexString+")/"+received_size+" (readable bytes)")
checkpoint(READ_CONTENT)
}
else if(state() == READ_CONTENT){
var bytes = new Array[Byte](blockSize)
in.getBytes(0,bytes,0,blockSize)
var frame = in.readBytes(blockSize)
//println("Total Size: "+blockSize + " - Bytes: "+Hex.encodeHexString(bytes))
checkpoint(READ_HEADER)
out.add(frame)
}
else {
throw new Error("Case not covered Exception")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment