List of usage examples for io.netty.util ReferenceCountUtil release
public static boolean release(Object msg)
From source file:io.gatling.http.client.impl.HttpAppHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (isInactive()) { return;/*from w w w .ja v a2s .c o m*/ } LOGGER.debug("Read msg='{}'", msg); try { if (msg instanceof HttpResponse) { httpResponseReceived = true; HttpResponse response = (HttpResponse) msg; if (exitOnDecodingFailure(ctx, response)) { return; } tx.listener.onHttpResponse(response.status(), response.headers()); tx.closeConnection = tx.closeConnection && HttpUtils.isConnectionClose(response.headers()); } else if (msg instanceof HttpContent) { HttpContent chunk = (HttpContent) msg; if (exitOnDecodingFailure(ctx, chunk)) { return; } boolean last = chunk instanceof LastHttpContent; // making a local copy because setInactive might be called (on last) HttpTx tx = this.tx; if (last) { tx.requestTimeout.cancel(); setInactive(); if (tx.closeConnection) { ctx.channel().close(); } else { channelPool.offer(ctx.channel()); } } try { tx.listener.onHttpResponseBodyChunk(chunk.content(), last); } catch (Exception e) { // can't let exceptionCaught handle this because setInactive might have been called (on last) crash0(ctx, e, true, tx); throw e; } } } finally { ReferenceCountUtil.release(msg); } }
From source file:io.grpc.netty.NettyServerHandler.java
License:Apache License
/** * Handler for commands sent from the stream. *//*from w w w. j a v a 2s. co m*/ @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof SendGrpcFrameCommand) { sendGrpcFrame(ctx, (SendGrpcFrameCommand) msg, promise); } else if (msg instanceof SendResponseHeadersCommand) { sendResponseHeaders(ctx, (SendResponseHeadersCommand) msg, promise); } else if (msg instanceof CancelServerStreamCommand) { cancelStream(ctx, (CancelServerStreamCommand) msg, promise); } else if (msg instanceof ForcefulCloseCommand) { forcefulClose(ctx, (ForcefulCloseCommand) msg, promise); } else { AssertionError e = new AssertionError("Write called for unexpected type: " + msg.getClass().getName()); ReferenceCountUtil.release(msg); promise.setFailure(e); throw e; } }
From source file:io.grpc.netty.WriteBufferingAndExceptionHandler.java
License:Apache License
/** * Buffers the write until either {@link #writeBufferedAndRemove(ChannelHandlerContext)} is * called, or we have somehow failed. If we have already failed in the past, then the write * will fail immediately./*from ww w. j av a 2s . c o m*/ */ @Override @SuppressWarnings("FutureReturnValueIgnored") public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { if (failCause != null) { promise.setFailure(failCause); ReferenceCountUtil.release(msg); } else { bufferedWrites.add(new ChannelWrite(msg, promise)); } }
From source file:io.grpc.netty.WriteBufferingAndExceptionHandler.java
License:Apache License
/** * Propagate failures to all buffered writes. *///from ww w.j a v a2s .c o m @SuppressWarnings("FutureReturnValueIgnored") private void failWrites(Throwable cause) { if (failCause == null) { failCause = cause; } else { logger.log(Level.FINE, "Ignoring duplicate failure", cause); } while (!bufferedWrites.isEmpty()) { ChannelWrite write = bufferedWrites.poll(); write.promise.setFailure(cause); ReferenceCountUtil.release(write.msg); } }
From source file:io.hekate.network.netty.NettyWriteQueue.java
License:Apache License
public NettyWriteQueue(boolean writable, NettySpy spy) { this.writable = writable ? WRITABLE_ON : WRITABLE_OFF; flushTask = () -> {/* www .ja v a2 s . c o m*/ flushScheduled.set(false); DeferredMessage lastNonFlushed = null; int cnt = 0; for (DeferredMessage msg = queue.poll(); msg != null; msg = queue.poll()) { Throwable err = this.alwaysFails; if (err == null && spy != null) { try { spy.onBeforeFlush(msg.source()); } catch (Throwable t) { err = t; } } if (err == null) { msg.channel().write(msg, msg.promise()); lastNonFlushed = msg; cnt++; if (cnt == MAX_FLUSH_BATCH_SIZE) { lastNonFlushed.channel().flush(); lastNonFlushed = null; cnt = 0; } } else if (msg.promise().tryFailure(err)) { if (msg.isPreEncoded()) { ReferenceCountUtil.release(msg); } } } if (lastNonFlushed != null) { lastNonFlushed.channel().flush(); } }; }
From source file:io.jsync.http.impl.ClientConnection.java
License:Open Source License
NetSocket createNetSocket() { // connection was upgraded to raw TCP socket upgradedConnection = true;/* ww w. j av a 2 s . c om*/ DefaultNetSocket socket = new DefaultNetSocket(async, channel, context, client.tcpHelper, true); Map<Channel, DefaultNetSocket> connectionMap = new HashMap<Channel, DefaultNetSocket>(1); connectionMap.put(channel, socket); // Flush out all pending data endReadAndFlush(); // remove old http handlers and replace the old handler with one that handle plain sockets ChannelPipeline pipeline = channel.pipeline(); ChannelHandler inflater = pipeline.get(HttpContentDecompressor.class); if (inflater != null) { pipeline.remove(inflater); } pipeline.remove("codec"); pipeline.replace("handler", "handler", new AsyncNetHandler(client.async, connectionMap) { @Override public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception { // remove from the real mapping client.connectionMap.remove(channel); super.exceptionCaught(chctx, t); } @Override public void channelInactive(ChannelHandlerContext chctx) throws Exception { // remove from the real mapping client.connectionMap.remove(channel); super.channelInactive(chctx); } @Override public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception { if (msg instanceof HttpContent) { ReferenceCountUtil.release(msg); return; } super.channelRead(chctx, msg); } }); return socket; }
From source file:io.jsync.http.impl.ServerConnection.java
License:Open Source License
NetSocket createNetSocket() { DefaultNetSocket socket = new DefaultNetSocket(async, channel, context, server.tcpHelper, false); Map<Channel, DefaultNetSocket> connectionMap = new HashMap<Channel, DefaultNetSocket>(1); connectionMap.put(channel, socket);/*from w w w . j a v a2 s. com*/ // Flush out all pending data endReadAndFlush(); // remove old http handlers and replace the old handler with one that handle plain sockets ChannelPipeline pipeline = channel.pipeline(); ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class); if (compressor != null) { pipeline.remove(compressor); } pipeline.remove("httpDecoder"); if (pipeline.get("chunkedWriter") != null) { pipeline.remove("chunkedWriter"); } channel.pipeline().replace("handler", "handler", new AsyncNetHandler(server.async, connectionMap) { @Override public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception { // remove from the real mapping server.connectionMap.remove(channel); super.exceptionCaught(chctx, t); } @Override public void channelInactive(ChannelHandlerContext chctx) throws Exception { // remove from the real mapping server.connectionMap.remove(channel); super.channelInactive(chctx); } @Override public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception { if (msg instanceof HttpContent) { ReferenceCountUtil.release(msg); return; } super.channelRead(chctx, msg); } }); // check if the encoder can be removed yet or not. if (lastWriteFuture == null) { channel.pipeline().remove("httpEncoder"); } else { lastWriteFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { channel.pipeline().remove("httpEncoder"); } }); } return socket; }
From source file:io.maelstorm.server.RequestHandler.java
License:Open Source License
private void sendResponse(final ChannelHandlerContext ctx, final FullHttpResponse response, final HttpRequest request) { ReferenceCountUtil.release(request); if (!ctx.channel().isActive()) { return;/*from w ww . jav a 2 s . com*/ } final boolean keepalive = HttpHeaders.isKeepAlive(request); if (keepalive) { HttpHeaders.setContentLength(response, response.content().readableBytes()); response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } final ChannelFuture future = ctx.write(response); ctx.flush(); if (!keepalive) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:io.netty.example.mqtt.heartBeat.MqttHeartBeatBrokerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { MqttMessage mqttMessage = (MqttMessage) msg; System.out.println("Received MQTT message: " + mqttMessage); switch (mqttMessage.fixedHeader().messageType()) { case CONNECT: MqttFixedHeader connackFixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0); MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader( MqttConnectReturnCode.CONNECTION_ACCEPTED, false); MqttConnAckMessage connack = new MqttConnAckMessage(connackFixedHeader, mqttConnAckVariableHeader); ctx.writeAndFlush(connack);//from w w w.java2 s. c om break; case PINGREQ: MqttFixedHeader pingreqFixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0); MqttMessage pingResp = new MqttMessage(pingreqFixedHeader); ctx.writeAndFlush(pingResp); break; case DISCONNECT: ctx.close(); break; default: System.out.println("Unexpected message type: " + mqttMessage.fixedHeader().messageType()); ReferenceCountUtil.release(msg); ctx.close(); } }
From source file:io.netty.example.mqtt.heartBeat.MqttHeartBeatClientHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // discard all messages ReferenceCountUtil.release(msg); }