1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.http2;
18
19 import io.netty.channel.ChannelDuplexHandler;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.channel.ChannelPipeline;
22 import io.netty.util.internal.StringUtil;
23 import io.netty.util.internal.UnstableApi;
24
25
26
27
28
29
30
31
32
33
34
35 @UnstableApi
36 public abstract class Http2ChannelDuplexHandler extends ChannelDuplexHandler {
37
38 private volatile Http2FrameCodec frameCodec;
39
40 @Override
41 public final void handlerAdded(ChannelHandlerContext ctx) throws Exception {
42 frameCodec = requireHttp2FrameCodec(ctx);
43 handlerAdded0(ctx);
44 }
45
46 protected void handlerAdded0(@SuppressWarnings("unused") ChannelHandlerContext ctx) throws Exception {
47
48 }
49
50 @Override
51 public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
52 try {
53 handlerRemoved0(ctx);
54 } finally {
55 frameCodec = null;
56 }
57 }
58
59 protected void handlerRemoved0(@SuppressWarnings("unused") ChannelHandlerContext ctx) throws Exception {
60
61 }
62
63
64
65
66
67
68 public final Http2FrameStream newStream() {
69 Http2FrameCodec codec = frameCodec;
70 if (codec == null) {
71 throw new IllegalStateException(StringUtil.simpleClassName(Http2FrameCodec.class) + " not found." +
72 " Has the handler been added to a pipeline?");
73 }
74 return codec.newStream();
75 }
76
77
78
79
80
81
82 protected final void forEachActiveStream(Http2FrameStreamVisitor streamVisitor) throws Http2Exception {
83 frameCodec.forEachActiveStream(streamVisitor);
84 }
85
86 private static Http2FrameCodec requireHttp2FrameCodec(ChannelHandlerContext ctx) {
87 ChannelHandlerContext frameCodecCtx = ctx.pipeline().context(Http2FrameCodec.class);
88 if (frameCodecCtx == null) {
89 throw new IllegalArgumentException(Http2FrameCodec.class.getSimpleName()
90 + " was not found in the channel pipeline.");
91 }
92 return (Http2FrameCodec) frameCodecCtx.handler();
93 }
94 }