public bool NewServiceSettings(ServiceSettings requestParams) { if (requestParams.WCFPort <= 0) { throw new PosException("WCF 端口配置错误!"); } if (requestParams.SocketPort <= 0) { throw new PosException("Socket 端口配置错误!"); } if (string.IsNullOrEmpty(requestParams.Redis)) { throw new PosException("Redis 配置不能为空!"); } if (!Regex.IsMatch(requestParams.SocketIp, @"^((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){3}$|^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$")) { throw new PosException("Socket Ip 非法!"); } if (!Regex.IsMatch(requestParams.WCFIp, @"^((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){3}$|^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$")) { throw new PosException("WCF Ip 非法!"); } System.Configuration.Configuration config1 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); RedisConfiguration redisconfig = new RedisConfiguration(); redisconfig.ConnectionString = requestParams.Redis; config1.Sections.Remove("RedisConfig"); config1.Sections.Add("RedisConfig", redisconfig); SocketClientConfig socketClientConfig = new SocketClientConfig(); socketClientConfig.Ip = requestParams.SocketIp; socketClientConfig.Port = requestParams.SocketPort; config1.Sections.Remove("SocketClientConfig"); config1.Sections.Add("SocketClientConfig", socketClientConfig); config1.Save(); SetEndpointAddress("WSHttpBinding_PosServerDbSyncService", string.Format(@"net.tcp://{0}:{1}/PosServerDbSyncService.svc", requestParams.WCFIp, requestParams.WCFPort)); return(true); }
public PosStoreClient() { ReflectCommand(); isDispose = false; this.Initialize(new PosStoreFixedHeaderReceiveFilter(new PosStoreCommandNameProvider()), (package) => { if (cmdRount.ContainsKey(package.Key) && cmdRount[package.Key] != null) { var type = cmdRount[package.Key]; var cmd = (ICommand)AppDomain.CurrentDomain.CreateInstance(type.Assembly.FullName, type.ToString()).Unwrap(); if (cmd.Key == package.Key) { cmd.Execute(this, package); } } }); var config = SocketClientConfig.GetConfig(); var connected = this.ConnectAsync(new IPEndPoint(IPAddress.Parse(config.Ip), config.Port)); connected.Wait(); SendHeartbeatPacket(); SubscribeStoreMesssage(); }
private void SendHeartbeatPacket() { Task.Factory.StartNew((s) => { if (isDispose) { return; } while (!cancelTokenSource.IsCancellationRequested) { try { var config = SocketClientConfig.GetConfig(); this.SendObject(new byte[4] { 0x00, 0x00, 0x00, 0x01 }, new StoreInfo { CompanyId = config.CompanyId, StoreId = config.StoreId }); Thread.Sleep(5000); } catch { } } }, cancelTokenSource.Token); }
private void SendHeartbeatPacket() { Task.Factory.StartNew(() => { if (isDispose) { return; } while (true) { try { var config = SocketClientConfig.GetConfig(); this.SendObject(new byte[4] { 0x00, 0x00, 0x00, 0x01 }, new StoreInfo { CompanyId = config.CompanyId, StoreId = config.StoreId }); Thread.Sleep(5000); } catch { } } }); }
/// <summary> /// Called by XmlSerializationSectionHandler when the config is reloaded. /// </summary> public void ReloadConfig(object sender, EventArgs args) { SocketClientConfig newConfig = GetConfig(); if (mySettings == defaultMessageSettings) //current using defaults, if defaults change we want to change the active socket pool { if (!newConfig.DefaultSocketSettings.SameAs(defaultMessageSettings)) //settings have changed, need to change "mySocketPool(s)" reference { if (log.IsInfoEnabled) log.Info("Default socket settings changed, updating default socket pool."); mySocketPools = SocketManager.Instance.GetSocketPools(newConfig.DefaultSocketSettings); if (mySocketPool != null && myEndPoint != null) { SocketPool oldDefault = mySocketPool; mySocketPool = GetSocketPool(myEndPoint, newConfig.DefaultSocketSettings); oldDefault.ReleaseAndDisposeAll(); } } mySettings = newConfig.DefaultSocketSettings; } defaultMessageSettings = newConfig.DefaultSocketSettings; config = newConfig; }
private static SocketClientConfig GetConfig() { SocketClientConfig config = null; try { config = (SocketClientConfig)ConfigurationManager.GetSection("SocketClient"); } catch (Exception ex) { if (log.IsErrorEnabled) log.ErrorFormat("Exception getting socket client config: {0}", ex); } if (config == null) { if (log.IsWarnEnabled) log.Warn("No Socket Client Config Found. Using defaults."); config = new SocketClientConfig(); config.DefaultSocketSettings = new SocketSettings(); } return config; }