Exemple #1
0
            protected override void Init()
            {
#if DEBUG
                NTStopwatch.Start();
#endif
                _localIps = GetLocalIps();
                RaiseEvent(new LocalIpSetInitedEvent());
#if DEBUG
                // 将近300毫秒
                var elapsedMilliseconds = NTStopwatch.Stop();
                if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
                {
                    NTMinerConsole.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.{nameof(Init)}");
                }
#endif
            }
Exemple #2
0
            private void Init()
            {
#if DEBUG
                NTStopwatch.Start();
#endif
                lock (_locker) {
                    if (!_isInited)
                    {
                        _isInited = true;
                        _localIps = GetLocalIps();
                        RaiseEvent(new LocalIpSetInitedEvent());
                    }
                }
#if DEBUG
                // 将近300毫秒
                var elapsedMilliseconds = NTStopwatch.Stop();
                if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
                {
                    NTMinerConsole.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.{nameof(Init)}");
                }
#endif
            }
            private void Init()
            {
#if DEBUG
                NTStopwatch.Start();
#endif
                lock (_locker) {
                    if (!_isInited)
                    {
                        _isInited = true;
                        NetworkChange.NetworkAddressChanged += (object sender, EventArgs e) => {
                            // 延迟获取网络信息以防止立即获取时获取不到
                            1.SecondsDelay().ContinueWith(t => {
                                var old   = _localIps;
                                _isInited = false;
                                InitOnece();
                                var localIps = _localIps;
                                if (localIps.Length == 0)
                                {
                                    ThisLocalWarn(nameof(LocalIpSetImpl), "网络连接已断开", toConsole: true);
                                }
                                else if (old.Length == 0)
                                {
                                    ThisLocalInfo(nameof(LocalIpSetImpl), "网络连接已连接", toConsole: true);
                                }
                            });
                        };
                        NetworkChange.NetworkAvailabilityChanged += (object sender, NetworkAvailabilityEventArgs e) => {
                            if (e.IsAvailable)
                            {
                                ThisLocalInfo(nameof(LocalIpSetImpl), $"网络可用", toConsole: true);
                            }
                            else
                            {
                                ThisLocalWarn(nameof(LocalIpSetImpl), $"网络不可用", toConsole: true);
                            }
                        };
                        AddCmdPath <SetLocalIpCommand>(action: message => {
                            #region
                            ManagementObject mo = null;
                            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                                using (ManagementObjectCollection moc = mc.GetInstances()) {
                                    foreach (ManagementObject item in moc)
                                    {
                                        if ((string)item["SettingID"] == message.Input.SettingID)
                                        {
                                            mo = item;
                                            break;
                                        }
                                    }
                                }
                            if (mo != null)
                            {
                                if (message.Input.DHCPEnabled)
                                {
                                    mo.InvokeMethod("EnableStatic", null);
                                    mo.InvokeMethod("SetGateways", null);
                                    mo.InvokeMethod("EnableDHCP", null);
                                    1.SecondsDelay().ContinueWith(t => {
                                        _isInited = false;
                                        InitOnece();
                                    });
                                }
                                else
                                {
                                    ManagementBaseObject inPar = mo.GetMethodParameters("EnableStatic");
                                    inPar["IPAddress"]         = new string[] { message.Input.IPAddress };
                                    inPar["SubnetMask"]        = new string[] { message.Input.IPSubnet };
                                    mo.InvokeMethod("EnableStatic", inPar, null);
                                    inPar = mo.GetMethodParameters("SetGateways");
                                    inPar["DefaultIPGateway"] = new string[] { message.Input.DefaultIPGateway };
                                    mo.InvokeMethod("SetGateways", inPar, null);
                                }

                                if (message.IsAutoDNSServer)
                                {
                                    mo.InvokeMethod("SetDNSServerSearchOrder", null);
                                }
                                else
                                {
                                    ManagementBaseObject inPar    = mo.GetMethodParameters("SetDNSServerSearchOrder");
                                    inPar["DNSServerSearchOrder"] = new string[] { message.Input.DNSServer0, message.Input.DNSServer1 };
                                    mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                                }
                            }
                            #endregion
                        }, location: this.GetType());
                        _localIps = GetLocalIps();
                        RaiseEvent(new LocalIpSetInitedEvent());
                    }
                }
#if DEBUG
                // 将近300毫秒
                var elapsedMilliseconds = NTStopwatch.Stop();
                if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
                {
                    NTMinerConsole.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.{nameof(Init)}");
                }
#endif
            }
Exemple #4
0
        public static Guid GetBrandId(string fileFullName, string keyword)
        {
            if (DevMode.IsInUnitTest)
            {
                throw new InvalidProgramException("不支持单元测试这个方法,因为该方法的逻辑依赖于主程序集而单元测试时主程序集是null");
            }
#if DEBUG
            NTStopwatch.Start();
#endif
            Guid guid = Guid.Empty;
            int  LEN  = keyword.Length;
            if (fileFullName == AppFileFullName)
            {
                Assembly assembly = Assembly.GetEntryAssembly();
                string   name     = $"NTMiner.Brand.{keyword}";
                using (var stream = assembly.GetManifestResourceStream(name)) {
                    if (stream == null)
                    {
                        return(guid);
                    }
                    byte[] data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                    string rawBrand   = Encoding.UTF8.GetString(data);
                    string guidString = rawBrand.Substring(LEN, rawBrand.Length - 2 * LEN);
                    Guid.TryParse(guidString, out guid);
                }
            }
            else
            {
                string rawBrand = $"{keyword}{Guid.Empty}{keyword}";
                byte[] rawData  = Encoding.UTF8.GetBytes(rawBrand);
                int    len      = rawData.Length;
                byte[] source   = File.ReadAllBytes(fileFullName);
                int    index    = 0;
                for (int i = 0; i < source.Length - len; i++)
                {
                    int j = 0;
                    for (; j < len; j++)
                    {
                        if ((j < LEN || j > len - LEN) && source[i + j] != rawData[j])
                        {
                            break;
                        }
                    }
                    if (j == rawData.Length)
                    {
                        index = i;
                        break;
                    }
                }
                string guidString = Encoding.UTF8.GetString(source, index + LEN, len - 2 * LEN);
                Guid.TryParse(guidString, out guid);
            }
#if DEBUG
            var elapsedMilliseconds = NTStopwatch.Stop();
            if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
            {
                NTMinerConsole.DevTimeSpan($"耗时{elapsedMilliseconds} {typeof(VirtualRoot).Name}.GetBrandId");
            }
#endif
            return(guid);
        }