Java Code Examples for io.netty.util.concurrent.EventExecutor#inEventLoop()

The following examples show how to use io.netty.util.concurrent.EventExecutor#inEventLoop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Lz4FrameEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Close this {@link Lz4FrameEncoder} and so finish the encoding.
 * The given {@link ChannelFuture} will be notified once the operation
 * completes and will also be returned.关闭这个Lz4FrameEncoder,然后完成编码。一旦操作完成,给定的ChannelFuture将被通知,并且也将被返回。
 */
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), promise);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return promise;
    }
}
 
Example 2
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
    public ChannelHandlerContext read() {
        final AbstractChannelHandlerContext next = findContextOutbound();
        EventExecutor executor = next.executor();
//        执行器在这个事件组中
        if (executor.inEventLoop()) {
//            执行读取事件
            next.invokeRead();
        } else {
            Runnable task = next.invokeReadTask;
            if (task == null) {
                next.invokeReadTask = task = new Runnable() {
                    @Override
                    public void run() {
                        next.invokeRead();
                    }
                };
            }
            executor.execute(task);
        }

        return this;
    }
 
Example 3
Source File: JdkZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        final ChannelPromise p = ctx.newPromise();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), p);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return p;
    }
}
 
Example 4
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: SslHandler.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Performs TLS renegotiation.
 */
public Future<Channel> renegotiate(final Promise<Channel> promise) {
    if (promise == null) {
        throw new NullPointerException("promise");
    }

    ChannelHandlerContext ctx = this.ctx;
    if (ctx == null) {
        throw new IllegalStateException();
    }

    EventExecutor executor = ctx.executor();
    if (!executor.inEventLoop()) {
        executor.execute(new OneTimeTask() {
            @Override
            public void run() {
                handshake(promise);
            }
        });
        return promise;
    }

    handshake(promise);
    return promise;
}
 
Example 6
Source File: DefaultStreamMessageDuplicator.java    From armeria with Apache License 2.0 6 votes vote down vote up
private void doSubscribe(DownstreamSubscription<T> subscription) {
    if (state == State.ABORTED) {
        final EventExecutor executor = subscription.executor;
        if (executor.inEventLoop()) {
            failLateProcessorSubscriber(subscription);
        } else {
            executor.execute(() -> failLateProcessorSubscriber(subscription));
        }
        return;
    }

    downstreamSubscriptions.add(subscription);
    unsubscribedUpdater.decrementAndGet(duplicator);
    if (upstreamSubscription != null) {
        subscription.invokeOnSubscribe();
    }
}
 
Example 7
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static void invokeChannelWritabilityChanged(final AbstractChannelHandlerContext next) {
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelWritabilityChanged();
    } else {
        Runnable task = next.invokeChannelWritableStateChangedTask;
        if (task == null) {
            next.invokeChannelWritableStateChangedTask = task = new Runnable() {
                @Override
                public void run() {
                    next.invokeChannelWritabilityChanged();
                }
            };
        }
        executor.execute(task);
    }
}
 
Example 8
Source File: JdkZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        final ChannelPromise p = ctx.newPromise();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), p);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return p;
    }
}
 
Example 9
Source File: JZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        final ChannelPromise p = ctx.newPromise();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), p);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return p;
    }
}
 
Example 10
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelHandlerContext fireChannelRead(final Object msg) {
    if (msg == null) {
        throw new NullPointerException("msg");
    }

    invokedNextChannelRead = true;
    final AbstractChannelHandlerContext next = findContextInbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelRead(msg);
    } else {
        executor.execute(new OneTimeTask() {
            @Override
            public void run() {
                next.invokeChannelRead(msg);
            }
        });
    }
    return this;
}
 
Example 11
Source File: DefaultChannelPipeline.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example 12
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void write(Object msg, boolean flush, ChannelPromise promise) {
//        找到pipeline的上一个节点
        AbstractChannelHandlerContext next = findContextOutbound();
        final Object m = pipeline.touch(msg, next);
        EventExecutor executor = next.executor();
//        如果是当前的NioEventLoop
        if (executor.inEventLoop()) {
            if (flush) {
//                如果是write和flush
                next.invokeWriteAndFlush(m, promise);
            } else {
                next.invokeWrite(m, promise);
            }
        } else {
//            创建WriteAndFlushTask
            AbstractWriteTask task;
            if (flush) {
                task = WriteAndFlushTask.newInstance(next, m, promise);
            }  else {
                task = WriteTask.newInstance(next, m, promise);
            }
            safeExecute(executor, task, promise, m);
        }
    }
 
Example 13
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelHandlerContext fireChannelUnregistered() {
    final AbstractChannelHandlerContext next = findContextInbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelUnregistered();
    } else {
        executor.execute(new OneTimeTask() {
            @Override
            public void run() {
                next.invokeChannelUnregistered();
            }
        });
    }
    return this;
}
 
Example 14
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture bind(final SocketAddress localAddress, final ChannelPromise promise) {
    if (localAddress == null) {
        throw new NullPointerException("localAddress");
    }
    if (!validatePromise(promise, false)) {
        // cancelled
        return promise;
    }

    final AbstractChannelHandlerContext next = findContextOutbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeBind(localAddress, promise);
    } else {
        safeExecute(executor, new OneTimeTask() {
            @Override
            public void run() {
                next.invokeBind(localAddress, promise);
            }
        }, promise, null);
    }

    return promise;
}
 
Example 15
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static void invokeChannelRegistered(final AbstractChannelHandlerContext next) {
//        返回这个渠道上注册的事件组
        EventExecutor executor = next.executor();
//        当前线程是否在事件组
        if (executor.inEventLoop()) {
//            触发渠道注册
            next.invokeChannelRegistered();
        } else {
            executor.execute(new Runnable() {
                @Override
                public void run() {
//                    执行渠道注册
                    next.invokeChannelRegistered();
                }
            });
        }
    }
 
Example 16
Source File: Http2StreamChannelBootstrap.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
public Future<Http2StreamChannel> open(final Promise<Http2StreamChannel> promise) {
    final ChannelHandlerContext ctx = channel.pipeline().context(Http2MultiplexCodec.class);
    if (ctx == null) {
        if (channel.isActive()) {
            promise.setFailure(new IllegalStateException(StringUtil.simpleClassName(Http2MultiplexCodec.class) +
                    " must be in the ChannelPipeline of Channel " + channel));
        } else {
            promise.setFailure(new ClosedChannelException());
        }
    } else {
        EventExecutor executor = ctx.executor();
        if (executor.inEventLoop()) {
            open0(ctx, promise);
        } else {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    open0(ctx, promise);
                }
            });
        }
    }
    return promise;
}
 
Example 17
Source File: DefaultChannelPipeline.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public final ChannelPipeline addBefore(
        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);

        addBefore0(ctx, newCtx);

        // If the registered is false it means that the channel was not registered on an eventloop yet.
        // In this case we add the context to the pipeline and add a task that will call
        // 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;
}
 
Example 18
Source File: DefaultChannelPipeline.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
    public final ChannelPipeline addFirst(EventExecutorGroup group, String name, ChannelHandler handler) {
        final AbstractChannelHandlerContext newCtx;
        synchronized (this) {
            checkMultiplicity(handler);
//            获取handler的名字
            name = filterName(name, handler);

//            创建channel handler上下文
            newCtx = newContext(group, name, handler);

//            添加handler到handler上下文链表的第一个位置
            addFirst0(newCtx);

            // If the registered is false it means that the channel was not registered on an eventloop yet.
            // In this case we add the context to the pipeline and add a task that will call
            // 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;
    }
 
Example 19
Source File: DefaultChannelGroupFuture.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void checkDeadLock() {
    EventExecutor e = executor();
    if (e != null && e != ImmediateEventExecutor.INSTANCE && e.inEventLoop()) {
        throw new BlockingOperationException();
    }
}
 
Example 20
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelFuture connect(
        final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {

    if (remoteAddress == null) {
        throw new NullPointerException("remoteAddress");
    }
    if (!validatePromise(promise, false)) {
        // cancelled
        return promise;
    }

    final AbstractChannelHandlerContext next = findContextOutbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeConnect(remoteAddress, localAddress, promise);
    } else {
        safeExecute(executor, new OneTimeTask() {
            @Override
            public void run() {
                next.invokeConnect(remoteAddress, localAddress, promise);
            }
        }, promise, null);
    }

    return promise;
}