每个远程主机具有不同网络管道的UDP不工作



我很难让netty使用UDP。最大的问题是,一旦我连接到服务器并完成服务器和客户端之间的交互,服务器就会变得毫无用处。我无法从同一个客户端或任何其他(不同的主机)与它进行任何其他连接。我觉得他们真的很简单,我很怀念。我已经配置了服务器,为每个连接到它的新主机创建一个新的管道(我想是?),代码如下:

public class DistinctChannelPipelineFactory implements ChannelPipelineFactory {
  private final ChannelPipelineFactory pipelineFactory;
  public DistinctChannelPipelineFactory(ChannelPipelineFactory pipelineFactory) {
    this.pipelineFactory = pipelineFactory;
  }
  @Override public ChannelPipeline getPipeline() throws Exception {
    return Channels.pipeline(new DistinctChannelPipelineHandler(pipelineFactory));
  }
}

在DistinctChannelPipelineHandler中,我尝试为每个远程主机创建一个不同的管道,并在10秒后对其进行计时。

    private final LoadingCache<SocketAddress, ChannelPipeline> pipelines;
  public DistinctChannelPipelineHandler(ChannelPipelineFactory factory) {
    this.pipelines = CacheBuilder.newBuilder()
        .concurrencyLevel(1)
        .expireAfterAccess(10, SECONDS)
        .removalListener(new PipelineRemovalListener())
        .build(new PipelineCacheLoader(factory));
  }
  public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    if (e instanceof MessageEvent) {
      final ChannelPipeline pipeline = pipelines.get(((MessageEvent) e).getRemoteAddress());
      if (!pipeline.isAttached()) {
        pipeline.attach(ctx.getChannel(), ctx.getPipeline().getSink());
        pipeline.sendUpstream(new UpstreamChannelStateEvent(ctx.getChannel(), OPEN, TRUE));
      }
      pipeline.sendUpstream(e);
    }
    if (e instanceof ChannelStateEvent) {
      for (final ChannelPipeline pipeline : pipelines.asMap().values()) {
        final ChannelStateEvent cse = (ChannelStateEvent) e;
        pipeline.sendUpstream(new UpstreamChannelStateEvent(ctx.getChannel(), cse.getState(), cse.getValue()));
      }
    }
  }
  public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    if (e instanceof MessageEvent) {
      final ChannelPipeline pipeline = pipelines.get(((MessageEvent) e).getRemoteAddress());
      if (!pipeline.isAttached()) {
        pipeline.attach(ctx.getChannel(), ctx.getPipeline().getSink());
      }
      pipeline.sendDownstream(e);
    } else {
      ctx.sendDownstream(e);
    }
  }
  private static final class PipelineCacheLoader extends CacheLoader<SocketAddress, ChannelPipeline> {
    private final ChannelPipelineFactory factory;
    public PipelineCacheLoader(ChannelPipelineFactory factory) {
      this.factory = factory;
    }
    @Override
    public ChannelPipeline load(SocketAddress key) throws Exception {
      return factory.getPipeline();
    }
  }
  private static final class PipelineRemovalListener implements RemovalListener<SocketAddress, ChannelPipeline> {
    private static final Logger logger = LoggerFactory.getLogger(PipelineRemovalListener.class);
    @Override
    public void onRemoval(RemovalNotification<SocketAddress, ChannelPipeline> n) {
      logger.info("UDP connection timed out, removing connection for {}", n.getKey());
      n.getValue().sendUpstream(new UpstreamChannelStateEvent(n.getValue().getChannel(), OPEN, FALSE));
    }
  }

这就是我初始化服务器的方式:

@Provides
  public ConnectionlessBootstrap getConnectionlessBootstrap(DatagramChannelFactory channelFactory,
                                                            @LocalAddress SocketAddress localAddress,
                                                            final UdpPipelineFactory pipelineFactory) {
    final ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(channelFactory);
    bootstrap.setOption("localAddress", localAddress);
    bootstrap.setPipelineFactory(new DistinctChannelPipelineFactory(pipelineFactory));
    return bootstrap;
  }
@Provides
  @Singleton
  public DatagramChannelFactory getDatagramChannelFatory(@WorkerExecutor Executor worker) {
    final DatagramChannelFactory channelFactory = new NioDatagramChannelFactory(worker);
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override public void run() {
        channelFactory.releaseExternalResources();
      }
    });
    return channelFactory;
  }

我省略了我实际添加所有处理程序的地方,因为我不认为这是问题所在。我是不是错过了一些基本的东西?我只想要一个超时的每个唯一远程地址的管道。启动服务器,让它只为客户端/服务器交互工作,真是太令人沮丧了!我已经通过调试验证了,一旦我向它发出额外的请求,它就不会创建新的管道。因此,最初的管道似乎处于一种非常陈旧的状态,这就是为什么它不会接受任何其他请求的原因。想法?建议?

犯了一个根本错误。有了ConnectionlessBootstrap,所有东西都在同一个通道上运行,每次调用服务器后我们都会关闭通道。。。从而禁用UDP。这就是我们的TCP代码所做的,我们花了一段时间才意识到它的工作方式不同。希望其他人能从中节省一些时间和头痛。

最新更新