Пример #1
0
        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            var loader = new ResourceLoader();

            if (!int.TryParse(portBox.Text, out int port))
            {
                NotifyUser(loader.GetString(PortIncorrect));
                return;
            }

            var    allowAnonymous = allowAnonymousBox.IsChecked == true;
            string userName       = userNameBox.Text;
            string password       = passwordBox.Text;

            var settings = ApplicationData.Current.LocalSettings;

            settings.Values[PortNumberSetting]     = port;
            settings.Values[AllowAnonymousSetting] = allowAnonymous;
            settings.Values[UserNameSetting]       = userName;
            settings.Values[PasswordSetting]       = password;

            var x = VisualStateManager.GoToState(this, nameof(runningState), true);

            statusBlock4.Text = string.Format(loader.GetString(Ipv4Listening), port);
            statusBlock6.Text = string.Format(loader.GetString(Ipv6Listening), port);

            cts = new CancellationTokenSource();
            IPEndPoint ep4 = new IPEndPoint(IPAddress.Any, port);
            IPEndPoint ep6 = new IPEndPoint(IPAddress.IPv6Any, port);

            await Task.Run(() =>
            {
                if (allowAnonymous)
                {
                    server4 = new FtpServer(
                        ep4,
                        new UwpFileProviderFactory(rootPath),
                        new Zhaobang.FtpServer.Connections.LocalDataConnectionFactory(),
                        new Zhaobang.FtpServer.Authenticate.AnonymousAuthenticator());
                    server6 = new FtpServer(
                        ep6,
                        new UwpFileProviderFactory(rootPath),
                        new Zhaobang.FtpServer.Connections.LocalDataConnectionFactory(),
                        new Zhaobang.FtpServer.Authenticate.AnonymousAuthenticator());
                }
                else
                {
                    server4 = new FtpServer(
                        ep4,
                        new UwpFileProviderFactory(rootPath),
                        new Zhaobang.FtpServer.Connections.LocalDataConnectionFactory(),
                        new Zhaobang.FtpServer.Authenticate.SimpleAuthenticator(userName, password));
                    server6 = new FtpServer(
                        ep6,
                        new UwpFileProviderFactory(rootPath),
                        new Zhaobang.FtpServer.Connections.LocalDataConnectionFactory(),
                        new Zhaobang.FtpServer.Authenticate.SimpleAuthenticator(userName, password));
                }

                server4.Tracer.UserConnected    += Tracer_UserConnected;
                server6.Tracer.UserConnected    += Tracer_UserConnected;
                server4.Tracer.CommandInvoked   += Tracer_CommandInvoked;
                server6.Tracer.CommandInvoked   += Tracer_CommandInvoked;
                server4.Tracer.ReplyInvoked     += Tracer_ReplyInvoked;
                server6.Tracer.ReplyInvoked     += Tracer_ReplyInvoked;
                server4.Tracer.UserDisconnected += Tracer_UserDisconnected;
                server6.Tracer.UserDisconnected += Tracer_UserDisconnected;
            });

            var server4Deferral = ExtendedExecutionHelper.GetDeferral();

            server4Run = server4.RunAsync(cts.Token).ContinueWith(async t =>
                                                                  await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (t.IsFaulted)
                {
                    statusBlock4.Text = string.Format(loader.GetString(Ipv4Error), t.Exception);
                }
                else
                {
                    statusBlock4.Text = loader.GetString(Ipv4Stopped);
                }
                server4Deferral.Complete();
            }));
            var server6Deferral = ExtendedExecutionHelper.GetDeferral();

            server6Run = server6.RunAsync(cts.Token).ContinueWith(async t =>
                                                                  await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (t.IsFaulted)
                {
                    statusBlock6.Text = string.Format(loader.GetString(Ipv6Error), t.Exception);
                }
                else
                {
                    statusBlock6.Text = loader.GetString(Ipv6Stopped);
                }
                server6Deferral.Complete();
            }));
        }