use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.
the class HttpApiServerHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
if (e.getCause() instanceof ClosedChannelException) {
return;
}
String errorid = UUID.randomUUID().toString();
logger.error("ERROR HTTP: {}\n", errorid, e.getCause());
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
httpResponse.setHeader("id", errorid);
if (logger.isDebugEnabled()) {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
PrintWriter printWriter = new PrintWriter(new ChannelBufferOutputStream(channelBuffer));
e.getCause().printStackTrace(printWriter);
printWriter.close();
httpResponse.setContent(channelBuffer);
}
ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
Channel ch = e.getChannel();
ch.close();
}
use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.
the class Nfs3Utils method writeChannelCommit.
public static void writeChannelCommit(Channel channel, XDR out, int xid) {
if (RpcProgramNfs3.LOG.isDebugEnabled()) {
RpcProgramNfs3.LOG.debug("Commit done:" + xid);
}
ChannelBuffer outBuf = XDR.writeMessageTcp(out, true);
channel.write(outBuf);
}
use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.
the class TestFrameDecoder method testSingleFrame.
@Test
public void testSingleFrame() {
RpcFrameDecoder decoder = new RpcFrameDecoder();
// Test "Length field is not received yet"
ByteBuffer buffer = ByteBuffer.allocate(1);
ChannelBuffer buf = new ByteBufferBackedChannelBuffer(buffer);
ChannelBuffer channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
assertTrue(channelBuffer == null);
// Test all bytes are not received yet
byte[] fragment = new byte[4 + 9];
// final fragment
fragment[0] = (byte) (1 << 7);
fragment[1] = 0;
fragment[2] = 0;
// fragment size = 10 bytes
fragment[3] = (byte) 10;
assertTrue(XDR.isLastFragment(fragment));
assertTrue(XDR.fragmentSize(fragment) == 10);
buffer = ByteBuffer.allocate(4 + 9);
buffer.put(fragment);
buffer.flip();
buf = new ByteBufferBackedChannelBuffer(buffer);
channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
assertTrue(channelBuffer == null);
}
use of org.jboss.netty.buffer.ChannelBuffer in project MSEC by Tencent.
the class RequestDecoder method decode.
@Override
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, Object msg) throws Exception {
long receiveTime = System.currentTimeMillis();
if (!(msg instanceof ChannelBuffer)) {
return msg;
}
ChannelBuffer cb = (ChannelBuffer) NettyCodecUtils.getAttachment(channelHandlerContext, Constants.ATTACHMENT_BYTEBUFFER);
ChannelBuffer cb_ = (ChannelBuffer) msg;
if (cb == null) {
cb = cb_;
} else {
cb.writeBytes(cb_);
}
List<Object> messages = null;
int lastReadIndex = cb.readerIndex();
int deserializeMode = -1;
while (cb.readable()) {
byte stx = cb.readByte();
RpcRequest rpcRequest = null;
if (stx == (byte) '(') {
if (cb.readableBytes() < Constants.PKG_LEAST_LENGTH - 1) {
setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
break;
}
//Test whether protocol is protobuf
//Format: ( + dwHeadLength + dwBodyLength + strHead + strBody + )
int headLength = cb.readInt();
int bodyLength = cb.readInt();
if (cb.readableBytes() < headLength + bodyLength + 1) {
setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
break;
}
byte[] headBytes = new byte[headLength];
byte[] bodyBytes = new byte[bodyLength];
cb.readBytes(headBytes);
cb.readBytes(bodyBytes);
byte etx = cb.readByte();
if (etx != ')') {
log.error("Invalid package etx.");
throw new IllegalArgumentException("Request decode error: invalid package etx " + etx);
}
//parse protobuf package
rpcRequest = deserializeProtobufPackage(headBytes, bodyBytes);
rpcRequest.setSerializeMode(RpcRequest.SerializeMode.SERIALIZE_MODE_PROTOBUF);
} else {
//Test whether protocol is HTTP
cb.readerIndex(lastReadIndex);
int totalLength = cb.readableBytes();
byte[] totalBytes = new byte[totalLength];
cb.readBytes(totalBytes);
String total = new String(totalBytes);
int pos = total.indexOf("\r\n\r\n");
if (pos < 0) {
setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
break;
}
int contentLength = getHTTPContentLength(total.substring(0, pos + 4));
if (totalLength < pos + 4 + contentLength) {
setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
break;
}
cb.readerIndex(pos + 4 + contentLength);
//parse HTTP package
rpcRequest = deserializeHTTPPackage(total.substring(0, pos + 4 + contentLength));
rpcRequest.setSerializeMode(RpcRequest.SerializeMode.SERIALIZE_MODE_HTTP);
}
if (rpcRequest != null) {
if (messages == null) {
messages = new ArrayList<Object>();
}
messages.add(rpcRequest);
lastReadIndex = cb.readerIndex();
} else {
setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
break;
}
}
return messages;
}
use of org.jboss.netty.buffer.ChannelBuffer in project MSEC by Tencent.
the class RequestDecoder method setAttachment.
private void setAttachment(ChannelHandlerContext ctx, Channel channel, ChannelBuffer cb, int lastReadIndex) {
cb.readerIndex(lastReadIndex);
if (!(cb instanceof DynamicChannelBuffer) || cb.writerIndex() > 102400) {
ChannelBuffer db = ChannelBuffers.dynamicBuffer(cb.readableBytes() * 2, channel.getConfig().getBufferFactory());
db.writeBytes(cb);
cb = db;
}
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_BYTEBUFFER, cb);
}
Aggregations