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.netty.handler.codec.http2;
17  
18  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
19  
20  import io.netty.buffer.ByteBuf;
21  import io.netty.buffer.DefaultByteBufHolder;
22  import io.netty.buffer.Unpooled;
23  import io.netty.util.internal.StringUtil;
24  import io.netty.util.internal.UnstableApi;
25  
26  /**
27   * The default {@link Http2GoAwayFrame} implementation.
28   */
29  @UnstableApi
30  public final class DefaultHttp2GoAwayFrame extends DefaultByteBufHolder implements Http2GoAwayFrame {
31  
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(errorCode, Unpooled.EMPTY_BUFFER);
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, ByteBuf 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, ByteBuf content) {
71          this(-1, errorCode, content);
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, ByteBuf content) {
81          super(content);
82          this.errorCode = errorCode;
83          this.lastStreamId = lastStreamId;
84      }
85  
86      @Override
87      public String name() {
88          return "GOAWAY";
89      }
90  
91      @Override
92      public long errorCode() {
93          return errorCode;
94      }
95  
96      @Override
97      public int extraStreamIds() {
98          return extraStreamIds;
99      }
100 
101     @Override
102     public Http2GoAwayFrame setExtraStreamIds(int extraStreamIds) {
103         checkPositiveOrZero(extraStreamIds, "extraStreamIds");
104         this.extraStreamIds = extraStreamIds;
105         return this;
106     }
107 
108     @Override
109     public int lastStreamId() {
110         return lastStreamId;
111     }
112 
113     @Override
114     public Http2GoAwayFrame copy() {
115         return new DefaultHttp2GoAwayFrame(lastStreamId, errorCode, content().copy());
116     }
117 
118     @Override
119     public Http2GoAwayFrame duplicate() {
120         return (Http2GoAwayFrame) super.duplicate();
121     }
122 
123     @Override
124     public Http2GoAwayFrame retainedDuplicate() {
125         return (Http2GoAwayFrame) super.retainedDuplicate();
126     }
127 
128     @Override
129     public Http2GoAwayFrame replace(ByteBuf content) {
130         return new DefaultHttp2GoAwayFrame(errorCode, content).setExtraStreamIds(extraStreamIds);
131     }
132 
133     @Override
134     public Http2GoAwayFrame retain() {
135         super.retain();
136         return this;
137     }
138 
139     @Override
140     public Http2GoAwayFrame retain(int increment) {
141         super.retain(increment);
142         return this;
143     }
144 
145     @Override
146     public Http2GoAwayFrame touch() {
147         super.touch();
148         return this;
149     }
150 
151     @Override
152     public Http2GoAwayFrame touch(Object hint) {
153         super.touch(hint);
154         return this;
155     }
156 
157     @Override
158     public boolean equals(Object o) {
159         if (!(o instanceof DefaultHttp2GoAwayFrame)) {
160             return false;
161         }
162         DefaultHttp2GoAwayFrame other = (DefaultHttp2GoAwayFrame) o;
163         return errorCode == other.errorCode && extraStreamIds == other.extraStreamIds && super.equals(other);
164     }
165 
166     @Override
167     public int hashCode() {
168         int hash = super.hashCode();
169         hash = hash * 31 + (int) (errorCode ^ errorCode >>> 32);
170         hash = hash * 31 + extraStreamIds;
171         return hash;
172     }
173 
174     @Override
175     public String toString() {
176         return StringUtil.simpleClassName(this) + "(errorCode=" + errorCode + ", content=" + content()
177                + ", extraStreamIds=" + extraStreamIds + ", lastStreamId=" + lastStreamId + ')';
178     }
179 }