?? basecommand.cs
字號:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Net.Sockets;
using System.Net;
using JeasonZhao.Sms.SGIP.Exceptions;
using JeasonZhao.Sms.BaseModule;
namespace JeasonZhao.Sms.SGIP.Command
{
public class BaseCommand
{
public DelegateSocketInformation DebugHandler = null;
#region Members
private uint m_nPackageLength = 20;
private Commands m_nCommand = Commands.Bind;
private uint m_nSrcNodeSequence = 0;
private uint m_nDateSequence = 0;
private uint m_nMsgSequence = 0;
#endregion
public BaseCommand(Commands cmd)
{
m_nCommand = cmd;
}
public void CopyHeader(BaseCommand cmd)
{
//m_nCommand = cmd.Command;
m_nDateSequence = cmd.m_nDateSequence;
m_nMsgSequence = cmd.m_nMsgSequence;
m_nPackageLength = cmd.m_nPackageLength;
m_nSrcNodeSequence = cmd.m_nSrcNodeSequence;
}
#region Properties
/// <summary>
/// Message Length 4 Integer 消息的總長度(字節)
/// </summary>
[SmsField(true, 0, 4)]
public uint PackageLength
{
get { return m_nPackageLength; }
set { m_nPackageLength = value; }
}
/// <summary>
/// Command ID 4 Integer 命令ID
/// </summary>
[SmsField(true, 1, 4)]
public Commands Command
{
get { return m_nCommand; }
set { m_nCommand = value; }
}
/// <summary>
/// 1/3 Sequence Number 12 Integer 序列號
/// </summary>
[SmsField(true, 2, 4)]
public uint SrcNodeSequence
{
get { return m_nSrcNodeSequence; }
set { m_nSrcNodeSequence = value; }
}
/// <summary>
/// 2/3 Sequence Number 12 Integer 序列號
/// </summary>
[SmsField(true, 3, 4)]
public uint DateSequence
{
get { return m_nDateSequence; }
set { m_nDateSequence = value; }
}
/// <summary>
/// 3/3 Sequence Number 12 Integer 序列號
/// </summary>
[SmsField(true, 4, 4)]
public uint MsgSequence
{
get { return m_nMsgSequence; }
set { m_nMsgSequence = value; }
}
#endregion
#region SendPackage
private int WriteUint(System.Net.Sockets.Socket socket, uint nValue)
{
byte[] bArray = new byte[4];
bArray[3] = (byte)(nValue & 0xFF);
bArray[2] = (byte)((nValue >> 8) & 0xFF);
bArray[1] = (byte)((nValue >> 16) & 0xFF);
bArray[0] = (byte)((nValue >> 24) & 0xFF);
return socket.Send(bArray);
}
private int WriteByte(System.Net.Sockets.Socket socket, uint n)
{
byte[] bArray = new byte[1];
bArray[0] = (byte)n;
return socket.Send(bArray);
}
private byte ReadByte(System.Net.Sockets.Socket socket)
{
byte[] bArray = new byte[1];
if (socket.Receive(bArray) != 1)
{
throw new Exception("讀取整數錯誤,讀取字節數不等于1");
}
return bArray[0];
}
protected uint ReadUint(Socket sock)
{
byte[] bArray = new byte[4];
if (sock.Receive(bArray) != 4)
{
throw new Exception("讀取整數錯誤,讀取字節數不等于4");
}
uint nValue = 0;
nValue ^= bArray[0];
nValue = nValue << 8;
nValue ^= bArray[1];
nValue = nValue << 8;
nValue ^= bArray[2];
nValue = nValue << 8;
nValue ^= bArray[3];
return nValue;
}
public bool SendPackage(System.Net.Sockets.Socket socket)
{
if (m_nMsgSequence < 1)
{
this.m_nMsgSequence = CommandFactory.GetNewMsgSequence();
}
if (m_nDateSequence < 1)
{
uint lf = (uint)(DateTime.Now.Month * 100000000 +
DateTime.Now.Day * 1000000 +
DateTime.Now.Hour * 10000 +
DateTime.Now.Minute * 100 +
DateTime.Now.Second);
m_nDateSequence = lf;
}
if (checkPackage() == false)
{
return false;
}
List<PropertyPackage> allHeaders = GetProperties(true);
List<PropertyPackage> allBodys = GetProperties(false);
uint nPackageLength = 0;
foreach (PropertyPackage p in allHeaders)
{
nPackageLength += p.Length;
}
foreach (PropertyPackage p in allBodys)
{
nPackageLength += p.Length;
}
this.m_nPackageLength = nPackageLength;
foreach (PropertyPackage p in allHeaders)
{
if (WriteProperties(socket, p) == false)
{
return false;
}
}
foreach (PropertyPackage p in allBodys)
{
if (WriteProperties(socket, p) == false)
{
return false;
}
}
return true;
}
protected virtual bool WriteProperties(Socket sock, PropertyPackage pack)
{
try
{
if (null == sock || null == pack)
{
return false;
}
//重新設置參數值
pack.ReBuildPackage();
//Debug("傳送參數 " + pack.ToString());
if (pack.PackageDataType == PackageDataTypes.Uint)
{
return WriteUint(sock, pack.UIntValue) == 4;
}
else if (pack.PackageDataType == PackageDataTypes.Byte)
{
return WriteByte(sock, pack.UIntValue) == 1;
}
else
{
string sValue = pack.StringValue;
if (null == sValue)
{
sValue = "";
}
int nLength = pack.Attribute.Length;
byte[] bStringArray = System.Text.Encoding.Default.GetBytes(sValue);
if (nLength <= 0)
{
nLength = bStringArray.Length;
}
byte[] bSend = new byte[nLength];
for (int n = 0; n < bSend.Length; n++)
{
if (n < bStringArray.Length)
{
bSend[n] = bStringArray[n];
}
else
{
bSend[n] = 0;
}
}
return sock.Send(bSend) == nLength;
}
}
catch (Exception exce)
{
throw new WritePackageException(pack, exce);
}
}
#endregion
#region Read Package
public void ReadHeader(System.Net.Sockets.Socket socket)
{
foreach (PropertyPackage pack in GetProperties(true))
{
ReadProperties(socket, pack);
}
//this.Debug("讀?。号袛喟^信息 " + this.Command);
}
protected virtual void ReadProperties(Socket sock, PropertyPackage pack)
{
try
{
if (null == sock || null == pack)
{
return;
}
int nLength = pack.Attribute.Length;
if (pack.PackageDataType == PackageDataTypes.Uint)
{
uint value = ReadUint(sock);
pack.Property.SetValue(this, value, null);
//Debug("讀取屬性 " + pack.Property.Name + "(" + pack.PackageDataType + ") " + value);
}
else if (pack.PackageDataType == PackageDataTypes.Byte)
{
byte value = ReadByte(sock);
pack.Property.SetValue(this, value, null);
//Debug("讀取屬性 " + pack.Property.Name + " (" + pack.PackageDataType + ")" + value);
}
else
{
string sValue = ReadString(sock, nLength);
pack.Property.SetValue(this, sValue, null);
//Debug("讀取屬性 " + pack.Property.Name + " (" + pack.PackageDataType + ")" + sValue);
}
}
catch (Exception e)
{
throw new ReadPackageException(pack, e);
}
}
public void ReadBody(System.Net.Sockets.Socket socket)
{
foreach (PropertyPackage pack in GetProperties(false))
{
ReadProperties(socket, pack);
}
}
protected string ReadString(Socket sock, int nLen)
{
byte[] bArray = new byte[nLen];
int nRead = sock.Receive(bArray);
if (nRead != nLen)
{
throw new Exception("讀取字符串錯誤,讀取字節數" + nRead + "不等于" + nLen);
}
int n = 0;
foreach (byte b in bArray)
{
if (b == 0)
{
break;
}
n++;
}
byte[] aNew = new byte[n];
for (int x = 0; x < n && x < bArray.Length; x++)
{
aNew[x] = bArray[x];
}
return System.Text.Encoding.Default.GetString(aNew);
}
public override string ToString()
{
String str = "";
foreach (PropertyInfo pInfo in this.GetType().GetProperties())
{
bool bFound = false;
SmsFieldAttribute smsAttr = null;
foreach (Attribute attr in pInfo.GetCustomAttributes(false))
{
if (attr.GetType() == typeof(SmsFieldAttribute))
{
smsAttr = attr as SmsFieldAttribute;
bFound = true;
break;
}
}
if (bFound && pInfo.CanRead)
{
Object obj = pInfo.GetValue(this, null);
str += "[" + pInfo.Name + "=" + obj + "]";
}
}
return str;
}
public List<PropertyInfo> GetAllFields()
{
List<PropertyInfo> ret = new List<PropertyInfo>();
foreach (PropertyInfo pInfo in this.GetType().GetProperties())
{
bool bFound = false;
SmsFieldAttribute smsAttr = null;
foreach (Attribute attr in pInfo.GetCustomAttributes(false))
{
if (attr.GetType() == typeof(SmsFieldAttribute))
{
smsAttr = attr as SmsFieldAttribute;
bFound = true;
break;
}
}
if (bFound)
{
System.Console.WriteLine("fieldInfo " + pInfo.Name + " " +
"[" + pInfo.PropertyType + "]" +
smsAttr.IsHeader + smsAttr.Order);
if (pInfo.CanRead)
{
Object obj = pInfo.GetValue(this, null);
System.Console.WriteLine("Values " + (obj == null ? "NULL" : obj));
}
ret.Add(pInfo);
}
}
return ret;
}
public List<PropertyPackage> GetProperties(bool bHeader)
{
PropertyInfo[] allPropertys = new PropertyInfo[255];
SmsFieldAttribute[] allAttributes = new SmsFieldAttribute[255];
int nRealGetCount = 0;
foreach (PropertyInfo pInfo in this.GetType().GetProperties())
{
bool bFound = false;
SmsFieldAttribute smsAttr = null;
foreach (Attribute attr in pInfo.GetCustomAttributes(false))
{
if (attr.GetType() == typeof(SmsFieldAttribute))
{
smsAttr = attr as SmsFieldAttribute;
bFound = true;
break;
}
}
if (bFound &&
smsAttr.IsHeader == bHeader)
{
int nIndex = smsAttr.Order;
allPropertys[nIndex] = pInfo;
allAttributes[nIndex] = smsAttr;
nRealGetCount++;
}
}
List<PropertyPackage> ret = new List<PropertyPackage>();
for (int nIndex = 0; nIndex < nRealGetCount; nIndex++)
{
if (allPropertys[nIndex] == null || allAttributes[nIndex] == null)
{
throw new ApplicationException("數據報文定義錯誤,沒有定義" + (nIndex + 1) + " 的元素!");
}
ret.Add(new PropertyPackage(this, allPropertys[nIndex], allAttributes[nIndex]));
}
return ret;
}
protected virtual bool checkPackage()
{
if (m_nDateSequence <= 0)
{
throw new InvalidPackageException("日期序列號不合法!");
}
if (m_nMsgSequence < 0)
{
throw new InvalidPackageException("消息序列號不合法!");
}
if (m_nSrcNodeSequence < 0)
{
throw new InvalidPackageException("節點序列號不合法!");
}
return true;
}
#endregion
#region Debug
private void Debug(String str)
{
try
{
if (null == DebugHandler)
{
System.Console.WriteLine(str);
}
else
{
DebugHandler(str);
}
}
catch (Exception)
{
}
}
#endregion
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -