Bootstrap clientBootstrap = sharedRef.get().getBootstrap();
ChannelFuture connectFuture = clientBootstrap.connect(address).syncUninterruptibly();
Channel channel = connectFuture.channel();
ProtocolImpl protocol = ProtocolImpl.newInstance(sharedRef, channel, context);
if (sslMode != SSLMode.Disable && sslMode != SSLMode.Allow) {
// Execute SSL request command
SSLRequestCommand sslRequestCommand = protocol.createSSLRequest();
if (sslRequestCommand == null && sslMode.isRequired()) {
throw new IOException("SSL not available");
}
protocol.execute(sslRequestCommand);
// Did server allow it?
if (sslRequestCommand.isAllowed()) {
// Attach the actual handler
SSLEngine sslEngine = SSLEngineFactory.create(sslMode, context);
final SslHandler sslHandler = new SslHandler(sslEngine);
channel.pipeline().addFirst("ssl", sslHandler);
try {
sslHandler.handshakeFuture().syncUninterruptibly();
}
catch (Exception e) {
// Retry with no SSL
if (sslMode == SSLMode.Prefer) {
return connect(SSLMode.Disable, address, context);
}
throw e;
}
}
else if (sslMode.isRequired()) {
throw new IOException("SSL not allowed by server");
}
}
try {
startup(protocol, context);
if (sslMode == SSLMode.VerifyFull) {
SslHandler sslHandler = channel.pipeline().get(SslHandler.class);
if (sslHandler != null) {
String hostname;
if (address instanceof InetSocketAddress) {
hostname = ((InetSocketAddress) address).getHostString();