-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
NettyEventLoopProxy.java
277 lines (234 loc) · 9.18 KB
/
NettyEventLoopProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.comphenix.protocol.injector.netty.channel;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.ProgressivePromise;
import io.netty.util.concurrent.Promise;
import io.netty.util.concurrent.ScheduledFuture;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
/**
* An abstract event loop implementation which delegates all calls to a given event loop, but proxies all calls which
* schedule something on the event loop to methods which decide what should happen to the scheduled task.
*/
abstract class NettyEventLoopProxy implements EventLoop {
private static final Callable<?> EMPTY_CALLABLE = () -> null;
private static final Runnable EMPTY_RUNNABLE = () -> {
};
private final EventLoop delegate;
private final NettyChannelInjector injector;
public NettyEventLoopProxy(EventLoop delegate, NettyChannelInjector injector) {
this.delegate = delegate;
this.injector = injector;
}
private Runnable proxyRunnable(Runnable original) {
// execute the proxy and check if we need to do anything
Runnable proxied = this.doProxyRunnable(original);
if (proxied != null && proxied == original) {
// was not changed, we need to mark the packet as processed manually
return () -> {
this.injector.processedPackets.set(Boolean.TRUE);
original.run();
};
} else {
// either the action was not executed, or the proxy will set the packet as processes
return proxied;
}
}
private <T> Callable<T> proxyCallable(Callable<T> original) {
// execute the proxy and check if we need to do anything
Callable<T> proxied = this.doProxyCallable(original);
if (proxied != null && proxied == original) {
// was not changed, we need to mark the packet as processed manually
return () -> {
this.injector.processedPackets.set(Boolean.TRUE);
return proxied.call();
};
} else {
// either the action was not executed, or the proxy will set the packet as processes
return proxied;
}
}
/**
* Proxies the given runnable. The returned runnable will be executed instead of the original. If this method returns
* null a no-op runnable will be scheduled instead, preventing the original action from happening.
*
* @param original the runnable to proxy.
* @return the runnable to execute instead, null to execute no action.
*/
protected abstract Runnable doProxyRunnable(Runnable original);
/**
* Proxies the given callable. The returned callable will be executed instead of the original. If this method returns
* null a callable which always returns null will be scheduled instead, preventing the original action from
* happening.
*
* @param original the callable to proxy.
* @param <T> the return type of the original callable.
* @return the callable to execute instead of the original, null to use a no-op callable instead.
*/
protected abstract <T> Callable<T> doProxyCallable(Callable<T> original);
@Override
public EventLoopGroup parent() {
return this.delegate.parent();
}
@Override
public EventLoop next() {
return this.delegate.next();
}
@Override
public boolean inEventLoop() {
return this.delegate.inEventLoop();
}
@Override
public boolean inEventLoop(Thread thread) {
return this.delegate.inEventLoop(thread);
}
@Override
public <V> Promise<V> newPromise() {
return this.delegate.newPromise();
}
@Override
public <V> ProgressivePromise<V> newProgressivePromise() {
return this.delegate.newProgressivePromise();
}
@Override
public <V> Future<V> newSucceededFuture(V result) {
return this.delegate.newSucceededFuture(result);
}
@Override
public <V> Future<V> newFailedFuture(Throwable cause) {
return this.delegate.newFailedFuture(cause);
}
@Override
public boolean isShuttingDown() {
return this.delegate.isShuttingDown();
}
@Override
public Future<?> shutdownGracefully() {
return this.delegate.shutdownGracefully();
}
@Override
public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
return this.delegate.shutdownGracefully(quietPeriod, timeout, unit);
}
@Override
public Future<?> terminationFuture() {
return this.delegate.terminationFuture();
}
@Override
@Deprecated
public void shutdown() {
this.delegate.shutdown();
}
@Override
@Deprecated
public List<Runnable> shutdownNow() {
return this.delegate.shutdownNow();
}
@Override
public Iterator<EventExecutor> iterator() {
return this.delegate.iterator();
}
@Override
public Future<?> submit(Runnable task) {
Runnable proxied = this.proxyRunnable(task);
return this.delegate.submit(proxied == null ? EMPTY_RUNNABLE : proxied);
}
@Override
public <T> Future<T> submit(Runnable task, T result) {
Runnable proxied = this.proxyRunnable(task);
return this.delegate.submit(proxied == null ? EMPTY_RUNNABLE : proxied, result);
}
@Override
@SuppressWarnings("unchecked")
public <T> Future<T> submit(Callable<T> task) {
Callable<T> proxied = this.proxyCallable(task);
return this.delegate.submit(proxied == null ? (Callable<T>) EMPTY_CALLABLE : proxied);
}
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
Runnable proxied = this.proxyRunnable(command);
return this.delegate.schedule(proxied == null ? EMPTY_RUNNABLE : proxied, delay, unit);
}
@Override
@SuppressWarnings("unchecked")
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
Callable<V> proxied = this.proxyCallable(callable);
return this.delegate.schedule(callable == null ? (Callable<V>) EMPTY_CALLABLE : proxied, delay, unit);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
Runnable proxied = this.proxyRunnable(command);
return this.delegate.scheduleAtFixedRate(proxied == null ? EMPTY_RUNNABLE : proxied, initialDelay, period, unit);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
Runnable proxied = this.proxyRunnable(command);
return this.delegate.scheduleWithFixedDelay(proxied == null ? EMPTY_RUNNABLE : proxied, initialDelay, delay, unit);
}
@Override
public boolean isShutdown() {
return this.delegate.isShutdown();
}
@Override
public boolean isTerminated() {
return this.delegate.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return this.delegate.awaitTermination(timeout, unit);
}
@Override
public <T> List<java.util.concurrent.Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
return this.delegate.invokeAll(tasks);
}
@Override
public <T> List<java.util.concurrent.Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout,
TimeUnit unit) throws InterruptedException {
return this.delegate.invokeAll(tasks, timeout, unit);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return this.delegate.invokeAny(tasks);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return this.delegate.invokeAny(tasks, timeout, unit);
}
@Override
public void execute(Runnable command) {
Runnable proxied = this.proxyRunnable(command);
if (proxied != null) {
this.delegate.execute(proxied);
}
}
@Override
public void forEach(Consumer<? super EventExecutor> action) {
this.delegate.forEach(action);
}
@Override
public Spliterator<EventExecutor> spliterator() {
return this.delegate.spliterator();
}
@Override
public ChannelFuture register(Channel channel) {
return this.delegate.register(channel);
}
@Override
public ChannelFuture register(Channel channel, ChannelPromise promise) {
return this.delegate.register(channel, promise);
}
}