Example #1
0
        private void SendData(Object state)
        {
            if (_Times-- <= 0)
            {
                if (sendTimer != null)
                {
                    sendTimer.Dispose();
                }
                return;
            }

            try
            {
                lock (this)
                {
                    if (socket.Connected)
                    {
                        socket.Send(_Buffer, 0, _Buffer.Length, SocketFlags.None);

                        if (Sent != null)
                        {
                            Sent(this, new EventArgs <Int32>(_Buffer.Length));
                        }
                    }
                }
            }
            catch
            {
                Disconnect();
            }
        }
Example #2
0
        void CheckCount()
        {
            if (Entity <TEntity> .Meta.LongCount != count)
            {
                NotifyDependencyChanged(this, EventArgs.Empty);

                if (timer != null)
                {
                    timer.Dispose();
                }
            }
        }
Example #3
0
        /// <summary>
        /// 连接到注册中心
        /// </summary>
        /// <returns></returns>
        internal void Connect()
        {
            String uri = m_ESBProxy.ConsumerConfig.Registry[0].Uri;

            if (m_ESBProxy.ConsumerConfig.ApplicationName.StartsWith("ESB_CallCenter"))
            {
                m_CometClient = new CometClient(uri, CometClientType.CallCenter);
            }
            else if (m_ESBProxy.ConsumerConfig.ApplicationName.StartsWith("ESB_Portal"))
            {
                m_CometClient = new CometClient(uri, CometClientType.Portal);
            }
            else if (m_ESBProxy.ConsumerConfig.ApplicationName.StartsWith("ESB_Monitor"))
            {
                m_CometClient = new CometClient(uri, CometClientType.Monitor);
            }
            else if (m_ESBProxy.ConsumerConfig.ApplicationName.StartsWith("ESB_QueueCenter"))
            {
                m_CometClient = new CometClient(uri, CometClientType.QueueCenter);
            }
            else
            {
                m_CometClient = new CometClient(uri, CometClientType.Consumer);
            }

            m_CometClient.OnReceiveNotify += m_CometClient_OnReceiveNotify;
            m_CometClient.Connect();

            //--连接成功后,释放定时器
            if (m_TimerX != null)
            {
                m_TimerX.Dispose();
                m_TimerX = null;
            }
        }
Example #4
0
        /// <summary>停止</summary>
        public void Stop()
        {
            if (cs != null)
            {
                XTrace.WriteLine("正在关闭连接……");
                for (int i = 0; i < cs.Length; i++)
                {
                    if (cs[i] != null)
                    {
                        try
                        {
                            cs[i].Dispose();
                        }
                        catch { }
                    }
                }
                cs = null;
            }

            if (timer != null)
            {
                timer.Dispose();
                timer = null;
            }
        }
Example #5
0
        /// <summary>入口方法</summary>
        public static void Main()
        {
            try
            {
                stress        = new TcpStress();
                stress.Config = TcpStressConfig.Current;
                stress.Config.Show();
                stress.Init();

                Console.WriteLine("准备就绪!任意键开始……");
                Console.ReadKey(true);

                sw = new Stopwatch();
                sw.Start();
                timerShowStatus = new TimerX(ShowStatus, null, 1000, 1000);

                stress.Start();
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }

            Console.WriteLine("压力测试中!任意键结束……");
            Console.ReadKey(true);

            stress.Stop();
            timerShowStatus.Dispose();
        }
Example #6
0
        private void btnStop_Click(object sender, EventArgs e)
        {
            m_TestManager.Stop();
            m_TimerX.Dispose();

            btnStart.Enabled = true;
            btnStop.Enabled  = false;
        }
 // Internal so it can be overridden in tests
 internal virtual void StopCleanupTimer()
 {
     lock (_cleanupTimerLock)
     {
         _cleanupTimer.Dispose();
         _cleanupTimer = null;
     }
 }
Example #8
0
 public void Shutdown()
 {
     lock (_lockObj)
     {
         _clusterDict.Clear();
     }
     _time?.Dispose();
 }
Example #9
0
 public override bool Stop()
 {
     if (_uploadDataTimer != null)
     {
         _uploadDataTimer.Dispose();
     }
     return(base.Stop());
 }
Example #10
0
        /// <summary>停止服务</summary>
        /// <param name="reason"></param>
        protected override void StopWork(String reason)
        {
            WriteLog("业务结束!{0}", reason);

            _timer.Dispose();
            _timer2.Dispose();

            base.StopWork(reason);
        }
Example #11
0
        /// <summary>销毁</summary>
        /// <param name="disposing"></param>
        protected override void OnDispose(Boolean disposing)
        {
            base.OnDispose(disposing);

            if (_Timer != null)
            {
                _Timer.Dispose();
            }

            Stop();
        }
Example #12
0
        protected override void OnDispose(Boolean disposing)
        {
            base.OnDispose(disposing);

            if (clearTimer != null)
            {
                clearTimer.Dispose();
            }

            CloseAll();
        }
Example #13
0
        void frm_FormClosing(Object sender, FormClosingEventArgs e)
        {
            SaveInfo();

            _timer.Dispose();
            _timer = null;

            if (Port != null)
            {
                Port.Close();
            }
        }
Example #14
0
        void ClearAboveMax(Object state)
        {
            var stack = Stock;

            // 把超标的全部清了
            while (stack.Count > Max)
            {
                //var obj = stack.Pop() as IDisposable;
                //if (obj != null) obj.Dispose();
                state.TryDispose();

                Interlocked.Increment(ref _FreeCount);
            }

            // 没到最大值,关闭定时器?
            if (stack.Count < Max - 100 && _clearTimers != null)
            {
                _clearTimers.Dispose();
                _clearTimers = null;
            }
        }
Example #15
0
        /// <summary>
        /// 连接到监控中心
        /// </summary>
        /// <returns></returns>
        public void Connect()
        {
            m_CometClient = new CometClient(m_MonitorCenterUri, m_CometClientType);
            m_CometClient.OnReceiveNotify += m_CometClient_OnReceiveNotify;
            m_CometClient.Connect();

            //--连接成功后,释放定时器
            if (m_TimerX != null)
            {
                m_TimerX.Dispose();
                m_TimerX = null;
            }
        }
        private void Timer_Tick(Object state)
        {
            Debug.Assert(_callback != null);
            Debug.Assert(_timer != null);

            lock (_lock)
            {
                if (_timer != null)
                {
                    _timer.Dispose();
                    _timer = null;

                    _callback(this);
                }
            }
        }
Example #17
0
        /// <summary>子类重载实现资源释放逻辑时必须首先调用基类方法</summary>
        /// <param name="disposing">从Dispose调用(释放所有资源)还是析构函数调用(释放非托管资源)。
        /// 因为该方法只会被调用一次,所以该参数的意义不太大。</param>
        protected override void OnDispose(bool disposing)
        {
            base.OnDispose(disposing);

            try
            {
                Clear("资源释放");
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
            }

            if (_Timer != null)
            {
                _Timer.Dispose();
            }
        }
Example #18
0
        private void Connect()
        {
            if (!m_IsConnected && m_ESBProxy.ESBConfig != null && m_ESBProxy.ESBConfig.Cache != null && m_ESBProxy.ESBConfig.Cache.Count > 0)
            {
                try
                {
                    String[] paramUri = m_ESBProxy.ESBConfig.Cache[0].Uri.Split(':');
                    m_RedisClient = new RedisClient(paramUri[0], Int32.Parse(paramUri[1]));

                    m_RedisClient.Set <String>(String.Format("MBSOA_Consumer:{0}:{1}", m_ESBProxy.ConsumerConfig.ApplicationName, m_ESBProxy.ClientIP)
                                               , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    m_IsConnected = true;

                    XTrace.WriteLine("成功连接到缓存中心。");

                    if (m_TimerX != null)   //--说明监控中心曾经不可用过
                    {
                        m_TimerX.Dispose();
                        m_TimerX       = null;
                        m_ReConnectNum = 0;
                    }
                }
                catch (Exception ex)
                {
                    m_IsConnected = false;

                    if (m_TimerX == null)
                    {
                        lock (m_TimerXLock)
                        {
                            m_TimerX = new TimerX(x => Connect(), null, 20000, 20000);
                            XTrace.WriteLine("无法连接到缓存中心:" + ex.Message);
                        }
                    }
                    else
                    {
                        XTrace.WriteLine("第{0}次重连缓存中心失败, 抛出异常:{1}", m_ReConnectNum, ex.Message);
                        m_ReConnectNum++;
                    }
                }
            }
        }
Example #19
0
        protected override void OnDispose(bool disposing)
        {
            base.OnDispose(disposing);

            if (_Serial != null && _Serial.IsOpen)
            {
                var sp = _Serial;
                _Serial = null;
                try
                {
                    sp.Close();
                    sp.Dispose();
                }
                catch { }
            }

            if (tx != null)
            {
                tx.Dispose();
            }
        }
Example #20
0
        void Disconnect()
        {
            if (_Client != null)
            {
                _Client.Dispose();
                _Client = null;
            }
            if (_Server != null)
            {
                _Server.Dispose();
                _Server = null;
            }
            if (_timer != null)
            {
                _timer.Dispose();
                _timer = null;
            }

            pnlSetting.Enabled = true;
            btnConnect.Text    = "打开";
        }
Example #21
0
        /// <summary>停止</summary>
        protected override void OnStop()
        {
            if (_netNetServer == null)
            {
                return;
            }
            try
            {
                _netNetServer.Stop();
                _processPackageTimerX?.Dispose();

                StopWork();
                WriteLine("{0}停止", DisplayName);
            }
            catch (Exception ex)
            {
                WriteLine("服务停止失败,{0}", ex.Message);
                WriteLine("XXXXXXXXXXXXXXXXXXXXXXX");
                WriteLine(ex.ToString());
                WriteLine("XXXXXXXXXXXXXXXXXXXXXXX");
            }
        }
Example #22
0
        /// <summary>
        /// 连接到监控中心
        /// </summary>
        private void Connect()
        {
            if (m_ESBProxy.ESBConfig != null && m_ESBProxy.ESBConfig.MessageQueue != null && m_ESBProxy.ESBConfig.MessageQueue.Count > 0)
            {
                try
                {
                    String[] paramMQ = m_ESBProxy.ESBConfig.MessageQueue[0].Uri.Split(':');
                    m_RabbitMQ = new RabbitMQClient(paramMQ[0], paramMQ[2], paramMQ[3], Int32.Parse(paramMQ[1]));

                    if (m_TimerX != null)   //--说明监控中心曾经不可用过
                    {
                        m_TimerX.Dispose();
                        m_TimerX       = null;
                        m_ReConnectNum = 0;
                    }

                    //--当监控中心可用时需要检测本地消息队列,如果有消息则需要发送
                    m_LocalMQ.CheckAndResendMessage();

                    m_RabbitMQAvailable = true;
                    XTrace.WriteLine("成功连接到消息队列。");
                }
                catch (Exception ex)
                {
                    if (m_TimerX == null)
                    {
                        m_TimerX = new TimerX(x => Connect(), null, 5000, 5000);
                        XTrace.WriteLine("无法连接到消息队列:" + ex.ToString());
                    }
                    else
                    {
                        XTrace.WriteLine("第{0}次重连消息队列失败, 抛出异常:{1}", m_ReConnectNum, ex.ToString());
                        m_ReConnectNum++;
                    }
                }
            }
        }
Example #23
0
        public static void StartService()
        {
            try
            {
                // XTrace.UseConsole(true,false);

                #region init
                dynamic type             = (new Program()).GetType();
                string  currentDirectory = Path.GetDirectoryName(type.Assembly.Location);
                var     services         = new ServiceCollection();
                var     builder          = new ConfigurationBuilder()
                                           .SetBasePath(currentDirectory)
                                           .AddJsonFile("appsettings.json", true, reloadOnChange: true);

                var configuration = builder.Build();
                services.AddSingleton <IConfiguration>(configuration);
                Logger.Create(configuration, new LoggerFactory(), "Dtsc.NodeService");
                var redisConn = configuration.GetSection("AppSettings").GetSection("Dtsc").GetValue <string>("RedisConn", "127.0.0.1");
                var redisPort = configuration.GetSection("AppSettings").GetSection("Dtsc").GetValue <int>("RedisPort", 6379);
                var redisPwd  = configuration.GetSection("AppSettings").GetSection("Dtsc").GetValue <string>("RedisPwd", "****");
                var nodeId    = configuration.GetSection("AppSettings").GetValue <int>("NodeId", 0);
                JobHelper.JobPath = configuration.GetSection("AppSettings").GetValue <string>("JobPath", "\\Yunt.Jobs");
                XTrace.Log.Level  = (LogLevel)configuration.GetSection("AppSettings").GetValue <int>("LogLevel", 3);
                var performanceInverval = configuration.GetSection("AppSettings").GetValue <int>("PerformanceInverval", 60);
                if (redisConn.IsNullOrWhiteSpace() || redisPwd.IsNullOrWhiteSpace() || nodeId == 0)
                {
                    //todo 可写入初始配置
                    Logger.Error($"[Device]:appsettings not entirely!");
                    Logger.Error($"please write Device service's settings into appsettings! \n exp:\"Device\":{{\"RedisConn\":\"***\"," +
                                 $"\"MySqlConn\":\"***\"}}");
                    new TbError()
                    {
                        Createtime = DateTime.Now.TimeSpan(),
                        JobID      = 0,
                        Msg        = $"Error:appsettings not entirely"
                    }.SaveAsync();
                }

                services.AddDefaultRedisCache(option =>
                {
                    option.RedisServer.Add(new HostItem()
                    {
                        Host = redisConn + ":" + redisPort
                    });
                    option.SingleMode = true;
                    option.Password   = redisPwd;
                });

                _redisProvider = ServiceProviderServiceExtensions.GetService <IRedisCachingProvider>(services.BuildServiceProvider());

                //#if DEBUG
                XTrace.UseConsole();
                //#endif

                #endregion

                var ip   = HostHelper.GetExtenalIpAddress();
                var node = TbNode.Find("ID", nodeId);
                if (node != null)
                {
                    if (!ip.IsNullOrWhiteSpace())
                    {
                        node.Ip = ip;
                    }
                    node.SaveAsync();
                }
                //恢复所有运行job
                ResumeJob(nodeId);

                using (var p = Process.GetCurrentProcess())
                {
                    //断线重连
                    while (true)
                    {
                        if (_isStop)
                        {
                            return;
                        }
                        try
                        {
                            if (_timer == null)
                            {
                                _timer = new TimerX(obj => { ListenCmd(nodeId); }, null, 500, 100);
                            }
                            if (_performanceTimer == null)
                            {
                                _performanceTimer = new TimerX(obj => { Performance(p, performanceInverval, nodeId); }, null, 1000, performanceInverval * 1000);
                            }
                        }
                        catch (Exception e)
                        {
                            _timer?.Dispose();
                            _performanceTimer?.Dispose();
                            new TbError()
                            {
                                Createtime = DateTime.Now.TimeSpan(),
                                JobID      = 0,
                                Msg        = $"Error:{e.Message},错误详情,请查看节点服务日志!"
                            }.SaveAsync();
                            Logger.Exception(e);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                new TbError()
                {
                    Createtime = DateTime.Now.TimeSpan(),
                    JobID      = 0,
                    Msg        = $"Error:{e.Message},错误详情,请查看节点服务日志!"
                }.SaveAsync();
                Logger.Exception(e);
            }
        }