Exemplo n.º 1
0
 private async void okTestButton_Click(object sender, EventArgs e)
 {
     if (isConnectionTested)
     {
         // add connection to file and close
         if (editingInfo != null)
         {
             if (nameInput.Text != editingInfo.ConnectionName &&
                 ConnectionsDAL.ConnectionExists(nameInput.Text))
             {
                 MessageBox.Show(
                     $"A connection with the name \"{nameInput.Text}\" " +
                     "already exists." + Environment.NewLine +
                     "Please select another name.",
                     "Connection rejected",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return;
             }
             else
             {
                 ConnectionsDAL.EditConnection(
                     editingInfo, nameInput.Text, ipInput.Text);
             }
         }
         else
         {
             ConnectionsDAL.TryAddConnection(new ConnectionInfo(nameInput.Text, ipInput.Text));
         }
         DialogResult = DialogResult.OK;
     }
     else
     {
         string ipString = ipInput.Text;
         // test connection
         if (ipString.IsIPv4Format() &&
             IPAddress.TryParse(ipString, out IPAddress ip))
         {
             okTestButton.Enabled       = false;
             cancelButton.Enabled       = false;
             connectionResultLabel.Text = "Connecting...";
             LSClient testClient = new LSClient();
             if (await testClient.TryConnectAsync(ip, 1337))
             {
                 // connection successful
                 testClient.Close();
                 isConnectionTested         = true;
                 okTestButton.Text          = "OK";
                 connectionResultLabel.Text = "Connected!";
             }
             else
             {
                 connectionResultLabel.Text = "Connection failed";
             }
             okTestButton.Enabled = true;
             cancelButton.Enabled = true;
         }
         else
         {
             // invalid IP
             connectionResultLabel.Text = "Invalid IP";
         }
     }
 }