@Override
public CompletableFuture<Void> listen() {
final CompletableFuture<Void> future = new CompletableFuture<>();
// TODO: Configure proper SSL trust store.
final SslContext sslContext;
if (protocol.isSsl()) {
try {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslContext = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
} catch (SSLException | CertificateException e) {
future.completeExceptionally(e);
return future;
}
} else {
sslContext = null;
}
final EventLoopGroup serverGroup = new NioEventLoopGroup();
final EventLoopGroup workerGroup = new NioEventLoopGroup(protocol.getThreads());
final ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(serverGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
if (sslContext != null) {
pipeline.addLast(sslContext.newHandler(channel.alloc()));
}
pipeline.addLast(
new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.softCachingConcurrentResolver(getClass().getClassLoader())),
new TcpProtocolServerHandler(NettyTcpProtocolServer.this)