简体   繁体   中英

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.

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. That is, if you have 1000 connections, you have 1000 pipelines. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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