Пример #1
0
  @Override
  public CompletableFuture<Void> listen(Address address, Consumer<Connection> listener) {
    Assert.notNull(address, "address");
    Assert.notNull(listener, "listener");
    if (listening) return CompletableFuture.completedFuture(null);

    ThreadContext context = ThreadContext.currentContextOrThrow();
    synchronized (this) {
      if (listenFuture == null) {
        listenFuture = new CompletableFuture<>();
        listen(address, listener, context);
      }
    }
    return listenFuture;
  }
Пример #2
0
  /** Starts listening for the given member. */
  private void listen(Address address, Consumer<Connection> listener, ThreadContext context) {
    channelGroup =
        new DefaultChannelGroup("catalyst-acceptor-channels", GlobalEventExecutor.INSTANCE);

    handler = new ServerHandler(connections, listener, context, transport.properties());

    final ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap
        .group(transport.eventLoopGroup())
        .channel(NioServerSocketChannel.class)
        .handler(new LoggingHandler(LogLevel.DEBUG))
        .childHandler(
            new ChannelInitializer<SocketChannel>() {
              @Override
              public void initChannel(SocketChannel channel) throws Exception {
                ChannelPipeline pipeline = channel.pipeline();
                if (transport.properties().sslEnabled()) {
                  pipeline.addFirst(
                      new SslHandler(new NettyTls(transport.properties()).initSslEngine(false)));
                }
                pipeline.addLast(FIELD_PREPENDER);
                pipeline.addLast(
                    new LengthFieldBasedFrameDecoder(
                        transport.properties().maxFrameSize(), 0, 4, 0, 4));
                pipeline.addLast(handler);
              }
            })
        .option(ChannelOption.SO_BACKLOG, transport.properties().acceptBacklog())
        .option(ChannelOption.TCP_NODELAY, transport.properties().tcpNoDelay())
        .option(ChannelOption.SO_REUSEADDR, transport.properties().reuseAddress())
        .childOption(ChannelOption.ALLOCATOR, ALLOCATOR)
        .childOption(ChannelOption.SO_KEEPALIVE, transport.properties().tcpKeepAlive());

    if (transport.properties().sendBufferSize() != -1) {
      bootstrap.childOption(ChannelOption.SO_SNDBUF, transport.properties().sendBufferSize());
    }
    if (transport.properties().receiveBufferSize() != -1) {
      bootstrap.childOption(ChannelOption.SO_RCVBUF, transport.properties().receiveBufferSize());
    }

    LOGGER.info("Binding to {}", address);

    ChannelFuture bindFuture = bootstrap.bind(address.socketAddress());
    bindFuture.addListener(
        (ChannelFutureListener)
            channelFuture -> {
              if (channelFuture.isSuccess()) {
                listening = true;
                context
                    .executor()
                    .execute(
                        () -> {
                          LOGGER.info("Listening at {}", bindFuture.channel().localAddress());
                          listenFuture.complete(null);
                        });
              } else {
                context.execute(() -> listenFuture.completeExceptionally(channelFuture.cause()));
              }
            });
    channelGroup.add(bindFuture.channel());
  }