use of org.msec.rpc.CustomPackageLengthChecker in project MSEC by Tencent.
the class ResponseDecoder method getCustomPackage.
private RpcResponse getCustomPackage(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer cb, ChannelHandlerContext encodeCtx) {
byte[] tmp_buff = new byte[cb.readableBytes()];
cb.readBytes(tmp_buff);
CustomPackageLengthChecker lengthChecker = (CustomPackageCodec) NettyCodecUtils.getAttachment(encodeCtx, Constants.ATTACHMENT_CUSTOM_PROTO_CODEC);
int checkRet = lengthChecker.checkPackageLength(tmp_buff);
if (checkRet < 0) {
return null;
} else if (checkRet == 0 || checkRet > tmp_buff.length) {
cb.resetReaderIndex();
setAttachment(channelHandlerContext, channel, cb, 0);
return null;
} else if (checkRet <= tmp_buff.length) {
if (checkRet < tmp_buff.length) {
tmp_buff = null;
tmp_buff = new byte[checkRet];
cb.resetReaderIndex();
cb.readBytes(tmp_buff);
}
long sequence = 0;
Long sequenceObj = (Long) NettyCodecUtils.getAttachment(encodeCtx, Constants.ATTACHMENT_CUSTOM_PROTO_SEQUENCE);
if (sequenceObj == null) {
CustomPackageCodec packageCodec = (CustomPackageCodec) lengthChecker;
sequence = packageCodec.decodeSequence(tmp_buff);
} else {
sequence = sequenceObj.longValue();
}
RpcResponse rpcResponse = new RpcResponse();
rpcResponse.setSeq(sequence);
rpcResponse.setResultObj(tmp_buff);
return rpcResponse;
}
return null;
}
Aggregations