@Override protected boolean scheduleWriteIfNecessary(final AbstractNioChannel<?> channel) { final Thread currentThread = Thread.currentThread(); final Thread workerThread = thread; if (currentThread != workerThread) { if (channel.writeTaskInTaskQueue.compareAndSet(false, true)) { registerTask(channel.writeTask); } return true; } return false; }
@Override protected NioWorker newWorker(Executor executor) { return new NioWorker(executor, determiner); } }
private static boolean isInIoThread(NioWorker worker) { final Thread[] ioThread = new Thread[]{null}; worker.executeInIoThread(new Runnable() { @Override public void run() { ioThread[0] = currentThread(); } }); boolean aligned = ioThread[0] == currentThread(); assert aligned : format("Current thread %s does not match I/O thread %s", currentThread(), ioThread[0]); return aligned; }
private static void registerAcceptedChannel(NioServerSocketChannel parent, SocketChannel acceptedSocket, Thread currentThread) { try { ChannelSink sink = parent.getPipeline().getSink(); ChannelPipeline pipeline = parent.getConfig().getPipelineFactory().getPipeline(); NioWorker worker = parent.workerPool.nextWorker(); worker.register(new NioAcceptedSocketChannel( parent.getFactory(), pipeline, parent, sink , acceptedSocket, worker, currentThread), null); } catch (Exception e) { if (logger.isWarnEnabled()) { logger.warn( "Failed to initialize an accepted socket.", e); } try { acceptedSocket.close(); } catch (IOException e2) { if (logger.isWarnEnabled()) { logger.warn( "Failed to close a partially accepted socket.", e2); } } } }
void writeFromUserCodeUdp(final AbstractNioChannel<?> channel) { assert channel instanceof NioDatagramChannel; /* * Note that we are not checking if the channel is connected. Connected * has a different meaning in UDP and means that the channels socket is * configured to only send and receive from a given remote peer. */ if (!channel.isBound()) { cleanUpWriteBuffer(channel); return; } if (scheduleWriteIfNecessary(channel)) { return; } // From here, we are sure Thread.currentThread() == workerThread. if (channel.writeSuspended) { return; } if (channel.inWriteNowLoop) { return; } write0(channel); }
setOpWrite(channel); } else if (removeOpWrite) { clearOpWrite(channel);
public void runOnCorrectThread(NioSocketChannel nch, Runnable runnable) { nch.getWorker().executeInIoThread(runnable, false); }
NioWorker worker = parent.workerPool.nextWorker(); worker.register(new NioAcceptedSocketChannel( parent.getFactory(), pipeline, parent, sink , acceptedSocket, worker, currentThread), null);
public void execute(Runnable task) { worker.executeInIoThread(wrapTask(task), true); }
private static void registerAcceptedChannel(NioServerSocketChannel parent, SocketChannel acceptedSocket, Thread currentThread) { try { ChannelSink sink = parent.getPipeline().getSink(); ChannelPipeline pipeline = parent.getConfig().getPipelineFactory().getPipeline(); NioWorker worker = parent.workerPool.nextWorker(); worker.register(new NioAcceptedSocketChannel( parent.getFactory(), pipeline, parent, sink , acceptedSocket, worker, currentThread), null); } catch (Exception e) { if (logger.isWarnEnabled()) { logger.warn( "Failed to initialize an accepted socket.", e); } try { acceptedSocket.close(); } catch (IOException e2) { if (logger.isWarnEnabled()) { logger.warn( "Failed to close a partially accepted socket.", e2); } } } }
close(channel, succeededFuture(channel)); return false;
@Override protected boolean scheduleWriteIfNecessary(final AbstractNioChannel<?> channel) { final Thread currentThread = Thread.currentThread(); final Thread workerThread = thread; if (currentThread != workerThread) { if (channel.writeTaskInTaskQueue.compareAndSet(false, true)) { registerTask(channel.writeTask); } return true; } return false; }
@Override @Deprecated protected NioWorker createWorker(Executor executor) { return new NioWorker(executor, determiner); } }
@Override public void executeInIoThread(Runnable runnable) { NioSocketChannel nioSocketChannel = (NioSocketChannel) getNettyChannel(); nioSocketChannel.getWorker().executeInIoThread(runnable, true); }
private static void registerAcceptedChannel(NioServerSocketChannel parent, SocketChannel acceptedSocket, Thread currentThread) { try { ChannelSink sink = parent.getPipeline().getSink(); ChannelPipeline pipeline = parent.getConfig().getPipelineFactory().getPipeline(); NioWorker worker = parent.workerPool.nextWorker(); worker.register(new NioAcceptedSocketChannel( parent.getFactory(), pipeline, parent, sink , acceptedSocket, worker, currentThread), null); } catch (Exception e) { if (logger.isWarnEnabled()) { logger.warn( "Failed to initialize an accepted socket.", e); } try { acceptedSocket.close(); } catch (IOException e2) { if (logger.isWarnEnabled()) { logger.warn( "Failed to close a partially accepted socket.", e2); } } } }
@Override protected void close(SelectionKey k) { ReadDispatcher dispatcher = (ReadDispatcher) k.attachment(); AbstractNioChannel<?> ch = dispatcher.channel(); close(ch, succeededFuture(ch)); }
@Override protected boolean scheduleWriteIfNecessary(final AbstractNioChannel<?> channel) { final Thread currentThread = Thread.currentThread(); final Thread workerThread = thread; if (currentThread != workerThread) { if (channel.writeTaskInTaskQueue.compareAndSet(false, true)) { registerTask(channel.writeTask); } return true; } return false; }
@Override public void operationComplete(ChannelFuture future) throws Exception { ((NioSocketChannel) ctx.getChannel()).getWorker().executeInIoThread(() -> { try { prepareReceived(ctx, evt); } catch (Exception e) { sendErrorMessage(ctx, e); } }, true); } });