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