简体   繁体   English

写入TCP Netty ChannelGroup中除一个以外的所有人

[英]Writing to all but one in a TCP Netty ChannelGroup

I have a TCP based Netty application with multiple clients that need to communicate with each other. 我有一个基于TCP的Netty应用程序,有多个客户端需要相互通信。 I store all of the Channel references in a ChannelGroup. 我将所有Channel引用存储在ChannelGroup中。

While I know that I can write to every client in a ChannelGroup by calling ChannelGroup.write, but what would be an efficient way to write to all but one of these channels, for example: 虽然我知道我可以通过调用ChannelGroup.write写入ChannelGroup中的每个客户端,但是除了其中一个通道之外的所有通道都是有效的方式,例如:

'A','B','C' are in a Channel Group together and 'A' needs to send to 'B' and 'C', 'B' needs to send to 'A' and 'C', etc. What would be the best way to do this? 'A','B','C'在一个频道组中,'A'需要发送到'B'和'C','B'需要发送到'A'和'C'等。最好的方法是什么? Here are some options I thought of but some feedback would be appreciated: 以下是我想到的一些选项,但有些反馈会受到赞赏:

  1. Just iterate through the Group and write to the channels individually (don't use ChannelGroup.write in this case) 只需遍历组并单独写入通道(在这种情况下不要使用ChannelGroup.write)

  2. Repeatedly "remake/clone" the ChannelGroup so it contains only the channels to write to. 重复“重制/克隆”ChannelGroup,使其仅包含要写入的通道。 I would then call ChannelGroup.write on the modified group, then restore the group to the full group. 然后我会在修改后的组上调用ChannelGroup.write,然后将组恢复到完整组。

  3. Write to all channels but implement some Handler logic to discard (either on Encode or Downstream Handler) and not write the message if it is from the excluded channel. 写入所有通道但实现一些处理程序逻辑以丢弃(在编码或下游处理程序上),如果来自排除的通道则不写入消息。

As far as design trade offs are concerned, messages are sent as rapidly as possible, ChannelGroups are relatively small <20 channels and messages are about 1kb in size each (500 bytes - 2 kb). 就设计权衡而言,消息尽可能快地发送,ChannelGroups相对较小<20个通道,消息大小约为1kb(500字节 - 2kb)。 Also, there is a scenario under which many of the connections are on the same host as the Server if different logic works better for localhost feedback on this would be greatly appreciated. 此外,还有一种情况,在这种情况下,如果不同的逻辑更适合本地主机反馈,许多连接与服务器位于同一主机上,将非常感激。

Thanks so much for any help! 非常感谢您的帮助!

I would not do #2. 我不会做#2。 So far as I know, there is no inherent performance benefit to writing to a channel group. 据我所知,写入频道组没有固有的性能优势。 If you look at the code for DefaultChannelGroup , it simply iterates through the registered channels, issues the write and collects the futures: 如果查看DefaultChannelGroup的代码,它只需遍历已注册的通道,发出写入并收集期货:

public ChannelGroupFuture write(Object message) {
         Map<Integer, ChannelFuture> futures =
             new LinkedHashMap<Integer, ChannelFuture>(size());
         if (message instanceof ChannelBuffer) {
             ChannelBuffer buf = (ChannelBuffer) message;
             for (Channel c: nonServerChannels.values()) {
                 futures.put(c.getId(), c.write(buf.duplicate()));
             }
         } else {
             for (Channel c: nonServerChannels.values()) {
                 futures.put(c.getId(), c.write(message));
             }
         }
         return new DefaultChannelGroupFuture(this, futures);
     }

.... although you do get the benefit of a multiplexed future, which if you care about the completion callbacks, is useful. ....虽然你确实得到了多路未来的好处,如果你关心完成回调,这是有用的。

I would not do #3 either since you are just giving the downstream handlers more work to do, discarding data that you went to all the bother to write in the first place. 我不会做#3,因为你只是给下游处理程序做更多的工作,丢弃你首先要写的所有麻烦的数据。

So I would either: 所以我要么:

  1. Implement Option #1 实施选项#1
  2. Extend DefaultChannelGroup and add a write method that is clever enough to skip the channel in question. 扩展DefaultChannelGroup并添加一个足够聪明的write方法来跳过相关频道。 You would probably have to provide a bit more meta-data when you add the channels to the group. 将通道添加到组时,可能需要提供更多的元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM