www.pudn.com > startprepare.rar > CommonSession.cs
using System;
using System.Collections;
namespace DS.EMIS.StartPrepare.Common
{
///
/// CommonSession 的摘要说明。
///
public class CommonSession
{
#region delegate
public delegate Object MsgEventHandler(string sourceName,string infoTypeName,object infoTypeObj);
#endregion
#region private variable
private Hashtable m_ListMsgObject = null;
// private string username;
// private string password;
// private string serviceName;
private string _license;
private ManageConfigs manageConfigs = null;
private ManageSQLScripts manageSQLScripts = null;
private ManageSysLogs manageSysLogs = null;
#endregion
#region co
public CommonSession(string license)
{
//
// TODO: 在此处添加构造函数逻辑
//
this._license = license;
if(this._license=="QAZXSW")
{
this.m_ListMsgObject = new Hashtable();
this.manageConfigs = new ManageConfigs();
this.manageSQLScripts = new ManageSQLScripts();
this.manageSysLogs = new ManageSysLogs();
}
}
#endregion
#region Property
// public string UserName
// {
// get
// {
// return this.username;
// }
// set
// {
// this.username = value;
// }
// }
// public string Password
// {
// get
// {
// return this.password;
// }
// set
// {
// this.password = value;
// }
// }
// public string ServiceName
// {
// get{
// return this.serviceName;
// }
// set{this.serviceName = value;}
// }
#endregion
#region public Mothed
#region about Message
///
/// 注册你的消息接收方法
///
/// 您的接收消息模块名称
/// 消息接收方法
public void OnMsgEventHndler(string myName,MsgEventHandler msgEventHandler)
{
if(this._license!="QAZXSW")
return ;
MsgEventHandler pMsgEventHandler = null;
pMsgEventHandler = this.FindMsgEventHndler(myName);
if(pMsgEventHandler != null)
{
pMsgEventHandler +=msgEventHandler;
this.m_ListMsgObject.Remove(myName);
m_ListMsgObject.Add(myName,pMsgEventHandler);
}
else
{
pMsgEventHandler +=msgEventHandler;
m_ListMsgObject.Add(myName,pMsgEventHandler);
}
}
///
/// 不再接收消息,撤消注册
///
/// 您的接收消息模块名称
/// 消息接收方法
public void DeleteMsgEvent(string myName,MsgEventHandler msgEventHandler)
{
if(this._license!="QAZXSW")
return ;
MsgEventHandler pMsgEventHandler = null;
pMsgEventHandler = this.FindMsgEventHndler(myName);
if(pMsgEventHandler != null)
{
pMsgEventHandler -=msgEventHandler;
this.m_ListMsgObject.Remove(myName);
if(pMsgEventHandler != null)
m_ListMsgObject.Add(myName,pMsgEventHandler);
}
}
///
/// 同步方式一对一发送消息方法
///
/// 本地模块名称
/// 目标模块名称
/// 消息类型
/// 消息内容
///
public Object SendMessage(string sourceName,string destName,string infoTypeName,object infoTypeObj)
{
if(this._license!="QAZXSW")
return null;
Object Obj = null;
MsgEventHandler pMsgEventHandler = null;
pMsgEventHandler = this.FindMsgEventHndler(destName);
if(pMsgEventHandler != null)
{
MsgEventHandler myDelegate = null;
System.Delegate[] Delegates = pMsgEventHandler.GetInvocationList();
foreach (System.Delegate delegateItem in Delegates)
{
try
{
myDelegate = (MsgEventHandler) delegateItem;
Obj = myDelegate(sourceName,infoTypeName,infoTypeObj);
}
catch
{
pMsgEventHandler -= myDelegate;
this.m_ListMsgObject.Remove(destName);
if(pMsgEventHandler != null)
m_ListMsgObject.Add(destName,pMsgEventHandler);
}
}
}
return Obj;
}
///
/// 同步方式一对多发送消息方法
///
/// 本地模块名称
/// 目标模块名称列表
/// 消息类型
/// 消息内容
///
public Object SendMessage(string sourceName,string[] destNames,string infoTypeName,object infoTypeObj)
{
if(this._license!="QAZXSW")
return null;
Object Obj = null;
MsgEventHandler pMsgEventHandler = null;
for(int i=0;i
/// 同步广播发送信息方法
///
/// 本地模块名称
/// 消息类型
/// 消息内容
///
public Object SendMessage(string sourceName,string infoTypeName,object infoTypeObj)
{
if(this._license!="QAZXSW")
return null;
Object Obj = null;
MsgEventHandler pMsgEventHandler = null;
string eventID = null;
System.Collections.IEnumerator keyEnumerator = this.m_ListMsgObject.Keys.GetEnumerator();
try
{
if(keyEnumerator != null)
{
while ( keyEnumerator.MoveNext() )
{
eventID=(string)keyEnumerator.Current;
pMsgEventHandler = this.m_ListMsgObject[eventID] as MsgEventHandler;
if(pMsgEventHandler != null)
{
MsgEventHandler myDelegate = null;
System.Delegate[] Delegates = pMsgEventHandler.GetInvocationList();
foreach (System.Delegate delegateItem in Delegates)
{
try
{
myDelegate = (MsgEventHandler) delegateItem;
Obj = myDelegate(sourceName,infoTypeName,infoTypeObj);
}
catch
{
pMsgEventHandler -= myDelegate;
this.m_ListMsgObject.Remove(eventID);
if(pMsgEventHandler != null)
m_ListMsgObject.Add(eventID,pMsgEventHandler);
}
}
}
}
}
}
catch
{
}
return Obj;
}
///
/// 异步方式一对一发送消息方法
///
/// 本地模块名称
/// 目标模块名称
/// 消息类型
/// 消息内容
public void AsySendMessage(string sourceName,string destName,string infoTypeName,object infoTypeObj)
{
SendAsgMessageOneDetegate sendMsg = new SendAsgMessageOneDetegate(this.SendMessage);
sendMsg.BeginInvoke(sourceName,destName,infoTypeName,infoTypeObj,new System.AsyncCallback(SendEventCallbackOne),null);
}
///
/// 异步方式一对多发送消息方法
///
/// 本地模块名称
/// 目标模块名称列表
/// 消息类型
/// 消息内容
public void AsySendMessage(string sourceName,string[] destNames,string infoTypeName,object infoTypeObj)
{
SendAsgMessagePartDetegate sendMsg = new SendAsgMessagePartDetegate(this.SendMessage);
sendMsg.BeginInvoke(sourceName,destNames,infoTypeName,infoTypeObj,new System.AsyncCallback(SendEventCallbackPart),null);
}
///
/// 异步广播发送信息方法
///
/// 本地模块名称
/// 消息类型
/// 消息内容
public void AsySendMessage(string sourceName,string infoTypeName,object infoTypeObj)
{
SendAsgMessageAllDetegate sendMsg = new SendAsgMessageAllDetegate(this.SendMessage);
sendMsg.BeginInvoke(sourceName,infoTypeName,infoTypeObj,new System.AsyncCallback(SendEventCallbackAll),null);
}
#endregion
#region about congfig file reading/writing
/*
*
*
*
*
*
*
*
*/
///
/// 读字典配置信息
///
/// 键名
/// 配置文件名
/// 键值
public string GetConfig(string KeyValue,string FileName)
{
if(this._license!="QAZXSW")
return null;
return this.manageConfigs.GetConfig(KeyValue,FileName);
}
///
/// 修改字典配置信息
///
/// 键名
/// 键值
/// 配置文件名
public void UpdateConfig(string KeyValue,string Xvalue,string FileName)
{
if(this._license!="QAZXSW")
return ;
this.manageConfigs.UpdateConfig(KeyValue,Xvalue ,FileName);
}
/*
*
*
* <接点名称 属性1="值" 属性2="值" 属性3="值" ...../>
*例:
*
*/
///
/// 读同名接点配置信息
///
/// 配置文件名
/// 列表接点名
/// 配置信息列表
public System.Data.DataTable GetConfigs(string fileName,string nodeName)
{
if(this._license!="QAZXSW")
return null;
return this.manageConfigs.GetConfigs(fileName,nodeName);
}
///
/// 修改节点属性值
///
/// 配置文件名
/// 节点名
/// 节点属性名
/// 属性值
/// 节点编号,从0开始
public void UpdateConfig(string fileName,string nodeName,string attriName,string attriValue,int pos)
{
if(this._license!="QAZXSW")
return ;
this.manageConfigs.UpdateConfig(fileName,nodeName,attriName,attriValue,pos);
}
///
/// 修改节点属性值
///
/// 配置文件名
/// 节点名
/// 节点属性名
/// 属性值
/// 匹配节点属性名
/// 匹配节点属性值
///
///
public void UpdateConfig(string fileName,string nodeName,string attriName,string attriValue,string matchAttriName,string matchAttriValue)
{
if(this._license!="QAZXSW")
return ;
this.manageConfigs.UpdateConfig(fileName,nodeName,attriName,attriValue,matchAttriName,matchAttriValue);
}
public void InsertNode(string fileName,string mainNode,string element,string[] attrib,string[] attribContent,string Content)
{
if(this._license!="QAZXSW")
return ;
this.manageConfigs.InsertNode(fileName,mainNode,element,attrib,attribContent,Content);
}
public void DeleteNode(string fileName,string nodeName,string[] attrib,string[] attribContent)
{
if(this._license!="QAZXSW")
return ;
this.manageConfigs.DeleteNode(fileName,nodeName,attrib,attribContent);
}
/*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* <表名>
* <字段名1>值字段名1>
* <字段名2>值字段名2>
* 表名>
* <表名>
* <字段名1>值字段名1>
* <字段名2>值字段名2>
* 表名>
*
*
*/
///
/// 读XML配置文件
///
/// 配置文件名
/// XML文件结构
public System.Data.DataSet ReadDataFromDataFile(string fileName)
{
if(this._license!="QAZXSW")
return null;
return this.manageSysLogs.ReadDataFromDataFile(fileName);
}
///
/// 写XML配置文件
///
/// 配置文件名
/// XML文件结构
public void WriteDataToDataFile(string fileName,System.Data.DataSet dataSet)
{
if(this._license!="QAZXSW")
return ;
this.manageSysLogs.WriteDataToDataFile(fileName,dataSet);
}
///
/// 读SQl脚本文件
///
/// 脚本文件名
/// 脚本
public string ReadSQLScript(string fileName)
{
if(this._license!="QAZXSW")
return null;
return this.manageSQLScripts.ReadSQLScript(fileName);
}
///
/// 写SQl脚本文件
///
/// 脚本文件名
/// 脚本内容
public void WriteSQLScript(string fileName,string sqlString)
{
if(this._license!="QAZXSW")
return ;
this.manageSQLScripts.WriteSQLScript(fileName,sqlString);
}
///
/// 写XML配置文件
///
/// 配置文件名
/// XML文件结构
public void WriteDataToDataFile(string fileName,string sqlString)
{
if(this._license!="QAZXSW")
return ;
this.manageSQLScripts.WriteSQLScript(fileName,sqlString);
}
#endregion
#endregion
#region private methods
private delegate object SendAsgMessageOneDetegate(string sourceName,string destName,string infoTypeName,object infoTypeObj);
private delegate object SendAsgMessagePartDetegate(string sourceName,string[] destNames,string infoTypeName,object infoTypeObj);
private delegate object SendAsgMessageAllDetegate(string sourceName,string infoTypeName,object infoTypeObj);
private static void SendEventCallbackOne(System.IAsyncResult ar)
{
System.Runtime.Remoting.Messaging.AsyncResult asyncResult = (System.Runtime.Remoting.Messaging.AsyncResult)ar;
SendAsgMessageOneDetegate uld = (SendAsgMessageOneDetegate)asyncResult.AsyncDelegate;
uld.EndInvoke(ar);
}
private static void SendEventCallbackPart(System.IAsyncResult ar)
{
System.Runtime.Remoting.Messaging.AsyncResult asyncResult = (System.Runtime.Remoting.Messaging.AsyncResult)ar;
SendAsgMessagePartDetegate uld = (SendAsgMessagePartDetegate)asyncResult.AsyncDelegate;
uld.EndInvoke(ar);
}
private static void SendEventCallbackAll(System.IAsyncResult ar)
{
System.Runtime.Remoting.Messaging.AsyncResult asyncResult = (System.Runtime.Remoting.Messaging.AsyncResult)ar;
SendAsgMessageAllDetegate uld = (SendAsgMessageAllDetegate)asyncResult.AsyncDelegate;
uld.EndInvoke(ar);
}
private MsgEventHandler FindMsgEventHndler(string myName)
{
MsgEventHandler pMsgEventHandler = null;
pMsgEventHandler = this.m_ListMsgObject[myName] as MsgEventHandler;
return pMsgEventHandler;
}
#endregion
}
}