1 /*
2 * Copyright 2018 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.netty5.channel;
17
18 /**
19 * Handles IO dispatching for an {@link EventLoop}
20 * All operations except {@link #wakeup(boolean)} and {@link #isCompatible(Class)} <strong>MUST</strong> be executed
21 * on the {@link EventLoop} thread and should never be called from the user-directly.
22 */
23 public interface IoHandler {
24 /**
25 * Run the IO handled by this {@link IoHandler}. The {@link IoExecutionContext} should be used
26 * to ensure we not execute too long and so block the processing of other task that are
27 * scheduled on the {@link EventLoop}. This is done by taking {@link IoExecutionContext#delayNanos(long)} or
28 * {@link IoExecutionContext#deadlineNanos()} into account.
29 *
30 * @return the number of {@link IoHandle} for which I/O was handled.
31 */
32 int run(IoExecutionContext context);
33
34 /**
35 * Prepare to destroy this {@link IoHandler}. This method will be called before {@link #destroy()} and may be
36 * called multiple times.
37 */
38 void prepareToDestroy();
39
40 /**
41 * Destroy the {@link IoHandler} and free all its resources.
42 */
43 void destroy();
44
45 /**
46 * Register a {@link Channel} for IO.
47 *
48 * @param channel the {@link Channel} to register..
49 * @throws Exception thrown if an error happens during registration.
50 */
51 void register(IoHandle channel) throws Exception;
52
53 /**
54 * Deregister a {@link IoHandle} for IO.
55 *
56 * @param handle the {@link IoHandle} to deregister..
57 * @throws Exception thrown if an error happens during deregistration.
58 */
59 void deregister(IoHandle handle) throws Exception;
60
61 /**
62 * Wakeup the {@link IoHandler}, which means if any operation blocks it should be unblocked and
63 * return as soon as possible.
64 */
65 void wakeup(boolean inEventLoop);
66
67 /**
68 * Returns {@code true} if the given type is compatible with this {@link IoHandler} and so can be registered,
69 * {@code false} otherwise.
70 *
71 * @param handleType the type of the {@link IoHandle}.
72 * @return if compatible of not.
73 */
74 boolean isCompatible(Class<? extends IoHandle> handleType);
75 }