Search in sources :

Example 26 with EventExecutor

use of io.netty.util.concurrent.EventExecutor in project netty by netty.

the class DefaultChannelPipeline method remove.

private AbstractChannelHandlerContext remove(final AbstractChannelHandlerContext ctx) {
    assert ctx != head && ctx != tail;
    synchronized (this) {
        remove0(ctx);
        // ChannelHandler.handlerRemoved(...) once the channel is registered.
        if (!registered) {
            callHandlerCallbackLater(ctx, false);
            return ctx;
        }
        EventExecutor executor = ctx.executor();
        if (!executor.inEventLoop()) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    callHandlerRemoved0(ctx);
                }
            });
            return ctx;
        }
    }
    callHandlerRemoved0(ctx);
    return ctx;
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor)

Example 27 with EventExecutor

use of io.netty.util.concurrent.EventExecutor in project netty by netty.

the class DefaultChannelPipeline method replace.

private ChannelHandler replace(final AbstractChannelHandlerContext ctx, String newName, ChannelHandler newHandler) {
    assert ctx != head && ctx != tail;
    final AbstractChannelHandlerContext newCtx;
    synchronized (this) {
        checkMultiplicity(newHandler);
        if (newName == null) {
            newName = generateName(newHandler);
        } else {
            boolean sameName = ctx.name().equals(newName);
            if (!sameName) {
                checkDuplicateName(newName);
            }
        }
        newCtx = newContext(ctx.executor, newName, newHandler);
        replace0(ctx, newCtx);
        // ChannelHandler.handlerRemoved(...) once the channel is registered.
        if (!registered) {
            callHandlerCallbackLater(newCtx, true);
            callHandlerCallbackLater(ctx, false);
            return ctx.handler();
        }
        EventExecutor executor = ctx.executor();
        if (!executor.inEventLoop()) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    // Invoke newHandler.handlerAdded() first (i.e. before oldHandler.handlerRemoved() is invoked)
                    // because callHandlerRemoved() will trigger channelRead() or flush() on newHandler and
                    // those event handlers must be called after handlerAdded().
                    callHandlerAdded0(newCtx);
                    callHandlerRemoved0(ctx);
                }
            });
            return ctx.handler();
        }
    }
    // Invoke newHandler.handlerAdded() first (i.e. before oldHandler.handlerRemoved() is invoked)
    // because callHandlerRemoved() will trigger channelRead() or flush() on newHandler and those
    // event handlers must be called after handlerAdded().
    callHandlerAdded0(newCtx);
    callHandlerRemoved0(ctx);
    return ctx.handler();
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor)

Example 28 with EventExecutor

use of io.netty.util.concurrent.EventExecutor in project netty by netty.

the class DefaultChannelPipeline method addFirst.

@Override
public final ChannelPipeline addFirst(EventExecutorGroup group, String name, ChannelHandler handler) {
    final AbstractChannelHandlerContext newCtx;
    synchronized (this) {
        checkMultiplicity(handler);
        name = filterName(name, handler);
        newCtx = newContext(group, name, handler);
        addFirst0(newCtx);
        // ChannelHandler.handlerAdded(...) once the channel is registered.
        if (!registered) {
            newCtx.setAddPending();
            callHandlerCallbackLater(newCtx, true);
            return this;
        }
        EventExecutor executor = newCtx.executor();
        if (!executor.inEventLoop()) {
            newCtx.setAddPending();
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    callHandlerAdded0(newCtx);
                }
            });
            return this;
        }
    }
    callHandlerAdded0(newCtx);
    return this;
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor)

Example 29 with EventExecutor

use of io.netty.util.concurrent.EventExecutor in project netty by netty.

the class DefaultChannelPipeline method destroyUp.

private void destroyUp(AbstractChannelHandlerContext ctx, boolean inEventLoop) {
    final Thread currentThread = Thread.currentThread();
    final AbstractChannelHandlerContext tail = this.tail;
    for (; ; ) {
        if (ctx == tail) {
            destroyDown(currentThread, tail.prev, inEventLoop);
            break;
        }
        final EventExecutor executor = ctx.executor();
        if (!inEventLoop && !executor.inEventLoop(currentThread)) {
            final AbstractChannelHandlerContext finalCtx = ctx;
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    destroyUp(finalCtx, true);
                }
            });
            break;
        }
        ctx = ctx.next;
        inEventLoop = false;
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor)

Example 30 with EventExecutor

use of io.netty.util.concurrent.EventExecutor in project netty by netty.

the class AbstractChannelHandlerContext method close.

@Override
public ChannelFuture close(final ChannelPromise promise) {
    if (isNotValidPromise(promise, false)) {
        // cancelled
        return promise;
    }
    final AbstractChannelHandlerContext next = findContextOutbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeClose(promise);
    } else {
        safeExecute(executor, new Runnable() {

            @Override
            public void run() {
                next.invokeClose(promise);
            }
        }, promise, null);
    }
    return promise;
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) OrderedEventExecutor(io.netty.util.concurrent.OrderedEventExecutor)

Aggregations

EventExecutor (io.netty.util.concurrent.EventExecutor)37 OrderedEventExecutor (io.netty.util.concurrent.OrderedEventExecutor)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 ChannelPromise (io.netty.channel.ChannelPromise)5 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelPromiseNotifier (io.netty.channel.ChannelPromiseNotifier)4 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)4 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)3 Test (org.junit.Test)3 LocalChannel (io.netty.channel.local.LocalChannel)2 AbstractEventExecutor (io.netty.util.concurrent.AbstractEventExecutor)2 ImmediateEventExecutor (io.netty.util.concurrent.ImmediateEventExecutor)2 UnorderedThreadPoolEventExecutor (io.netty.util.concurrent.UnorderedThreadPoolEventExecutor)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelHandler (io.netty.channel.ChannelHandler)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 ChannelGroup (io.netty.channel.group.ChannelGroup)1 DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)1 ServerSocketChannel (io.netty.channel.socket.ServerSocketChannel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1