private void ServerStop() { if (ClientMaintaining.IsRunningAsLocalAdmin() && ServerMaintaining.host.State != CommunicationState.Closed && ServerMaintaining.host.State != CommunicationState.Closing && ServerMaintaining.host.State != CommunicationState.Created) { ServerMaintaining.ServerStop(); } }
public MainWindow() { const string WINDOWS_FIREWALL_SERVICE = "MpsSvc"; using (var sc = new ServiceController(WINDOWS_FIREWALL_SERVICE)) { if ((sc.Status == ServiceControllerStatus.Running)) { Log.Instance.W(this, "Found working Windows Firewall Service"); MessageBoxResult result = MessageBox.Show(ClientUIPresenter.GetString("FoundworkingWindowsFirewallServiceWouldyouliketostopit") , ClientUIPresenter.GetString("Question") , MessageBoxButton.YesNo); switch (result) { case MessageBoxResult.Yes: if (ClientMaintaining.IsRunningAsLocalAdmin()) { Log.Instance.W(this, ClientUIPresenter.GetString("Tryingtostop") ); try { sc.Stop(); ServiceHelper.ChangeStartMode(sc, ServiceStartMode.Disabled); System.Windows.MessageBox.Show( ClientUIPresenter.GetString("WindowsFirewallServicehasbeenstopped") , ClientUIPresenter.GetString("Success") , MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception e) { Log.Instance.W(this, ClientUIPresenter.GetString("Errorwhilestoptrying") + e.Message); System.Windows.MessageBox.Show( "Error while stop trying: " + e.Message, "Attention", MessageBoxButton.OK, MessageBoxImage.Warning); } //sc.sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); if (sc.Status == ServiceControllerStatus.Stopped) { Console.WriteLine("Service '{0}' has been stopped", sc.DisplayName); } } else { System.Windows.MessageBox.Show( ClientUIPresenter.GetString("TostoptheWindowsFirewallServiceyoumustRunthisappasAdministrator") , ClientUIPresenter.GetString("Insufficientrightstostoptheservice") , MessageBoxButton.OK, MessageBoxImage.Warning); } break; case MessageBoxResult.No: System.Windows.MessageBox.Show( ClientUIPresenter.GetString("Youmayencountersomeproblemswhensearchingforotherpeoplesservers") , ClientUIPresenter.GetString("Attention") , MessageBoxButton.OK, MessageBoxImage.Warning); break; } } } settings.LocaleChanged += ChangeFormLocale; ClientMaintaining.ClientStateChanged += ClientEventsHandler; InitializeComponent(); cbLanguage.ItemsSource = new List <string> { "ru-RU", "en-US", "ms-MY" }; FillUpTextFields(); ChatGrid.Width = 0; SetDefaultValues(); ChatClientHandler.ChatMessageCame += (string s) => { Dispatcher.BeginInvoke(new ThreadStart(delegate { lbChatMessages.ItemsSource = new List <string>(); lbChatMessages.ItemsSource = ChatClientHandler.ChatBoxContainment; lbChatMessages.ScrollIntoView(lbChatMessages.Items[lbChatMessages.Items.Count - 1]); })); }; void SetDefaultValues() { tbLeaderName.Text = ClientMaintaining.IsRunningAsLocalAdmin() ? Miscelanious.GetComputerName() : string.Empty; tbPort.Text = ClientMaintaining.IsRunningAsLocalAdmin() ? "666" : string.Empty; tbYourName.Text = Miscelanious.GetComputerName(); } void ChangeFormLocale(string locale) { FillUpTextFields(); } void FillUpTextFields() { ClientUIPresenter presenter = new ClientUIPresenter(this); presenter.FillUpTextFields(); } StartupManual manual = new StartupManual(); if (!File.Exists(Path.Combine(Assembly.GetExecutingAssembly().Location .Replace(Assembly.GetExecutingAssembly().ManifestModule.Name, "") + "firstrun"))) { manual.ShowDialog(); File.Create(Path.Combine(Assembly.GetExecutingAssembly().Location .Replace(Assembly.GetExecutingAssembly().ManifestModule.Name, "") + "firstrun")); } // Create a timer with a two second interval. aTimer = new System.Timers.Timer(2000); // Hook up the Elapsed event for the timer. aTimer.Elapsed += MonitoringStringAutoUpdating; aTimer.AutoReset = true; aTimer.Enabled = true; }
private async void BtHost_Click(object sender, RoutedEventArgs e) { LeaderServer.Leader.ServerDNSName = tbLeaderName.Text.Trim(); LeaderServer.Leader.VisibleName = tbYourName.Text.Trim(); LeaderServer.Leader.ServerIP = Miscelanious.ResolveIP(tbLeaderName.Text.Trim()).FirstOrDefault(); if (ClientMaintaining.IsRunningAsLocalAdmin()) { if (ServerMaintaining.host.State != CommunicationState.Opened && ServerMaintaining.host.State != CommunicationState.Opening) { imgCircleArrows.RenderTransform = new RotateTransform(); IsScanningForServers = false; ClientMaintaining.scan.ScanCancel(); bool ServerSuccessfullyStarted = false; try { ServerSuccessfullyStarted = await Task.Run(async() => { ServerMaintaining.ServerStart(); return(true); }); } catch (InvalidOperationException exception) { System.Windows.MessageBox.Show( ClientUIPresenter.GetString("ThehostnameandportarealreadyinuseWaitforalittle") + exception.Message, ClientUIPresenter.GetString("Error") , MessageBoxButton.OK, MessageBoxImage.Error); ClientMaintaining.Disconnect(); } catch (AddressAlreadyInUseException exception) { System.Windows.MessageBox.Show( ClientUIPresenter.GetString("Thehostnameandportarealreadyinuse") , ClientUIPresenter.GetString("Error") , MessageBoxButton.OK, MessageBoxImage.Error); ClientMaintaining.Disconnect(); } if (ServerSuccessfullyStarted) { ClientMaintaining.isSelfServer = true; expander.IsExpanded = false; Expander_Collapsed(this, null); LeaderServer.Leader.ServerState = ServerStates.DelivererSet; //ServerStatus.Current.ServerState = ServerStates.DelivererSet; GridContainerForUC.Children.Clear(); GridContainerForUC.Children.Add(new ucSelector()); Log.Instance.W(this, "Server is launched now trying to connect to ourselves"); ClientMaintaining.Join(tbYourName.Text, true); //if (ClientMaintaining.GetConnectionStatus()) //{ // ClientMaintaining.SendMessage("Hello to Everyone!"); //} } } else { string infoString = ClientUIPresenter.GetString("Yourserverisalreadystarted") ; if (LeaderServer.Leader.ServerDNSName != string.Empty) { infoString += Environment.NewLine + ClientUIPresenter.GetString("Itiscalled") + LeaderServer.Leader.VisibleName + Environment.NewLine + ClientUIPresenter.GetString("Theotherusersoughttolookfor") + LeaderServer.Leader.ServerDNSName; foreach (var ip in NetworkComputers.ResolveIP(LeaderServer.Leader.ServerDNSName).Select(x => x.ToString()).Distinct()) { infoString += Environment.NewLine + ClientUIPresenter.GetString("or") + ip; } } System.Windows.MessageBox.Show(infoString , "Информация", MessageBoxButton.OK, MessageBoxImage.Information); } } else { System.Windows.MessageBox.Show(ClientUIPresenter.GetString("TolaunchyourownserveryouneedtoRunthisappasAdministrator") , ClientUIPresenter.GetString("Insufficientrightstolaunchaserver") , MessageBoxButton.OK, MessageBoxImage.Warning); } }