* Added WebSocketFrame.CLOSING_HANDSHAKE

* WebSocketFrameDecoder discards data if closing handshake was received before
This commit is contained in:
Trustin Lee 2010-07-07 05:07:39 +00:00
parent 35351701ff
commit 3156ed5dd5
2 changed files with 19 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package org.jboss.netty.handler.codec.http.websocket;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
/**
* A Web Socket frame that represents either text or binary data.
@ -26,6 +27,11 @@ import org.jboss.netty.buffer.ChannelBuffer;
*/
public interface WebSocketFrame {
/**
* Closing handshake message (<tt>0xFF, 0x00</tt>)
*/
WebSocketFrame CLOSING_HANDSHAKE = new DefaultWebSocketFrame(0xFF, ChannelBuffers.EMPTY_BUFFER);
/**
* Returns the type of this frame.
* <tt>0x00-0x7F</tt> means a text frame encoded in UTF-8, and

View File

@ -42,6 +42,7 @@ public class WebSocketFrameDecoder extends ReplayingDecoder<VoidEnum> {
public static final int DEFAULT_MAX_FRAME_SIZE = 16384;
private final int maxFrameSize;
private boolean receivedClosingHandshake;
public WebSocketFrameDecoder() {
this(DEFAULT_MAX_FRAME_SIZE);
@ -60,6 +61,14 @@ public class WebSocketFrameDecoder extends ReplayingDecoder<VoidEnum> {
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
ChannelBuffer buffer, VoidEnum state) throws Exception {
// Discard all data received if closing handshake was received before.
if (receivedClosingHandshake) {
buffer.skipBytes(actualReadableBytes());
return null;
}
// Decode a frame otherwise.
byte type = buffer.readByte();
if ((type & 0x80) == 0x80) {
// If the MSB on type is set, decode the frame length
@ -88,6 +97,10 @@ public class WebSocketFrameDecoder extends ReplayingDecoder<VoidEnum> {
}
} while ((b & 0x80) == 0x80);
if (type == 0xFF && frameSize == 0) {
receivedClosingHandshake = true;
}
return new DefaultWebSocketFrame(
type, buffer.readBytes((int) frameSize));
}