⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 represent.cs

📁 网通网关Cngp
💻 CS
字号:
using System;
using System.Collections;
using System.Threading;
using System.Timers;
using Pierce.AttributeClass;
using WriteLog;
using Pierce.Data;
namespace Cngp
{
	/// <summary>
	/// Represent:表示其它底层类的调用
	/// </summary>
	[LastModified("2006-01-12","修改Timer的事件,更改调度静态栏目时间")]
	internal class Represent
	{
		private CngpSendMessage cngpSendMessage=new CngpSendMessage();
		/// <summary>
		/// 存储连接数的ArrayList
		/// </summary>
		public static ArrayList ProtocolArray;
		private static ArrayList ThreadArray;
		private System.Timers.Timer timerTask;
		private GetTaskTime getTaskTime;
		private CallProcedure callProcedure;
		private Thread threadGetData;
		public static Thread threadCheckSubmitArray;
		private DateTime timeToGetService;
		private DateTime timeToCallProcedure;
		public static ArrayList ArrayTaskTime;
		/// <summary>
		/// 构造函数
		/// </summary>
		public Represent()
		{
			//
			//存储静态栏目列表的ArrayList
			//
			ArrayTaskTime=new ArrayList(3);
			//
			//读取静态栏目信息的标识时间
			//
			timeToGetService=DateTime.Now;
			//
			//调用静态过程的标识时间
			//
			timeToCallProcedure=DateTime.Now;
			
			//
			//存储连接数的ArrayList
			//
			ProtocolArray=new ArrayList(3);
			//
			//存储Thread的ArrayList
			//
			ThreadArray=new ArrayList(3);
			//
			//timerTask的配置,Interval=1000,在构造函数中不启动
			//
			timerTask=new System.Timers.Timer();
			timerTask.Interval=1000;
			timerTask.Elapsed+=new ElapsedEventHandler(timerTask_Elapsed);
			timerTask.Enabled=false;
		}
		/// <summary>
		/// 控制Timer
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		[LastModified("2006-01-12","修改检查静态栏目发送时间")]
		private void timerTask_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
		{
			try
			{

				//
				//时间间隔:现在到上次获取发送时间的间隔
				//
				TimeSpan timeSpan=DateTime.Now-timeToGetService;
				if(timeSpan.Minutes>20)
				{
					ArrayTaskTime.Clear();										//清除Array的信息
					getTaskTime.GetServiceTaskTime(ref ArrayTaskTime);			//获取栏目和时间
					timeToGetService=DateTime.Now;							//更新上次获取时间
				}
				//
				//检查调度时间Array中的发送时间,1分钟执行1次,如果到了发送时间,那么调用发送过程
				//
				TimeSpan spanCall=DateTime.Now-timeToCallProcedure;
#if DEBUG
				Logs.writeLog("spanCall:"+spanCall.Seconds.ToString(),"callprocedure");
#endif
				if(ArrayTaskTime.Count>0 && spanCall.Seconds>=59)
				{
					timeToCallProcedure=DateTime.Now;
					foreach(TaskTime task in ArrayTaskTime)
					{
#if DEBUG
						Logs.writeLog(task.Servicetype+":"+task.Begintime+
						":"+DateTime.Now.ToString("HHmm"),"callprocedure");
#endif
						if(task.Begintime==DateTime.Now.ToString("HHmm"))
						{
							callProcedure.Callprocedure("procedure_CallProcedure",task.Servicetype);
							Logs.writeLog("call "+task.Servicetype+" success","callprocedure");
						}
					}
				}
				//
				//链路维持
				//									
				if(ProtocolArray.Count>0)
				{
					CheckLine();										
				}				
				//
				//检查连接数
				//
				CheckConnection();
			}
			catch(Exception ex)
			{
				Logs.writeLog(String.Concat(
					ex.Message.ToString(),
					ex.StackTrace.ToString(),
					ex.TargetSite.ToString(),
					ex.Source.ToString()
					),false);
			}
			
		}
		/// <summary>
		/// 链路维持
		/// </summary>
		private void CheckLine()
		{
			//维持每个连接的Active,有异常,断开,重新连接
			foreach(CngpProtocol protocol in ProtocolArray)
			{
				if(protocol.LastTime.AddSeconds(30)<DateTime.Now)
				{
					try
					{						
						protocol.Cngp_Active_Test();						
						
					}
					catch
					{
						protocol.EndConnect();
					}
				}
				if(protocol.IsConnectedWithIsmg==false)
				{
					if(protocol.Cngp_Login(
						ConfigInformation.GateUserName,
						ConfigInformation.GateUserPassword,
						ConfigInformation.GateIp,
						ConfigInformation.GatePort))
					{
						Logs.writeLog("Reconnect Ok");
					}
				}
			}
			
		}
		/// <summary>
		/// 检查连接数,是否同配置文件中设定的相同
		/// </summary>
		private void CheckConnection()
		{
			if(ProtocolArray.Count<ConfigInformation.InstanceCount)
			{
				for(int i=0;i<ConfigInformation.InstanceCount-ProtocolArray.Count;i++)
				{
					CngpProtocol protocol=new CngpProtocol();
					if(protocol.Cngp_Login(
						ConfigInformation.GateUserName,
						ConfigInformation.GateUserPassword,
						ConfigInformation.GateIp,
						ConfigInformation.GatePort
						))
					{
					
						Logs.writeLog(String.Concat("Socket[",i.ToString(),"] Start"));
						//Add to Array
						lock(ProtocolArray)
						{
							ProtocolArray.Add(protocol);
						}
					}
				}
			}
		}

		/// <summary>
		/// 开始连接网关,发送数据
		/// </summary>
		public void StartProgram()
		{
			//
			//建立配置文件中定义的Socket连接
			//
			for(int i=0;i<ConfigInformation.InstanceCount;i++)
			{
				CngpProtocol protocol=new CngpProtocol();
				
				//连接
				if(protocol.Cngp_Login(
					ConfigInformation.GateUserName,
					ConfigInformation.GateUserPassword,
					ConfigInformation.GateIp,
					ConfigInformation.GatePort
					))
				{
					
					Logs.writeLog(String.Concat("Socket[",i.ToString(),"] Start"));
					//Add to Array
					ProtocolArray.Add(protocol);
				}

			}
			//
			//同网关的Socket连接建立后,启动获取静态栏目timer
			//启动获取数据线程,
			//启动检查数据重发进程
			//
			if(ProtocolArray.Count>0) 
			{
				
				CngpProtocol protocol=new CngpProtocol();
				
				//如果允许连接数据库,那么读取数据
				if(ConfigInformation.ConnectDb=="1")
				{
					//
					//读取数据,把读取数据线程放入Array中
					//				
					threadGetData=new Thread(new ThreadStart(protocol.GetDataFromOracle));
					threadGetData.Start();
					ThreadArray.Add(threadGetData);
					//
					//启动调度静态栏目的Timer,并初始化相关类
					//
					timerTask.Enabled=true;
					//实例化获取栏目发送时间和栏目名称类
					getTaskTime=new GetTaskTime(cngpSendMessage.Con);
					//实例化调用存储过程类
					callProcedure=new CallProcedure(cngpSendMessage.Con);
					//获取栏目和时间
					getTaskTime.GetServiceTaskTime(ref ArrayTaskTime);	
				}
				//
				//重发线程
				//
				threadCheckSubmitArray=new Thread(new ThreadStart(protocol.CheckSubmit));
				threadCheckSubmitArray.Start();
				//重发线程加入到Thread Array中				
				ThreadArray.Add(threadCheckSubmitArray);
				
			}
			
			
		
			
		}
		/// <summary>
		/// 停止程序
		/// </summary>
		public void EndRepresent()
		{
			//
			//设置退出标志
			//
			CngpProtocol.quitFlag=true;
			//
			//停止线程Array中的所有线程
			//
			if(ThreadArray.Count>0)
			{
				foreach(Thread thread in ThreadArray)
				{
					if(thread.ThreadState!=System.Threading.ThreadState.Aborted)
					{
						//唤醒Sleep的线程
						if(thread.ThreadState==System.Threading.ThreadState.WaitSleepJoin)
						{
							thread.Interrupt();
						}
						thread.Abort();
						thread.Join(1000);
					}
				}
			}
			
			//
			//停止链路检查timer和调度静态栏目timer
			//			
			timerTask.Stop();
			//
			//停止协议中的连接层
			//
			if(ProtocolArray.Count!=0)
			{
				foreach(CngpProtocol protocol in ProtocolArray)
				{
					protocol.Cngp_Terminate();
					Thread.Sleep(1000);
					protocol.EndConnect();
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -