Added documentation on how to implement protocol multiplexer
This commit is contained in:
parent
c4449a170d
commit
338a5cba4a
@ -28,7 +28,9 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
import org.jboss.netty.buffer.ChannelBuffer;
|
||||||
import org.jboss.netty.buffer.ChannelBuffers;
|
import org.jboss.netty.buffer.ChannelBuffers;
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
|
import org.jboss.netty.channel.ChannelHandler;
|
||||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||||
|
import org.jboss.netty.channel.ChannelPipeline;
|
||||||
import org.jboss.netty.channel.ChannelPipelineCoverage;
|
import org.jboss.netty.channel.ChannelPipelineCoverage;
|
||||||
import org.jboss.netty.channel.ChannelStateEvent;
|
import org.jboss.netty.channel.ChannelStateEvent;
|
||||||
import org.jboss.netty.channel.ChannelUpstreamHandler;
|
import org.jboss.netty.channel.ChannelUpstreamHandler;
|
||||||
@ -36,6 +38,7 @@ import org.jboss.netty.channel.Channels;
|
|||||||
import org.jboss.netty.channel.ExceptionEvent;
|
import org.jboss.netty.channel.ExceptionEvent;
|
||||||
import org.jboss.netty.channel.MessageEvent;
|
import org.jboss.netty.channel.MessageEvent;
|
||||||
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
||||||
|
import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes the received {@link ChannelBuffer}s into a meaningful frame object.
|
* Decodes the received {@link ChannelBuffer}s into a meaningful frame object.
|
||||||
@ -137,6 +140,43 @@ import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
|||||||
* {@link ChannelUpstreamHandler} receives a {@link MessageEvent} which
|
* {@link ChannelUpstreamHandler} receives a {@link MessageEvent} which
|
||||||
* contains a POJO rather than a {@link ChannelBuffer}.
|
* contains a POJO rather than a {@link ChannelBuffer}.
|
||||||
*
|
*
|
||||||
|
* <h3>Replacing a decoder with another decoder in a pipeline</h3>
|
||||||
|
* <p>
|
||||||
|
* If you are going to write a protocol multiplexer, you will probably want to
|
||||||
|
* replace a {@link FrameDecoder} (protocol detector) with another
|
||||||
|
* {@link FrameDecoder} or {@link ReplayingDecoder} (actual protocol decoder).
|
||||||
|
* It is not possible to achieve this simply by calling
|
||||||
|
* {@link ChannelPipeline#replace(ChannelHandler, String, ChannelHandler)}, but
|
||||||
|
* some additional steps are required:
|
||||||
|
* <pre>
|
||||||
|
* public class FirstDecoder extends FrameDecoder {
|
||||||
|
*
|
||||||
|
* public FirstDecoder() {
|
||||||
|
* super(true); // Enable unfold
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* protected Object decode(ChannelHandlerContext ctx, Channel ch, ChannelBuffer buf) {
|
||||||
|
* ...
|
||||||
|
* // Decode the first message
|
||||||
|
* Object firstMessage = ...;
|
||||||
|
*
|
||||||
|
* // Add the second decoder
|
||||||
|
* ctx.getPipeline().addLast("second", new SecondDecoder());
|
||||||
|
*
|
||||||
|
* // Remove the first decoder (me)
|
||||||
|
* ctx.getPipeline().remove(this);
|
||||||
|
*
|
||||||
|
* if (buf.readable()) {
|
||||||
|
* // Hand off the remaining data to the second decoder
|
||||||
|
* return new Object[] { firstMessage, buf.readBytes(buf.readableBytes()) };
|
||||||
|
* } else {
|
||||||
|
* // Nothing to hand off
|
||||||
|
* return firstMessage;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
*
|
*
|
||||||
|
@ -28,7 +28,9 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
import org.jboss.netty.buffer.ChannelBuffer;
|
||||||
import org.jboss.netty.buffer.ChannelBufferFactory;
|
import org.jboss.netty.buffer.ChannelBufferFactory;
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
|
import org.jboss.netty.channel.ChannelHandler;
|
||||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||||
|
import org.jboss.netty.channel.ChannelPipeline;
|
||||||
import org.jboss.netty.channel.ChannelPipelineCoverage;
|
import org.jboss.netty.channel.ChannelPipelineCoverage;
|
||||||
import org.jboss.netty.channel.ChannelStateEvent;
|
import org.jboss.netty.channel.ChannelStateEvent;
|
||||||
import org.jboss.netty.channel.Channels;
|
import org.jboss.netty.channel.Channels;
|
||||||
@ -241,6 +243,42 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder;
|
|||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
|
* <h3>Replacing a decoder with another decoder in a pipeline</h3>
|
||||||
|
* <p>
|
||||||
|
* If you are going to write a protocol multiplexer, you will probably want to
|
||||||
|
* replace a {@link ReplayingDecoder} (protocol detector) with another
|
||||||
|
* {@link ReplayingDecoder} or {@link FrameDecoder} (actual protocol decoder).
|
||||||
|
* It is not possible to achieve this simply by calling
|
||||||
|
* {@link ChannelPipeline#replace(ChannelHandler, String, ChannelHandler)}, but
|
||||||
|
* some additional steps are required:
|
||||||
|
* <pre>
|
||||||
|
* public class FirstDecoder extends ReplayingDecoder<VoidEnum> {
|
||||||
|
*
|
||||||
|
* public FirstDecoder() {
|
||||||
|
* super(true); // Enable unfold
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* protected Object decode(ChannelHandlerContext ctx, Channel ch, ChannelBuffer buf, VoidEnum state) {
|
||||||
|
* ...
|
||||||
|
* // Decode the first message
|
||||||
|
* Object firstMessage = ...;
|
||||||
|
*
|
||||||
|
* // Add the second decoder
|
||||||
|
* ctx.getPipeline().addLast("second", new SecondDecoder());
|
||||||
|
*
|
||||||
|
* // Remove the first decoder (me)
|
||||||
|
* ctx.getPipeline().remove(this);
|
||||||
|
*
|
||||||
|
* if (buf.readable()) {
|
||||||
|
* // Hand off the remaining data to the second decoder
|
||||||
|
* return new Object[] { firstMessage, buf.readBytes(<b>super.actualReadableBytes()</b>) };
|
||||||
|
* } else {
|
||||||
|
* // Nothing to hand off
|
||||||
|
* return firstMessage;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user