From 2a63acef4de7f3b832356f2ee7a3d3f74d460e0d Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 31 May 2012 09:03:31 -0700 Subject: [PATCH] More convenient inbound stream handler / Smarter inbound buffer cleanup - Added a new convenience method to ChannelInboundstreamHandlerAdapter - EchoServerHandler uses the new method - DefaultChannelPipeline calls inboundByteBuffer.discardReadBytes() when it is sure there's no memory copy involved --- .../java/io/netty/example/echo/EchoClient.java | 4 +--- .../java/io/netty/example/echo/EchoServer.java | 2 +- .../io/netty/example/echo/EchoServerHandler.java | 15 +++------------ .../ChannelInboundStreamHandlerAdapter.java | 14 ++++++++++++++ .../io/netty/channel/DefaultChannelPipeline.java | 5 +++++ 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/example/src/main/java/io/netty/example/echo/EchoClient.java b/example/src/main/java/io/netty/example/echo/EchoClient.java index 5792107eac..7bd14c3a60 100644 --- a/example/src/main/java/io/netty/example/echo/EchoClient.java +++ b/example/src/main/java/io/netty/example/echo/EchoClient.java @@ -22,8 +22,6 @@ import io.netty.channel.ChannelOption; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioEventLoop; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.logging.LogLevel; -import io.netty.handler.logging.LoggingHandler; import java.net.InetSocketAddress; @@ -57,7 +55,7 @@ public class EchoClient { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( - new LoggingHandler(LogLevel.INFO), + //new LoggingHandler(LogLevel.INFO), new EchoClientHandler(firstMessageSize)); } }); diff --git a/example/src/main/java/io/netty/example/echo/EchoServer.java b/example/src/main/java/io/netty/example/echo/EchoServer.java index 634b7c7245..58f0f7179d 100644 --- a/example/src/main/java/io/netty/example/echo/EchoServer.java +++ b/example/src/main/java/io/netty/example/echo/EchoServer.java @@ -58,7 +58,7 @@ public class EchoServer { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( - new LoggingHandler(LogLevel.INFO), + //new LoggingHandler(LogLevel.INFO), new EchoServerHandler()); } }); diff --git a/example/src/main/java/io/netty/example/echo/EchoServerHandler.java b/example/src/main/java/io/netty/example/echo/EchoServerHandler.java index f24c10408f..3b462c3e0a 100644 --- a/example/src/main/java/io/netty/example/echo/EchoServerHandler.java +++ b/example/src/main/java/io/netty/example/echo/EchoServerHandler.java @@ -16,10 +16,8 @@ package io.netty.example.echo; import io.netty.buffer.ChannelBuffer; -import io.netty.channel.ChannelBufferHolder; -import io.netty.channel.ChannelBufferHolders; -import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerContext; +import io.netty.channel.ChannelInboundStreamHandlerAdapter; import java.util.logging.Level; import java.util.logging.Logger; @@ -27,23 +25,16 @@ import java.util.logging.Logger; /** * Handler implementation for the echo server. */ -public class EchoServerHandler extends ChannelInboundHandlerAdapter { +public class EchoServerHandler extends ChannelInboundStreamHandlerAdapter { private static final Logger logger = Logger.getLogger( EchoServerHandler.class.getName()); @Override - public ChannelBufferHolder newInboundBuffer(ChannelInboundHandlerContext ctx) { - return ChannelBufferHolders.byteBuffer(); - } - - @Override - public void inboundBufferUpdated(ChannelInboundHandlerContext ctx) { - ChannelBuffer in = ctx.inbound().byteBuffer(); + public void inboundBufferUpdated(ChannelInboundHandlerContext ctx, ChannelBuffer in) { ChannelBuffer out = ctx.nextOutboundByteBuffer(); out.discardReadBytes(); out.writeBytes(in); - in.discardReadBytes(); ctx.flush(); } diff --git a/transport/src/main/java/io/netty/channel/ChannelInboundStreamHandlerAdapter.java b/transport/src/main/java/io/netty/channel/ChannelInboundStreamHandlerAdapter.java index fe99879f50..f60d0068e4 100644 --- a/transport/src/main/java/io/netty/channel/ChannelInboundStreamHandlerAdapter.java +++ b/transport/src/main/java/io/netty/channel/ChannelInboundStreamHandlerAdapter.java @@ -1,5 +1,7 @@ package io.netty.channel; +import io.netty.buffer.ChannelBuffer; + public class ChannelInboundStreamHandlerAdapter extends ChannelInboundHandlerAdapter { @Override @@ -7,4 +9,16 @@ public class ChannelInboundStreamHandlerAdapter extends ChannelInboundHandlerAda ChannelInboundHandlerContext ctx) throws Exception { return ChannelBufferHolders.byteBuffer(); } + + @Override + public void inboundBufferUpdated(ChannelInboundHandlerContext ctx) + throws Exception { + inboundBufferUpdated(ctx, ctx.inbound().byteBuffer()); + } + + public void inboundBufferUpdated(ChannelInboundHandlerContext ctx, ChannelBuffer in) + throws Exception { + ctx.nextInboundByteBuffer().writeBytes(in); + in.discardReadBytes(); + } } diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java index 57e46b7f3d..a891893773 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java @@ -740,6 +740,11 @@ public class DefaultChannelPipeline implements ChannelPipeline { ((ChannelInboundHandler) ctx.handler()).inboundBufferUpdated(ctx); } catch (Throwable t) { notifyHandlerException(t); + } finally { + ChannelBufferHolder inbound = ctx.inbound(); + if (inbound.isEmpty() && inbound.hasByteBuffer()) { + inbound.byteBuffer().discardReadBytes(); + } } }