コード例 #1
0
ファイル: NettyServer.cs プロジェクト: wutao0315/zooland
        public NettyServer(URL config,
                           object service,
                           bool isSsl,
                           string pfx,
                           string pwd,
                           IRegistryService registryService)
            : base(registryService)
        {
            Settings        = NettyTransportSettings.Create(config);
            ConnectionGroup = new ConcurrentSet <IChannel>();

            _serverEventLoopGroup = new MultithreadEventLoopGroup(Settings.ServerSocketWorkerPoolSize);

            _service = service;
            _isSsl   = isSsl;
            _pfx     = pfx;
            _pwd     = pwd;
        }
コード例 #2
0
ファイル: NettyClientPool.cs プロジェクト: wutao0315/zooland
        protected override async Task <IClient> CreateClient(URL url)
        {
            Settings = NettyTransportSettings.Create(url);

            var protocol = _nettyProtocols[url.Protocol];

            IEventLoopGroup group = protocol.EventLoopGroupType
                                    .GetConstructor(Array.Empty <Type>())
                                    .Invoke(Array.Empty <object>()) as IEventLoopGroup;

            IChannel clientChannel = protocol.ChannelType
                                     .GetConstructor(Array.Empty <Type>())
                                     .Invoke(Array.Empty <object>()) as IChannel;

            var isSsl = url.GetParameter(SSL_KEY, DEFAULT_SSL);

            X509Certificate2 cert       = null;
            string           targetHost = null;

            if (isSsl)
            {
                var pfx = url.GetParameter(PFX_KEY, DEFAULT_PFX);
                var pwd = url.GetParameter(PWD_KEY, DEFAULT_PWD);
                cert       = new X509Certificate2(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{pfx}.pfx"), pwd);
                targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
            }


            var bootstrap = new Bootstrap();

            bootstrap
            .ChannelFactory(() => clientChannel)
            .Option(ChannelOption.SoReuseaddr, Settings.TcpReuseAddr)
            .Option(ChannelOption.SoKeepalive, Settings.TcpKeepAlive)
            .Option(ChannelOption.TcpNodelay, Settings.TcpNoDelay)
            .Option(ChannelOption.ConnectTimeout, Settings.ConnectTimeout)
            .Option(ChannelOption.AutoRead, true)
            .Option(ChannelOption.Allocator, PooledByteBufferAllocator.Default)
            .Group(group)
            .Handler(new ActionChannelInitializer <IChannel>(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(new LengthFieldPrepender(4));
                //pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4));

                pipeline.AddLast(new NettyLoggingHandler());
                SetInitialChannelPipeline(channel);
                pipeline.AddLast(new ClientHandler(ReceivedMessage));
            }));

            if (Settings.ReceiveBufferSize.HasValue)
            {
                bootstrap.Option(ChannelOption.SoRcvbuf, Settings.ReceiveBufferSize.Value);
            }
            if (Settings.SendBufferSize.HasValue)
            {
                bootstrap.Option(ChannelOption.SoSndbuf, Settings.SendBufferSize.Value);
            }
            if (Settings.WriteBufferHighWaterMark.HasValue)
            {
                bootstrap.Option(ChannelOption.WriteBufferHighWaterMark, Settings.WriteBufferHighWaterMark.Value);
            }
            if (Settings.WriteBufferLowWaterMark.HasValue)
            {
                bootstrap.Option(ChannelOption.WriteBufferLowWaterMark, Settings.WriteBufferLowWaterMark.Value);
            }


            var client          = bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(url.Host), url.Port)).GetAwaiter().GetResult();
            var messageListener = new MessageListener();

            client.GetAttribute(messageListenerKey).Set(messageListener);

            var timeout = url.GetParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);

            await Task.CompletedTask;

            return(new NettyClient(group, client, messageListener, timeout, url));
        }