View Javadoc
1   /*
2    * Copyright 2016 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.netty5.handler.codec.http2;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.buffer.api.BufferHolder;
20  import io.netty5.util.Send;
21  import io.netty5.util.internal.StringUtil;
22  import io.netty5.util.internal.UnstableApi;
23  
24  import static io.netty5.buffer.api.DefaultBufferAllocators.onHeapAllocator;
25  import static io.netty5.util.internal.ObjectUtil.checkPositiveOrZero;
26  
27  /**
28   * The default {@link Http2GoAwayFrame} implementation.
29   */
30  @UnstableApi
31  public final class DefaultHttp2GoAwayFrame extends BufferHolder<Http2GoAwayFrame> implements Http2GoAwayFrame {
32      private final long errorCode;
33      private final int lastStreamId;
34      private int extraStreamIds;
35  
36      /**
37       * Equivalent to {@code new DefaultHttp2GoAwayFrame(error.code())}.
38       *
39       * @param error non-{@code null} reason for the go away
40       */
41      public DefaultHttp2GoAwayFrame(Http2Error error) {
42          this(error.code());
43      }
44  
45      /**
46       * Equivalent to {@code new DefaultHttp2GoAwayFrame(content, Unpooled.EMPTY_BUFFER)}.
47       *
48       * @param errorCode reason for the go away
49       */
50      public DefaultHttp2GoAwayFrame(long errorCode) {
51          this(-1, errorCode, onHeapAllocator().allocate(0));
52      }
53  
54      /**
55       *
56       *
57       * @param error non-{@code null} reason for the go away
58       * @param content non-{@code null} debug data
59       */
60      public DefaultHttp2GoAwayFrame(Http2Error error, Send<Buffer> content) {
61          this(error.code(), content);
62      }
63  
64      /**
65       * Construct a new GOAWAY message.
66       *
67       * @param errorCode reason for the go away
68       * @param content non-{@code null} debug data
69       */
70      public DefaultHttp2GoAwayFrame(long errorCode, Send<Buffer> content) {
71          this(-1, errorCode, content.receive());
72      }
73  
74      /**
75       * Construct a new GOAWAY message.
76       *
77       * This constructor is for internal use only. A user should not have to specify a specific last stream identifier,
78       * but use {@link #setExtraStreamIds(int)} instead.
79       */
80      DefaultHttp2GoAwayFrame(int lastStreamId, long errorCode, Send<Buffer> content) {
81          this(lastStreamId, errorCode, content.receive());
82      }
83  
84      private DefaultHttp2GoAwayFrame(int lastStreamId, long errorCode, Buffer content) {
85          super(content);
86          this.errorCode = errorCode;
87          this.lastStreamId = lastStreamId;
88      }
89  
90      @Override
91      public String name() {
92          return "GOAWAY";
93      }
94  
95      @Override
96      public long errorCode() {
97          return errorCode;
98      }
99  
100     @Override
101     public int extraStreamIds() {
102         return extraStreamIds;
103     }
104 
105     @Override
106     public Http2GoAwayFrame setExtraStreamIds(int extraStreamIds) {
107         checkPositiveOrZero(extraStreamIds, "extraStreamIds");
108         this.extraStreamIds = extraStreamIds;
109         return this;
110     }
111 
112     @Override
113     public int lastStreamId() {
114         return lastStreamId;
115     }
116 
117     @Override
118     public Buffer content() {
119         return getBuffer();
120     }
121 
122     @Override
123     public Http2GoAwayFrame copy() {
124         return new DefaultHttp2GoAwayFrame(lastStreamId, errorCode, content().copy());
125     }
126 
127     @Override
128     protected Http2GoAwayFrame receive(Buffer buf) {
129         return new DefaultHttp2GoAwayFrame(lastStreamId, errorCode, buf);
130     }
131 
132     @Override
133     public Http2GoAwayFrame touch(Object hint) {
134         super.touch(hint);
135         return this;
136     }
137 
138     @Override
139     public boolean equals(Object o) {
140         if (!(o instanceof DefaultHttp2GoAwayFrame)) {
141             return false;
142         }
143         DefaultHttp2GoAwayFrame other = (DefaultHttp2GoAwayFrame) o;
144         return errorCode == other.errorCode && extraStreamIds == other.extraStreamIds && super.equals(other);
145     }
146 
147     @Override
148     public int hashCode() {
149         int hash = super.hashCode();
150         hash = hash * 31 + (int) (errorCode ^ errorCode >>> 32);
151         hash = hash * 31 + extraStreamIds;
152         return hash;
153     }
154 
155     @Override
156     public String toString() {
157         return StringUtil.simpleClassName(this) + "(errorCode=" + errorCode + ", content=" + content()
158                + ", extraStreamIds=" + extraStreamIds + ", lastStreamId=" + lastStreamId + ')';
159     }
160 }