View Javadoc
1   /*
2    * Copyright 2024 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.util.concurrent.ThreadAwareExecutor;
19  /**
20   * A handle that can be registered to an {@link IoHandler}.
21   * All methods must be called from the {@link ThreadAwareExecutor} thread (which means
22   * {@link ThreadAwareExecutor#isExecutorThread(Thread)} must return {@code true}).
23   *<p>
24   * All the methods are expected to be called from the {@link IoHandler} on which this {@link IoHandle}
25   * was registered via {@link IoHandler#register(IoHandle)}.
26   */
27  public interface IoHandle extends AutoCloseable {
28  
29      /**
30       * Be called once there is something to handle.
31       *
32       * @param registration  the {@link IoRegistration} for this {@link IoHandle}.
33       * @param ioEvent       the {@link IoEvent} that must be handled. The {@link IoEvent} is only valid
34       *                      while this method is executed and so must not escape it.
35       */
36      void handle(IoRegistration registration, IoEvent ioEvent);
37  
38      /**
39       * Called once this {@link IoHandle} was registered and so will start to receive events
40       * via {@link #handle(IoRegistration, IoEvent)}.
41       */
42      default void registered() {
43          // Noop by default.
44      }
45  
46      /**
47       * Called once this {@link IoHandle} was unregistered and so will not receive any more events
48       * via {@link #handle(IoRegistration, IoEvent)}.
49       */
50      default void unregistered() {
51          // Noop by default.
52      }
53  
54      /**
55       * Called once the {@link IoHandle} should be closed. Even once this method is called this handle might
56       * still receive events via {@link #handle(IoRegistration, IoEvent)} (if it was previous be registered and so its
57       * {@link #registered()} method was called) until the {@link #unregistered()} method is called.
58       */
59      void close() throws Exception;
60  }