-
Notifications
You must be signed in to change notification settings - Fork 565
/
SocketChannelFrameHandlerFactory.java
170 lines (145 loc) · 6.44 KB
/
SocketChannelFrameHandlerFactory.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
// Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.
package com.rabbitmq.client.impl.nio;
import com.rabbitmq.client.Address;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.SslContextFactory;
import com.rabbitmq.client.impl.AbstractFrameHandlerFactory;
import com.rabbitmq.client.impl.FrameHandler;
import com.rabbitmq.client.impl.TlsUtils;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
*/
public class SocketChannelFrameHandlerFactory extends AbstractFrameHandlerFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(SocketChannelFrameHandler.class);
final NioParams nioParams;
private final SslContextFactory sslContextFactory;
private final Lock stateLock = new ReentrantLock();
private final AtomicLong globalConnectionCount = new AtomicLong();
private final List<NioLoopContext> nioLoopContexts;
public SocketChannelFrameHandlerFactory(int connectionTimeout, NioParams nioParams, boolean ssl, SslContextFactory sslContextFactory) {
super(connectionTimeout, null, ssl);
this.nioParams = new NioParams(nioParams);
this.sslContextFactory = sslContextFactory;
this.nioLoopContexts = new ArrayList<>(this.nioParams.getNbIoThreads());
for (int i = 0; i < this.nioParams.getNbIoThreads(); i++) {
this.nioLoopContexts.add(new NioLoopContext(this, this.nioParams));
}
}
@Override
public FrameHandler create(Address addr, String connectionName) throws IOException {
int portNumber = ConnectionFactory.portOrDefault(addr.getPort(), ssl);
SSLEngine sslEngine = null;
SocketChannel channel = null;
try {
if (ssl) {
SSLContext sslContext = sslContextFactory.create(connectionName);
sslEngine = sslContext.createSSLEngine(addr.getHost(), portNumber);
sslEngine.setUseClientMode(true);
if (nioParams.getSslEngineConfigurator() != null) {
nioParams.getSslEngineConfigurator().configure(sslEngine);
}
}
SocketAddress address = new InetSocketAddress(addr.getHost(), portNumber);
// No Sonar: the channel is closed in case of error and it cannot
// be closed here because it's part of the state of the connection
// to be returned.
channel = SocketChannel.open(); //NOSONAR
channel.configureBlocking(true);
if(nioParams.getSocketChannelConfigurator() != null) {
nioParams.getSocketChannelConfigurator().configure(channel);
}
channel.socket().connect(address, this.connectionTimeout);
if (ssl) {
int initialSoTimeout = channel.socket().getSoTimeout();
channel.socket().setSoTimeout(this.connectionTimeout);
sslEngine.beginHandshake();
try {
ReadableByteChannel wrappedReadChannel = Channels.newChannel(
channel.socket().getInputStream());
WritableByteChannel wrappedWriteChannel = Channels.newChannel(
channel.socket().getOutputStream());
boolean handshake = SslEngineHelper.doHandshake(
wrappedWriteChannel, wrappedReadChannel, sslEngine);
if (!handshake) {
LOGGER.error("TLS connection failed");
throw new SSLException("TLS handshake failed");
}
channel.socket().setSoTimeout(initialSoTimeout);
} catch (SSLHandshakeException e) {
LOGGER.error("TLS connection failed: {}", e.getMessage());
throw e;
}
TlsUtils.logPeerCertificateInfo(sslEngine.getSession());
}
channel.configureBlocking(false);
// lock
stateLock.lock();
NioLoopContext nioLoopContext = null;
try {
long modulo = globalConnectionCount.getAndIncrement() % nioParams.getNbIoThreads();
nioLoopContext = nioLoopContexts.get((int) modulo);
nioLoopContext.initStateIfNecessary();
SocketChannelFrameHandlerState state = new SocketChannelFrameHandlerState(
channel,
nioLoopContext,
nioParams,
sslEngine
);
state.startReading();
SocketChannelFrameHandler frameHandler = new SocketChannelFrameHandler(state);
return frameHandler;
} finally {
stateLock.unlock();
}
} catch(IOException e) {
try {
if(sslEngine != null && channel != null) {
SslEngineHelper.close(channel, sslEngine);
}
if (channel != null) {
channel.close();
}
} catch(IOException closingException) {
// ignore
}
throw e;
}
}
void lock() {
stateLock.lock();
}
void unlock() {
stateLock.unlock();
}
}