Search in sources :

Example 1 with ChannelInfo

use of com.baidu.brpc.ChannelInfo in project brpc-java by baidu.

the class HuluRpcProtocol method decodeResponse.

@Override
public Response decodeResponse(Object packet, ChannelHandlerContext ctx) throws Exception {
    HuluRpcDecodePacket responsePacket = (HuluRpcDecodePacket) packet;
    ByteBuf metaBuf = responsePacket.getMetaBuf();
    ByteBuf protoAndAttachmentBuf = responsePacket.getProtoAndAttachmentBuf();
    ByteBuf protoBuf = null;
    try {
        RpcResponse rpcResponse = new RpcResponse();
        HuluRpcProto.HuluRpcResponseMeta responseMeta = (HuluRpcProto.HuluRpcResponseMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcResponseMetaInstance);
        Long correlationId = responseMeta.getCorrelationId();
        rpcResponse.setCorrelationId(correlationId);
        ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
        RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
        if (future == null) {
            return rpcResponse;
        }
        rpcResponse.setRpcFuture(future);
        int compressType = responseMeta.getCompressType();
        rpcResponse.setCompressType(compressType);
        try {
            if (responseMeta != null && responseMeta.getErrorCode() == 0) {
                Compress compress = compressManager.getCompress(compressType);
                int userMessageSize = responseMeta.getUserMessageSize();
                if (userMessageSize > 0) {
                    protoBuf = protoAndAttachmentBuf.readSlice(userMessageSize);
                } else {
                    protoBuf = protoAndAttachmentBuf;
                }
                Object responseProto = compress.uncompressOutput(protoBuf, future.getRpcMethodInfo());
                rpcResponse.setResult(responseProto);
                // attachment
                if (responseMeta.getUserMessageSize() > 0) {
                    rpcResponse.setBinaryAttachment(protoAndAttachmentBuf);
                    responsePacket.setProtoAndAttachmentBuf(null);
                }
            } else {
                rpcResponse.setException(new RpcException(RpcException.SERVICE_EXCEPTION, responseMeta.getErrorText()));
            }
        } catch (Exception ex) {
            // 解析失败直接抛异常
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "decode response failed", ex);
        }
        return rpcResponse;
    } finally {
        if (responsePacket.getMetaBuf() != null) {
            responsePacket.getMetaBuf().release();
        }
        if (responsePacket.getProtoAndAttachmentBuf() != null) {
            responsePacket.getProtoAndAttachmentBuf().release();
        }
    }
}
Also used : Compress(com.baidu.brpc.compress.Compress) ChannelInfo(com.baidu.brpc.ChannelInfo) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcResponse(com.baidu.brpc.protocol.RpcResponse) 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) RpcException(com.baidu.brpc.exceptions.RpcException) RpcFuture(com.baidu.brpc.client.RpcFuture)

Example 2 with ChannelInfo

use of com.baidu.brpc.ChannelInfo in project brpc-java by baidu.

the class PublicPbrpcProtocol method decodeResponse.

@Override
public Response decodeResponse(Object in, ChannelHandlerContext ctx) throws Exception {
    PublicPbRpcPacket packet = (PublicPbRpcPacket) in;
    RpcResponse rpcResponse = new RpcResponse();
    ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
    ByteBuf bodyBuf = packet.getBody();
    try {
        PublicPbrpcResponse pbResponse = (PublicPbrpcResponse) ProtobufUtils.parseFrom(bodyBuf, PublicPbrpcResponse.getDefaultInstance());
        ResponseBody body = pbResponse.getResponseBody(0);
        ResponseHead head = pbResponse.getResponseHead();
        if (head.getCode() != 0) {
            rpcResponse.setException(new RpcException(head.getText()));
        } else {
            rpcResponse.setCorrelationId(body.getId());
            RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
            if (future == null) {
                return rpcResponse;
            }
            rpcResponse.setRpcFuture(future);
            Object responseBody;
            int compressType = head.getCompressType();
            try {
                Compress compress = compressManager.getCompress(compressType);
                responseBody = compress.uncompressOutput(body.getSerializedResponse().toByteArray(), future.getRpcMethodInfo());
            } catch (Exception ex) {
                String errorMsg = String.format("decode failed, msg=%s", ex.getMessage());
                log.error(errorMsg);
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg, ex);
            }
            rpcResponse.setResult(responseBody);
        }
    } finally {
        if (bodyBuf != null) {
            bodyBuf.release();
        }
    }
    return rpcResponse;
}
Also used : Compress(com.baidu.brpc.compress.Compress) ChannelInfo(com.baidu.brpc.ChannelInfo) ByteString(com.google.protobuf.ByteString) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcResponse(com.baidu.brpc.protocol.RpcResponse) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) ResponseBody(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.ResponseBody) ResponseHead(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.ResponseHead) RpcException(com.baidu.brpc.exceptions.RpcException) PublicPbrpcResponse(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.PublicPbrpcResponse) RpcFuture(com.baidu.brpc.client.RpcFuture)

Example 3 with ChannelInfo

use of com.baidu.brpc.ChannelInfo in project brpc-java by baidu.

the class DefaultServerPushProtocol method decodeResponse.

@Override
public Response decodeResponse(Object in, ChannelHandlerContext ctx) throws Exception {
    DefaultServerPushPacket packet = (DefaultServerPushPacket) in;
    RpcResponse rpcResponse = new RpcResponse();
    // channel info是在客户端生成连接池的时候生成的
    ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
    Long correlationId = packet.getSpHead().getCorrelationId();
    rpcResponse.setCorrelationId(correlationId);
    RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
    if (future == null || future.getRpcMethodInfo() == null) {
        // rpc method info为null,表示register请求
        try {
            ByteBuf byteBuf = packet.getBodyBuf();
            byteBuf.release();
        } catch (Exception e) {
            throw new RpcException(e);
        }
        return rpcResponse;
    }
    rpcResponse.setRpcFuture(future);
    SPBody spBody = decodeBodyByteBuf(packet.getBodyBuf());
    Object responseBody = spBody.getContent();
    if (responseBody == null) {
        return null;
    }
    rpcResponse.setResult(responseBody);
    return rpcResponse;
}
Also used : RpcException(com.baidu.brpc.exceptions.RpcException) ChannelInfo(com.baidu.brpc.ChannelInfo) RpcFuture(com.baidu.brpc.client.RpcFuture) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcResponse(com.baidu.brpc.protocol.RpcResponse) 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 4 with ChannelInfo

use of com.baidu.brpc.ChannelInfo in project brpc-java by baidu.

the class SofaRpcProtocol method decodeResponse.

@Override
public RpcResponse decodeResponse(Object packet, ChannelHandlerContext ctx) throws Exception {
    SofaRpcDecodePacket responsePacket = (SofaRpcDecodePacket) packet;
    ByteBuf metaBuf = responsePacket.getMetaBuf();
    ByteBuf protoBuf = responsePacket.getProtoBuf();
    try {
        RpcResponse rpcResponse = new RpcResponse();
        SofaRpcProto.SofaRpcMeta responseMeta = (SofaRpcProto.SofaRpcMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcMetaInstance);
        Long correlationId = responseMeta.getSequenceId();
        rpcResponse.setCorrelationId(correlationId);
        ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
        RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
        if (future == null) {
            return rpcResponse;
        }
        rpcResponse.setRpcFuture(future);
        int compressType = getStandardCompressType(responseMeta.getCompressType());
        rpcResponse.setCompressType(compressType);
        try {
            if (responseMeta != null && responseMeta.getErrorCode() == 0) {
                Compress compress = compressManager.getCompress(compressType);
                Object result = compress.uncompressOutput(protoBuf, future.getRpcMethodInfo());
                rpcResponse.setResult(result);
            } else {
                rpcResponse.setException(new RpcException(RpcException.SERVICE_EXCEPTION, responseMeta.getReason()));
            }
        } catch (Exception ex) {
            LOG.warn("decode response failed");
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "decode response failed", ex);
        }
        return rpcResponse;
    } finally {
        if (metaBuf != null) {
            metaBuf.release();
        }
        if (protoBuf != null) {
            protoBuf.release();
        }
    }
}
Also used : Compress(com.baidu.brpc.compress.Compress) ChannelInfo(com.baidu.brpc.ChannelInfo) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcResponse(com.baidu.brpc.protocol.RpcResponse) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) RpcException(com.baidu.brpc.exceptions.RpcException) RpcFuture(com.baidu.brpc.client.RpcFuture)

Example 5 with ChannelInfo

use of com.baidu.brpc.ChannelInfo in project brpc-java by baidu.

the class HttpRpcProtocol method decodeResponse.

@Override
public Response decodeResponse(Object msg, ChannelHandlerContext ctx) {
    FullHttpResponse httpResponse = (FullHttpResponse) msg;
    try {
        ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
        Long correlationId = parseCorrelationId(httpResponse.headers().get(CORRELATION_ID), channelInfo.getCorrelationId());
        HttpResponse response = new HttpResponse();
        response.setCorrelationId(correlationId);
        RpcFuture future = channelInfo.removeRpcFuture(response.getCorrelationId());
        if (future == null) {
            return response;
        }
        response.setRpcFuture(future);
        int bodyLen = httpResponse.content().readableBytes();
        byte[] bytes = new byte[bodyLen];
        httpResponse.content().readBytes(bytes);
        if (!httpResponse.status().equals(HttpResponseStatus.OK)) {
            String body = new String(bytes);
            String message = String.format("http status=%d, message=%s", httpResponse.status().code(), body);
            LOG.warn("{}", message);
            response.setException(new RpcException(RpcException.SERVICE_EXCEPTION, message));
            return response;
        }
        String contentTypeAndEncoding = httpResponse.headers().get(HttpHeaderNames.CONTENT_TYPE).toLowerCase();
        String[] splits = StringUtils.split(contentTypeAndEncoding, ";");
        int protocolType = HttpRpcProtocol.parseProtocolType(splits[0]);
        String encoding = this.encoding;
        // 由于uc服务返回的encoding是错误的,所以这里以client端设置的encoding为准。
        // for (String split : splits) {
        // split = split.trim();
        // if (split.startsWith("charset=")) {
        // encoding = split.substring("charset=".length());
        // }
        // }
        Object body = null;
        if (bodyLen != 0) {
            try {
                body = decodeBody(protocolType, encoding, bytes);
            } catch (Exception ex) {
                LOG.error("decode response body failed");
                response.setException(ex);
                return response;
            }
        }
        if (body != null) {
            try {
                response.setResult(parseHttpResponse(body, future.getRpcMethodInfo()));
            } catch (Exception ex) {
                LOG.error("failed to parse result from HTTP body");
                response.setException(ex);
            }
        } else {
            response.setResult(null);
        }
        // set response attachment
        if (response.getKvAttachment() == null) {
            response.setKvAttachment(new HashMap<String, Object>());
        }
        for (Map.Entry<String, String> entry : httpResponse.headers()) {
            response.getKvAttachment().put(entry.getKey(), entry.getValue());
        }
        return response;
    } finally {
        httpResponse.release();
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(com.baidu.brpc.protocol.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ChannelInfo(com.baidu.brpc.ChannelInfo) RpcException(com.baidu.brpc.exceptions.RpcException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) RpcException(com.baidu.brpc.exceptions.RpcException) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) RpcFuture(com.baidu.brpc.client.RpcFuture) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ChannelInfo (com.baidu.brpc.ChannelInfo)19 RpcException (com.baidu.brpc.exceptions.RpcException)13 RpcFuture (com.baidu.brpc.client.RpcFuture)10 BadSchemaException (com.baidu.brpc.exceptions.BadSchemaException)9 NotEnoughDataException (com.baidu.brpc.exceptions.NotEnoughDataException)9 TooBigDataException (com.baidu.brpc.exceptions.TooBigDataException)9 ByteBuf (io.netty.buffer.ByteBuf)8 RpcResponse (com.baidu.brpc.protocol.RpcResponse)7 DynamicCompositeByteBuf (com.baidu.brpc.buffer.DynamicCompositeByteBuf)6 Compress (com.baidu.brpc.compress.Compress)4 Channel (io.netty.channel.Channel)4 Timeout (io.netty.util.Timeout)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Hessian2Input (com.alibaba.com.caucho.hessian.io.Hessian2Input)1 BrpcChannel (com.baidu.brpc.client.channel.BrpcChannel)1 HttpResponse (com.baidu.brpc.protocol.HttpResponse)1 Response (com.baidu.brpc.protocol.Response)1 PublicPbrpcResponse (com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.PublicPbrpcResponse)1