Search in sources :

Example 1 with DynamicChannelBuffer

use of org.apache.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();
                }
            } while (frame.readable());
        } else {
            // Other events are passed down directly
            return context.getInvokeAction();
        }
    } finally {
        GrizzlyChannel.removeChannelIfDisconnected(connection);
    }
}
Also used : ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Buffer(org.glassfish.grizzly.Buffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer) Channel(org.apache.dubbo.remoting.Channel) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer) IOException(java.io.IOException) IOException(java.io.IOException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)

Aggregations

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