use of com.baidu.brpc.protocol.RpcResponse 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();
}
}
}
use of com.baidu.brpc.protocol.RpcResponse 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;
}
use of com.baidu.brpc.protocol.RpcResponse 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;
}
use of com.baidu.brpc.protocol.RpcResponse in project brpc-java by baidu.
the class DefaultServerPushProtocol method decodeServerPushResponse.
/**
* decode 从客户端返回的 serverPushResponse
*
* @param in
* @param ctx
*
* @return
*
* @throws Exception
*/
public Response decodeServerPushResponse(Object in, ChannelHandlerContext ctx) {
DefaultServerPushPacket packet = (DefaultServerPushPacket) in;
RpcResponse rpcResponse = new RpcResponse();
Long correlationId = packet.getSpHead().getCorrelationId();
RpcFuture future = PushServerRpcFutureManager.getInstance().removeRpcFuture(correlationId);
rpcResponse.setCorrelationId(correlationId);
if (future == null) {
return rpcResponse;
}
rpcResponse.setRpcFuture(future);
ByteBuf bodyBuf = packet.getBodyBuf();
SPBody spBody = decodeBodyByteBuf(bodyBuf);
Object responseBody = spBody.getContent();
if (responseBody == null) {
return null;
}
rpcResponse.setResult(responseBody);
return rpcResponse;
}
use of com.baidu.brpc.protocol.RpcResponse 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();
}
}
}
Aggregations