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
24
25
26
27
28
29
30
31
32
33
34 public abstract class Http2ChannelDuplexHandler extends ChannelDuplexHandler {
35
36 private volatile Http2FrameCodec frameCodec;
37
38 @Override
39 public final void handlerAdded(ChannelHandlerContext ctx) throws Exception {
40 frameCodec = requireHttp2FrameCodec(ctx);
41 handlerAdded0(ctx);
42 }
43
44 protected void handlerAdded0(@SuppressWarnings("unused") ChannelHandlerContext ctx) throws Exception {
45
46 }
47
48 @Override
49 public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
50 try {
51 handlerRemoved0(ctx);
52 } finally {
53 frameCodec = null;
54 }
55 }
56
57 protected void handlerRemoved0(@SuppressWarnings("unused") ChannelHandlerContext ctx) throws Exception {
58
59 }
60
61
62
63
64
65
66 public final Http2FrameStream newStream() {
67 Http2FrameCodec codec = frameCodec;
68 if (codec == null) {
69 throw new IllegalStateException(StringUtil.simpleClassName(Http2FrameCodec.class) + " not found." +
70 " Has the handler been added to a pipeline?");
71 }
72 return codec.newStream();
73 }
74
75
76
77
78
79
80 protected final void forEachActiveStream(Http2FrameStreamVisitor streamVisitor) throws Http2Exception {
81 frameCodec.forEachActiveStream(streamVisitor);
82 }
83
84 private static Http2FrameCodec requireHttp2FrameCodec(ChannelHandlerContext ctx) {
85 ChannelHandlerContext frameCodecCtx = ctx.pipeline().context(Http2FrameCodec.class);
86 if (frameCodecCtx == null) {
87 throw new IllegalArgumentException(Http2FrameCodec.class.getSimpleName()
88 + " was not found in the channel pipeline.");
89 }
90 return (Http2FrameCodec) frameCodecCtx.handler();
91 }
92 }