Example #1
0
        static async Task RunServerAsync(int port)
        {
            var bossGroup   = new MultithreadEventLoopGroup(1);
            var workerGroup = new MultithreadEventLoopGroup();

            var stringEncoder = new StringEncoder();
            var stringDecoder = new StringDecoder();
            var serverHandler = new ServerHandler(GameCore);

            try
            {
                var bootstrap = new ServerBootstrap();
                bootstrap
                .Group(bossGroup, workerGroup)
                .Channel <TcpServerSocketChannel>()
                .Option(ChannelOption.SoBacklog, 100)
                .Handler(new LoggingHandler(LogLevel.INFO))
                .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    var pipeline = channel.Pipeline;

                    pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, Delimiters.LineDelimiter()));
                    pipeline.AddLast(stringEncoder, stringDecoder, serverHandler);
                }));

                var bootstrapChannel = await bootstrap.BindAsync(port);

                GameCore.RunContainers();
                await bootstrapChannel.CloseAsync();
            }
            finally
            {
                Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
            }
        }
Example #2
0
        static async Task RunServerAsync()
        {
            ExampleHelper.SetConsoleLogger();

            var bossGroup   = new MultithreadEventLoopGroup(1);
            var workerGroup = new MultithreadEventLoopGroup();

            try
            {
                var bootstrap = new ServerBootstrap();
                bootstrap
                .Group(bossGroup, workerGroup)
                .Channel <TcpServerSocketChannel>()
                .Option(ChannelOption.SoBacklog, 100)
                .Handler(new LoggingHandler("SRV-LSTN"))
                .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    pipeline = channel.Pipeline;

                    pipeline.AddLast(new LoggingHandler("SRV-CONN"));

                    pipeline.AddLast(new XmlStreamDecoder());

                    pipeline.AddLast(new XmppXElementEncoder());
                    pipeline.AddLast(new UTF8StringEncoder());

                    pipeline.AddLast(new ServerConnectionHandler());

                    pipeline.AddLast(new XmppStreamEventHandler());

                    pipeline.AddLast(new StartTlsHandler(new PfxCertificateProvider()));
                    pipeline.AddLast(new SaslPlainHandler());
                    pipeline.AddLast(new SaslScramHandler());
                    pipeline.AddLast(new BindHandler());
                    pipeline.AddLast(new SessionHandler());
                    pipeline.AddLast(new RosterHandler());

                    pipeline.AddLast(new XmppStanzaHandler());
                }));

                IChannel boundChannel = await bootstrap.BindAsync(IPAddress.Any, ServerSettings.Port);

                Console.ReadLine();

                await boundChannel.CloseAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                await Task.WhenAll(
                    bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)),
                    workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
            }
        }