-
-
Save autronix/fe1a90f2de6e9c2806df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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