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 */
49 int run(IoHandlerContext context);
50
51 /**
52 * Prepare to destroy this {@link IoHandler}. This method will be called before {@link #destroy()} and may be
53 * called multiple times.
54 */
55 default void prepareToDestroy() { }
56
57 /**
58 * Destroy the {@link IoHandler} and free all its resources. Once destroyed using the {@link IoHandler} will
59 * cause undefined behaviour.
60 */
61 default void destroy() { }
62
63 /**
64 * Register a {@link IoHandle} for IO.
65 *
66 * @param handle the {@link IoHandle} to register.
67 * @throws Exception thrown if an error happens during registration.
68 */
69 IoRegistration register(IoHandle handle) throws Exception;
70
71 /**
72 * Wakeup the {@link IoHandler}, which means if any operation blocks it should be unblocked and
73 * return as soon as possible.
74 */
75 void wakeup();
76
77 /**
78 * Returns {@code true} if the given type is compatible with this {@link IoHandler} and so can be registered,
79 * {@code false} otherwise.
80 *
81 * @param handleType the type of the {@link IoHandle}.
82 * @return if compatible of not.
83 */
84 boolean isCompatible(Class<? extends IoHandle> handleType);
85 }