public class Request
{
/// <summary>
/// 璇诲啓m_SessionID
/// </summary>
public String SessionID
{
get
{
return m_SessionID;
}
set
{
m_SessionID = value;
}
}
/// <summary>
/// 璇诲啓m_Type
/// </summary>
public RequestPacketType Type
{
get
{
return m_Type;
}
set
{
m_Type = value;
}
}
/// <summary>
/// 璇诲啓m_RequestCode
/// </summary>
public String RequestCode
{
get
{
return m_RequestCode;
}
set
{
m_RequestCode = value;
}
}
protected Request()
{
m_Timer = new System.Windows.Forms.Timer();
m_Timer.Tick += new EventHandler(TimerOnTick);
m_Timer.Interval = 6000;
}
/// <summary>
/// 鎶婂懡浠ゆ墦鍖呮垚XML鏁版嵁
/// </summary>
/// <returns>鍛戒护鐨剎ml瀛楃涓?/returns>
public virtual string PackMessage()
{
return null;
}
/// <summary>
/// 瑙e寘杩斿洖鏁版嵁
/// </summary>
/// <param name="xmlString">鏁版嵁鐨剎ml瀛楃涓?/param>
/// <returns> 瑙f瀽鍚庣殑鏁版嵁</returns>
public virtual ResultValue ParseMessage(string xmlString)
{
ResultValue resultValue = new ResultValue();
resultValue.SessionID = this.SessionID;
resultValue.Param = this.m_Param;
XmlDataDocument responseData = new XmlDataDocument();
XmlElement element;
//Invert string to xml
try {
responseData.LoadXml(xmlString);
element = (XmlElement)responseData.GetElementsByTagName("Server")[0];
//server failed
if (element.GetAttribute("resNo") == ReturnFlag.Failed) {
resultValue.Flag = ReturnFlag.Failed;
//element = (XmlElement)responseData.GetElementsByTagName("ErrorCode")[0];
resultValue.Data = new ServerReturnException(Info + element["ErrorCode"].InnerText.Trim());
return resultValue;
}
resultValue.Data = null;
resultValue.Flag = ReturnFlag.Successed;
} catch (Exception e) {
resultValue.Flag = ReturnFlag.Failed;
resultValue.Data = new ParsePacketException(Info + " " + e.Message);
}
return resultValue;
}
#region GenSession
public static int session = 0;
public static Random random = new Random((int)DateTime.Now.Ticks);
//璁板綍涓婁釜璇锋眰鐨剆ession
private static int sessionRecord;
// 鍒ゆ柇璇锋眰鏄惁鏄噸璇?
private static bool firstSession = true;
//褰撳墠浣跨敤鐨処P
private static string localIP = setupIp();
private static IPAddress localIPAddress;
public static IPAddress LocalIPAddress
{
get
{
return localIPAddress;
}
set
{
localIPAddress = value;
}
}
/// <summary>
/// 鑾峰彇澶氱綉鍗℃椂鐨処P
/// </summary>
private static string setupIp()
{
TcpClient tc;
try {
Configurations config = ConfigurationManager.GetConfig();
tc = new TcpClient(config["SC204IP"], Convert.ToInt32(config["SendPort"]));
IPAddress ip = ((IPEndPoint)tc.Client.LocalEndPoint).Address;
LocalIPAddress = ip;
//local
//for (int i = 0; i < 3 - ip.GetAddressBytes()[3].ToString().Length; i++) {
// localIP += "0";
//}
localIP += ip.GetAddressBytes()[3].ToString();
tc.Close();
if (localIP.Length < 1) {
MessageBox.Show("Can't get the ip, please check the network. The system will exit");
Environment.Exit(0);
}
} catch (Exception) {
MessageBox.Show("Can't get the ip, please check the network. The system will exit");
Environment.Exit(0);
}
return localIP;
}
/// <summary>
/// 鐢熸垚鍏ㄥ眬鍞竴鐨凷essionID
/// </summary>
/// <returns>sessionID瀛楃涓?/returns>
protected /*static*/ String GenSession()
{
if (SessionID.Length != 10) {
string reqSession = string.Empty;
if (firstSession) {
StringBuilder sb = new StringBuilder();
lock (random) {
for (int i = 0; i < 7; i++) {
sb.Append(random.Next(9).ToString());
}
}
sessionRecord = Convert.ToInt32(session);
reqSession = localIP + sb.ToString();
firstSession = false;
} else {
sessionRecord = (sessionRecord + 1) % 10000000;
int len = 7 - sessionRecord.ToString().Length;
for (int i = 0; i < len; i++) {
reqSession += "0";
}
reqSession = localIP + reqSession + sessionRecord.ToString();
}
this.SessionID = reqSession;
}
return this.SessionID;
}
#endregion
/// <summary>
/// 鍒濆璇诲彇fuctionCode閰嶇疆鏂囦欢
/// </summary>
/// <param name="functionCode">鍛戒护鐮?/param>
/// <returns></returns>
public XmlNodeList CreateRequest(String functionCode)
{
if (m_CommandDoucument.Value == null) {
try {
m_CommandDoucument.Load(@"D:\fuctionConfig.xml");
} catch (Exception e) {
return null;
}
}
return m_CommandDoucument.GetElementsByTagName(m_RequestCode);
}
public void OnRequestCompleted(ResultValue value)
{
if (RequestCommpleted != null) {
RequestCommpleted(value);
}
}
private String info;
public String Info
{
get
{
return (this.info);
}
set
{
this.info = value;
}
}
protected void TimerOnTick(Object obj, EventArgs args)
{
System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)obj;
//Console.WriteLine("TimeOut:{0}", m_SessionID);
//if time is running
if (timer.Enabled) {
timer.Stop();
//timer.Dispose();
ResultValue resultValue = new ResultValue();
resultValue.SessionID = m_SessionID;
resultValue.Flag = ReturnFlag.Failed;
resultValue.Param = this.m_Param;
resultValue.Data = new RequestTimeoutException(info + "Err.6.222");
OnRequestCompleted(resultValue);
}
}
public void Start()
{
m_Timer.Start();
}
public void Stop()
{
m_Timer.Stop();
}
public bool IsTimeOut
{
get
{
return !m_Timer.Enabled;
}
}
public void SetParam(Object param)
{
m_Param = param;
}
protected Object m_Param;
protected System.Windows.Forms.Timer m_Timer;
protected static XmlDocument m_CommandDoucument = new XmlDocument();
protected String m_SessionID = string.Empty;
protected RequestPacketType m_Type;
protected String m_RequestCode;
public event RequestCompletedEventHandler RequestCommpleted;
public delegate void RequestCompletedEventHandler(ResultValue value);
public bool HasRequestCommpleted()
{
return RequestCommpleted != null;
}
}