| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using System;
- using System.IO;
- using System.Xml;
- using System.Data;
- using System.Text;
- using System.Collections;
- using System.Reflection;
- using System.Configuration;
- using Core.Mes.Common;
- using Core.Mes.IBaseInterface;
- namespace Core.Mes.ServerFrameWork
- {
- [Serializable]
- public class Dispatcher : MarshalByRefObject, ICommon
- {
- private ArrayList _objects = null;
- private ServerConfigure _servConf = null;
- public ServerConfigure ServConfig
- {
- get { return _servConf; }
- }
- #region " Construct "
- public Dispatcher() { }
- public Dispatcher(IServerPool pool, ServerConfigure _conf, ArrayList _objs)
- {
- Pool = pool;
- _servConf = _conf;
- _objects = _objs;
- }
- private IServerPool _pool = null;
- public IServerPool Pool
- {
- get { return _pool; }
- set
- {
- _pool = value;
- if (_pool.OwnObjects == null && _objects != null) _pool.OwnObjects = _objects;
- }
- }
- #endregion
- #region " Variable "
- //== 子服务集合
- public static Hashtable _htServers = new Hashtable();
- //== 数据链接集合
- public static Hashtable htDBManager = new Hashtable();
- public event GetStatusInfoHandler getStatusInfo;
- public delegate void GetStatusInfoHandler(string info);
- protected virtual void SetStatusMessage(string info)
- {
- if (getStatusInfo != null)
- {
- getStatusInfo(info);
- }
- }
- #endregion
- #region 内存释放
- public void MemoryDispose()
- {
- long memorysize = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64;
- int PagedMemorySize = (int)memorysize / (1024 * 1024);
- if (PagedMemorySize > _servConf.MemoryMaxSize)//进程占用内存>100M 做GC.
- {
- try
- {
- GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
- }
- catch
- {
- }
- System.Diagnostics.Process.GetCurrentProcess().Dispose();
- }
- }
- #endregion
- #region MethodTimeLog
- private static object lockObj = new object();
- public void MethodTimeLog(TimeSpan tspan, object[] objs, object[] args)
- {
- string path = string.Format(@"./log/Method/MethodTimeLog_{0}.txt", System.DateTime.Now.ToString("yyyy_MM_dd"));
- lock (lockObj)
- {
- using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
- {
- StringBuilder sbtxt = new StringBuilder();
- sbtxt.AppendLine("==============================================");
- sbtxt.AppendLine(string.Format("LogWriteTime:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
- sbtxt.AppendLine(string.Format("Time:{0}", tspan.TotalMilliseconds));
- sbtxt.AppendLine(string.Format("ClassName:{0}", objs[0].ToString()));
- sbtxt.AppendLine(string.Format("MethodName:{0}", objs[1].ToString()));
- if (args != null)
- {
- foreach (object obj in args)
- {
- if (obj != null)
- {
- sbtxt.AppendLine(string.Format("Parameters:{0}", obj.ToString()));
- }
- }
- }
- sbtxt.AppendLine("==============================================");
- sw.WriteLine(sbtxt.ToString());
- }
- }
- }
- public void MethodErrLog(Exception ex, object[] objs, object[] args)
- {
- string path = string.Format(@"./log/Method/MethodErrLog_{0}.txt", System.DateTime.Now.ToString("yyyy_MM_dd"));
- lock (lockObj)
- {
- using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
- {
- StringBuilder sbtxt = new StringBuilder();
- sbtxt.AppendLine("==============================================");
- sbtxt.AppendLine(string.Format("LogWriteTime:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
- sbtxt.AppendLine(string.Format("ClassName:{0}", objs[0].ToString()));
- sbtxt.AppendLine(string.Format("MethodName:{0}", objs[1].ToString()));
- if (args != null)
- {
- foreach (object obj in args)
- {
- if (obj != null)
- {
- sbtxt.AppendLine(string.Format("Parameters:{0}", obj.ToString()));
- }
- }
- }
- sbtxt.AppendLine(ex.Message);
- sbtxt.AppendLine(ex.StackTrace);
- sbtxt.AppendLine("==============================================");
- sw.WriteLine(sbtxt.ToString());
- }
- }
- }
- public void DebugLog(object[] objs, object[] args)
- {
- string path = string.Format(@"./log/Method/DebugLog_{0}.txt", System.DateTime.Now.ToString("yyyy_MM_dd"));
- lock (lockObj)
- {
- if (File.Exists(Path.GetFullPath(path)))
- {
- FileInfo fileInfo = new FileInfo(Path.GetFullPath(path));
- if (fileInfo.Length / (1024 * 1024) > 6)
- {
- File.Delete(path);
- }
- }
- using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
- {
- StringBuilder sbtxt = new StringBuilder();
- sbtxt.AppendLine("==============================================");
- sbtxt.AppendLine(string.Format("LogWriteTime:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
- sbtxt.AppendLine(string.Format("ClassName:{0}", objs[0].ToString()));
- sbtxt.AppendLine(string.Format("MethodName:{0}", objs[1].ToString()));
- if (args != null)
- {
- foreach (object obj in args)
- {
- if (obj != null)
- {
- sbtxt.AppendLine(string.Format("Parameters:{0}", obj.ToString()));
- }
- }
- }
- sbtxt.AppendLine("==============================================");
- sw.WriteLine(sbtxt.ToString());
- }
- }
- }
- #endregion
- #region " 调用服务 "
- /// <summary>
- /// 服务端公开给客户端的调用后台服务的方法
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- public ReturnObject MethodHandler(CallingMessage message, ValidateInfo validateInfo)
- {
- CallingMessageEx messageEx = new CallingMessageEx();
- messageEx.FromCallingMessage(message);
- messageEx.RedirectLimit = 0;
- messageEx.InnerServerRedirect = true;
- ReturnObjectEx rtnObjEx = RelayMessage(messageEx, validateInfo);
- ReturnObject rtnObj = new ReturnObject();
- rtnObj.ErrCode = rtnObjEx.ErrCode;
- rtnObj.ErrMessage = rtnObjEx.ErrMessage;
- rtnObj.RealDataSet = rtnObjEx.RealDataSet;
- rtnObj.RealObject = rtnObjEx.RealObject;
- return rtnObj;
- }
- public ReturnObjectEx MethodHandlerEx(CallingMessageEx message, ValidateInfo validateInfo)
- {
- return RelayMessage(message, validateInfo);
- }
- private ReturnObjectEx RelayMessage(CallingMessageEx messageEx, ValidateInfo validateInfo)
- {
- string serverName = messageEx.ServerName;
- string assemblyName = messageEx.AssemblyName;
- string className = messageEx.ClassName;
- string methodName = messageEx.MethodName;
- object[] args = messageEx.args;
- ReturnObjectEx rtnObjEx = new ReturnObjectEx();
- try
- {
- if (serverName.ToUpper() != "SERVERCOMMON" && messageEx.RedirectLimit > 0)
- {
- string RedirURL = "";
- double sum_factor = 0;
- ArrayList serviceProvider = new ArrayList();
- ArrayList pfactors = new ArrayList();
- serviceProvider.Add("THIS");
- sum_factor += this.ServConfig.PriorityFactor;
- pfactors.Add(sum_factor);
- foreach (string server in Pool.HtExtServer.Keys)
- {
- RemotingServer rs = (RemotingServer)Pool.HtExtServer[server];
- if (rs.Valid != RemoteServerStatus.Normal || rs.Enable == false) continue;
- rs.FindRemoteServer(serverName);
- if (!rs.HTServices.ContainsKey(serverName)) continue;
- ServiceObject so = ((ServiceObject)(rs.HTServices[serverName]));
- if (so.Valid != ServiceObjectStatus.Normal || so.Enable == false) continue;
- RedirURL = so.URL;
- sum_factor += rs.PriorityFactor;
- serviceProvider.Add(server);
- pfactors.Add(sum_factor);
- }
- Random rand = new Random();
- double rand_selector = rand.NextDouble() * sum_factor;
- int selector = 0;
- for (int idx = 0 ; idx < pfactors.Count ; idx++)
- {
- if (rand_selector <= (double)pfactors[idx])
- {
- selector = idx;
- break;
- }
- }
- if (selector != 0)
- {
- SimpleReturnObject _out = new SimpleReturnObject();
- if (messageEx.InnerServerRedirect)
- {
- //通过服务器转发调用。(由本服务器 调用其他服务器方法,并转发结果到客户机)
- RemotingServer rs = (RemotingServer)Pool.HtExtServer[serviceProvider[selector]];
- rtnObjEx = (ReturnObjectEx)rs.ExecuteMethod(messageEx, out _out);
- return rtnObjEx;
- }
- else
- {
- //不转发调用。(返回其他服务器地址,由客户机自行调用)
- rtnObjEx.ReDirectURL = RedirURL;
- return rtnObjEx;
- }
- }
- }
- //调用本地服务
- return ExecuteLocalService(messageEx, validateInfo);
- }
- catch (Exception ex)
- {
- MethodErrLog(ex, new object[] { className, methodName }, args);
- return new ReturnObjectEx(null, "服务调用发生异常! \n" + ex.Message);
- }
- }
- private ReturnObjectEx ExecuteLocalService(CallingMessageEx messageEx, ValidateInfo validateInfo)
- {
- //step1:
- MemoryDispose();
- //step2:
- string serverName = messageEx.ServerName;
- string assemblyName = messageEx.AssemblyName;
- string className = messageEx.ClassName;
- string methodName = messageEx.MethodName;
- object[] args = messageEx.args;
- ReturnObjectEx rtnObjEx = new ReturnObjectEx();
- MethodInfo myMethod = null;
- try
- {
- if (Pool.HtComponent.Contains(className))
- {
- try
- {
- //使用本地服务
- myMethod = Pool.GetType().GetMethod("HandleMethodEx");
- DateTime startTime = DateTime.Now;
- if (_servConf.Debug == "true")
- {
- DebugLog(new object[] { className, methodName }, args);
- }
- ReturnObject rtnObj = (ReturnObject)myMethod.Invoke(Pool, new object[] { className, methodName, args, _objects });
- rtnObjEx.ReDirectURL = "";
- rtnObjEx.ErrCode = rtnObj.ErrCode;
- rtnObjEx.ErrMessage = rtnObj.ErrMessage;
- rtnObjEx.RealDataSet = rtnObj.RealDataSet;
- rtnObjEx.RealObject = rtnObj.RealObject;
- DateTime endTime = DateTime.Now;
- TimeSpan tspan = endTime - startTime;
- if (tspan.TotalMilliseconds > (_servConf.MethodTime * 1000))
- {
- MethodTimeLog(tspan, new object[] { className, methodName }, args);
- }
- if (rtnObjEx.RealObject != null)
- {
- #region 返回值为表格
- if (rtnObjEx.RealObject.GetType() == typeof(DataSet))
- {
- DataSet _ds = (DataSet)rtnObjEx.RealObject;
- bool largeData = false;
- int row_cnt = 0;
- foreach (DataTable _dt in _ds.Tables)
- {
- row_cnt += _dt.Rows.Count;
- if (_dt.Rows.Count > _servConf.CompressThreshold || row_cnt > _servConf.CompressThreshold)
- {
- largeData = true;
- break;
- }
- }
- if (largeData)
- {
- byte[] _rtn = Utility.SerializeDataSet(_ds);
- _ds.Dispose();
- _ds = null;
- rtnObjEx.RealObject = null;
- byte[] _rtn_comp = Utility.CompressBytes(_rtn);
- DataTable dt_rus = new DataTable("RETURN_RESULT");
- dt_rus.Columns.AddRange(new DataColumn[]{
- new DataColumn("IsCompressed", System.Type.GetType("System.Int32") ),
- new DataColumn("UnCompress_size", System.Type.GetType("System.Int32")),
- new DataColumn("Compress_size", System.Type.GetType("System.Int32"))}
- );
- dt_rus.AcceptChanges();
- dt_rus.Rows.Add(new object[] { 1, _rtn.Length, _rtn_comp.Length });
- dt_rus.AcceptChanges();
- if (rtnObjEx.RealDataSet == null)
- {
- rtnObjEx.RealDataSet = new DataSet();
- }
- rtnObjEx.RealDataSet.Tables.Add(dt_rus);
- rtnObjEx.RealObject = _rtn_comp;
- }
- }
- else if (rtnObjEx.RealObject.GetType() == typeof(ArrayList))
- {
- }
- #endregion
- }
- return rtnObjEx;
- }
- catch (Exception ex)
- {
- MethodErrLog(ex, new object[] { className, methodName }, args);
- return new ReturnObjectEx(null, ex.Message);
- }
- finally
- {
- try
- {
- if (rtnObjEx.RealObject != null)
- {
- rtnObjEx.RealObject = null;
- }
- if (rtnObjEx.RealDataSet != null)
- {
- rtnObjEx.RealDataSet = null;
- }
- }
- catch { }
- }
- }
- else
- {
- return new ReturnObjectEx(null, "未找到请求的服务![2]");
- }
- }
- catch (Exception ex)
- {
- return new ReturnObjectEx(null, "服务调用发生异常! \n" + ex.Message);
- }
- }
- #endregion
- #region " Common Handler "
- //=======================================================
- // 用来确保当创建 Singleton 时, 第一个实例永远不会过期
- //=======================================================
- public override object InitializeLifetimeService()
- {
- return null;
- }
- #endregion
- }
- }
|