|
| 1 | +/* |
| 2 | + * Copyright 2020 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.incubator.codec.http3; |
| 17 | + |
| 18 | +import io.netty.buffer.ByteBufAllocator; |
| 19 | +import io.netty.channel.ChannelConfig; |
| 20 | +import io.netty.channel.ChannelFuture; |
| 21 | +import io.netty.channel.ChannelHandler; |
| 22 | +import io.netty.channel.ChannelOption; |
| 23 | +import io.netty.channel.ChannelPromise; |
| 24 | +import io.netty.channel.DefaultChannelId; |
| 25 | +import io.netty.channel.MessageSizeEstimator; |
| 26 | +import io.netty.channel.RecvByteBufAllocator; |
| 27 | +import io.netty.channel.WriteBufferWaterMark; |
| 28 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 29 | +import io.netty.incubator.codec.quic.QuicChannel; |
| 30 | +import io.netty.incubator.codec.quic.QuicStreamAddress; |
| 31 | +import io.netty.incubator.codec.quic.QuicStreamChannel; |
| 32 | +import io.netty.incubator.codec.quic.QuicStreamChannelConfig; |
| 33 | +import io.netty.incubator.codec.quic.QuicStreamType; |
| 34 | + |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +final class EmbeddedQuicStreamChannel extends EmbeddedChannel implements QuicStreamChannel { |
| 38 | + private final boolean localCreated; |
| 39 | + private final QuicStreamType type; |
| 40 | + private final long id; |
| 41 | + private QuicStreamChannelConfig config; |
| 42 | + private boolean inputShutdown; |
| 43 | + private boolean outputShutdown; |
| 44 | + |
| 45 | + EmbeddedQuicStreamChannel(ChannelHandler... handlers) { |
| 46 | + this(null, false, QuicStreamType.BIDIRECTIONAL, 0, handlers); |
| 47 | + } |
| 48 | + |
| 49 | + EmbeddedQuicStreamChannel(boolean localCreated, QuicStreamType type, long id, ChannelHandler... handlers) { |
| 50 | + this(null, localCreated, type, id, handlers); |
| 51 | + } |
| 52 | + |
| 53 | + EmbeddedQuicStreamChannel(QuicChannel parent, boolean localCreated, QuicStreamType type, |
| 54 | + long id, ChannelHandler... handlers) { |
| 55 | + super(parent, DefaultChannelId.newInstance(), true, false, handlers); |
| 56 | + this.localCreated = localCreated; |
| 57 | + this.type = type; |
| 58 | + this.id = id; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public QuicStreamAddress localAddress() { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public QuicStreamAddress remoteAddress() { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public QuicChannel parent() { |
| 73 | + return (QuicChannel) super.parent(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public QuicStreamChannelConfig config() { |
| 78 | + if (config == null) { |
| 79 | + config = new EmbeddedQuicStreamChannelConfig(super.config()); |
| 80 | + } |
| 81 | + return config; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean isLocalCreated() { |
| 86 | + return localCreated; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public QuicStreamType type() { |
| 91 | + return type; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public long streamId() { |
| 96 | + return id; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean isInputShutdown() { |
| 101 | + return inputShutdown; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public ChannelFuture shutdownInput() { |
| 106 | + inputShutdown = true; |
| 107 | + return newSucceededFuture(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public ChannelFuture shutdownInput(ChannelPromise promise) { |
| 112 | + inputShutdown = true; |
| 113 | + return promise.setSuccess(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean isOutputShutdown() { |
| 118 | + return outputShutdown; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public ChannelFuture shutdownOutput() { |
| 123 | + outputShutdown = true; |
| 124 | + return newSucceededFuture(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public ChannelFuture shutdownOutput(ChannelPromise promise) { |
| 129 | + outputShutdown = true; |
| 130 | + return promise.setSuccess(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public boolean isShutdown() { |
| 135 | + return outputShutdown; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public ChannelFuture shutdown() { |
| 140 | + inputShutdown = true; |
| 141 | + outputShutdown = true; |
| 142 | + return newSucceededFuture(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public ChannelFuture shutdown(ChannelPromise promise) { |
| 147 | + inputShutdown = true; |
| 148 | + outputShutdown = true; |
| 149 | + return promise.setSuccess(); |
| 150 | + } |
| 151 | + |
| 152 | + private static final class EmbeddedQuicStreamChannelConfig implements QuicStreamChannelConfig { |
| 153 | + private final ChannelConfig config; |
| 154 | + private boolean allowHalfClosure; |
| 155 | + |
| 156 | + EmbeddedQuicStreamChannelConfig(ChannelConfig config) { |
| 157 | + this.config = config; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public QuicStreamChannelConfig setReadFrames(boolean readFrames) { |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean isReadFrames() { |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public QuicStreamChannelConfig setAllowHalfClosure(boolean allowHalfClosure) { |
| 172 | + this.allowHalfClosure = allowHalfClosure; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public QuicStreamChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) { |
| 178 | + config.setMaxMessagesPerRead(maxMessagesPerRead); |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public QuicStreamChannelConfig setWriteSpinCount(int writeSpinCount) { |
| 184 | + config.setWriteSpinCount(writeSpinCount); |
| 185 | + return this; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public QuicStreamChannelConfig setAllocator(ByteBufAllocator allocator) { |
| 190 | + config.setAllocator(allocator); |
| 191 | + return this; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public QuicStreamChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) { |
| 196 | + config.setRecvByteBufAllocator(allocator); |
| 197 | + return this; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public QuicStreamChannelConfig setAutoRead(boolean autoRead) { |
| 202 | + config.setAutoRead(autoRead); |
| 203 | + return this; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public QuicStreamChannelConfig setAutoClose(boolean autoClose) { |
| 208 | + config.setAutoClose(autoClose); |
| 209 | + return this; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public QuicStreamChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) { |
| 214 | + config.setMessageSizeEstimator(estimator); |
| 215 | + return this; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public QuicStreamChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) { |
| 220 | + config.setWriteBufferWaterMark(writeBufferWaterMark); |
| 221 | + return this; |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public QuicStreamChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) { |
| 226 | + config.setConnectTimeoutMillis(connectTimeoutMillis); |
| 227 | + return this; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public QuicStreamChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) { |
| 232 | + config.setWriteBufferHighWaterMark(writeBufferHighWaterMark); |
| 233 | + return this; |
| 234 | + } |
| 235 | + |
| 236 | + @Override |
| 237 | + public QuicStreamChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) { |
| 238 | + config.setWriteBufferLowWaterMark(writeBufferLowWaterMark); |
| 239 | + return this; |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public boolean isAllowHalfClosure() { |
| 244 | + return allowHalfClosure; |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public Map<ChannelOption<?>, Object> getOptions() { |
| 249 | + return config.getOptions(); |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + public boolean setOptions(Map<ChannelOption<?>, ?> options) { |
| 254 | + return config.setOptions(options); |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public <T> T getOption(ChannelOption<T> option) { |
| 259 | + return config.getOption(option); |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public <T> boolean setOption(ChannelOption<T> option, T value) { |
| 264 | + return config.setOption(option, value); |
| 265 | + } |
| 266 | + |
| 267 | + @Override |
| 268 | + public int getConnectTimeoutMillis() { |
| 269 | + return config.getConnectTimeoutMillis(); |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public int getMaxMessagesPerRead() { |
| 274 | + return config.getMaxMessagesPerRead(); |
| 275 | + } |
| 276 | + |
| 277 | + @Override |
| 278 | + public int getWriteSpinCount() { |
| 279 | + return config.getWriteSpinCount(); |
| 280 | + } |
| 281 | + |
| 282 | + @Override |
| 283 | + public ByteBufAllocator getAllocator() { |
| 284 | + return config.getAllocator(); |
| 285 | + } |
| 286 | + |
| 287 | + @Override |
| 288 | + public <T extends RecvByteBufAllocator> T getRecvByteBufAllocator() { |
| 289 | + return config.getRecvByteBufAllocator(); |
| 290 | + } |
| 291 | + |
| 292 | + @Override |
| 293 | + public boolean isAutoRead() { |
| 294 | + return config.isAutoRead(); |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public boolean isAutoClose() { |
| 299 | + return config.isAutoClose(); |
| 300 | + } |
| 301 | + |
| 302 | + @Override |
| 303 | + public int getWriteBufferHighWaterMark() { |
| 304 | + return config.getWriteBufferHighWaterMark(); |
| 305 | + } |
| 306 | + |
| 307 | + @Override |
| 308 | + public int getWriteBufferLowWaterMark() { |
| 309 | + return config.getWriteBufferLowWaterMark(); |
| 310 | + } |
| 311 | + |
| 312 | + @Override |
| 313 | + public MessageSizeEstimator getMessageSizeEstimator() { |
| 314 | + return config.getMessageSizeEstimator(); |
| 315 | + } |
| 316 | + |
| 317 | + @Override |
| 318 | + public WriteBufferWaterMark getWriteBufferWaterMark() { |
| 319 | + return config.getWriteBufferWaterMark(); |
| 320 | + } |
| 321 | + } |
| 322 | +} |