コード例 #1
0
ファイル: TcpSocketClient.cs プロジェクト: dora-BYR/Fenix
        //public static TcpSocketClient Instance = new TcpSocketClient();

        public bool init(TcpChannelConfig channelConfig)
        {
            this.channelConfig = channelConfig;

            group = new MultithreadEventLoopGroup();

            X509Certificate2 cert       = null;
            string           targetHost = null;

            if (channelConfig.UseSSL)
            {
                cert       = new X509Certificate2("dotnetty.com.pfx", "password");
                targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
            }

            try
            {
                bootstrap = new Bootstrap();
                bootstrap
                .Group(group)
                .Channel <TcpSocketChannel>()
                .Option(ChannelOption.TcpNodelay, true)
                .Handler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;

                    if (cert != null)
                    {
                        pipeline.AddLast("tls",
                                         new TlsHandler(
                                             stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true),
                                             new ClientTlsSettings(targetHost)));
                    }

                    pipeline.AddLast(new LoggingHandler());
                    pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));
                    pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
                    pipeline.AddLast("tcp-handler", new TcpChannelHandler(this));
                }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return(false);
            }

            return(true);
        }
コード例 #2
0
ファイル: TcpSocketServer.cs プロジェクト: wxlonstar/Fenix
        public bool Start(TcpChannelConfig channelConfig, ITcpListener listener)
        {
#if !CLIENT
            if (channelConfig.UseLibuv)
            {
                ResourceLeakDetector.Level = ResourceLeakDetector.DetectionLevel.Disabled;
                var dispatcher = new DispatcherEventLoopGroup();
                bossGroup   = dispatcher;
                workerGroup = new WorkerEventLoopGroup(dispatcher);
            }
            else
            {
                bossGroup   = new MultithreadEventLoopGroup(1);
                workerGroup = new MultithreadEventLoopGroup();
            }
#else
            bossGroup   = new MultithreadEventLoopGroup(1);
            workerGroup = new MultithreadEventLoopGroup();
#endif
            X509Certificate tlsCertificate = null;
            if (channelConfig.UseSSL)
            {
                tlsCertificate = new X509Certificate("dotnetty.com.pfx", "password");
            }

            try
            {
                bootstrap = new ServerBootstrap();
                bootstrap.Group(bossGroup, workerGroup);
#if !CLIENT
                if (channelConfig.UseLibuv)
                {
                    bootstrap.Channel <TcpServerChannel>();
                }
                else
                {
                    bootstrap.Channel <TcpServerSocketChannel>();
                }
#else
                bootstrap.Channel <TcpServerSocketChannel>();
#endif
                bootstrap
                .Option(ChannelOption.SoBacklog, 8192)
                .Option(ChannelOption.SoReuseaddr, true)
                .Option(ChannelOption.SoReuseport, true)
                .Option(ChannelOption.SoBroadcast, true)
                .Option(ChannelOption.SoKeepalive, true)
                .Handler(new LoggingHandler())
                .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    if (tlsCertificate != null)
                    {
                        pipeline.AddLast("tls", TlsHandler.Server(tlsCertificate));
                    }

                    pipeline.AddLast(new LoggingHandler("SRV-CONN"));
                    pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));
                    pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
                    pipeline.AddLast("tcp-handler", new TcpChannelHandler(listener));
                }));

                var task = Task <IChannel> .Run(() => bootstrap.BindAsync(channelConfig.Address));

                task.Wait();
                boundChannel = task.Result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return(false);
            }

            if (boundChannel == null)
            {
                return(false);
            }

            return(true);
        }