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  /**
21   * Handles IO dispatching for an {@link ThreadAwareExecutor}.
22   * All operations except {@link #wakeup()} and {@link #isCompatible(Class)} <strong>MUST</strong> be executed
23   * on the {@link ThreadAwareExecutor} thread (which means {@link ThreadAwareExecutor#isExecutorThread(Thread)} must
24   * return {@code true}) and should never be called from the user-directly.
25   * <p>
26   * Once a {@link IoHandle} is registered via the {@link #register(IoHandle)} method it's possible
27   * to submit {@link IoOps} related to the {@link IoHandle} via {@link IoRegistration#submit(IoOps)}.
28   * These submitted {@link IoOps} are the "source" of {@link IoEvent}s that are dispatched to the registered
29   * {@link IoHandle} via the {@link IoHandle#handle(IoRegistration, IoEvent)} method.
30   * These events must be consumed (and handled) as otherwise they might be reported again until handled.
31   *
32   */
33  public interface IoHandler {
34  
35      /**
36       * Initialize this {@link IoHandler}.
37       */
38      default void initialize() { }
39  
40      /**
41       * Run the IO handled by this {@link IoHandler}. The {@link IoHandlerContext} should be used
42       * to ensure we not execute too long and so block the processing of other task that are
43       * scheduled on the {@link ThreadAwareExecutor}. This is done by taking {@link IoHandlerContext#delayNanos(long)}
44       * or {@link IoHandlerContext#deadlineNanos()} into account.
45       *
46       * @param  context  the {@link IoHandlerContext}.
47       * @return          the number of {@link IoHandle} for which I/O was handled.
48       *                  Internal events such as wakeups and timer expirations must not be included in this count.
49       */
50      int run(IoHandlerContext context);
51  
52      /**
53       * Prepare to destroy this {@link IoHandler}. This method will be called before {@link #destroy()} and may be
54       * called multiple times.
55       */
56      default void prepareToDestroy() { }
57  
58      /**
59       * Destroy the {@link IoHandler} and free all its resources. Once destroyed using the {@link IoHandler} will
60       * cause undefined behaviour.
61       */
62      default void destroy() { }
63  
64      /**
65       * Register a {@link IoHandle} for IO.
66       *
67       * @param handle        the {@link IoHandle} to register.
68       * @throws Exception    thrown if an error happens during registration.
69       */
70      IoRegistration register(IoHandle handle) throws Exception;
71  
72      /**
73       * Wakeup the {@link IoHandler}, which means if any operation blocks it should be unblocked and
74       * return as soon as possible.
75       */
76      void wakeup();
77  
78      /**
79       * Returns {@code true} if the given type is compatible with this {@link IoHandler} and so can be registered,
80       * {@code false} otherwise.
81       *
82       * @param handleType the type of the {@link IoHandle}.
83       * @return if compatible of not.
84       */
85      boolean isCompatible(Class<? extends IoHandle> handleType);
86  }