-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
/
EmbeddedChannel.java
892 lines (782 loc) · 29.6 KB
/
EmbeddedChannel.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty5.channel.embedded;
import io.netty5.util.Resource;
import io.netty5.channel.AbstractChannel;
import io.netty5.channel.Channel;
import io.netty5.channel.ChannelConfig;
import io.netty5.channel.ChannelHandler;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.channel.ChannelId;
import io.netty5.channel.ChannelInitializer;
import io.netty5.channel.ChannelMetadata;
import io.netty5.channel.ChannelOutboundBuffer;
import io.netty5.channel.ChannelPipeline;
import io.netty5.channel.DefaultChannelConfig;
import io.netty5.channel.DefaultChannelPipeline;
import io.netty5.channel.EventLoop;
import io.netty5.channel.RecvBufferAllocator;
import io.netty5.util.ReferenceCountUtil;
import io.netty5.util.concurrent.Future;
import io.netty5.util.concurrent.FutureListener;
import io.netty5.util.concurrent.Promise;
import io.netty5.util.internal.RecyclableArrayList;
import io.netty5.util.internal.logging.InternalLogger;
import io.netty5.util.internal.logging.InternalLoggerFactory;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import static io.netty5.util.internal.PlatformDependent.throwException;
import static java.util.Objects.requireNonNull;
/**
* Base class for {@link Channel} implementations that are used in an embedded fashion.
*/
public class EmbeddedChannel extends AbstractChannel {
private static final SocketAddress LOCAL_ADDRESS = new EmbeddedSocketAddress();
private static final SocketAddress REMOTE_ADDRESS = new EmbeddedSocketAddress();
private static final ChannelHandler[] EMPTY_HANDLERS = new ChannelHandler[0];
private enum State { OPEN, ACTIVE, CLOSED }
private static final InternalLogger logger = InternalLoggerFactory.getInstance(EmbeddedChannel.class);
private static final ChannelMetadata METADATA_NO_DISCONNECT = new ChannelMetadata(false);
private static final ChannelMetadata METADATA_DISCONNECT = new ChannelMetadata(true);
private final FutureListener<Void> recordExceptionListener = this::recordException;
private final ChannelMetadata metadata;
private final ChannelConfig config;
private Queue<Object> inboundMessages;
private Queue<Object> outboundMessages;
private Throwable lastException;
private State state;
/**
* Create a new instance with an {@link EmbeddedChannelId} and an empty pipeline.
*/
public EmbeddedChannel() {
this(EMPTY_HANDLERS);
}
/**
* Create a new instance with the specified ID and an empty pipeline.
*
* @param channelId the {@link ChannelId} that will be used to identify this channel
*/
public EmbeddedChannel(ChannelId channelId) {
this(channelId, EMPTY_HANDLERS);
}
/**
* Create a new instance with the pipeline initialized with the specified handlers.
*
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(ChannelHandler... handlers) {
this(EmbeddedChannelId.INSTANCE, handlers);
}
/**
* Create a new instance with the pipeline initialized with the specified handlers.
*
* @param hasDisconnect {@code false} if this {@link Channel} will delegate {@link #disconnect()}
* to {@link #close()}, {@link false} otherwise.
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(boolean hasDisconnect, ChannelHandler... handlers) {
this(EmbeddedChannelId.INSTANCE, hasDisconnect, handlers);
}
/**
* Create a new instance with the pipeline initialized with the specified handlers.
*
* @param register {@code true} if this {@link Channel} is registered to the {@link EventLoop} in the
* constructor. If {@code false} the user will need to call {@link #register()}.
* @param hasDisconnect {@code false} if this {@link Channel} will delegate {@link #disconnect()}
* to {@link #close()}, {@link false} otherwise.
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(boolean register, boolean hasDisconnect, ChannelHandler... handlers) {
this(EmbeddedChannelId.INSTANCE, register, hasDisconnect, handlers);
}
/**
* Create a new instance with the channel ID set to the given ID and the pipeline
* initialized with the specified handlers.
*
* @param channelId the {@link ChannelId} that will be used to identify this channel
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(ChannelId channelId, ChannelHandler... handlers) {
this(channelId, false, handlers);
}
/**
* Create a new instance with the channel ID set to the given ID and the pipeline
* initialized with the specified handlers.
*
* @param channelId the {@link ChannelId} that will be used to identify this channel
* @param hasDisconnect {@code false} if this {@link Channel} will delegate {@link #disconnect()}
* to {@link #close()}, {@link false} otherwise.
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelHandler... handlers) {
this(channelId, true, hasDisconnect, handlers);
}
/**
* Create a new instance with the channel ID set to the given ID and the pipeline
* initialized with the specified handlers.
*
* @param channelId the {@link ChannelId} that will be used to identify this channel
* @param register {@code true} if this {@link Channel} is registered to the {@link EventLoop} in the
* constructor. If {@code false} the user will need to call {@link #register()}.
* @param hasDisconnect {@code false} if this {@link Channel} will delegate {@link #disconnect()}
* to {@link #close()}, {@link false} otherwise.
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(ChannelId channelId, boolean register, boolean hasDisconnect,
ChannelHandler... handlers) {
this(null, channelId, register, hasDisconnect, handlers);
}
/**
* Create a new instance with the channel ID set to the given ID and the pipeline
* initialized with the specified handlers.
*
* @param parent the parent {@link Channel} of this {@link EmbeddedChannel}.
* @param channelId the {@link ChannelId} that will be used to identify this channel
* @param register {@code true} if this {@link Channel} is registered to the {@link EventLoop} in the
* constructor. If {@code false} the user will need to call {@link #register()}.
* @param hasDisconnect {@code false} if this {@link Channel} will delegate {@link #disconnect()}
* to {@link #close()}, {@link false} otherwise.
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(Channel parent, ChannelId channelId, boolean register, boolean hasDisconnect,
final ChannelHandler... handlers) {
super(parent, new EmbeddedEventLoop(), channelId);
metadata = metadata(hasDisconnect);
config = new DefaultChannelConfig(this);
setup(register, handlers);
}
/**
* Create a new instance with the channel ID set to the given ID and the pipeline
* initialized with the specified handlers.
*
* @param channelId the {@link ChannelId} that will be used to identify this channel
* @param hasDisconnect {@code false} if this {@link Channel} will delegate {@link #disconnect()}
* to {@link #close()}, {@link false} otherwise.
* @param config the {@link ChannelConfig} which will be returned by {@link #config()}.
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, final ChannelConfig config,
final ChannelHandler... handlers) {
super(null, new EmbeddedEventLoop(), channelId);
metadata = metadata(hasDisconnect);
this.config = requireNonNull(config, "config");
setup(true, handlers);
}
private static ChannelMetadata metadata(boolean hasDisconnect) {
return hasDisconnect ? METADATA_DISCONNECT : METADATA_NO_DISCONNECT;
}
private void setup(boolean register, final ChannelHandler... handlers) {
requireNonNull(handlers, "handlers");
ChannelPipeline p = pipeline();
p.addLast(new ChannelInitializer<>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
for (ChannelHandler h : handlers) {
if (h == null) {
break;
}
pipeline.addLast(h);
}
}
});
if (register) {
Future<Void> future = register();
assert future.isDone();
}
}
@Override
public Future<Void> register() {
Future<Void> future = super.register();
assert future.isDone();
Throwable cause = future.cause();
if (cause != null) {
throwException(cause);
}
return future;
}
@Override
protected final DefaultChannelPipeline newChannelPipeline() {
return new EmbeddedChannelPipeline(this);
}
@Override
public ChannelMetadata metadata() {
return metadata;
}
@Override
public ChannelConfig config() {
return config;
}
@Override
public boolean isOpen() {
return state != State.CLOSED;
}
@Override
public boolean isActive() {
return state == State.ACTIVE;
}
/**
* Returns the {@link Queue} which holds all the {@link Object}s that were received by this {@link Channel}.
*/
public Queue<Object> inboundMessages() {
if (inboundMessages == null) {
inboundMessages = new ArrayDeque<>();
}
return inboundMessages;
}
/**
* @deprecated use {@link #inboundMessages()}
*/
@Deprecated
public Queue<Object> lastInboundBuffer() {
return inboundMessages();
}
/**
* Returns the {@link Queue} which holds all the {@link Object}s that were written by this {@link Channel}.
*/
public Queue<Object> outboundMessages() {
if (outboundMessages == null) {
outboundMessages = new ArrayDeque<>();
}
return outboundMessages;
}
/**
* @deprecated use {@link #outboundMessages()}
*/
@Deprecated
public Queue<Object> lastOutboundBuffer() {
return outboundMessages();
}
/**
* Return received data from this {@link Channel}
*/
@SuppressWarnings("unchecked")
public <T> T readInbound() {
T message = (T) poll(inboundMessages);
if (message != null) {
Resource.touch(message, "Caller of readInbound() will handle the message from this point");
}
return message;
}
/**
* Read data from the outbound. This may return {@code null} if nothing is readable.
*/
@SuppressWarnings("unchecked")
public <T> T readOutbound() {
T message = (T) poll(outboundMessages);
if (message != null) {
Resource.touch(message, "Caller of readOutbound() will handle the message from this point.");
}
return message;
}
/**
* Write messages to the inbound of this {@link Channel}.
*
* @param msgs the messages to be written
*
* @return {@code true} if the write operation did add something to the inbound buffer
*/
public boolean writeInbound(Object... msgs) {
ensureOpen();
if (msgs.length == 0) {
return isNotEmpty(inboundMessages);
}
ChannelPipeline p = pipeline();
for (Object m: msgs) {
p.fireChannelRead(m);
}
flushInbound(false);
return isNotEmpty(inboundMessages);
}
/**
* Writes one message to the inbound of this {@link Channel} and does not flush it. This
* method is conceptually equivalent to {@link #write(Object)}.
*
* @see #writeOneOutbound(Object)
*/
public Future<Void> writeOneInbound(Object msg) {
if (checkOpen(true)) {
pipeline().fireChannelRead(msg);
}
return checkException0();
}
/**
* Flushes the inbound of this {@link Channel}. This method is conceptually equivalent to {@link #flush()}.
*
* @see #flushOutbound()
*/
public EmbeddedChannel flushInbound() {
flushInbound(true);
return this;
}
private void flushInbound(boolean recordException) {
if (checkOpen(recordException)) {
pipeline().fireChannelReadComplete();
readIfIsAutoRead();
runPendingTasks();
}
checkException();
}
/**
* Write messages to the outbound of this {@link Channel}.
*
* @param msgs the messages to be written
* @return bufferReadable returns {@code true} if the write operation did add something to the outbound buffer
*/
public boolean writeOutbound(Object... msgs) {
ensureOpen();
if (msgs.length == 0) {
return isNotEmpty(outboundMessages);
}
RecyclableArrayList futures = RecyclableArrayList.newInstance(msgs.length);
try {
for (Object m: msgs) {
if (m == null) {
break;
}
futures.add(write(m));
}
flushOutbound0();
int size = futures.size();
for (int i = 0; i < size; i++) {
Future<Void> future = (Future<Void>) futures.get(i);
if (future.isDone()) {
recordException(future);
} else {
// The write may be delayed to run later by runPendingTasks()
future.addListener(recordExceptionListener);
}
}
checkException();
return isNotEmpty(outboundMessages);
} finally {
futures.recycle();
}
}
/**
* Writes one message to the outbound of this {@link Channel} and does not flush it. This
* method is conceptually equivalent to {@link #write(Object)}.
*
* @see #writeOneInbound(Object)
*/
public Future<Void> writeOneOutbound(Object msg) {
if (checkOpen(true)) {
return write(msg);
}
return checkException0();
}
/**
* Flushes the outbound of this {@link Channel}. This method is conceptually equivalent to {@link #flush()}.
*
* @see #flushInbound()
*/
public EmbeddedChannel flushOutbound() {
if (checkOpen(true)) {
flushOutbound0();
}
checkException();
return this;
}
private void flushOutbound0() {
// We need to call runPendingTasks first as a ChannelOutboundHandler may used eventloop.execute(...) to
// delay the write on the next eventloop run.
runPendingTasks();
flush();
}
/**
* Mark this {@link Channel} as finished. Any further try to write data to it will fail.
*
* @return bufferReadable returns {@code true} if any of the used buffers has something left to read
*/
public boolean finish() {
return finish(false);
}
/**
* Mark this {@link Channel} as finished and release all pending message in the inbound and outbound buffer.
* Any further try to write data to it will fail.
*
* @return bufferReadable returns {@code true} if any of the used buffers has something left to read
*/
public boolean finishAndReleaseAll() {
return finish(true);
}
/**
* Mark this {@link Channel} as finished. Any further try to write data to it will fail.
*
* @param releaseAll if {@code true} all pending message in the inbound and outbound buffer are released.
* @return bufferReadable returns {@code true} if any of the used buffers has something left to read
*/
private boolean finish(boolean releaseAll) {
close();
try {
checkException();
return isNotEmpty(inboundMessages) || isNotEmpty(outboundMessages);
} finally {
if (releaseAll) {
releaseAll(inboundMessages);
releaseAll(outboundMessages);
}
}
}
/**
* Release all buffered inbound messages and return {@code true} if any were in the inbound buffer, {@code false}
* otherwise.
*/
public boolean releaseInbound() {
return releaseAll(inboundMessages);
}
/**
* Release all buffered outbound messages and return {@code true} if any were in the outbound buffer, {@code false}
* otherwise.
*/
public boolean releaseOutbound() {
return releaseAll(outboundMessages);
}
private static boolean releaseAll(Queue<Object> queue) {
Exception closeFailed = null;
if (isNotEmpty(queue)) {
for (;;) {
Object msg = queue.poll();
if (msg == null) {
break;
}
try {
Resource.dispose(msg);
} catch (Exception e) {
if (closeFailed == null) {
closeFailed = e;
} else {
closeFailed.addSuppressed(e);
}
}
}
if (closeFailed != null) {
throwException(closeFailed);
}
return true;
}
return false;
}
private void finishPendingTasks(boolean cancel) {
runPendingTasks();
if (cancel) {
// Cancel all scheduled tasks that are left.
((EmbeddedEventLoop) executor()).cancelScheduled();
}
}
@Override
public final Future<Void> close() {
// We need to call runPendingTasks() before calling super.close() as there may be something in the queue
// that needs to be run before the actual close takes place.
runPendingTasks();
Future<Void> future = super.close();
// Now finish everything else and cancel all scheduled tasks that were not ready set.
finishPendingTasks(true);
return future;
}
@Override
public final Future<Void> disconnect() {
Future<Void> future = super.disconnect();
finishPendingTasks(!metadata.hasDisconnect());
return future;
}
private static boolean isNotEmpty(Queue<Object> queue) {
return queue != null && !queue.isEmpty();
}
private static Object poll(Queue<Object> queue) {
return queue != null ? queue.poll() : null;
}
/**
* Run all tasks (which also includes scheduled tasks) that are pending in the {@link EventLoop}
* for this {@link Channel}
*/
public void runPendingTasks() {
EmbeddedEventLoop embeddedEventLoop = (EmbeddedEventLoop) executor();
try {
embeddedEventLoop.runTasks();
} catch (Exception e) {
recordException(e);
}
runScheduledPendingTasks();
}
/**
* Run all pending scheduled tasks in the {@link EventLoop} for this {@link Channel} and return the
* {@code nanoseconds} when the next scheduled task is ready to run. If no other task was scheduled it will return
* {@code -1}.
*/
public long runScheduledPendingTasks() {
EmbeddedEventLoop embeddedEventLoop = (EmbeddedEventLoop) executor();
try {
return embeddedEventLoop.runScheduledTasks();
} catch (Exception e) {
recordException(e);
return embeddedEventLoop.nextScheduledTask();
} finally {
// A scheduled task may put something on the taskQueue so lets run it.
embeddedEventLoop.runTasks();
}
}
private void recordException(Future<?> future) {
if (future.isFailed()) {
recordException(future.cause());
}
}
private void recordException(Throwable cause) {
if (lastException == null) {
lastException = cause;
} else {
logger.warn(
"More than one exception was raised. " +
"Will report only the first one and log others.", cause);
}
}
private EmbeddedEventLoop embeddedEventLoop() {
return (EmbeddedEventLoop) executor();
}
/**
* Advance the clock of the event loop of this channel by the given duration. Any scheduled tasks will execute
* sooner by the given time (but {@link #runScheduledPendingTasks()} still needs to be called).
*/
public void advanceTimeBy(long duration, TimeUnit unit) {
embeddedEventLoop().advanceTimeBy(unit.toNanos(duration));
}
/**
* Freeze the clock of this channel's event loop. Any scheduled tasks that are not already due will not run on
* future {@link #runScheduledPendingTasks()} calls. While the event loop is frozen, it is still possible to
* {@link #advanceTimeBy(long, TimeUnit) advance time} manually so that scheduled tasks execute.
*/
public void freezeTime() {
embeddedEventLoop().freezeTime();
}
/**
* Unfreeze an event loop that was {@link #freezeTime() frozen}. Time will continue at the point where
* {@link #freezeTime()} stopped it: if a task was scheduled ten minutes in the future and {@link #freezeTime()}
* was called, it will run ten minutes after this method is called again (assuming no
* {@link #advanceTimeBy(long, TimeUnit)} calls, and assuming pending scheduled tasks are run at that time using
* {@link #runScheduledPendingTasks()}).
*/
public void unfreezeTime() {
embeddedEventLoop().unfreezeTime();
}
/**
* Checks for the presence of an {@link Exception}.
*/
private Future<Void> checkException0() {
try {
checkException();
} catch (Throwable cause) {
return newFailedFuture(cause);
}
return newSucceededFuture();
}
/**
* Check if there was any {@link Throwable} received and if so rethrow it.
*/
public void checkException() {
Throwable t = lastException;
if (t != null) {
lastException = null;
throwException(t);
}
}
/**
* Returns {@code true} if the {@link Channel} is open and records optionally
* an {@link Exception} if it isn't.
*/
private boolean checkOpen(boolean recordException) {
if (!isOpen()) {
if (recordException) {
recordException(new ClosedChannelException());
}
return false;
}
return true;
}
/**
* Ensure the {@link Channel} is open and if not throw an exception.
*/
protected final void ensureOpen() {
if (!checkOpen(true)) {
checkException();
}
}
@Override
protected SocketAddress localAddress0() {
return isActive()? LOCAL_ADDRESS : null;
}
@Override
protected SocketAddress remoteAddress0() {
return isActive()? REMOTE_ADDRESS : null;
}
void setActive() {
state = State.ACTIVE;
}
@Override
protected void doBind(SocketAddress localAddress) throws Exception {
// NOOP
}
@Override
protected void doDisconnect() throws Exception {
if (!metadata.hasDisconnect()) {
doClose();
}
}
@Override
protected void doClose() throws Exception {
state = State.CLOSED;
}
@Override
protected void doBeginRead() throws Exception {
// NOOP
}
@Override
protected AbstractUnsafe newUnsafe() {
return new EmbeddedUnsafe();
}
@Override
public Unsafe unsafe() {
return ((EmbeddedUnsafe) super.unsafe()).wrapped;
}
@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
for (;;) {
Object msg = in.current();
if (msg == null) {
break;
}
if (msg instanceof Resource<?>) {
// Exfiltrate message for later inspection in tests.
// Resource life-cycle otherwise normally ends in ChannelOutboundBuffer.remove(), but using send()
// here allows the close() in remove() to become a no-op.
handleOutboundMessage(((Resource<?>) msg).send().receive());
} else {
handleOutboundMessage(ReferenceCountUtil.retain(msg));
}
in.remove();
}
}
/**
* Called for each outbound message.
*
* @see #doWrite(ChannelOutboundBuffer)
*/
protected void handleOutboundMessage(Object msg) {
outboundMessages().add(msg);
}
/**
* Called for each inbound message.
*/
protected void handleInboundMessage(Object msg) {
inboundMessages().add(msg);
}
private final class EmbeddedUnsafe extends AbstractUnsafe {
// Delegates to the EmbeddedUnsafe instance but ensures runPendingTasks() is called after each operation
// that may change the state of the Channel and may schedule tasks for later execution.
final Unsafe wrapped = new Unsafe() {
@Override
public RecvBufferAllocator.Handle recvBufAllocHandle() {
return EmbeddedUnsafe.this.recvBufAllocHandle();
}
@Override
public SocketAddress localAddress() {
return EmbeddedUnsafe.this.localAddress();
}
@Override
public SocketAddress remoteAddress() {
return EmbeddedUnsafe.this.remoteAddress();
}
private void mayRunPendingTasks() {
if (!((EmbeddedEventLoop) executor()).running) {
runPendingTasks();
}
}
@Override
public void register(Promise<Void> promise) {
EmbeddedUnsafe.this.register(promise);
mayRunPendingTasks();
}
@Override
public void bind(SocketAddress localAddress, Promise<Void> promise) {
EmbeddedUnsafe.this.bind(localAddress, promise);
mayRunPendingTasks();
}
@Override
public void connect(SocketAddress remoteAddress, SocketAddress localAddress, Promise<Void> promise) {
EmbeddedUnsafe.this.connect(remoteAddress, localAddress, promise);
mayRunPendingTasks();
}
@Override
public void disconnect(Promise<Void> promise) {
EmbeddedUnsafe.this.disconnect(promise);
mayRunPendingTasks();
}
@Override
public void close(Promise<Void> promise) {
EmbeddedUnsafe.this.close(promise);
mayRunPendingTasks();
}
@Override
public void closeForcibly() {
EmbeddedUnsafe.this.closeForcibly();
mayRunPendingTasks();
}
@Override
public void deregister(Promise<Void> promise) {
EmbeddedUnsafe.this.deregister(promise);
mayRunPendingTasks();
}
@Override
public void beginRead() {
EmbeddedUnsafe.this.beginRead();
mayRunPendingTasks();
}
@Override
public void write(Object msg, Promise<Void> promise) {
EmbeddedUnsafe.this.write(msg, promise);
mayRunPendingTasks();
}
@Override
public void flush() {
EmbeddedUnsafe.this.flush();
mayRunPendingTasks();
}
@Override
public ChannelOutboundBuffer outboundBuffer() {
return EmbeddedUnsafe.this.outboundBuffer();
}
};
@Override
public void connect(SocketAddress remoteAddress, SocketAddress localAddress, Promise<Void> promise) {
safeSetSuccess(promise);
}
}
private final class EmbeddedChannelPipeline extends DefaultChannelPipeline {
EmbeddedChannelPipeline(EmbeddedChannel channel) {
super(channel);
}
@Override
protected void onUnhandledInboundException(Throwable cause) {
recordException(cause);
}
@Override
protected void onUnhandledInboundMessage(ChannelHandlerContext ctx, Object msg) {
handleInboundMessage(msg);
}
}
}