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    *   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 io.netty.handler.codec.http2;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.DefaultByteBufHolder;
20  import io.netty.buffer.Unpooled;
21  import io.netty.util.internal.StringUtil;
22  import io.netty.util.internal.UnstableApi;
23  
24  /**
25   * The default {@link Http2GoAwayFrame} implementation.
26   */
27  @UnstableApi
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         if (extraStreamIds < 0) {
102             throw new IllegalArgumentException("extraStreamIds must be non-negative");
103         }
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 }