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);
}
}
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();
}
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);
}
}
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;
}
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;
}
Aggregations