Search in sources :

Example 1 with RpcResponse

use of org.msec.rpc.RpcResponse in project MSEC by Tencent.

the class ResponseEncoder method encode.

@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
    if (msg instanceof RpcResponse) {
        RpcResponse message = (RpcResponse) msg;
        try {
            ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(estimatedLength, ctx.getChannel().getConfig().getBufferFactory()));
            ChannelBuffer buffer;
            if (message.getSerializeMode() == RpcRequest.SerializeMode.SERIALIZE_MODE_PROTOBUF) {
                buffer = serializeProtobufPakcage(ctx, message, bout);
            } else {
                buffer = serializeHTTPPakcage(ctx, message, bout);
            }
            return buffer;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw ex;
        }
    }
    return null;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) RpcResponse(org.msec.rpc.RpcResponse) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with RpcResponse

use of org.msec.rpc.RpcResponse in project MSEC by Tencent.

the class ResponseDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, Object msg) throws Exception {
    long receiveTime = System.currentTimeMillis();
    if (!(msg instanceof ChannelBuffer)) {
        return msg;
    }
    // System.out.println("Response recvtime: " + System.currentTimeMillis());
    List<Object> messages = null;
    ChannelBuffer cb = (ChannelBuffer) NettyCodecUtils.getAttachment(channelHandlerContext, Constants.ATTACHMENT_BYTEBUFFER);
    ChannelBuffer cb_ = (ChannelBuffer) msg;
    if (cb == null) {
        cb = cb_;
    } else {
        cb.writeBytes(cb_);
    }
    ChannelHandlerContext encodeCtx = channelHandlerContext.getPipeline().getContext("encoder");
    Integer protoFlag = (Integer) NettyCodecUtils.getAttachment(encodeCtx, Constants.ATTACHMENT_CUSTOM_PROTO_FLAG);
    if (protoFlag != null && protoFlag.intValue() == 1) {
        RpcResponse rpcResponse = getCustomPackage(channelHandlerContext, channel, cb, encodeCtx);
        if (rpcResponse == null) {
            return null;
        }
        if (messages == null) {
            messages = new ArrayList<Object>();
        }
        messages.add(rpcResponse);
        return messages;
    }
    int lastReadIndex = cb.readerIndex();
    while (cb.readable()) {
        if (cb.readableBytes() < Constants.PKG_LEAST_LENGTH) {
            setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
            break;
        }
        byte stx = cb.readByte();
        if (stx != (byte) '(') {
            log.error("Invalid packge stx.");
            channelHandlerContext.getChannel().close();
            throw new IllegalArgumentException("Request decode error: invalid package stx " + stx);
        }
        int headLength = cb.readInt();
        int bodyLength = cb.readInt();
        if (cb.readableBytes() < headLength + bodyLength + 1) {
            setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
            break;
        }
        byte[] headBytes = new byte[headLength];
        byte[] bodyBytes = new byte[bodyLength];
        cb.readBytes(headBytes);
        cb.readBytes(bodyBytes);
        byte etx = cb.readByte();
        if (etx != ')') {
            log.error("Invalid package etx.");
            channelHandlerContext.getChannel().close();
            throw new IllegalArgumentException("Request decode error: invalid package etx " + etx);
        }
        RpcResponse rpcResponse = null;
        // parse protobuf
        try {
            Head.CRpcHead pbHead = Head.CRpcHead.parseFrom(headBytes);
            String serviceMethodName = pbHead.getMethodName().toStringUtf8();
            // String[] splits = serviceMethodName.split(":");
            // String serviceName = splits[0];
            // String methodName = splits[1];
            rpcResponse = new RpcResponse();
            rpcResponse.setSeq(pbHead.getSequence());
            rpcResponse.setErrno(pbHead.getErr());
            if (pbHead.getErrMsg() != null && !pbHead.getErrMsg().isEmpty()) {
                rpcResponse.setError(new Exception(pbHead.getErrMsg().toStringUtf8()));
            } else {
                rpcResponse.setError(null);
            }
            if (pbHead.getErr() == 0) {
                rpcResponse.setResultObj(bodyBytes);
            } else {
                rpcResponse.setResultObj(null);
            }
        } catch (com.google.protobuf.InvalidProtocolBufferException ex) {
            rpcResponse = null;
            log.error("RPC Response parse failed.");
            throw new IllegalArgumentException("Response decode error: protobuf parse failed.");
        }
        if (rpcResponse != null) {
            if (messages == null) {
                messages = new ArrayList<Object>();
            }
            messages.add(rpcResponse);
            lastReadIndex = cb.readerIndex();
        } else {
            setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
            break;
        }
    }
    return messages;
}
Also used : Head(srpc.Head) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) RpcResponse(org.msec.rpc.RpcResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DynamicChannelBuffer(org.jboss.netty.buffer.DynamicChannelBuffer)

Example 3 with RpcResponse

use of org.msec.rpc.RpcResponse 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;
}
Also used : CustomPackageLengthChecker(org.msec.rpc.CustomPackageLengthChecker) CustomPackageCodec(org.msec.rpc.CustomPackageCodec) RpcResponse(org.msec.rpc.RpcResponse)

Aggregations

RpcResponse (org.msec.rpc.RpcResponse)3 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 IOException (java.io.IOException)1 ChannelBufferOutputStream (org.jboss.netty.buffer.ChannelBufferOutputStream)1 DynamicChannelBuffer (org.jboss.netty.buffer.DynamicChannelBuffer)1 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)1 CustomPackageCodec (org.msec.rpc.CustomPackageCodec)1 CustomPackageLengthChecker (org.msec.rpc.CustomPackageLengthChecker)1 Head (srpc.Head)1