View Javadoc

    1   /*
    2    * Copyright 2010 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.http.websocket;
    17
    18  import org.jboss.netty.buffer.ChannelBuffer;
    19  import org.jboss.netty.channel.Channel;
    20  import org.jboss.netty.channel.ChannelHandlerContext;
    21  import org.jboss.netty.channel.ChannelHandler.Sharable;
    22  import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
    23
    24  /**
    25   * Encodes a {@link WebSocketFrame} into a {@link ChannelBuffer}.
    26   * <p>
    27   * For the detailed instruction on adding add Web Socket support to your HTTP
    28   * server, take a look into the <tt>WebSocketServer</tt> example located in the
    29   * {@code org.jboss.netty.example.http.websocket} package.
    30   *
    31   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    32   * @author Mike Heath (mheath@apache.org)
    33   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    34   * @version $Rev: 2362 $, $Date: 2010-09-09 19:59:22 +0900 (Thu, 09 Sep 2010) $
    35   *
    36   * @apiviz.landmark
    37   * @apiviz.uses org.jboss.netty.handler.codec.http.websocket.WebSocketFrame
    38   */
    39  @Sharable
    40  public class WebSocketFrameEncoder extends OneToOneEncoder {
    41
    42      @Override
    43      protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
    44          if (msg instanceof WebSocketFrame) {
    45              WebSocketFrame frame = (WebSocketFrame) msg;
    46              int type = frame.getType();
    47              if (frame.isText()) {
    48                  // Text frame
    49                  ChannelBuffer data = frame.getBinaryData();
    50                  ChannelBuffer encoded =
    51                      channel.getConfig().getBufferFactory().getBuffer(
    52                              data.order(), data.readableBytes() + 2);
    53                  encoded.writeByte((byte) type);
    54                  encoded.writeBytes(data, data.readerIndex(), data.readableBytes());
    55                  encoded.writeByte((byte) 0xFF);
    56                  return encoded;
    57              } else {
    58                  // Binary frame
    59                  ChannelBuffer data = frame.getBinaryData();
    60                  int dataLen = data.readableBytes();
    61                  ChannelBuffer encoded =
    62                      channel.getConfig().getBufferFactory().getBuffer(
    63                              data.order(), dataLen + 5);
    64
    65                  // Encode type.
    66                  encoded.writeByte((byte) type);
    67
    68                  // Encode length.
    69                  int b1 = dataLen >>> 28 & 0x7F;
    70                  int b2 = dataLen >>> 14 & 0x7F;
    71                  int b3 = dataLen >>> 7 & 0x7F;
    72                  int b4 = dataLen & 0x7F;
    73                  if (b1 == 0) {
    74                      if (b2 == 0) {
    75                          if (b3 == 0) {
    76                              encoded.writeByte(b4);
    77                          } else {
    78                              encoded.writeByte(b3 | 0x80);
    79                              encoded.writeByte(b4);
    80                          }
    81                      } else {
    82                          encoded.writeByte(b2 | 0x80);
    83                          encoded.writeByte(b3 | 0x80);
    84                          encoded.writeByte(b4);
    85                      }
    86                  } else {
    87                      encoded.writeByte(b1 | 0x80);
    88                      encoded.writeByte(b2 | 0x80);
    89                      encoded.writeByte(b3 | 0x80);
    90                      encoded.writeByte(b4);
    91                  }
    92
    93                  // Encode binary data.
    94                  encoded.writeBytes(data, data.readerIndex(), dataLen);
    95                  return encoded;
    96              }
    97          }
    98          return msg;
    99      }
    100 }