简体   繁体   English

网络设计[使用Netty]

[英]Networking Design [Using Netty]

I'm working on a multiplayer gaming project and I'm a tad bit confused on how to set it up. 我正在做一个多人游戏项目,我对如何设置有点困惑。 Mainly because I'm not familiar with the Netty framework. 主要是因为我对Netty框架不熟悉。

Should each player have his own Pipe for handling packets? 每个玩家都应该有自己的管道来处理数据包吗? Or should there just be one pipe for handling all inbound packets? 还是应该只有一个管道来处理所有入站数据包?

If a player should have his own packet, how would i make that player the owner of the pipeline? 如果玩家应该拥有自己的数据包,我将如何使其成为管道的所有者?

Currently this is my server-code 目前这是我的服务器代码

public static void main(String[] params) throws Exception
    {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try 
        {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,  100)
                .handler(new LoggingHandler(LogLevel.DEBUG))
                .childHandler(new ChannelInitializer<SocketChannel>()
            {
                @Override
                public void initChannel(SocketChannel channel) throws Exception 
                {
                    ChannelPipeline pipeline = channel.pipeline();
                    pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
                    pipeline.addLast("PacketHandler", new SinglePacketPipe());
                    System.err.println("Connection Established - Pipes constructed..");
                }
            });

            ChannelFuture future = bootstrap.bind(SERVER_PORT).sync();
            System.err.println("Server initialized..");
            // When the server socket is closed, destroy the future.
            future.channel().closeFuture().sync();
        }
        finally
        {
            // Destroy all executor groups
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

By definition, a Channel in Netty always has its own pipeline. 根据定义,Netty中的通道始终具有自己的管道。 That is, if you have 1000 connections, you have 1000 pipelines. 也就是说,如果您有1000个连接,则有1000个管道。 It is completely up to you to let them have the same set of handlers in those pipelines, or to let some of them have different pipeline configuration. 让它们在这些管道中具有相同的处理程序集,或者让其中一些具有不同的管道配置完全取决于您。

Note that a pipeline is dynamically configurable. 请注意,管道是可动态配置的。 You can decide what handlers to add/remove in the initChannel() or in your handler's channelRegistered() (or even in any handler methods). 您可以在initChannel()或处理程序的channelRegistered() (甚至在任何处理程序方法中)决定要添加/删除的处理程序。

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

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