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
This commit is contained in:
Trustin Lee 2012-05-31 09:03:31 -07:00
parent 665777e6f9
commit 2a63acef4d
5 changed files with 24 additions and 16 deletions

View File

@ -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));
}
});

View File

@ -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());
}
});

View File

@ -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<Byte> {
public class EchoServerHandler extends ChannelInboundStreamHandlerAdapter {
private static final Logger logger = Logger.getLogger(
EchoServerHandler.class.getName());
@Override
public ChannelBufferHolder<Byte> newInboundBuffer(ChannelInboundHandlerContext<Byte> ctx) {
return ChannelBufferHolders.byteBuffer();
}
@Override
public void inboundBufferUpdated(ChannelInboundHandlerContext<Byte> ctx) {
ChannelBuffer in = ctx.inbound().byteBuffer();
public void inboundBufferUpdated(ChannelInboundHandlerContext<Byte> ctx, ChannelBuffer in) {
ChannelBuffer out = ctx.nextOutboundByteBuffer();
out.discardReadBytes();
out.writeBytes(in);
in.discardReadBytes();
ctx.flush();
}

View File

@ -1,5 +1,7 @@
package io.netty.channel;
import io.netty.buffer.ChannelBuffer;
public class ChannelInboundStreamHandlerAdapter extends ChannelInboundHandlerAdapter<Byte> {
@Override
@ -7,4 +9,16 @@ public class ChannelInboundStreamHandlerAdapter extends ChannelInboundHandlerAda
ChannelInboundHandlerContext<Byte> ctx) throws Exception {
return ChannelBufferHolders.byteBuffer();
}
@Override
public void inboundBufferUpdated(ChannelInboundHandlerContext<Byte> ctx)
throws Exception {
inboundBufferUpdated(ctx, ctx.inbound().byteBuffer());
}
public void inboundBufferUpdated(ChannelInboundHandlerContext<Byte> ctx, ChannelBuffer in)
throws Exception {
ctx.nextInboundByteBuffer().writeBytes(in);
in.discardReadBytes();
}
}

View File

@ -740,6 +740,11 @@ public class DefaultChannelPipeline implements ChannelPipeline {
((ChannelInboundHandler<Object>) ctx.handler()).inboundBufferUpdated(ctx);
} catch (Throwable t) {
notifyHandlerException(t);
} finally {
ChannelBufferHolder<Object> inbound = ctx.inbound();
if (inbound.isEmpty() && inbound.hasByteBuffer()) {
inbound.byteBuffer().discardReadBytes();
}
}
}