Search in sources :

Example 61 with ChannelBuffer

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;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) RpcRequest(org.msec.rpc.RpcRequest) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 62 with ChannelBuffer

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;
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 63 with ChannelBuffer

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;
}
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 64 with ChannelBuffer

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);
}
Also used : DynamicChannelBuffer(org.jboss.netty.buffer.DynamicChannelBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DynamicChannelBuffer(org.jboss.netty.buffer.DynamicChannelBuffer)

Example 65 with ChannelBuffer

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;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) RpcResponse(org.msec.rpc.RpcResponse) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)494 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)70 Test (org.junit.Test)67 DeviceSession (org.traccar.DeviceSession)64 Position (org.traccar.model.Position)62 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)61 Test (org.testng.annotations.Test)49 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)48 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)44 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)37 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)34 ChannelFuture (org.jboss.netty.channel.ChannelFuture)28 Checkpoint (com.linkedin.databus.core.Checkpoint)27 ByteBuffer (java.nio.ByteBuffer)27 RecoverablePduException (com.cloudhopper.smpp.type.RecoverablePduException)26 UnrecoverablePduException (com.cloudhopper.smpp.type.UnrecoverablePduException)26 DateBuilder (org.traccar.helper.DateBuilder)26 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 IOException (java.io.IOException)23 ArrayList (java.util.ArrayList)23