查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 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.channel;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.util.Attribute;
21  import io.netty.util.AttributeKey;
22  import io.netty.util.AttributeMap;
23  import io.netty.util.concurrent.EventExecutor;
24  
25  /**
26   * Enables a {@link ChannelHandler} to interact with its {@link ChannelPipeline}
27   * and other handlers. Among other things a handler can notify the next {@link ChannelHandler} in the
28   * {@link ChannelPipeline} as well as modify the {@link ChannelPipeline} it belongs to dynamically.
29   *
30   * <h3>Notify</h3>
31   *
32   * You can notify the closest handler in the same {@link ChannelPipeline} by calling one of the various methods
33   * provided here.
34   *
35   * Please refer to {@link ChannelPipeline} to understand how an event flows.
36   *
37   * <h3>Modifying a pipeline</h3>
38   *
39   * You can get the {@link ChannelPipeline} your handler belongs to by calling
40   * {@link #pipeline()}.  A non-trivial application could insert, remove, or
41   * replace handlers in the pipeline dynamically at runtime.
42   *
43   * <h3>Retrieving for later use</h3>
44   *
45   * You can keep the {@link ChannelHandlerContext} for later use, such as
46   * triggering an event outside the handler methods, even from a different thread.
47   * <pre>
48   * public class MyHandler extends {@link ChannelDuplexHandler} {
49   *
50   *     <b>private {@link ChannelHandlerContext} ctx;</b>
51   *
52   *     public void beforeAdd({@link ChannelHandlerContext} ctx) {
53   *         <b>this.ctx = ctx;</b>
54   *     }
55   *
56   *     public void login(String username, password) {
57   *         ctx.write(new LoginMessage(username, password));
58   *     }
59   *     ...
60   * }
61   * </pre>
62   *
63   * <h3>Storing stateful information</h3>
64   *
65   * {@link #attr(AttributeKey)} allow you to
66   * store and access stateful information that is related with a {@link ChannelHandler} / {@link Channel} and its
67   * context. Please refer to {@link ChannelHandler} to learn various recommended
68   * ways to manage stateful information.
69   *
70   * <h3>A handler can have more than one {@link ChannelHandlerContext}</h3>
71   *
72   * Please note that a {@link ChannelHandler} instance can be added to more than
73   * one {@link ChannelPipeline}.  It means a single {@link ChannelHandler}
74   * instance can have more than one {@link ChannelHandlerContext} and therefore
75   * the single instance can be invoked with different
76   * {@link ChannelHandlerContext}s if it is added to one or more {@link ChannelPipeline}s more than once.
77   * Also note that a {@link ChannelHandler} that is supposed to be added to multiple {@link ChannelPipeline}s should
78   * be marked as {@link io.netty.channel.ChannelHandler.Sharable}.
79   *
80   * <h3>Additional resources worth reading</h3>
81   * <p>
82   * Please refer to the {@link ChannelHandler}, and
83   * {@link ChannelPipeline} to find out more about inbound and outbound operations,
84   * what fundamental differences they have, how they flow in a  pipeline,  and how to handle
85   * the operation in your application.
86   */
87  public interface ChannelHandlerContext extends AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker {
88  
89      /**
90       * Return the {@link Channel} which is bound to the {@link ChannelHandlerContext}.
91       */
92      Channel channel();
93  
94      /**
95       * Returns the {@link EventExecutor} which is used to execute an arbitrary task.
96       */
97      EventExecutor executor();
98  
99      /**
100      * The unique name of the {@link ChannelHandlerContext}.The name was used when then {@link ChannelHandler}
101      * was added to the {@link ChannelPipeline}. This name can also be used to access the registered
102      * {@link ChannelHandler} from the {@link ChannelPipeline}.
103      */
104     String name();
105 
106     /**
107      * The {@link ChannelHandler} that is bound this {@link ChannelHandlerContext}.
108      */
109     ChannelHandler handler();
110 
111     /**
112      * Return {@code true} if the {@link ChannelHandler} which belongs to this context was removed
113      * from the {@link ChannelPipeline}. Note that this method is only meant to be called from with in the
114      * {@link EventLoop}.
115      */
116     boolean isRemoved();
117 
118     @Override
119     ChannelHandlerContext fireChannelRegistered();
120 
121     @Override
122     ChannelHandlerContext fireChannelUnregistered();
123 
124     @Override
125     ChannelHandlerContext fireChannelActive();
126 
127     @Override
128     ChannelHandlerContext fireChannelInactive();
129 
130     @Override
131     ChannelHandlerContext fireExceptionCaught(Throwable cause);
132 
133     @Override
134     ChannelHandlerContext fireUserEventTriggered(Object evt);
135 
136     @Override
137     ChannelHandlerContext fireChannelRead(Object msg);
138 
139     @Override
140     ChannelHandlerContext fireChannelReadComplete();
141 
142     @Override
143     ChannelHandlerContext fireChannelWritabilityChanged();
144 
145     @Override
146     ChannelHandlerContext read();
147 
148     @Override
149     ChannelHandlerContext flush();
150 
151     /**
152      * Return the assigned {@link ChannelPipeline}
153      */
154     ChannelPipeline pipeline();
155 
156     /**
157      * Return the assigned {@link ByteBufAllocator} which will be used to allocate {@link ByteBuf}s.
158      */
159     ByteBufAllocator alloc();
160 
161     /**
162      * @deprecated Use {@link Channel#attr(AttributeKey)}
163      */
164     @Deprecated
165     @Override
166     <T> Attribute<T> attr(AttributeKey<T> key);
167 
168     /**
169      * @deprecated Use {@link Channel#hasAttr(AttributeKey)}
170      */
171     @Deprecated
172     @Override
173     <T> boolean hasAttr(AttributeKey<T> key);
174 }