List of usage examples for io.netty.channel ChannelHandlerContext alloc
ByteBufAllocator alloc();
From source file:org.quartzpowered.network.pipeline.CompressionHandler.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { Buffer prefixBuf = new Buffer(ctx.alloc().buffer(5)); ByteBuf contentsBuf;/*ww w . ja v a 2 s . c o m*/ if (msg.readableBytes() >= threshold) { // message should be compressed int index = msg.readerIndex(); int length = msg.readableBytes(); byte[] sourceData = new byte[length]; msg.readBytes(sourceData); deflater.setInput(sourceData); deflater.finish(); byte[] compressedData = new byte[length]; int compressedLength = deflater.deflate(compressedData); deflater.reset(); if (compressedLength == 0) { // compression failed in some weird way throw new EncoderException("Failed to compress message of size " + length); } else if (compressedLength >= length) { // compression increased the size. threshold is probably too low // send as an uncompressed packet prefixBuf.writeVarInt(0); msg.readerIndex(index); msg.retain(); contentsBuf = msg; } else { // all is well prefixBuf.writeVarInt(length); contentsBuf = Unpooled.wrappedBuffer(compressedData, 0, compressedLength); } } else { // message should be sent through prefixBuf.writeVarInt(0); msg.retain(); contentsBuf = msg; } out.add(Unpooled.wrappedBuffer(prefixBuf, contentsBuf)); }
From source file:org.quartzpowered.network.pipeline.CompressionHandler.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { Buffer buffer = new Buffer(msg); int index = buffer.readerIndex(); int uncompressedSize = buffer.readVarInt(); if (uncompressedSize == 0) { // message is uncompressed int length = buffer.readableBytes(); if (length >= threshold) { // invalid throw new DecoderException( "Received uncompressed message of size " + length + " greater than threshold " + threshold); }/* w w w . j a v a 2 s.c o m*/ ByteBuf buf = ctx.alloc().buffer(length); buffer.readBytes(buf, length); out.add(buf); } else { // message is compressed byte[] sourceData = new byte[buffer.readableBytes()]; buffer.readBytes(sourceData); inflater.setInput(sourceData); byte[] destData = new byte[uncompressedSize]; int resultLength = inflater.inflate(destData); inflater.reset(); if (resultLength == 0) { // might be a leftover from before compression was enabled (no compression header) // uncompressedSize is likely to be < threshold buffer.readerIndex(index); buffer.retain(); out.add(buffer); } else if (resultLength != uncompressedSize) { throw new DecoderException("Received compressed message claiming to be of size " + uncompressedSize + " but actually " + resultLength); } else { out.add(Unpooled.wrappedBuffer(destData)); } } }
From source file:org.rzo.netty.ahessian.heartbeat.ClientHeartbeatHandlerOutbound.java
License:Apache License
@Override public void timedOut(ChannelHandlerContext ctx) { Constants.ahessianLogger/*from w ww .ja v a2 s. c om*/ .info("no writes since " + new Date(_handler.getLastCalled()) + " -> send empty buffer heartbeat"); ByteBuf b = ctx.alloc().buffer(1); b.writeByte(0); ChannelFuture f = ctx.writeAndFlush(b); try { f.await(); } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:org.rzo.netty.ahessian.session.ClientSessionFilter.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { String id = _session == null ? "?" : _session.getId(); // send the session id to server ctx.writeAndFlush(ctx.alloc().buffer().writeBytes(id.getBytes())); }
From source file:org.spongepowered.clean.network.netty.PacketDecryptor.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { int length = msg.readableBytes(); if (this.dataBuffer.length < length) { this.dataBuffer = new byte[length]; }/*from ww w . j av a2s. c o m*/ msg.readBytes(this.dataBuffer, 0, length); ByteBuf plain = ctx.alloc().heapBuffer(this.cipher.getOutputSize(length)); int actual = this.cipher.update(this.dataBuffer, 0, length, plain.array(), plain.arrayOffset()); plain.writerIndex(actual); out.add(plain); }
From source file:org.teiid.transport.ObjectEncoder.java
License:Apache License
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, int estimatedSize, boolean preferDirect) throws Exception { if (preferDirect) { return ctx.alloc().ioBuffer(estimatedSize); } else {//from w w w . j a va2 s. co m return ctx.alloc().heapBuffer(estimatedSize); } }
From source file:org.wildfly.swarm.arquillian.daemon.server.Server.java
License:Apache License
private static ChannelFuture sendResponse(final ChannelHandlerContext ctx, final String response) { ByteBuf buf = ctx.alloc().buffer(); buf.writeBytes(response.getBytes(WireProtocol.CHARSET)); ctx.write(buf);/*from www . j a va 2 s . c o m*/ return ctx.writeAndFlush(Delimiters.lineDelimiter()[0]); }
From source file:org.wso2.esb.integration.common.utils.servers.http2.Http1Handler.java
License:Open Source License
@Override public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { if (HttpUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }/*from w ww .ja va2 s . c o m*/ boolean keepAlive = HttpUtil.isKeepAlive(req); ByteBuf content = ctx.alloc().buffer(); content.writeBytes(Http2Handler.DATA_RESPONSE.duplicate()); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/xml"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.writeAndFlush(response); } }
From source file:org.wso2.esb.integration.common.utils.servers.http2.Http2Handler.java
License:Open Source License
public void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception { if (data.isEndStream()) { ByteBuf content = ctx.alloc().buffer(); content.writeBytes(DATA_RESPONSE.duplicate()); Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText()); headers.add(HttpHeaderNames.CONTENT_TYPE, "text/xml"); ctx.write(new DefaultHttp2HeadersFrame(headers)); ctx.writeAndFlush(new DefaultHttp2DataFrame(content, true)); }/*from ww w.j a v a2 s . c o m*/ }
From source file:p2p_server.AppHandler.java
private void write_error(ChannelHandlerContext ctx, byte[] arry) { ByteBuf sendbuf = ctx.alloc().heapBuffer(arry.length); sendbuf.writeBytes(arry);// w ww. j a va 2 s .co m ChannelFuture write = ctx.writeAndFlush(arry); /* if (!write.isSuccess()) { System.out.println("send unkown failed: " + write.cause()); } */ write.addListener(ChannelFutureListener.CLOSE); ctx.close(); }