Search in sources :

Example 6 with Buffer

use of org.glassfish.grizzly.Buffer 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)

Example 7 with Buffer

use of org.glassfish.grizzly.Buffer in project dubbo by alibaba.

the class GrizzlyCodecAdapter method handleWrite.

@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
    Connection<?> connection = context.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        // Do not need to close
        ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(1024);
        Object msg = context.getMessage();
        codec.encode(channel, channelBuffer, msg);
        GrizzlyChannel.removeChannelIfDisconnected(connection);
        Buffer buffer = connection.getTransport().getMemoryManager().allocate(channelBuffer.readableBytes());
        buffer.put(channelBuffer.toByteBuffer());
        buffer.flip();
        buffer.allowBufferDispose(true);
        context.setMessage(buffer);
    } finally {
        GrizzlyChannel.removeChannelIfDisconnected(connection);
    }
    return context.getInvokeAction();
}
Also used : ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Buffer(org.glassfish.grizzly.Buffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)

Example 8 with Buffer

use of org.glassfish.grizzly.Buffer 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)

Example 9 with Buffer

use of org.glassfish.grizzly.Buffer in project Payara by payara.

the class Request method readPostBody.

// END SJSAS 6346738
/**
 * Read post body in an array.
 */
protected int readPostBody(byte[] body, int len) throws IOException {
    Buffer b = coyoteRequest.getPostBody(len).duplicate();
    final int length = b.limit() - b.position();
    b.get(body, b.position(), length);
    return length;
}
Also used : Buffer(org.glassfish.grizzly.Buffer)

Example 10 with Buffer

use of org.glassfish.grizzly.Buffer in project Payara by payara.

the class XProtocolFinder method find.

@Override
public Result find(PUContext puContext, FilterChainContext ctx) {
    final Buffer buffer = ctx.getMessage();
    if (buffer.remaining() < signature.length) {
        return Result.NEED_MORE_DATA;
    }
    final int start = buffer.position();
    for (int i = 0; i < signature.length; i++) {
        if (buffer.get(i + start) != signature[i]) {
            return Result.NOT_FOUND;
        }
    }
    return Result.FOUND;
}
Also used : Buffer(org.glassfish.grizzly.Buffer)

Aggregations

Buffer (org.glassfish.grizzly.Buffer)11 IOException (java.io.IOException)4 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)2 DynamicChannelBuffer (com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer)2 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)2 DynamicChannelBuffer (org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)2 Connection (org.glassfish.grizzly.Connection)2 Channel (com.alibaba.dubbo.remoting.Channel)1 RemoteRestAdminCommand (com.sun.enterprise.admin.remote.RemoteRestAdminCommand)1 GrizzlyProxy (com.sun.enterprise.v3.services.impl.GrizzlyProxy)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 SocketChannel (java.nio.channels.SocketChannel)1 LoginException (javax.security.auth.login.LoginException)1 LifecycleException (org.apache.catalina.LifecycleException)1 Channel (org.apache.dubbo.remoting.Channel)1 PayloadFilesManager (org.glassfish.admin.payload.PayloadFilesManager)1 AdminCommand (org.glassfish.api.admin.AdminCommand)1