示例#1
0
        public Entry ShowEntryDialog(Entry entry = null)
        {
            if (entry == null) entry = new Entry();

            this.txtDescription.Text = entry.Name;
            this.txtMediaAccessControl.Text = entry.FormattedMac(":");
            this.txtHostName.Text = entry.HostOrIP;
            this.txtPort.Text = entry.PortNumber.ToString();
            this.cbUsePingPacket.Checked = entry.UsePingPacket;
            this.cbUseBroadcast.Checked = entry.UseBroadcast;
            this.txtSecureOnPassword.Text = entry.Password;

            var result = this.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                entry.Name = this.txtDescription.Text.Trim();
                entry.HostOrIP = Utilities.IsValidIpAddress(this.txtHostName.Text) ? Utilities.FormatIpAddress(this.txtHostName.Text) : this.txtHostName.Text.Trim();
                entry.MediaAccessControl = Utilities.FormatMediaAccessControl(this.txtMediaAccessControl.Text, "");
                entry.PortNumber = Utilities.ParsePort(this.txtPort.Text);
                entry.UsePingPacket = this.cbUsePingPacket.Checked;
                entry.UseBroadcast = this.cbUseBroadcast.Checked;
                entry.Password = this.txtSecureOnPassword.Text.Trim().ToUpper();

                return entry;
            }
            else
            {
                return null;
            }
        }
示例#2
0
文件: WOL.cs 项目: gyphie/wakeonlan
        public static WOL.Results Wake(Entry entry)
        {
            try
            {
                var hasPassword = !string.IsNullOrEmpty(entry.Password) && entry.Password.Length == 12;
                var payloadLength = hasPassword ? 114 : 102;

                byte[] payload = new byte[payloadLength];
                Buffer.BlockCopy(sync, 0, payload, 0, sync.Length);

                int offset = sync.Length;
                var mac = entry.GetMacBytes();
                for (int i = 0; i < 16; i += 1)
                {
                    Buffer.BlockCopy(mac, 0, payload, i * mac.Length + offset, mac.Length);
                }

                if (hasPassword)
                {
                    Buffer.BlockCopy(Utilities.HexStringToBytes(entry.Password), 0, payload, 102, 0);
                }

                var host = entry.UseBroadcast ? IPAddress.Broadcast.ToString() : entry.HostOrIP;
                if (entry.UsePingPacket)
                {
                    using (var ping = new Ping())
                    {
                        ping.Send(host, 5, payload);
                        return Results.Sent;
                    }
                }
                else
                {
                    using (var client = new UdpClient())
                    {
                        client.Send(payload, payload.Length, host, entry.PortNumber);
                        return Results.Sent;
                    }
                }
            }
            catch
            {
                return Results.Error;
            }
        }
示例#3
0
        public Entry ShowPreferencesDialog(Entry entry = null)
        {
            if (entry == null) entry = new Entry();

            this.cbUsePingPacket.Checked = entry.UsePingPacket;
            this.cbUseBroadcast.Checked = entry.UseBroadcast;
            this.txtPortNumber.Text = entry.PortNumber.ToString();

            var result = this.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                entry.PortNumber = Utilities.ParsePort(this.txtPortNumber.Text);
                entry.UsePingPacket = this.cbUsePingPacket.Checked;
                entry.UseBroadcast = this.cbUseBroadcast.Checked; ;

                return entry;
            }
            else
            {
                return null;
            }
        }
示例#4
0
        public static bool LoadData(out Entry defaults, out List<Entry> entries)
        {
            defaults = null;
            entries = null;

            var dataFileName = Persistence.GetFilePath();
            if (!File.Exists(dataFileName))
            {
                return false;
            }

            var dataFileContent = File.ReadAllText(dataFileName, Encoding.UTF8);
            JObject jsonObj = JObject.Parse(dataFileContent);

            defaults = new Entry();
            defaults.PortNumber = jsonObj["default"]["PortNumber"].Value<UInt16>();
            defaults.UsePingPacket = jsonObj["default"]["UsePingPacket"].Value<bool>();
            defaults.UseBroadcast = jsonObj["default"]["UseBroadcast"].Value<bool>();

            entries = new List<Entry>();

            foreach (var jsEntry in jsonObj["entries"])
            {
                var entry = new Entry();
                entry.Name = jsEntry["Name"].Value<string>();
                entry.HostOrIP = jsEntry["HostOrIP"].Value<string>();
                entry.PortNumber = jsEntry["PortNumber"].Value<UInt16>();
                entry.MediaAccessControl = jsEntry["MediaAccessControl"].Value<string>();
                entry.UsePingPacket = jsEntry["UsePingPacket"].Value<bool>();
                entry.UseBroadcast = jsEntry["UseBroadcast"].Value<bool>();
                entry.Password = jsEntry["Password"].Value<string>();

                entries.Add(entry);
            }

            return true;
        }
示例#5
0
        public static void SaveData(Entry defaults, List<Entry> entries)
        {
            var saveObj = new
            {
                @default = defaults,
                entries = entries
            };

            string fileName = Persistence.GetFilePath();

            JsonSerializer serializer = new JsonSerializer();
            serializer.Converters.Add(new JavaScriptDateTimeConverter());
            serializer.NullValueHandling = NullValueHandling.Include;

            Directory.CreateDirectory(Path.GetDirectoryName(fileName));

            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
            {
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, saveObj);
                }
            }
        }