Example usage for io.netty.channel ChannelHandlerContext isRemoved

List of usage examples for io.netty.channel ChannelHandlerContext isRemoved

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext isRemoved.

Prototype

boolean isRemoved();

Source Link

Document

Return true if the ChannelHandler which belongs to this context was removed from the ChannelPipeline .

Usage

From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java

License:Apache License

/**
 * Called once data should be decoded from the given {@link ByteBuf}. This method will call
 * {@link #decode(ChannelHandlerContext, ByteBuf, List)} as long as decoding should take place.
 *
 * @param ctx           the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to
 * @param in            the {@link ByteBuf} from which to read data
 * @param out           the {@link List} to which decoded messages should be added
 */// w w  w. j  a  v  a 2s.c  o m
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    try {
        while (in.isReadable()) {
            int outSize = out.size();
            int oldInputLength = in.readableBytes();
            decode(ctx, in, out);

            // Check if this handler was removed before try to continue the loop.
            // If it was removed it is not safe to continue to operate on the buffer
            //
            // See https://github.com/netty/netty/issues/1664
            if (ctx.isRemoved()) {
                break;
            }

            if (outSize == out.size()) {
                if (oldInputLength == in.readableBytes()) {
                    break;
                } else {
                    continue;
                }
            }

            if (oldInputLength == in.readableBytes()) {
                throw new DecoderException(StringUtil.simpleClassName(getClass())
                        + ".decode() did not read anything but decoded a message.");
            }

            if (isSingleDecode()) {
                break;
            }
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Throwable cause) {
        throw new DecoderException(cause);
    }
}

From source file:com.emin.igwmp.skm.core.netty.handler.SocksServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    distributeService.Distribute(ctx, JSON.parseObject(msg, ChiefEntity.class));
    if (!ctx.isRemoved()) {
        return;//from  ww  w. jav  a2s .  c om
    }
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    try {//from  www.  j  av a  2  s .  c  o m
        while (in.isReadable()) {
            int outSize = out.size();
            int oldInputLength = in.readableBytes();

            this.decode(ctx, in, out);

            // Check if this handler was removed before continuing the loop.
            // If it was removed, it is not safe to continue to operate on the buffer.
            //
            // See https://github.com/netty/netty/issues/1664
            if (ctx.isRemoved()) {
                break;
            }

            if (outSize == out.size()) {
                if (oldInputLength == in.readableBytes()) {
                    break;
                } else {
                    continue;
                }
            }

            if (oldInputLength == in.readableBytes()) {
                throw new DecoderException(StringUtil.simpleClassName(getClass())
                        + ".decode() did not read anything but decoded a message.");
            }

            if (this.isSingleDecode()) {
                break;
            }
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Throwable cause) {
        throw new DecoderException(cause);
    }
}

From source file:net.NettyEngine4.ServerHandler.java

License:Apache License

/**
 * Calls {@link io.netty.channel.ChannelHandlerContext#fireChannelInactive()} to forward
 * to the next {@link io.netty.channel.Channel} in the {@link io.netty.channel.ChannelPipeline}.
 *
 * Sub-classes may override this method to change behavior.
 *
 * ??/*www .  ja va 2 s.c o m*/
 */
@Override
public void channelInactive(ChannelHandlerContext ctx) {
    try {
        ctx.channel().close();
        //clear and store data!  channel closed!
        // this.player.saveAndDestory();

        countConnection.decrementAndGet();// -1

        if (logger.isDebugEnabled())
            logger.debug(ctx.channel().remoteAddress() + " channelClosed , Concurrent  connection... "
                    + countConnection.get());

        ChannelPipeline channelPipeline = ctx.channel().pipeline();

        if (null == channelPipeline) { // if null ,will be returned
            return;
        }
        //remove pipeline object
        while (channelPipeline.last() != null) {
            channelPipeline.removeLast();
        }
        if (ctx.isRemoved()) {
            ctx.close();
            // Set to null so the GC can collect them directly
            channelPipeline = null;
            ctx = null;
            player = null;
        }
    } catch (NoSuchElementException ee) {
        //do nothing
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.error("", e);
        //do nothing
    }
}

From source file:org.opendaylight.sxp.core.SxpConnection.java

License:Open Source License

/**
 * Gets ChannelHandlerContext according to specified type
 *
 * @param channelHandlerContextType Type of ChannelHandlerContext
 * @return ChannelHandlerContext marked with specified type
 * @throws ChannelHandlerContextNotFoundException    If ChannelHandlerContext isn't present
 * @throws ChannelHandlerContextDiscrepancyException If there are more ChannelHandlerContext,
 *                                                   that it used to be
 *//*from   w  ww  . j a  v a2 s.c o  m*/
public ChannelHandlerContext getChannelHandlerContext(ChannelHandlerContextType channelHandlerContextType)
        throws ChannelHandlerContextNotFoundException, ChannelHandlerContextDiscrepancyException {
    synchronized (ctxs) {
        if ((isModeBoth() && ctxs.size() > 2) || (!isModeBoth() && ctxs.size() > 1)) {
            LOG.warn(this + " Registered contexts: " + ctxs);
            throw new ChannelHandlerContextDiscrepancyException();
        }
        ChannelHandlerContext ctx = ctxs.get(channelHandlerContextType);
        if (ctx == null || ctx.isRemoved()) {
            throw new ChannelHandlerContextNotFoundException();
        }
        return ctx;
    }
}

From source file:org.opendaylight.sxp.core.SxpConnection.java

License:Open Source License

/**
 * @param channelHandlerContextType Type of ChannelHandlerContext
 * @return If ChannelHandlerContext is actively used
 *///from  w  w w  .ja va 2 s  .c o  m
public boolean hasChannelHandlerContext(ChannelHandlerContextType channelHandlerContextType) {
    synchronized (ctxs) {
        ChannelHandlerContext ctx = ctxs.get(channelHandlerContextType);
        if (ctx == null || ctx.isRemoved()) {
            return false;
        }
        return true;
    }
}

From source file:org.opendaylight.sxp.core.SxpConnectionTest.java

License:Open Source License

@Test
public void testIsStateOn() throws Exception {
    assertTrue(sxpConnection.isStateOn(SxpConnection.ChannelHandlerContextType.ListenerContext));
    assertTrue(sxpConnection.isStateOn(SxpConnection.ChannelHandlerContextType.SpeakerContext));

    sxpConnection.setStateOff();/*from  www .  j  ava 2 s .  c o  m*/
    assertFalse(sxpConnection.isStateOn(SxpConnection.ChannelHandlerContextType.ListenerContext));
    assertFalse(sxpConnection.isStateOn(SxpConnection.ChannelHandlerContextType.SpeakerContext));

    sxpConnection = SxpConnection.create(sxpNode, mockConnection(ConnectionMode.Both, ConnectionState.On),
            DOMAIN_NAME);

    ChannelHandlerContext context = mock(ChannelHandlerContext.class);
    when(context.isRemoved()).thenReturn(false);
    sxpConnection.markChannelHandlerContext(context, SxpConnection.ChannelHandlerContextType.ListenerContext);
    assertTrue(sxpConnection.isStateOn(SxpConnection.ChannelHandlerContextType.ListenerContext));

    context = mock(ChannelHandlerContext.class);
    when(context.isRemoved()).thenReturn(true);
    sxpConnection.markChannelHandlerContext(context, SxpConnection.ChannelHandlerContextType.SpeakerContext);
    assertFalse(sxpConnection.isStateOn(SxpConnection.ChannelHandlerContextType.SpeakerContext));
}