Search in sources :

Example 21 with EventExecutor

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

the class AbstractChannelHandlerContext method invokeChannelRead.

static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
    final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelRead(m);
    } else {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                next.invokeChannelRead(m);
            }
        });
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) OrderedEventExecutor(io.netty.util.concurrent.OrderedEventExecutor)

Example 22 with EventExecutor

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

the class AbstractChannelHandlerContext method write.

private void write(Object msg, boolean flush, ChannelPromise promise) {
    AbstractChannelHandlerContext next = findContextOutbound();
    final Object m = pipeline.touch(msg, next);
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        if (flush) {
            next.invokeWriteAndFlush(m, promise);
        } else {
            next.invokeWrite(m, promise);
        }
    } else {
        AbstractWriteTask task;
        if (flush) {
            task = WriteAndFlushTask.newInstance(next, m, promise);
        } else {
            task = WriteTask.newInstance(next, m, promise);
        }
        safeExecute(executor, task, promise, m);
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) OrderedEventExecutor(io.netty.util.concurrent.OrderedEventExecutor)

Example 23 with EventExecutor

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

the class AbstractChannelHandlerContext method disconnect.

@Override
public ChannelFuture disconnect(final ChannelPromise promise) {
    if (isNotValidPromise(promise, false)) {
        // cancelled
        return promise;
    }
    final AbstractChannelHandlerContext next = findContextOutbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        // So far, UDP/IP is the only transport that has such behavior.
        if (!channel().metadata().hasDisconnect()) {
            next.invokeClose(promise);
        } else {
            next.invokeDisconnect(promise);
        }
    } else {
        safeExecute(executor, new Runnable() {

            @Override
            public void run() {
                if (!channel().metadata().hasDisconnect()) {
                    next.invokeClose(promise);
                } else {
                    next.invokeDisconnect(promise);
                }
            }
        }, promise, null);
    }
    return promise;
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) OrderedEventExecutor(io.netty.util.concurrent.OrderedEventExecutor)

Example 24 with EventExecutor

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

the class DefaultChannelPipeline method destroyDown.

private void destroyDown(Thread currentThread, AbstractChannelHandlerContext ctx, boolean inEventLoop) {
    // We have reached at tail; now traverse backwards.
    final AbstractChannelHandlerContext head = this.head;
    for (; ; ) {
        if (ctx == head) {
            break;
        }
        final EventExecutor executor = ctx.executor();
        if (inEventLoop || executor.inEventLoop(currentThread)) {
            synchronized (this) {
                remove0(ctx);
            }
            callHandlerRemoved0(ctx);
        } else {
            final AbstractChannelHandlerContext finalCtx = ctx;
            executor.execute(new Runnable() {

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

Example 25 with EventExecutor

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

the class DefaultChannelPipeline method addAfter.

@Override
public final ChannelPipeline addAfter(EventExecutorGroup group, String baseName, String name, ChannelHandler handler) {
    final AbstractChannelHandlerContext newCtx;
    final AbstractChannelHandlerContext ctx;
    synchronized (this) {
        checkMultiplicity(handler);
        name = filterName(name, handler);
        ctx = getContextOrDie(baseName);
        newCtx = newContext(group, name, handler);
        addAfter0(ctx, newCtx);
        // ChannelHandler.handlerRemoved(...) 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)

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