forked from reactor/reactor-netty
/
ChannelOperationsHandler.java
executable file
·172 lines (157 loc) · 5.64 KB
/
ChannelOperationsHandler.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
/*
* Copyright (c) 2011-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed 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 reactor.netty.channel;
import io.netty.buffer.EmptyByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.DecoderResultProvider;
import io.netty.handler.ssl.SslCloseCompletionEvent;
import io.netty.util.IllegalReferenceCountException;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
import reactor.netty.Connection;
import reactor.netty.ConnectionObserver;
import reactor.netty.NettyOutbound;
import reactor.util.Logger;
import reactor.util.Loggers;
import static reactor.netty.ReactorNetty.format;
import static reactor.netty.ReactorNetty.toPrettyHexDump;
/**
* Netty {@link io.netty.channel.ChannelDuplexHandler} implementation that bridge data
* via an IPC {@link NettyOutbound}
*
* @author Stephane Maldini
*/
final class ChannelOperationsHandler extends ChannelInboundHandlerAdapter {
final ConnectionObserver listener;
final ChannelOperations.OnSetup opsFactory;
ChannelOperationsHandler(ChannelOperations.OnSetup opsFactory, ConnectionObserver listener) {
this.listener = listener;
this.opsFactory = opsFactory;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
// When AbstractNioChannel.AbstractNioUnsafe.finishConnect/fulfillConnectPromise,
// fireChannelActive will be triggered regardless that the channel might be closed in the meantime
if (ctx.channel().isActive()) {
Connection c = Connection.from(ctx.channel());
listener.onStateChange(c, ConnectionObserver.State.CONNECTED);
ChannelOperations<?, ?> ops = opsFactory.create(c, listener, null);
if (ops != null) {
ops.bind();
listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
}
}
}
@Override
final public void channelInactive(ChannelHandlerContext ctx) {
try {
Connection connection = Connection.from(ctx.channel());
ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
if (ops != null) {
ops.onInboundClose();
}
else {
listener.onStateChange(connection, ConnectionObserver.State.DISCONNECTING);
}
}
catch (Throwable err) {
exceptionCaught(ctx, err);
}
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof SslCloseCompletionEvent) {
SslCloseCompletionEvent sslCloseCompletionEvent = (SslCloseCompletionEvent) evt;
// When a close_notify is received, the SSLHandler fires an SslCloseCompletionEvent.SUCCESS event,
// so if the event is success and if the channel is still active (not closing for example),
// then immediately close the channel.
// see https://www.rfc-editor.org/rfc/rfc5246#section-7.2.1, which states that when receiving a close_notify,
// then the connection must be closed down immediately.
if (sslCloseCompletionEvent.isSuccess() && ctx.channel().isActive()) {
if (log.isDebugEnabled()) {
log.debug(format(ctx.channel(), "Received a TLS close_notify, closing the channel now."));
}
ctx.close();
}
}
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
final public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg == null || msg == Unpooled.EMPTY_BUFFER || msg instanceof EmptyByteBuf) {
return;
}
try {
ChannelOperations<?, ?> ops = ChannelOperations.get(ctx.channel());
if (ops != null) {
ops.onInboundNext(ctx, msg);
}
else {
if (log.isDebugEnabled()) {
if (msg instanceof DecoderResultProvider) {
DecoderResult decoderResult = ((DecoderResultProvider) msg).decoderResult();
if (decoderResult.isFailure()) {
log.debug(format(ctx.channel(), "Decoding failed: " + msg + " : "),
decoderResult.cause());
}
}
log.debug(format(ctx.channel(), "No ChannelOperation attached. Dropping: {}"),
toPrettyHexDump(msg));
}
ReferenceCountUtil.release(msg);
}
}
catch (Throwable err) {
safeRelease(msg);
log.error(format(ctx.channel(), "Error was received while reading the incoming data." +
" The connection will be closed."), err);
//"FutureReturnValueIgnored" this is deliberate
ctx.close();
exceptionCaught(ctx, err);
}
}
@Override
final public void exceptionCaught(ChannelHandlerContext ctx, Throwable err) {
Connection connection = Connection.from(ctx.channel());
ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
if (ops != null) {
ops.onInboundError(err);
}
else {
listener.onUncaughtException(connection, err);
}
}
static void safeRelease(Object msg) {
if (msg instanceof ReferenceCounted) {
ReferenceCounted referenceCounted = (ReferenceCounted) msg;
if (referenceCounted.refCnt() > 0) {
try {
referenceCounted.release();
}
catch (IllegalReferenceCountException e) {
if (log.isDebugEnabled()) {
log.debug("", e);
}
}
}
}
}
static final Logger log = Loggers.getLogger(ChannelOperationsHandler.class);
}