示例#1
0
        /// <summary>
        /// 启动连接
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public async Task Start(string host, int port)
        {
            group      = new MultithreadEventLoopGroup();
            resetEvent = new AutoResetEvent(false);
            X509Certificate2 cert       = null;
            string           targetHost = null;

            if (ClientSettings.IsSsl)
            {
                cert       = new X509Certificate2(Path.Combine("", "dotnetty.com.pfx"), "password");
                targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
            }
            try
            {
                var 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("framing-enc", new LengthFieldPrepender(2));
                    pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
                    simpleClient = new SimpleClientHandler();
                    pipeline.AddLast("echo", simpleClient);
                    simpleClient.DataNotify += SimpleClient_DataNotity;
                }));
                conTime = DateTime.Now.Ticks;
                Console.WriteLine("客户端连接中");
                clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(host), port));

                isConnected = true;
            }
            catch
            {
                IsUnConnect = true;
                Logger.Singleton.ErrorFormat("客户端无法连接成功,服务端IP{0},端口{1}", host, port);
                if (clientChannel != null)
                {
                    await clientChannel.CloseAsync();
                }
                await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            }
        }
示例#2
0
        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public async Task Close()
        {
            Console.WriteLine("NettyClient关闭");
            isConnected = false;
            queue.Dispose();
            if (simpleClient != null)
            {
                simpleClient.DataNotify -= SimpleClient_DataNotity;
                simpleClient             = null;
            }
            if (clientChannel != null)
            {
                await clientChannel.CloseAsync();
            }
            await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));

            clientChannel = null;
            group         = null;
        }