Search in sources :

Example 1 with TooBigDataException

use of com.baidu.brpc.exceptions.TooBigDataException in project brpc-java by baidu.

the class DubboRpcProtocol method decode.

@Override
public Object decode(ChannelHandlerContext ctx, DynamicCompositeByteBuf in, boolean isDecodingRequest) throws TooBigDataException, NotEnoughDataException, BadSchemaException {
    if (in.readableBytes() < DubboConstants.FIXED_HEAD_LEN) {
        throw notEnoughDataException;
    }
    ByteBuf headerBuf = in.retainedSlice(DubboConstants.FIXED_HEAD_LEN);
    try {
        DubboHeader dubboHeader = DubboHeader.decode(headerBuf);
        if (dubboHeader.getMagic() != DubboConstants.MAGIC) {
            throw new BadSchemaException("not valid magic head for dubbo");
        }
        // 512M
        if (dubboHeader.getBodyLength() > 512 * 1024 * 1024) {
            throw new TooBigDataException("dubbo too big body size:" + dubboHeader.getBodyLength());
        }
        if (in.readableBytes() < dubboHeader.getBodyLength() + DubboConstants.FIXED_HEAD_LEN) {
            throw notEnoughDataException;
        }
        in.skipBytes(DubboConstants.FIXED_HEAD_LEN);
        ByteBuf bodyBuf = in.readRetainedSlice(dubboHeader.getBodyLength());
        DubboPacket dubboPacket = new DubboPacket();
        dubboPacket.setHeader(dubboHeader);
        dubboPacket.setBodyBuf(bodyBuf);
        return dubboPacket;
    } finally {
        headerBuf.release();
    }
}
Also used : TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with TooBigDataException

use of com.baidu.brpc.exceptions.TooBigDataException in project brpc-java by baidu.

the class HuluRpcProtocol method decode.

@Override
public HuluRpcDecodePacket decode(ChannelHandlerContext ctx, DynamicCompositeByteBuf in, boolean isDecodingRequest) throws BadSchemaException, TooBigDataException, NotEnoughDataException {
    if (in.readableBytes() < FIXED_LEN) {
        throw notEnoughDataException;
    }
    ByteBuf fixHeaderBuf = in.retainedSlice(FIXED_LEN);
    try {
        byte[] magic = new byte[4];
        fixHeaderBuf.readBytes(magic);
        if (!Arrays.equals(magic, MAGIC_HEAD)) {
            throw new BadSchemaException("not valid magic head for hulu");
        }
        int bodySize = fixHeaderBuf.readIntLE();
        int metaSize = fixHeaderBuf.readIntLE();
        // 512M
        if (bodySize > 512 * 1024 * 1024) {
            throw new TooBigDataException("to big body size:" + bodySize);
        }
        if (in.readableBytes() < FIXED_LEN + bodySize) {
            throw notEnoughDataException;
        }
        in.skipBytes(FIXED_LEN);
        HuluRpcDecodePacket packet = new HuluRpcDecodePacket();
        try {
            // meta
            ByteBuf metaBuf = in.readRetainedSlice(metaSize);
            packet.setMetaBuf(metaBuf);
            // body
            ByteBuf protoAndAttachmentBuf = in.readRetainedSlice(bodySize - metaSize);
            packet.setProtoAndAttachmentBuf(protoAndAttachmentBuf);
            return packet;
        } catch (Exception ex) {
            LOG.warn("decode failed, ex={}", ex.getMessage());
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, ex);
        }
    } finally {
        fixHeaderBuf.release();
    }
}
Also used : TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) RpcException(com.baidu.brpc.exceptions.RpcException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) IOException(java.io.IOException)

Example 3 with TooBigDataException

use of com.baidu.brpc.exceptions.TooBigDataException in project brpc-java by baidu.

the class PublicPbrpcProtocol method decode.

@Override
public PublicPbRpcPacket decode(ChannelHandlerContext ctx, DynamicCompositeByteBuf in, boolean isDecodingRequest) throws BadSchemaException, TooBigDataException, NotEnoughDataException {
    if (in.readableBytes() < NSHead.NSHEAD_LENGTH) {
        throw notEnoughDataException;
    }
    PublicPbRpcPacket packet = new PublicPbRpcPacket();
    ByteBuf fixHeaderBuf = in.retainedSlice(NSHead.NSHEAD_LENGTH);
    try {
        NSHead nsHead = NSHead.fromByteBuf(fixHeaderBuf);
        packet.setNsHead(nsHead);
        int bodyLength = nsHead.bodyLength;
        // 512M
        if (bodyLength > 512 * 1024 * 1024) {
            throw new TooBigDataException("to big body size:" + bodyLength);
        }
        if (in.readableBytes() < NSHead.NSHEAD_LENGTH + bodyLength) {
            throw notEnoughDataException;
        }
        in.skipBytes(NSHead.NSHEAD_LENGTH);
        ByteBuf bodyBuf = in.readRetainedSlice(bodyLength);
        packet.setBody(bodyBuf);
        return packet;
    } finally {
        fixHeaderBuf.release();
    }
}
Also used : TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NSHead(com.baidu.brpc.protocol.nshead.NSHead) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf)

Example 4 with TooBigDataException

use of com.baidu.brpc.exceptions.TooBigDataException in project brpc-java by baidu.

the class SofaRpcProtocol method decode.

@Override
public SofaRpcDecodePacket decode(ChannelHandlerContext ctx, DynamicCompositeByteBuf in, boolean isDecodingRequest) throws BadSchemaException, TooBigDataException, NotEnoughDataException {
    if (in.readableBytes() < FIXED_LEN) {
        throw notEnoughDataException;
    }
    ByteBuf fixHeaderBuf = in.retainedSlice(FIXED_LEN);
    try {
        byte[] magic = new byte[4];
        fixHeaderBuf.readBytes(magic);
        if (!Arrays.equals(magic, MAGIC_HEAD)) {
            throw new BadSchemaException("not valid magic head for sofa");
        }
        int metaSize = fixHeaderBuf.readIntLE();
        int bodySize = (int) fixHeaderBuf.readLongLE();
        int msgSize = (int) fixHeaderBuf.readLongLE();
        if (msgSize != metaSize + bodySize) {
            throw new BadSchemaException("msgSize != metaSize + bodySize");
        }
        // 512M
        if (bodySize > 512 * 1024 * 1024) {
            throw new TooBigDataException("to big body size:" + bodySize);
        }
        if (in.readableBytes() < FIXED_LEN + msgSize) {
            throw notEnoughDataException;
        }
        in.skipBytes(FIXED_LEN);
        SofaRpcDecodePacket packet = new SofaRpcDecodePacket();
        try {
            // meta
            ByteBuf metaBuf = in.readRetainedSlice(metaSize);
            packet.setMetaBuf(metaBuf);
            // body
            ByteBuf protoBuf = in.readRetainedSlice(bodySize);
            packet.setProtoBuf(protoBuf);
            return packet;
        } catch (Exception ex) {
            LOG.debug("decode failed, ex={}", ex.getMessage());
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, ex);
        }
    } finally {
        fixHeaderBuf.release();
    }
}
Also used : TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) RpcException(com.baidu.brpc.exceptions.RpcException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException)

Example 5 with TooBigDataException

use of com.baidu.brpc.exceptions.TooBigDataException in project brpc-java by baidu.

the class BaiduRpcProtocol method decode.

@Override
public BaiduRpcDecodePacket decode(ChannelHandlerContext ctx, DynamicCompositeByteBuf in, boolean isDecodingRequest) throws BadSchemaException, TooBigDataException, NotEnoughDataException {
    if (in.readableBytes() < FIXED_LEN) {
        throw notEnoughDataException;
    }
    ByteBuf fixHeaderBuf = in.retainedSlice(FIXED_LEN);
    try {
        byte[] magic = new byte[4];
        fixHeaderBuf.readBytes(magic);
        if (!Arrays.equals(magic, MAGIC_HEAD)) {
            throw new BadSchemaException("not valid magic head for brpc");
        }
        int bodySize = fixHeaderBuf.readInt();
        // 512M
        if (bodySize > 512 * 1024 * 1024) {
            throw new TooBigDataException("to big body size:" + bodySize);
        }
        if (in.readableBytes() < FIXED_LEN + bodySize) {
            throw notEnoughDataException;
        }
        int metaSize = fixHeaderBuf.readInt();
        in.skipBytes(FIXED_LEN);
        BaiduRpcDecodePacket packet = new BaiduRpcDecodePacket();
        try {
            // meta
            ByteBuf metaBuf = in.readRetainedSlice(metaSize);
            packet.setMetaBuf(metaBuf);
            // proto and attachment
            ByteBuf protoAndAttachmentBuf = in.readRetainedSlice(bodySize - metaSize);
            packet.setProtoAndAttachmentBuf(protoAndAttachmentBuf);
            return packet;
        } catch (Exception ex) {
            LOG.warn("decode failed:", ex);
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, ex);
        }
    } finally {
        fixHeaderBuf.release();
    }
}
Also used : TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) RpcException(com.baidu.brpc.exceptions.RpcException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException)

Aggregations

TooBigDataException (com.baidu.brpc.exceptions.TooBigDataException)11 ByteBuf (io.netty.buffer.ByteBuf)11 DynamicCompositeByteBuf (com.baidu.brpc.buffer.DynamicCompositeByteBuf)10 BadSchemaException (com.baidu.brpc.exceptions.BadSchemaException)7 NotEnoughDataException (com.baidu.brpc.exceptions.NotEnoughDataException)7 RpcException (com.baidu.brpc.exceptions.RpcException)6 ChannelInfo (com.baidu.brpc.ChannelInfo)2 NSHead (com.baidu.brpc.protocol.nshead.NSHead)1 HttpMessage (io.netty.handler.codec.http.HttpMessage)1 IOException (java.io.IOException)1