Search in sources :

Example 1 with ChannelBufferInputStream

use of org.apache.dubbo.remoting.buffer.ChannelBufferInputStream in project dubbo by alibaba.

the class TransportCodec method decode.

@Override
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    InputStream input = new ChannelBufferInputStream(buffer);
    ObjectInput objectInput = getSerialization(channel).deserialize(channel.getUrl(), input);
    Object object = decodeData(channel, objectInput);
    if (objectInput instanceof Cleanable) {
        ((Cleanable) objectInput).cleanup();
    }
    return object;
}
Also used : ChannelBufferInputStream(org.apache.dubbo.remoting.buffer.ChannelBufferInputStream) InputStream(java.io.InputStream) ChannelBufferInputStream(org.apache.dubbo.remoting.buffer.ChannelBufferInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Cleanable(org.apache.dubbo.common.serialize.Cleanable)

Example 2 with ChannelBufferInputStream

use of org.apache.dubbo.remoting.buffer.ChannelBufferInputStream in project dubbo by alibaba.

the class ThriftCodec method decode.

@Override
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int available = buffer.readableBytes();
    if (available < MESSAGE_SHORTEST_LENGTH) {
        return DecodeResult.NEED_MORE_INPUT;
    } else {
        TIOStreamTransport transport = new TIOStreamTransport(new ChannelBufferInputStream(buffer));
        TBinaryProtocol protocol = new TBinaryProtocol(transport);
        short magic;
        int messageLength;
        try {
            // protocol.readI32(); // skip the first message length
            byte[] bytes = new byte[4];
            transport.read(bytes, 0, 4);
            magic = protocol.readI16();
            messageLength = protocol.readI32();
        } catch (TException e) {
            throw new IOException(e.getMessage(), e);
        }
        if (MAGIC != magic) {
            throw new IOException("Unknown magic code " + magic);
        }
        if (available < messageLength) {
            return DecodeResult.NEED_MORE_INPUT;
        }
        return decode(protocol);
    }
}
Also used : TException(org.apache.thrift.TException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) ChannelBufferInputStream(org.apache.dubbo.remoting.buffer.ChannelBufferInputStream) IOException(java.io.IOException)

Example 3 with ChannelBufferInputStream

use of org.apache.dubbo.remoting.buffer.ChannelBufferInputStream in project dubbo by alibaba.

the class ExchangeCodec method decode.

@Override
protected Object decode(Channel channel, ChannelBuffer buffer, int readable, byte[] header) throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH || readable > 1 && header[1] != MAGIC_LOW) {
        int length = header.length;
        if (header.length < readable) {
            header = Bytes.copyOf(header, readable);
            buffer.readBytes(header, length, readable - length);
        }
        for (int i = 1; i < header.length - 1; i++) {
            if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
                buffer.readerIndex(buffer.readerIndex() - header.length + i);
                header = Bytes.copyOf(header, i);
                break;
            }
        }
        return super.decode(channel, buffer, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
        return DecodeResult.NEED_MORE_INPUT;
    }
    // get data length.
    int len = Bytes.bytes2int(header, 12);
    // When receiving response, how to exceed the length, then directly construct a response to the client.
    // see more detail from https://github.com/apache/dubbo/issues/7021.
    Object obj = finishRespWhenOverPayload(channel, len, header);
    if (null != obj) {
        return obj;
    }
    checkPayload(channel, len);
    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
        return DecodeResult.NEED_MORE_INPUT;
    }
    // limit input stream.
    ChannelBufferInputStream is = new ChannelBufferInputStream(buffer, len);
    try {
        return decodeBody(channel, is, header);
    } finally {
        if (is.available() > 0) {
            try {
                if (logger.isWarnEnabled()) {
                    logger.warn("Skip input stream " + is.available());
                }
                StreamUtils.skipUnusedStream(is);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}
Also used : ChannelBufferInputStream(org.apache.dubbo.remoting.buffer.ChannelBufferInputStream) IOException(java.io.IOException)

Aggregations

ChannelBufferInputStream (org.apache.dubbo.remoting.buffer.ChannelBufferInputStream)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)1 Cleanable (org.apache.dubbo.common.serialize.Cleanable)1 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)1 TException (org.apache.thrift.TException)1 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)1 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)1