Esempio n. 1
0
  private <In, Out> Channel listen(int port, NetworkEndpointFactory<In, Out> endpointFactory) {
    ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);

    bootstrap.setPipelineFactory(
        () ->
            Channels.pipeline(
                new ObjectEncoder(),
                new ObjectDecoder(ClassResolvers.softCachingResolver(getClass().getClassLoader())),
                new LoggingHandler(NettyNetworkServer.class, logLevel),
                new NettyNetworkEndpointAdapter<>(endpointFactory.createEndpoint()),
                new AddToChannelGroupHandler(allChannels)));

    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", true);

    Channel channel = bootstrap.bind(new InetSocketAddress(port));
    allChannels.add(channel);
    return channel;
  }
  @Override
  public ChannelPipeline getPipeline() throws Exception {
    idleStateHandler =
        new IdleStateHandler(new HashedWheelTimer(), 6000, 0, 0, TimeUnit.MILLISECONDS);

    ChannelPipeline pipeline = pipeline();

    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast(
        "decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));

    // 添加心跳机制
    pipeline.addLast("idle", idleStateHandler);

    // 具体逻辑管理者Handler
    pipeline.addLast("nexus", nexusHandler);

    return pipeline;
  }