View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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 io.netty.handler.codec.http.websocketx;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.ChannelHandler.Sharable;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.handler.codec.MessageToMessageEncoder;
23  import io.netty.util.LeakPresenceDetector;
24  
25  import java.util.List;
26  
27  /**
28   * Encodes a {@link WebSocketFrame} into a {@link ByteBuf}.
29   * <p>
30   * For the detailed instruction on adding add Web Socket support to your HTTP server, take a look into the
31   * <tt>WebSocketServer</tt> example located in the {@code io.netty.example.http.websocket} package.
32   */
33  @Sharable
34  public class WebSocket00FrameEncoder extends MessageToMessageEncoder<WebSocketFrame> implements WebSocketFrameEncoder {
35      private static final ByteBuf _0X00 = LeakPresenceDetector.staticInitializer(() -> Unpooled.unreleasableBuffer(
36              Unpooled.directBuffer(1, 1).writeByte(0x00)).asReadOnly());
37      private static final ByteBuf _0XFF = LeakPresenceDetector.staticInitializer(() -> Unpooled.unreleasableBuffer(
38              Unpooled.directBuffer(1, 1).writeByte((byte) 0xFF)).asReadOnly());
39      private static final ByteBuf _0XFF_0X00 = LeakPresenceDetector.staticInitializer(() -> Unpooled.unreleasableBuffer(
40              Unpooled.directBuffer(2, 2).writeByte((byte) 0xFF).writeByte(0x00)).asReadOnly());
41  
42      public WebSocket00FrameEncoder() {
43          super(WebSocketFrame.class);
44      }
45  
46      @Override
47      protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
48          if (msg instanceof TextWebSocketFrame) {
49              // Text frame
50              ByteBuf data = msg.content();
51  
52              out.add(_0X00.duplicate());
53              out.add(data.retain());
54              out.add(_0XFF.duplicate());
55          } else if (msg instanceof CloseWebSocketFrame) {
56              // Close frame, needs to call duplicate to allow multiple writes.
57              // See https://github.com/netty/netty/issues/2768
58              out.add(_0XFF_0X00.duplicate());
59          } else {
60              // Binary frame
61              ByteBuf data = msg.content();
62              int dataLen = data.readableBytes();
63  
64              ByteBuf buf = ctx.alloc().buffer(5);
65              boolean release = true;
66              try {
67                  // Encode type.
68                  buf.writeByte((byte) 0x80);
69  
70                  // Encode length.
71                  int b1 = dataLen >>> 28 & 0x7F;
72                  int b2 = dataLen >>> 14 & 0x7F;
73                  int b3 = dataLen >>> 7 & 0x7F;
74                  int b4 = dataLen & 0x7F;
75                  if (b1 == 0) {
76                      if (b2 == 0) {
77                          if (b3 != 0) {
78                              buf.writeByte(b3 | 0x80);
79                          }
80                          buf.writeByte(b4);
81                      } else {
82                          buf.writeByte(b2 | 0x80);
83                          buf.writeByte(b3 | 0x80);
84                          buf.writeByte(b4);
85                      }
86                  } else {
87                      buf.writeByte(b1 | 0x80);
88                      buf.writeByte(b2 | 0x80);
89                      buf.writeByte(b3 | 0x80);
90                      buf.writeByte(b4);
91                  }
92  
93                  // Encode binary data.
94                  out.add(buf);
95                  out.add(data.retain());
96                  release = false;
97              } finally {
98                  if (release) {
99                      buf.release();
100                 }
101             }
102         }
103     }
104 }