use of org.jboss.netty.buffer.ChannelBuffer in project MSEC by Tencent.
the class RequestEncoder method encode.
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (msg instanceof RpcRequest) {
RpcRequest message = (RpcRequest) msg;
try {
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(estimatedLength, ctx.getChannel().getConfig().getBufferFactory()));
//Set proto flag = 0
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_FLAG, new Integer(0));
ChannelBuffer ret = encode(ctx, message, bout);
return ret;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
throw ex;
}
}
if (msg instanceof Object[]) {
Object[] objects = (Object[]) msg;
//Set proto flag=1
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_FLAG, new Integer(1));
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_CODEC, objects[1]);
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_SEQUENCE, objects[2]);
if (objects[0] instanceof byte[]) {
byte[] msgData = (byte[]) objects[0];
int tmpLength = estimatedLength;
if (msgData.length > tmpLength) {
tmpLength = msgData.length;
}
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(tmpLength, ctx.getChannel().getConfig().getBufferFactory()));
bout.write(msgData);
return bout.buffer();
} else {
return objects[0];
}
}
return null;
}
use of org.jboss.netty.buffer.ChannelBuffer in project MSEC by Tencent.
the class RequestEncoder method encode.
protected ChannelBuffer encode(ChannelHandlerContext channelHandlerContext, RpcRequest rpcRequest, ChannelBufferOutputStream stream) throws IOException {
stream.writeByte('(');
stream.writeLong(0);
int headLen = encodeHead(rpcRequest, stream);
int bodyLen = encodeBody(rpcRequest, stream);
stream.writeByte(')');
ChannelBuffer buffer = stream.buffer();
buffer.setInt(1, headLen);
buffer.setInt(5, bodyLen);
return buffer;
}
use of org.jboss.netty.buffer.ChannelBuffer 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;
}
use of org.jboss.netty.buffer.ChannelBuffer in project MSEC by Tencent.
the class ResponseDecoder method setAttachment.
private void setAttachment(ChannelHandlerContext ctx, Channel channel, ChannelBuffer cb, int lastReadIndex) {
cb.readerIndex(lastReadIndex);
if (!(cb instanceof DynamicChannelBuffer) || cb.writerIndex() > 102400) {
ChannelBuffer db = ChannelBuffers.dynamicBuffer(cb.readableBytes() * 2, channel.getConfig().getBufferFactory());
db.writeBytes(cb);
cb = db;
}
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_BYTEBUFFER, cb);
}
use of org.jboss.netty.buffer.ChannelBuffer 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;
}
Aggregations