You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wol/WoL/Waker/MainForm.cs

230 lines
4.8 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WakeOnLan;
using System.Configuration;
using System.Threading;
namespace Waker
{
public partial class MainForm : Form
{
private readonly Color PING_SUCCESS = Color.Green;
private readonly Color PING_FAIL = Color.Red;
private readonly Color PING_UNKNOWN = Color.Gray;
private string ip;
private string mac;
private string ports;
private string pingPort;
private Thread pingThread;
public MainForm()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
if (!string.IsNullOrEmpty(Settings.Default.ip))
ip = Settings.Default.ip;
if (!string.IsNullOrEmpty(Settings.Default.mac))
mac = Settings.Default.mac;
if (!string.IsNullOrEmpty(Settings.Default.ports))
ports = Settings.Default.ports;
if (!string.IsNullOrEmpty(Settings.Default.pingPort))
pingPort = Settings.Default.pingPort;
FillInformation();
AddressTextBox.TextChanged += AddressTextBox_TextChanged;
AddressTextBox.KeyUp += TextBox_KeyUp;
MacTextBox.KeyUp += TextBox_KeyUp;
PortTextBox.KeyUp += TextBox_KeyUp;
}
private void AddressTextBox_TextChanged(object sender, EventArgs e)
{
SetIp();
}
private bool SetIp()
{
ip = AddressTextBox.Text.Replace(" ", "");
if (string.IsNullOrEmpty(ip))
return false;
AddressTextBox.Text = ip;
Settings.Default.ip = ip;
Settings.Default.Save();
return true;
}
private bool SetInformation()
{
ip = AddressTextBox.Text.Replace(" ", "");
mac = MacTextBox.Text.Replace(" ", "");
ports = PortTextBox.Text.Replace(" ", "");
if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(mac))
return false;
AddressTextBox.Text = ip;
MacTextBox.Text = mac;
PortTextBox.Text = ports;
Settings.Default.ip = ip;
Settings.Default.mac = mac;
Settings.Default.ports = ports;
Settings.Default.Save();
return true;
}
private void FillInformation()
{
if (!string.IsNullOrEmpty(ip))
AddressTextBox.Text = ip;
if (!string.IsNullOrEmpty(mac))
MacTextBox.Text = mac;
if (!string.IsNullOrEmpty(ports))
PortTextBox.Text = ports;
if (!string.IsNullOrEmpty(pingPort))
PingPortTextBox.Text = pingPort;
}
private void WakeButton_Click(object sender, EventArgs e)
{
if (!SetInformation())
{
MessageBox.Show("Invalid information");
return;
}
try
{
string portsString = ports.Replace(" ", "");
if (string.IsNullOrEmpty(portsString))
{
WoL.Wake(ip, mac);
return;
}
string[] portStringArray = portsString.Split(',');
if (portStringArray == null || portStringArray.Length < 1 || string.IsNullOrEmpty(portStringArray[0]))
{
WoL.Wake(ip, mac);
return;
}
int[] portArray = portStringArray.Select(int.Parse).ToArray();
if (portArray == null || portArray.Length< 1)
{
WoL.Wake(ip, mac);
return;
}
WoL.Wake(ip, mac, portArray);
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Enter)
return;
WakeButton_Click(sender, e);
}
private void PingPortTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(PingPortTextBox.Text))
return;
pingPort = PingPortTextBox.Text;
if (!pingTimer.Enabled)
pingTimer.Enabled = true;
}
private void pingTimer_Tick(object sender, EventArgs e)
{
try
{
int targetPort = 0;
if (!int.TryParse(pingPort, out targetPort))
{
PingStatusPanel.BackColor = PING_UNKNOWN;
return;
}
if (targetPort < 1)
{
PingStatusPanel.BackColor = PING_UNKNOWN;
return;
}
if (string.IsNullOrEmpty(ip))
return;
Settings.Default.pingPort = pingPort;
Settings.Default.Save();
object[] parameters = new object[2] { ip, targetPort };
pingThread = new Thread(new ParameterizedThreadStart(SendPing));
pingThread.Start(parameters);
} catch (Exception ex)
{
pingTimer.Enabled = false;
MessageBox.Show(ex.Message);
}
}
public void SendPing(object parameters)
{
object[] src = parameters as object[];
if (src == null || src.Length < 2)
return;
string ip = src[0] as string;
int? port = src[1] as int?;
if (ip == null || port == null)
return;
if (PingCheck.Test(ip, port == null ? default(int) : port.Value))
{
PingStatusPanel.BackColor = PING_SUCCESS;
}
else
PingStatusPanel.BackColor = PING_FAIL;
}
}
}