From f2d490ef9f7bdaf19466ba28e0495488b99458fd Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 16 Jun 2010 03:10:19 +0000 Subject: [PATCH] Suppress system calls when a user attempts to write an empty buffer --- .../channel/socket/nio/NioDatagramWorker.java | 4 +-- .../netty/channel/socket/nio/NioWorker.java | 35 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java index 3cce67f52b..9fcfd92cdd 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java @@ -530,7 +530,7 @@ class NioDatagramWorker implements Runnable { long localWrittenBytes = 0; SocketAddress raddr = evt.getRemoteAddress(); if (raddr == null) { - for (int i = writeSpinCount; i > 0; i --) { + for (int i = writeSpinCount; i > 0 && !buf.finished(); i --) { localWrittenBytes = buf.transferTo(ch); if (localWrittenBytes != 0) { writtenBytes += localWrittenBytes; @@ -538,7 +538,7 @@ class NioDatagramWorker implements Runnable { } } } else { - for (int i = writeSpinCount; i > 0; i --) { + for (int i = writeSpinCount; i > 0 && !buf.finished(); i --) { localWrittenBytes = buf.transferTo(ch, raddr); if (localWrittenBytes != 0) { writtenBytes += localWrittenBytes; diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java index f3c250c18e..050fcc2573 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java @@ -15,21 +15,18 @@ */ package org.jboss.netty.channel.socket.nio; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBufferFactory; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.*; -import org.jboss.netty.channel.socket.nio.SocketSendBufferPool.SendBuffer; -import org.jboss.netty.logging.InternalLogger; -import org.jboss.netty.logging.InternalLoggerFactory; -import org.jboss.netty.util.ThreadRenamingRunnable; -import org.jboss.netty.util.internal.IoWorkerRunnable; -import org.jboss.netty.util.internal.LinkedTransferQueue; +import static org.jboss.netty.channel.Channels.*; import java.io.IOException; import java.net.SocketAddress; import java.nio.ByteBuffer; -import java.nio.channels.*; +import java.nio.channels.AsynchronousCloseException; +import java.nio.channels.CancelledKeyException; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.NotYetConnectedException; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Queue; import java.util.Set; @@ -39,7 +36,19 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import static org.jboss.netty.channel.Channels.*; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBufferFactory; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelException; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.channel.ReceiveBufferSizePredictor; +import org.jboss.netty.channel.socket.nio.SocketSendBufferPool.SendBuffer; +import org.jboss.netty.logging.InternalLogger; +import org.jboss.netty.logging.InternalLoggerFactory; +import org.jboss.netty.util.ThreadRenamingRunnable; +import org.jboss.netty.util.internal.IoWorkerRunnable; +import org.jboss.netty.util.internal.LinkedTransferQueue; /** * @@ -456,7 +465,7 @@ class NioWorker implements Runnable { ChannelFuture future = evt.getFuture(); try { long localWrittenBytes = 0; - for (int i = writeSpinCount; i > 0; i --) { + for (int i = writeSpinCount; i > 0 && !buf.finished(); i --) { localWrittenBytes = buf.transferTo(ch); if (localWrittenBytes != 0) { writtenBytes += localWrittenBytes;