private void btnAdd_Click(object sender, RoutedEventArgs e) { if (IPTool.ValidateIP(txbIpToAdd.Text)) { if (!addresses.Contains(IPAddress.Parse(txbIpToAdd.Text))) { addresses.Add(IPAddress.Parse(txbIpToAdd.Text)); lsbAddresses.Items.Refresh(); mWhitelist.Ips.Add(txbIpToAdd.Text); DaWhitelist.SaveToJson(mWhitelist); set = false; active = false; FirewallRule.DeleteRules(); SetIpCount(); UpdateNotActive(); } } }
// 系统环境检测者 public static bool Check(out string errMsg) { errMsg = ""; if (!XMLHelper.LoadDoc(Config.projectConfigPath, out doc, out errMsg)) { return(false); } if (!CheckAndCreateConnStr(out errMsg)) { return(false); } if (!CheckAndCreateConfig(out errMsg)) { return(false); } if (!IPTool.PingIP(Config.ip)) { errMsg = "业务数据库网络不通,请保证网络通畅!"; return(false); } if (!checkConnect()) { errMsg = "测试连接业务数据库失败!"; return(false); } // 二供 Config.pumpParam = new CityWEBDataService.PandaParam(); Config.pumpParam.appKey = "34h3rj3ri3jrt5y778934t5yfg3333h4h"; Config.pumpParam.appSecret = "45tnn5juyojgn3rn3fnn3t5j4to3fn6y64p3"; Config.pumpParam.getTokenUrl = @"https://new.s-water.cn/App/GetAccessToken"; Config.pumpParam.getDataUrl = @"https://new.s-water.cn/App/GetPumpData"; Config.pumpParam.useName = "滦县"; Config.pumpParam.saveInterVal = Config.saveInterVal; if (!Config.pumpParam.Check(out errMsg)) { return(false); } return(true); }
private void BtnAdd_Click(object sender, RoutedEventArgs e) { if (IPTool.ValidateIP(txbIpToAdd.Text)) { IPAddress newIp = IPAddress.Parse(txbIpToAdd.Text); if (!_addresses.Contains(newIp)) { _addresses.Add(newIp); lsbAddresses.Items.Refresh(); _settingsLoader.Save(); _lobbyRulesSet = false; _lobbyRulesActive = false; FirewallRule.DeleteRules(); SetIpCount(); UpdateNotActive(); } } }
public bool Check(out string err) { err = ""; if (!string.IsNullOrWhiteSpace(errMsg)) { err = errMsg; return(false); } if (string.IsNullOrWhiteSpace(commandIP)) { err = "配置文件物联控制主机不是有效的IP地址"; return(false); } if (!IPTool.PingIP(commandIP)) { err = "配置文件物联控制主机网络不通"; return(false); } if (port == 0) { err = "配置文件物联控制主机端口不能为0"; return(false); } if (IPTool.IsValidPort(port)) { err = "配置文件物联控制主机端口为空闲端口,请检查物联网服务是否启动"; return(false); } if (timeoutSeconds == 0) { err = "配置文件物联控制主机缺少服务超时时间数配置"; return(false); } if (string.IsNullOrWhiteSpace(DBUtil.dbConnectString)) { err = "数据库连接字符串未配置"; return(false); } return(true); }
public static List <IPAddress> ReadIPsFromJSON() { List <IPAddress> addresses = new List <IPAddress>(); if (!File.Exists(path)) { SaveToJson(new MWhitelist()); } using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); MWhitelist whitelist = JsonConvert.DeserializeObject <MWhitelist>(json); foreach (string address in whitelist.Ips) { if (IPTool.ValidateIP(address.ToString())) { addresses.Add(IPAddress.Parse(address)); } } } return(addresses); }
private Server() { base.Start(_portUdp, 1000 * 20, 1000, IPTool.GetLocalIPAddress(), _portTcp); _timer = new Timer(SendSignal, "LIGHTHOUSE", 0, 1000); }
public void Start() { try { // 启动前配置文件检查--包括端口检查 Config.configInfo = EnvConfigInfo.SingleInstanceForCS; if (!Config.configInfo.EnvCheckForBeforeRun(out string errMsg)) { return; } // 日志管理器 if (LogManager.IsRuning) { LogManager.Stop(); } LogManager.Start(Config.configInfo.confLogServiceInfo, out errMsg); if (!string.IsNullOrWhiteSpace(errMsg) || LogManager.IsRuning == false) { Stop(); return; } LogManager.AppendDebug(ServerTypeName.Core, "日志管理器已经启动"); // 环境检查-日志启动后,在检查其它配置环境,让错误信息可以报出来 if (!IPTool.PingIP(Config.configInfo.confDataBaseInfo.IP)) { TraceManagerForDispatch.AppendWarning("ping:" + Config.configInfo.confDataBaseInfo.IP + "返回超时"); // 有可能开了防火墙 } if (!EnvChecker.Check(Config.configInfo, out errMsg)) { TraceManagerForDispatch.AppendErrMsg("环境检查未通过:" + errMsg); // 不用听,让客户端主动停,看到消息 return; } else { TraceManagerForDispatch.AppendInfo(string.Format("环境检查通过:已加载【{0}】配置", Config.configInfo.confProjectInfo.ProjectName)); } //// 数据发布器 //if (dataPublish != null) // dataPublish.Stop(); //dataPublish = new DataPublishServer(Config.projectConfigPath); //dataPublish.Start(out errMsg); //if (!string.IsNullOrWhiteSpace(errMsg) || dataPublish.IsRuning == false) //{ // Stop(); // return; //} //LogManager.AppendDebug(ServerTypeName.Dispatch, "数据发布器已经启动"); // 子服务管理器 if (sonServiceManager != null) { sonServiceManager.Stop(); } sonServiceManager = new SonServiceManager(); sonServiceManager.Start(out errMsg); if (!string.IsNullOrWhiteSpace(errMsg) || sonServiceManager.IsRuning == false) { Stop(); return; } LogManager.AppendDebug(ServerTypeName.Core, "子服务管理器器已经启动"); // 命令管理器 if (CommandManager.IsRuning) { CommandManager.Stop(); } CommandManager.Start(this.sonServiceManager.ReceiveCommand, Config.configInfo.confCommandServiceInfo, out errMsg); if (!string.IsNullOrWhiteSpace(errMsg) || CommandManager.IsRuning == false) { Stop(); return; } LogManager.AppendDebug(ServerTypeName.Core, "命令管理器已经启动"); LogManager.AppendInfo(ServerTypeName.Core, "所有服务都已经启动"); IsRuning = true; } catch (Exception e) { if (LogManager.IsRuning) { LogManager.AppendErrMsg(ServerTypeName.Core, "服务启动失败:" + e.Message + "堆栈:" + e.StackTrace); } Stop(); } }
public void Start(out string errMsg) { errMsg = ""; if (IsRuning) { return; } if (this.mqttParam == null) { errMsg = "MQTT服务启动失败,没有指明服务所需参数"; return; } // 检查端口是否空闲 if (!IPTool.IsValidPort(this.mqttParam.port)) { errMsg = "数据发布服务器的监听端口被占用,请更换端口号"; return; } if (mqttServer == null) { try { mqttServer.StopAsync(); mqttServer = null; } catch { } } // MQTT 动态库本身已经实现异步启动,这里不用在用异步调用了 try { var optionsBuilder = new MqttServerOptionsBuilder(); // //在 MqttServerOptions 选项中,你可以使用 ConnectionValidator 来对客户端连接进行验证。 // //比如客户端ID标识 ClientId,用户名 Username 和密码 Password 等。 optionsBuilder.WithConnectionValidator(ClientCheck); //指定 ip地址,默认为本地 if (string.IsNullOrWhiteSpace(this.mqttParam.ip)) { optionsBuilder.WithDefaultEndpointBoundIPAddress(IPAddress.Parse(this.mqttParam.ip)); } else { optionsBuilder.WithDefaultEndpointBoundIPAddress(IPAddress.Any); } //指定端口 optionsBuilder.WithDefaultEndpointPort(this.mqttParam.port); // // optionsBuilder.WithConnectionBacklog(100).WithEncryptedEndpointBoundIPAddress(IPAddress.Any).WithEncryptedEndpointPort(9323); // var certificate = new X509Certificate(@"D:\ddd.cer", ""); //var options = optionsBuilder.Build(); // options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert); // //ssl //var certificate = new X509Certificate(@"D:\ddd.cer", ""); //MqttServerOptions certifOption = new MqttServerOptions(); //certifOption.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert); //certifOption.TlsEndpointOptions.IsEnabled = true; //optionsBuilder.WithEncryptionCertificate(certifOption.TlsEndpointOptions.Certificate); // var optionsBuilder = new MqttServerOptionsBuilder() //.WithConnectionBacklog(100) //.WithEncryptedEndpointPort(1884) //.WithoutDefaultEndpoint(); var mqttServer = new MqttFactory().CreateMqttServer(); //连接记录数,默认 一般为2000 //optionsBuilder.WithConnectionBacklog(2000); mqttServer = new MqttFactory().CreateMqttServer() as MqttServer; // 客户端支持 Connected、Disconnected 和 ApplicationMessageReceived 事件, //用来处理客户端与服务端连接、客户端从服务端断开以及客户端收到消息的事情。 //其中 ClientConnected 和 ClientDisconnected 事件的事件参数一个客户端连接对象 ConnectedMqttClient, //通过该对象可以获取客户端ID标识 ClientId 和 MQTT 版本 ProtocolVersion。 mqttServer.ClientConnected += MqttServer_ClientConnected; mqttServer.ClientDisconnected += MqttServer_ClientDisconnected; //ApplicationMessageReceived 的事件参数包含了客户端ID标识 ClientId 和 MQTT 应用消息 MqttApplicationMessage 对象, //通过该对象可以获取主题 Topic、QoS QualityOfServiceLevel 和消息内容 Payload 等信息。 mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived; // Task task = mqttServer.StartAsync(options); Task task = mqttServer.StartAsync(optionsBuilder.Build()); task.Wait(5000); IsRuning = task.IsCompleted; } catch (Exception ex) { // log.Info("创建Mqtt服务,连接客户端的Id长度过短(不得小于5),或不是指定的合法客户端(以Eohi_开头)"); errMsg = "创建MQTT服务失败:" + ex.Message + "堆栈:" + ex.StackTrace; return; } //Task task = mqttServer.StartAsync(options); //// Task task = mqttServer.StartAsync(optionsBuilder.Build()); //task.Wait(5000); }