Search in sources :

Example 1 with DynamicChannelBuffer

use of com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer in project dubbo by alibaba.

the class GrizzlyCodecAdapter method handleRead.

@Override
public NextAction handleRead(FilterChainContext context) throws IOException {
    Object message = context.getMessage();
    Connection<?> connection = context.getConnection();
    Channel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        if (message instanceof Buffer) {
            // receive a new packet
            // buffer
            Buffer grizzlyBuffer = (Buffer) message;
            ChannelBuffer frame;
            if (previousData.readable()) {
                if (previousData instanceof DynamicChannelBuffer) {
                    previousData.writeBytes(grizzlyBuffer.toByteBuffer());
                    frame = previousData;
                } else {
                    int size = previousData.readableBytes() + grizzlyBuffer.remaining();
                    frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize);
                    frame.writeBytes(previousData, previousData.readableBytes());
                    frame.writeBytes(grizzlyBuffer.toByteBuffer());
                }
            } else {
                frame = ChannelBuffers.wrappedBuffer(grizzlyBuffer.toByteBuffer());
            }
            Object msg;
            int savedReadIndex;
            do {
                savedReadIndex = frame.readerIndex();
                try {
                    msg = codec.decode(channel, frame);
                } catch (Exception e) {
                    previousData = ChannelBuffers.EMPTY_BUFFER;
                    throw new IOException(e.getMessage(), e);
                }
                if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) {
                    frame.readerIndex(savedReadIndex);
                    return context.getStopAction();
                } else {
                    if (savedReadIndex == frame.readerIndex()) {
                        previousData = ChannelBuffers.EMPTY_BUFFER;
                        throw new IOException("Decode without read data.");
                    }
                    if (msg != null) {
                        context.setMessage(msg);
                        return context.getInvokeAction();
                    } else {
                        return context.getInvokeAction();
                    }
                }
            } while (frame.readable());
        } else {
            // Other events are passed down directly
            return context.getInvokeAction();
        }
    } finally {
        GrizzlyChannel.removeChannelIfDisconnectd(connection);
    }
}
Also used : Buffer(org.glassfish.grizzly.Buffer) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer) Channel(com.alibaba.dubbo.remoting.Channel) DynamicChannelBuffer(com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer) IOException(java.io.IOException) IOException(java.io.IOException) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer)

Aggregations

Channel (com.alibaba.dubbo.remoting.Channel)1 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)1 DynamicChannelBuffer (com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer)1 IOException (java.io.IOException)1 Buffer (org.glassfish.grizzly.Buffer)1