Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package io.undertow.websockets.core.protocol.version07;
 
 
 import org.xnio.Pool;
 
 import java.util.Set;


 
 public class WebSocket07Channel extends WebSocketChannel {
 
     private enum State {
         READING_FIRST,
         READING_SECOND,
         READING_EXTENDED_SIZE1,
         READING_EXTENDED_SIZE2,
         READING_EXTENDED_SIZE3,
         READING_EXTENDED_SIZE4,
         READING_EXTENDED_SIZE5,
         READING_EXTENDED_SIZE6,
         READING_EXTENDED_SIZE7,
         READING_EXTENDED_SIZE8,
         READING_MASK_1,
         READING_MASK_2,
         READING_MASK_3,
         READING_MASK_4,
         DONE,
     }
 
     private int fragmentedFramesCount;
     private final ByteBuffer lengthBuffer = ByteBuffer.allocate(8);
 
     private UTF8Checker checker;
 
     protected static final byte OPCODE_CONT = 0x0;
     protected static final byte OPCODE_TEXT = 0x1;
     protected static final byte OPCODE_BINARY = 0x2;
     protected static final byte OPCODE_CLOSE = 0x8;
     protected static final byte OPCODE_PING = 0x9;
     protected static final byte OPCODE_PONG = 0xA;
 
     private static final ChannelFunction[] EMPTY_FUNCTIONS = new ChannelFunction[0];

    
Create a new WebSocket07Channel

Parameters:
channel The org.xnio.StreamConnection over which the WebSocket Frames should get send and received. Be aware that it already must be "upgraded".
bufferPool The org.xnio.Pool which will be used to acquire java.nio.ByteBuffer's from.
wsUrl The url for which the WebSocket07Channel was created.
 
     public WebSocket07Channel(StreamConnection channelPool<ByteBufferbufferPool,
                               String wsUrlString subProtocolfinal boolean clientboolean allowExtensionsSet<WebSocketChannelopenConnections) {
         super(channelbufferPool.wsUrlsubProtocolclientallowExtensionsopenConnections);
     }
 
     @Override
     protected PartialFrame receiveFrame() {
         return new WebSocketFrameHeader();
     }
 
     @Override
    protected void markReadsBroken(Throwable cause) {
        super.markReadsBroken(cause);
    }
    @Override
    protected void closeSubChannels() {
        IoUtils.safeClose();
    }
    @Override
    protected StreamSinkFrameChannel createStreamSinkChannel(WebSocketFrameType typelong payloadSize) {
        switch (type) {
            case :
                return new WebSocket07TextFrameSinkChannel(thispayloadSize);
            case :
                return new WebSocket07BinaryFrameSinkChannel(thispayloadSize);
            case :
                return new WebSocket07CloseFrameSinkChannel(thispayloadSize);
            case :
                return new WebSocket07PongFrameSinkChannel(thispayloadSize);
            case :
                return new WebSocket07PingFrameSinkChannel(thispayloadSize);
            default:
                throw ..unsupportedFrameType(type);
        }
    }
    class WebSocketFrameHeader implements WebSocketFrame {
        private boolean frameFinalFlag;
        private int frameRsv;
        private int frameOpcode;
        private int maskingKey;
        private boolean frameMasked;
        private long framePayloadLength;
        private State state = .;
        private int framePayloadLen1;
        private boolean done = false;
        @Override
        public StreamSourceFrameChannel getChannel(Pooled<ByteBufferpooled) {
            StreamSourceFrameChannel channel = createChannel(pooled);
            if () {
                channel.finalFrame();
            } else {
                 = channel;
            }
            return channel;
        }
        public StreamSourceFrameChannel createChannel(Pooled<ByteBufferpooled) {
            // Processing ping/pong/close frames because they cannot be
            // fragmented as per spec
            if ( == ) {
                if () {
                    return new WebSocket07PingFrameSourceChannel(WebSocket07Channel.thisnew Masker(), pooled);
                } else {
                    return new WebSocket07PingFrameSourceChannel(WebSocket07Channel.thispooled);
                }
            }
            if ( == ) {
                if () {
                    return new WebSocket07PongFrameSourceChannel(WebSocket07Channel.thisnew Masker(), pooled);
                } else {
                    return new WebSocket07PongFrameSourceChannel(WebSocket07Channel.thispooled);
                }
            }
            if ( == ) {
                if () {
                    return new WebSocket07CloseFrameSourceChannel(WebSocket07Channel.thisnew Masker(), pooled);
                } else {
                    return new WebSocket07CloseFrameSourceChannel(WebSocket07Channel.thispooled);
                }
            }
            if ( == ) {
                // try to grab the checker which was used before
                UTF8Checker checker = WebSocket07Channel.this.;
                if (checker == null) {
                    checker = new UTF8Checker();
                }
                if (!) {
                    // if this is not the final fragment store the used checker to use it in later fragments also
                    WebSocket07Channel.this. = checker;
                } else {
                    // was the final fragment reset the checker to null
                    WebSocket07Channel.this. = null;
                }
                if () {
                    return new WebSocket07TextFrameSourceChannel(WebSocket07Channel.thisnew Masker(), checkerpooled);
                } else {
                    return new WebSocket07TextFrameSourceChannel(WebSocket07Channel.thischeckerpooled);
                }
            } else if ( == ) {
                if () {
                    return new WebSocket07BinaryFrameSourceChannel(WebSocket07Channel.thisnew Masker(), pooled);
                } else {
                    return new WebSocket07BinaryFrameSourceChannel(WebSocket07Channel.thispooled);
                }
            } else if ( == ) {
                final ChannelFunction[] functions;
                if ( &&  != null) {
                    functions = new ChannelFunction[2];
                    functions[0] = new Masker();
                    functions[1] = ;
                } else if () {
                    functions = new ChannelFunction[1];
                    functions[0] = new Masker();
                } else if ( != null) {
                    functions = new ChannelFunction[1];
                    functions[0] = ;
                } else {
                    functions = ;
                }
                if () {
                    return new WebSocket07ContinuationFrameSourceChannel(WebSocket07Channel.thispooledfunctions);
                } else {
                    return new WebSocket07ContinuationFrameSourceChannel(WebSocket07Channel.thispooledfunctions);
                }
            } else {
                throw ..unsupportedOpCode();
            }
        }
        @Override
        public void handle(final ByteBuffer bufferthrows WebSocketException {
            if (!buffer.hasRemaining()) {
                return;
            }
            while ( != .) {
                byte b;
                switch () {
                    case :
                        // Read FIN, RSV, OPCODE
                        b = buffer.get();
                         = (b & 0x80) != 0;
                         = (b & 0x70) >> 4;
                         = b & 0x0F;
                        if (..isDebugEnabled()) {
                            ..decodingFrameWithOpCode();
                        }
                         = .;
                        // clear the lengthBuffer to reuse it later
                        .clear();
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        // Read MASK, PAYLOAD LEN 1
                        //
                         = (b & 0x80) != 0;
                         = b & 0x7F;
                        if ( != 0 && !areExtensionsSupported()) {
                            throw ..extensionsNotAllowed();
                        }
                        if ( > 7) { // control frame (have MSB in opcode set)
                            validateControlFrame();
                        } else { // data frame
                            validateDataFrame();
                        }
                        if ( == 126 ||  == 127) {
                             = .;
                        } else {
                             = ;
                            if () {
                                 = .;
                            } else {
                                 = .;
                            }
                            continue;
                        }
                    case :
                        // Read frame payload length
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                        if ( == 126) {
                            .flip();
                            // must be unsigned short
                             = .getShort() & 0xFFFF;
                            if () {
                                 = .;
                            } else {
                                 = .;
                            }
                            continue;
                        }
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                        .put(b);
                        .flip();
                         = .getLong();
                        if () {
                             = .;
                        } else {
                             = .;
                            break;
                        }
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                         = b & 0xFF;
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                         =  << 8 | b & 0xFF;
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                         =  << 8 | b & 0xFF;
                         = .;
                    case :
                        if (!buffer.hasRemaining()) {
                            return;
                        }
                        b = buffer.get();
                         =  << 8 | b & 0xFF;
                         = .;
                        break;
                    default:
                        throw new IllegalStateException(.toString());
                }
            }
            if () {
                // check if the frame is a ping frame as these are allowed in the middle
                if ( != ) {
                     = 0;
                }
            } else {
                // Increment counter
                ++;
            }
             = true;
        }
        private void validateDataFrame() throws WebSocketFrameCorruptedException {
            if (!isClient() && !) {
                throw ..frameNotMasked();
            }
            // check for reserved data frame opcodes
            if (!( ==  ||  ==  ||  == )) {
                throw ..reservedOpCodeInDataFrame();
            }
            // check opcode vs message fragmentation state 1/2
            if ( == 0 &&  == ) {
                throw ..continuationFrameOutsideFragmented();
            }
            // check opcode vs message fragmentation state 2/2
            if ( != 0 &&  != ) {
                throw ..nonContinuationFrameInsideFragmented();
            }
        }
        private void validateControlFrame() throws WebSocketFrameCorruptedException {
            // control frames MUST NOT be fragmented
            if (!) {
                throw ..fragmentedControlFrame();
            }
            // control frames MUST have payload 125 octets or less as stated in the spec
            if ( > 125) {
                throw ..toBigControlFrame();
            }
            // check for reserved control frame opcodes
            if (!( ==  ||  ==  ||  == )) {
            }
            // close frame : if there is a body, the first two bytes of the
            // body MUST be a 2-byte unsigned integer (in network byte
            // order) representing a status code
            if ( == 8 &&  == 1) {
                throw ..controlFrameWithPayloadLen1();
            }
        }
        @Override
        public boolean isDone() {
            return ;
        }
        @Override
        public long getFrameLength() {
            return ;
        }
        int getMaskingKey() {
            return ;
        }
        @Override
        public AbstractFramedStreamSourceChannel<?, ?, ?> getExistingChannel() {
            if ( == ) {
                StreamSourceFrameChannel ret = ;
                if() {
                     = null;
                }
                return ret;
            }
            return null;
        }
        @Override
        public boolean isFinalFragment() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X