CMPP30 类

//转自: 月光下的卡瓦博格
None.gif using  System;
None.gif
using  System.Text;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Threading;
None.gif
using  System.Collections;
None.gif
using  System.Diagnostics;
None.gif
using  System.Net.Sockets;
None.gif
using  System.Security.Cryptography;
None.gif
None.gif
namespace  Cmpp.Components
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// CMPP30 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CMPP30
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
变量以及初始值#region 变量以及初始值
InBlock.gif        
protected string m_strSPID;//SP企业代码;
InBlock.gif
        protected string m_strUsername;
InBlock.gif        
protected string m_strPassword;//SP密码;
InBlock.gif
        protected string m_strAddress;//短信网关地址
InBlock.gif
        protected int m_iPort;//短信网关端口号;
InBlock.gif
        protected static UInt32 m_iSeqID = 0;//命令的序号
InBlock.gif

InBlock.gif        
protected int m_iSlidingWindowSize = 16;//滑动窗口大小(W)
InBlock.gif
        protected int m_iActiveTestSpan = 30;//ACTIVETEST的时间间隔(C,以秒为单位),标准为180
InBlock.gif
        protected DateTime m_dtLastTransferTime;//最近一次网络传输时间
InBlock.gif
        protected int m_iTimeOut = 60;//响应超时时间(T,以秒为单位)
InBlock.gif
        protected int m_iSendCount = 3;//最大发送次数(N)
InBlock.gif
        protected DATA_PACKAGE[] SlidingWindow = null;
InBlock.gif        
protected TcpClient m_TcpClient = null;
InBlock.gif        
protected NetworkStream m_NetworkStream = null;
InBlock.gif        
protected Queue m_MessageQueue = null;//消息队列,用于保存所有待发送数据
InBlock.gif
        protected int m_iTcpClientTimeout = 5;//TcpClient接收和发送超时(以秒为单位)
InBlock.gif
        protected int m_iSendSpan = 10;//发送间隔,以毫秒为单位
ExpandedSubBlockEnd.gif
        #endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
InBlock.gif        
public CMPP30(string SPID, string Username, string Password, string Address, int Port)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PClsIni ini 
= new PClsIni(System.Windows.Forms.Application.StartupPath + "\\Option.ini");
InBlock.gif            
if (ini.ExistINIFile())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_iSeqID 
= UInt32.Parse(ini.IniReadValue("CMPP""SeqID"));
InBlock.gif                m_iSlidingWindowSize 
= int.Parse(ini.IniReadValue("CMPP""SlidingWindowSize"));
InBlock.gif                m_iActiveTestSpan 
= int.Parse(ini.IniReadValue("CMPP""ActiveTestSpan"));
InBlock.gif                m_iTimeOut 
= int.Parse(ini.IniReadValue("CMPP""TimeOut"));
InBlock.gif                m_iSendCount 
= int.Parse(ini.IniReadValue("CMPP""SendCount"));
InBlock.gif                m_iTcpClientTimeout 
= int.Parse(ini.IniReadValue("CMPP""TcpClientTimeout"));
InBlock.gif                m_iSendSpan 
= int.Parse(ini.IniReadValue("CMPP""SendSpan"));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            m_strSPID 
= SPID;
InBlock.gif            m_strUsername 
= Username;
InBlock.gif            m_strPassword 
= Password;
InBlock.gif            m_strAddress 
= Address;
InBlock.gif            m_iPort 
= Port;
InBlock.gif            SlidingWindow 
= new DATA_PACKAGE[m_iSlidingWindowSize];//初始化滑动窗口
InBlock.gif
            for (int i = 0; i < m_iSlidingWindowSize; i++)
InBlock.gif                SlidingWindow[i] 
= new DATA_PACKAGE();
InBlock.gif            m_MessageQueue 
= new Queue();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Constants#region Constants
InBlock.gif        
public const Byte CMPP_VERSION_30 = 0x30;
InBlock.gif        
public const Byte CMPP_VERSION_21 = 0x20;
InBlock.gif        
public const UInt32 CMD_ERROR = 0xFFFFFFFF;
InBlock.gif        
public const UInt32 CMD_CONNECT = 0x00000001;
InBlock.gif        
public const UInt32 CMD_CONNECT_RESP = 0x80000001;
InBlock.gif        
public const UInt32 CMD_TERMINATE = 0x00000002// 终止连接
InBlock.gif
        public const UInt32 CMD_TERMINATE_RESP = 0x80000002// 终止连接应答
InBlock.gif
        public const UInt32 CMD_SUBMIT = 0x00000004// 提交短信
InBlock.gif
        public const UInt32 CMD_SUBMIT_RESP = 0x80000004// 提交短信应答
InBlock.gif
        public const UInt32 CMD_DELIVER = 0x00000005// 短信下发
InBlock.gif
        public const UInt32 CMD_DELIVER_RESP = 0x80000005// 下发短信应答
InBlock.gif
        public const UInt32 CMD_QUERY = 0x00000006// 短信状态查询
InBlock.gif
        public const UInt32 CMD_QUERY_RESP = 0x80000006// 短信状态查询应答
InBlock.gif
        public const UInt32 CMD_CANCEL = 0x00000007// 删除短信
InBlock.gif
        public const UInt32 CMD_CANCEL_RESP = 0x80000007// 删除短信应答
InBlock.gif
        public const UInt32 CMD_ACTIVE_TEST = 0x00000008// 激活测试
InBlock.gif
        public const UInt32 CMD_ACTIVE_TEST_RESP = 0x80000008// 激活测试应答
ExpandedSubBlockEnd.gif
        #endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
工作线程#region 工作线程
InBlock.gif        
protected System.Threading.Thread m_SendThread = null;
InBlock.gif        
protected System.Threading.Thread m_ReceiveThread = null;
InBlock.gif
InBlock.gif        
protected AutoResetEvent m_eventSendExit = new AutoResetEvent(false);
InBlock.gif        
protected AutoResetEvent m_eventReceiveExit = new AutoResetEvent(false);
InBlock.gif        
protected AutoResetEvent m_eventConnect = new AutoResetEvent(false);
InBlock.gif        
protected AutoResetEvent m_eventDisconnect = new AutoResetEvent(false);
InBlock.gif        
protected ManualResetEvent m_eventSend = new ManualResetEvent(false);
InBlock.gif        
protected ManualResetEvent m_eventReceive = new ManualResetEvent(false);
InBlock.gif
InBlock.gif        
protected void SendThreadProc()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_eventSendExit.WaitOne(TimeSpan.FromMilliseconds(0), false))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Disconnect();
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (m_eventConnect.WaitOne(TimeSpan.FromMilliseconds(0), false))//连接
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    
if (Connect())//连接上,开始发送和接收
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        m_eventSend.Set();
InBlock.gif                        m_eventReceive.Set();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Close();
InBlock.gif                        Thread.Sleep(
5000);
InBlock.gif                        m_eventConnect.Set();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (m_eventDisconnect.WaitOne(TimeSpan.FromMilliseconds(0), false))//拆除连接
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    m_eventSend.Reset();
InBlock.gif                    m_eventReceive.Reset();
InBlock.gif                    Disconnect();
InBlock.gif                    Thread.Sleep(
5000);
InBlock.gif                    m_eventConnect.Set();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ((m_eventSend.WaitOne(TimeSpan.FromMilliseconds(0), false)) && (m_NetworkStream != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
bool bOK = true;
InBlock.gif                    ActiveTest();
InBlock.gif                    Monitor.Enter(SlidingWindow);
InBlock.gif                    
for (int i = 0; i < m_iSlidingWindowSize; i++)//首先用消息队列中的数据填充滑动窗口
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        
if (SlidingWindow[i].Status == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            DATA_PACKAGE dp 
= new DATA_PACKAGE();
InBlock.gif                            dp.Data 
= null;
InBlock.gif                            Monitor.Enter(m_MessageQueue);
InBlock.gif                            
if (m_MessageQueue.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                dp 
= (DATA_PACKAGE)m_MessageQueue.Dequeue();
InBlock.gif                                SlidingWindow[i] 
= dp;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            Monitor.Exit(m_MessageQueue);
InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
for (int i = 0; i < m_iSlidingWindowSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DATA_PACKAGE dp 
= SlidingWindow[i];
InBlock.gif                        
if ((dp.Status == 1&& (dp.SendCount == 0))//第一次发送
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            bOK 
= Send(dp);
InBlock.gif                            
if ((bOK) && (dp.Command > 0x80000000))//发送的是Response类的消息,不需等待Response
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                SlidingWindow[i].Status 
= 0;//清空窗口
ExpandedSubBlockEnd.gif
                            }

InBlock.gif                            
else if ((bOK) && (dp.Command < 0x80000000))//发送的是需要等待Response的消息
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif
InBlock.gif                                SlidingWindow[i].SendTime 
= DateTime.Now;
InBlock.gif                                SlidingWindow[i].SendCount
++;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                bOK 
= false;
InBlock.gif                                
break;//网络出错
ExpandedSubBlockEnd.gif
                            }

InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else if ((dp.Status == 1&& (dp.SendCount > 0))//第N次发送
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            
if (dp.SendCount > m_iSendCount - 1)//已发送m_iSendCount次,丢弃数据包
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                SlidingWindow[i].Status 
= 0;//清空窗口
InBlock.gif
                                if (dp.Command == CMPP30.CMD_ACTIVE_TEST)//是ActiveTest
ExpandedSubBlockStart.gifContractedSubBlock.gif
                                dot.gif{
InBlock.gif                                    bOK 
= false;
InBlock.gif                                    
break;//ActiveTest出错
ExpandedSubBlockEnd.gif
                                }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                TimeSpan ts 
= DateTime.Now - dp.SendTime;
InBlock.gif                                
if (ts.TotalSeconds >= m_iTimeOut)//超时后未收到回应包
ExpandedSubBlockStart.gifContractedSubBlock.gif
                                dot.gif{
InBlock.gif                                    bOK 
= Send(dp);//再次发送
InBlock.gif
                                    if (bOK)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
dot.gif{
InBlock.gif                                        SlidingWindow[i].SendTime 
= DateTime.Now;
InBlock.gif                                        SlidingWindow[i].SendCount
++;
ExpandedSubBlockEnd.gif                                    }

InBlock.gif                                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
dot.gif{
InBlock.gif                                        bOK 
= false;
InBlock.gif                                        
break;//网络出错
ExpandedSubBlockEnd.gif
                                    }

ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

InBlock.gif
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    Monitor.Exit(SlidingWindow);
InBlock.gif
InBlock.gif
InBlock.gif                    
if (!bOK)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Close();
//关闭连接
InBlock.gif
                        Thread.Sleep(5000);//等待5秒
InBlock.gif
                        m_eventSend.Reset();
InBlock.gif                        m_eventConnect.Set();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                Thread.Sleep(
1);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected void ReceiveThreadProc()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_eventReceiveExit.WaitOne(TimeSpan.FromMilliseconds(0), false))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ((m_eventReceive.WaitOne(TimeSpan.FromMilliseconds(0), false&& (m_NetworkStream != null)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    CMPP_HEAD Head 
= ReadHead();
InBlock.gif                    
if (Head.CommandID == CMPP30.CMD_SUBMIT_RESP)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ReadSubmitResp(Head);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (Head.CommandID == CMPP30.CMD_ACTIVE_TEST)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ActiveTestResponse(Head.SequenceID);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (Head.CommandID == CMPP30.CMD_ACTIVE_TEST_RESP)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ReadActiveTestResponse(Head);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (Head.CommandID == CMPP30.CMD_DELIVER)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ReadDeliver(Head);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (Head.CommandID == CMPP30.CMD_ERROR)//网络故障
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        m_eventReceive.Reset();
InBlock.gif                        m_eventDisconnect.Set();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                Thread.Sleep(
1);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SMSEvents#region SMSEvents
InBlock.gif        
public event SMSEventHandler SMSStateChanged;
InBlock.gif
InBlock.gif        
protected void RaiseEvent(SMS_STATE State, Object Data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (null != SMSStateChanged)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SMSEventArgs e 
= new SMSEventArgs();
InBlock.gif                e.Time 
= DateTime.Now;
InBlock.gif                e.State 
= State;
InBlock.gif                e.Data 
= Data;
InBlock.gif                SMSStateChanged(
this, e);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有方法#region 私有方法
InBlock.gif        
protected UInt32 TimeStamp(DateTime dt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string str = String.Format("{0:MMddhhmmss}", dt);
InBlock.gif            
return Convert.ToUInt32(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected UInt32 CreateID()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UInt32 id 
= m_iSeqID;
InBlock.gif            m_iSeqID
++;
InBlock.gif            
if (m_iSeqID > UInt32.MaxValue)
InBlock.gif                m_iSeqID 
= 0;
InBlock.gif            
return id;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected Byte[] CreateDigest(DateTime dt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int iLength = 25 + m_strPassword.Length;
InBlock.gif            Byte[] btContent 
= new Byte[iLength];
InBlock.gif            Array.Clear(btContent, 
0, iLength);
InBlock.gif            
int iPos = 0;
InBlock.gif            
foreach (char ch in m_strSPID)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btContent[iPos] 
= (Byte)ch;
InBlock.gif                iPos
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            iPos 
+= 9;
InBlock.gif            
foreach (char ch in m_strPassword)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btContent[iPos] 
= (Byte)ch;
InBlock.gif                iPos
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string strTimeStamp = String.Format("{0:MMddhhmmss}", dt);
InBlock.gif            
foreach (char ch in strTimeStamp)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btContent[iPos] 
= (Byte)ch;
InBlock.gif                iPos
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            MD5 md5 
= new MD5CryptoServiceProvider();
InBlock.gif            
return md5.ComputeHash(btContent);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected bool Close()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (m_NetworkStream != null)
InBlock.gif                m_NetworkStream.Close();
InBlock.gif            
if (m_TcpClient != null)
InBlock.gif                m_TcpClient.Close();
InBlock.gif
InBlock.gif            m_TcpClient 
= null;
InBlock.gif            m_NetworkStream 
= null;
InBlock.gif
InBlock.gif            
return true;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected bool Connect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bOK = true;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            CMPP_CONNECT_RESP resp 
= new CMPP_CONNECT_RESP();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_TcpClient 
= new TcpClient();
InBlock.gif                m_TcpClient.ReceiveTimeout 
= m_TcpClient.SendTimeout = m_iTcpClientTimeout * 1000;
InBlock.gif                m_TcpClient.Connect(m_strAddress, m_iPort);
InBlock.gif                m_NetworkStream 
= m_TcpClient.GetStream();
InBlock.gif
InBlock.gif                DateTime dt 
= DateTime.Now;
InBlock.gif                CMPP_CONNECT conn 
= new CMPP_CONNECT();
InBlock.gif                conn.Head 
= new CMPP_HEAD();
InBlock.gif                conn.Head.CommandID 
= CMPP30.CMD_CONNECT;
InBlock.gif                conn.Head.SequenceID 
= CreateID();
InBlock.gif                conn.SourceAddress 
= m_strSPID;
InBlock.gif                conn.TimeStamp 
= TimeStamp(dt);
InBlock.gif                conn.AuthenticatorSource 
= CreateDigest(dt);
InBlock.gif                conn.Version 
= CMPP_VERSION_30;
InBlock.gif                Byte[] buffer 
= conn.GetBuffer();
InBlock.gif                m_NetworkStream.Write(buffer, 
0, (Int32)conn.Head.TotalLength);
InBlock.gif                
int iSpan = 0;
InBlock.gif                
bool bTimeOut = false;
InBlock.gif                
while (!m_NetworkStream.DataAvailable)//等待RESPONSE 5秒
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    Thread.Sleep(
10);
InBlock.gif                    iSpan
++;
InBlock.gif                    
if (iSpan > 500)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        bTimeOut 
= true;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (!bTimeOut)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    CMPP_HEAD Head 
= ReadHead();
InBlock.gif                    
if (Head.CommandID == CMD_CONNECT_RESP)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        resp 
= ReadConnectResp(Head);
InBlock.gif                        
if (resp.Status == 0)
InBlock.gif                            bOK 
= true;
InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            bOK 
= false;
InBlock.gif                            strError 
= "未正确接收CONNECT_RESP";
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bOK 
= false;
InBlock.gif                    strError 
= "等待CONNECT_RESP超时";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strError 
= e.Message;
InBlock.gif                bOK 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (bOK)
InBlock.gif                RaiseEvent(SMS_STATE.SP_CONNECT, resp);
InBlock.gif            
else
InBlock.gif                RaiseEvent(SMS_STATE.SP_CONNECT_ERROR, strError);
InBlock.gif
InBlock.gif            
return bOK;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected bool Disconnect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bOK = true;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DateTime dt 
= DateTime.Now;
InBlock.gif                CMPP_HEAD Head 
= new CMPP_HEAD();
InBlock.gif                Head.CommandID 
= CMPP30.CMD_TERMINATE;
InBlock.gif                Head.SequenceID 
= CreateID();
InBlock.gif                Head.TotalLength 
= (UInt32)Marshal.SizeOf(Head);
InBlock.gif                Byte[] buffer 
= Head.GetBuffer();
InBlock.gif                m_NetworkStream.Write(buffer, 
0, (Int32)Head.TotalLength);
InBlock.gif                
int iSpan = 0;
InBlock.gif                
bool bTimeOut = false;
InBlock.gif                
while (!m_NetworkStream.DataAvailable)//等待RESPONSE 5秒
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    Thread.Sleep(
10);
InBlock.gif                    iSpan
++;
InBlock.gif                    
if (iSpan > 500)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        bTimeOut 
= true;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (!bTimeOut)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Head 
= ReadHead();
InBlock.gif                    
if (Head.CommandID == CMD_TERMINATE_RESP)
InBlock.gif                        bOK 
= true;
InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        bOK 
= false;
InBlock.gif                        strError 
= "未正确接收TERMINATE_RESP";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bOK 
= false;
InBlock.gif                    strError 
= "等待TERMINATE_RESP超时";
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bOK 
= false;
InBlock.gif                strError 
= ex.Message;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (m_NetworkStream != null)
InBlock.gif                m_NetworkStream.Close();
InBlock.gif            
if (m_TcpClient != null)
InBlock.gif                m_TcpClient.Close();
InBlock.gif            m_TcpClient 
= null;
InBlock.gif            m_NetworkStream 
= null;
InBlock.gif
InBlock.gif            
if (bOK)
InBlock.gif                RaiseEvent(SMS_STATE.SP_DISCONNECT, 
null);
InBlock.gif            
else
InBlock.gif                RaiseEvent(SMS_STATE.SP_DISCONNECT_ERROR, strError);
InBlock.gif
InBlock.gif            
return bOK;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected bool Send(DATA_PACKAGE dp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bOK = true;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            SMS_STATE state 
= SMS_STATE.UNKNOW_ERROR;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Thread.Sleep(m_iSendSpan);
InBlock.gif                Byte[] btData 
= null;
InBlock.gif                
if (dp.Command == CMD_ACTIVE_TEST)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    btData 
= ((CMPP_HEAD)dp.Data).GetBuffer();
InBlock.gif                    state 
= SMS_STATE.ACTIVE_TEST;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (dp.Command == CMD_ACTIVE_TEST_RESP)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    btData 
= ((CMPP_ACTIVE_TEST_RESP)dp.Data).GetBuffer();
InBlock.gif                    state 
= SMS_STATE.ACTIVE_TEST_RESPONSE;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (dp.Command == CMD_DELIVER_RESP)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    btData 
= ((CMPP_DELIVER_RESP)dp.Data).GetBuffer();
InBlock.gif                    state 
= SMS_STATE.DELIVER_RESPONSE;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (dp.Command == CMD_SUBMIT)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    btData 
= ((CMPP_SUBMIT)dp.Data).GetBuffer();
InBlock.gif                    state 
= SMS_STATE.SUBMIT;
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_NetworkStream.Write(btData, 
0, btData.Length);
InBlock.gif                m_dtLastTransferTime 
= DateTime.Now;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                bOK 
= false;
InBlock.gif                strError 
= ex.Message;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (bOK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RaiseEvent(state, dp.Data);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (state == SMS_STATE.ACTIVE_TEST)
InBlock.gif                    state 
= SMS_STATE.ACTIVE_TEST_ERROR;
InBlock.gif                
else if (state == SMS_STATE.ACTIVE_TEST_RESPONSE)
InBlock.gif                    state 
= SMS_STATE.ACTIVE_TEST_RESPONSE_ERROR;
InBlock.gif                
else if (state == SMS_STATE.DELIVER_RESPONSE)
InBlock.gif                    state 
= SMS_STATE.DELIVER_RESPONSE_ERROR;
InBlock.gif                
else if (state == SMS_STATE.SUBMIT)
InBlock.gif                    state 
= SMS_STATE.SUBMIT_ERROR;
InBlock.gif
InBlock.gif                RaiseEvent(state, strError);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return bOK;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected CMPP_HEAD ReadHead()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CMPP_HEAD head 
= new CMPP_HEAD();
InBlock.gif            head.CommandID 
= 0;
InBlock.gif            Byte[] buffer 
= new Byte[12];
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_NetworkStream.DataAvailable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    m_NetworkStream.Read(buffer, 
0, buffer.Length);
InBlock.gif                    head.TotalLength 
= (UInt32)Utility.NetBytesToInt(buffer, 04);
InBlock.gif                    head.CommandID 
= (UInt32)Utility.NetBytesToInt(buffer, 44);
InBlock.gif                    head.SequenceID 
= (UInt32)Utility.NetBytesToInt(buffer, 84);
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                head.CommandID 
= CMD_ERROR;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return head;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected CMPP_CONNECT_RESP ReadConnectResp(CMPP_HEAD Head)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CMPP_CONNECT_RESP resp 
= new CMPP_CONNECT_RESP();
InBlock.gif            resp.Head 
= Head;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_NetworkStream.DataAvailable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Byte[] buffer 
= new Byte[resp.Head.TotalLength - Marshal.SizeOf(resp.Head)];
InBlock.gif                    m_NetworkStream.Read(buffer, 
0, buffer.Length);
InBlock.gif                    resp.Status 
= (UInt32)Utility.NetBytesToInt(buffer, 04);
InBlock.gif                    resp.AuthenticatorISMG 
= new Byte[16];
InBlock.gif                    Array.Copy(buffer, 
4, resp.AuthenticatorISMG, 016);
InBlock.gif                    resp.Version 
= buffer[buffer.Length - 1];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                resp.Head.CommandID 
= CMD_ERROR;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return resp;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected CMPP_SUBMIT_RESP ReadSubmitResp(CMPP_HEAD Head)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CMPP_SUBMIT_RESP resp 
= new CMPP_SUBMIT_RESP();
InBlock.gif            resp.Head 
= Head;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            
bool bOK = true;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_NetworkStream.DataAvailable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Byte[] buffer 
= new Byte[resp.Head.TotalLength - Marshal.SizeOf(resp.Head)];
InBlock.gif                    m_NetworkStream.Read(buffer, 
0, buffer.Length);
InBlock.gif                    
//resp.MsgID=(UInt64)Utility.NetBytesToInt(buffer,0,8);
InBlock.gif
                    resp.Msg_ID = (UInt64)BitConverter.ToUInt64(buffer, 0);
InBlock.gif                    resp.Result 
= (UInt32)Utility.NetBytesToInt(buffer, 84);
InBlock.gif                    Monitor.Enter(SlidingWindow);
InBlock.gif                    
for (int i = 0; i < m_iSlidingWindowSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if ((SlidingWindow[i].Status == 1&&//已发送,等待回应
InBlock.gif
                            (SlidingWindow[i].SequenceID == resp.Head.SequenceID) &&//序列号相同
InBlock.gif
                            (SlidingWindow[i].Command == CMD_SUBMIT))//是Submit
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            SlidingWindow[i].Status 
= 0;//清空窗口
InBlock.gif
                            break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    Monitor.Exit(SlidingWindow);
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                resp.Head.CommandID 
= CMD_ERROR;
InBlock.gif                strError 
= ex.Message;
InBlock.gif                bOK 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (bOK)
InBlock.gif                RaiseEvent(SMS_STATE.SUBMIT_RESPONSE, resp);
InBlock.gif            
else
InBlock.gif                RaiseEvent(SMS_STATE.SUBMIT_RESPONSE_ERROR, strError);
InBlock.gif            
return resp;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected CMPP_DELIVER ReadDeliver(CMPP_HEAD Head)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CMPP_DELIVER deliver 
= new CMPP_DELIVER();
InBlock.gif            deliver.Head 
= Head;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_NetworkStream.DataAvailable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Byte[] buffer 
= new Byte[deliver.Head.TotalLength - Marshal.SizeOf(deliver.Head)];
InBlock.gif                    m_NetworkStream.Read(buffer, 
0, buffer.Length);
InBlock.gif                    deliver.Init(buffer);
InBlock.gif                    DeliverResponse(deliver.Head.SequenceID, deliver.Msg_ID, 
0);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                deliver.Head.CommandID 
= CMD_ERROR;
InBlock.gif                strError 
= ex.Message;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ((deliver.Head.CommandID == CMD_DELIVER) && (deliver.Registered_Delivery == 0))/**/////是短消息
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                RaiseEvent(SMS_STATE.DELIVER, deliver);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else if ((deliver.Head.CommandID == CMD_DELIVER) && (deliver.Registered_Delivery == 1))/**/////是状态报告
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                RaiseEvent(SMS_STATE.REPORT, deliver);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else//错误
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                RaiseEvent(SMS_STATE.DELIVER_ERROR, strError);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return deliver;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected bool DeliverResponse(UInt32 SequenceID, UInt64 Msg_Id, UInt32 Result)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bOK = true;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            CMPP_DELIVER_RESP resp 
= new CMPP_DELIVER_RESP();
InBlock.gif            resp.Head 
= new CMPP_HEAD();
InBlock.gif            resp.Head.CommandID 
= CMPP30.CMD_DELIVER_RESP;
InBlock.gif            resp.Head.SequenceID 
= SequenceID;
InBlock.gif            resp.Msg_Id 
= Msg_Id;
InBlock.gif            resp.Result 
= Result;
InBlock.gif
InBlock.gif            DATA_PACKAGE dp 
= new DATA_PACKAGE();
InBlock.gif            dp.SequenceID 
= resp.Head.SequenceID;
InBlock.gif            dp.Command 
= resp.Head.CommandID;
InBlock.gif            dp.SendCount 
= 0;
InBlock.gif            dp.Data 
= resp;
InBlock.gif            dp.Status 
= 1;
InBlock.gif
InBlock.gif            Monitor.Enter(m_MessageQueue);
InBlock.gif            m_MessageQueue.Enqueue(dp);
InBlock.gif            Monitor.Exit(m_MessageQueue);
InBlock.gif
InBlock.gif            
return bOK;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected bool ActiveTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bOK = true;
InBlock.gif            TimeSpan ts 
= DateTime.Now - m_dtLastTransferTime;
InBlock.gif            
if (ts.TotalSeconds > m_iActiveTestSpan)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CMPP_HEAD Head 
= new CMPP_HEAD();
InBlock.gif                Head.CommandID 
= CMPP30.CMD_ACTIVE_TEST;
InBlock.gif                Head.SequenceID 
= CreateID();
InBlock.gif                Head.TotalLength 
= 12;
InBlock.gif
InBlock.gif                DATA_PACKAGE dp 
= new DATA_PACKAGE();
InBlock.gif                dp.SequenceID 
= Head.SequenceID;
InBlock.gif                dp.Command 
= Head.CommandID;
InBlock.gif                dp.SendCount 
= 0;
InBlock.gif                dp.Data 
= Head;
InBlock.gif                dp.Status 
= 1;
InBlock.gif
InBlock.gif                Monitor.Enter(m_MessageQueue);
InBlock.gif                m_MessageQueue.Enqueue(dp);
InBlock.gif                Monitor.Exit(m_MessageQueue);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return bOK;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected CMPP_ACTIVE_TEST_RESP ReadActiveTestResponse(CMPP_HEAD Head)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CMPP_ACTIVE_TEST_RESP resp 
= new CMPP_ACTIVE_TEST_RESP();
InBlock.gif            resp.Head 
= Head;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            
bool bOK = true;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_NetworkStream.DataAvailable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Byte[] buffer 
= new Byte[resp.Head.TotalLength - Marshal.SizeOf(resp.Head)];
InBlock.gif                    m_NetworkStream.Read(buffer, 
0, buffer.Length);
InBlock.gif                    resp.Reserved 
= buffer[0];
InBlock.gif                    Monitor.Enter(SlidingWindow);
InBlock.gif                    
for (int i = 0; i < m_iSlidingWindowSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if ((SlidingWindow[i].Status == 1&&//已发送,等待回应
InBlock.gif
                            (SlidingWindow[i].SequenceID == resp.Head.SequenceID) &&//序列号相同
InBlock.gif
                            (SlidingWindow[i].Command == CMD_ACTIVE_TEST))//是ACTIVE_TEST
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            SlidingWindow[i].Status 
= 0;//清空窗口
InBlock.gif
                            break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    Monitor.Exit(SlidingWindow);
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                resp.Head.CommandID 
= CMD_ERROR;
InBlock.gif                strError 
= ex.Message;
InBlock.gif                bOK 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (bOK)
InBlock.gif                RaiseEvent(SMS_STATE.ACTIVE_TEST_RESPONSE, resp);
InBlock.gif            
else
InBlock.gif                RaiseEvent(SMS_STATE.ACTIVE_TEST_RESPONSE_ERROR, strError);
InBlock.gif
InBlock.gif            
return resp;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected bool ActiveTestResponse(UInt32 SequenceID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bOK = true;
InBlock.gif            CMPP_ACTIVE_TEST_RESP resp 
= new CMPP_ACTIVE_TEST_RESP();
InBlock.gif            resp.Head 
= new CMPP_HEAD();
InBlock.gif            resp.Head.CommandID 
= CMPP30.CMD_ACTIVE_TEST_RESP;
InBlock.gif            resp.Head.SequenceID 
= SequenceID;
InBlock.gif            resp.Reserved 
= 0;
InBlock.gif
InBlock.gif            DATA_PACKAGE dp 
= new DATA_PACKAGE();
InBlock.gif            dp.SequenceID 
= resp.Head.SequenceID;
InBlock.gif            dp.Command 
= resp.Head.CommandID;
InBlock.gif            dp.SendCount 
= 0;
InBlock.gif            dp.Data 
= resp;
InBlock.gif            dp.Status 
= 1;
InBlock.gif
InBlock.gif            Monitor.Enter(m_MessageQueue);
InBlock.gif            m_MessageQueue.Enqueue(dp);
InBlock.gif            Monitor.Exit(m_MessageQueue);
InBlock.gif
InBlock.gif            
return bOK;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
公有方法#region 公有方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 开始启动发送和接收线程
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void StartThread()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (m_SendThread == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_dtLastTransferTime 
= DateTime.Now;
InBlock.gif                m_SendThread 
= new Thread(new ThreadStart(this.SendThreadProc));
InBlock.gif                m_SendThread.IsBackground 
= true;
InBlock.gif                m_SendThread.Name 
= m_strSPID + "_Send";
InBlock.gif                m_SendThread.Start();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (m_ReceiveThread == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_ReceiveThread 
= new Thread(new ThreadStart(this.ReceiveThreadProc));
InBlock.gif                m_ReceiveThread.IsBackground 
= true;
InBlock.gif                m_ReceiveThread.Name 
= m_strSPID + "_Receive";
InBlock.gif                m_ReceiveThread.Start();
ExpandedSubBlockEnd.gif            }

InBlock.gif            m_eventConnect.Set();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 结束发送和接收线程
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void EndThread()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_eventSend.Reset();
InBlock.gif            m_eventReceive.Reset();
InBlock.gif            m_eventReceiveExit.Set();
InBlock.gif            m_eventSendExit.Set();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// SubMit发送函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Message">信息内容</param>
InBlock.gif        
/// <param name="Destination">接收短信的MSISDN号码</param>
InBlock.gif        
/// <param name="Source">SP的服务代码</param>
InBlock.gif        
/// <param name="ServiceID">业务标识,是数字、字母和符号的组合</param>
InBlock.gif        
/// <param name="Coding">信息格式:    0:ASCII串;3:短信写卡操作4:二进制信息;8:UCS2编码;15:含GB汉字dot.gif dot.gif</param>
InBlock.gif        
/// <param name="NeedReport">是否要求返回状态确认报告:    0:不需要;    1:需要。</param>
InBlock.gif        
/// <param name="FeeUserType">计费用户类型字段:0:对目的终端MSISDN计费;1:对源终端MSISDN计费;2:对SP计费;3:表示本字段无效,对谁计费参见Fee_terminal_Id字段。</param>
InBlock.gif        
/// <param name="FeeType">资费类别:01:对“计费用户号码”免费;02:对“计费用户号码”按条计信息费;03:对“计费用户号码”按包月收取信息费。</param>
InBlock.gif        
/// <param name="InfoFee">资费代码(以分为单位)</param>
InBlock.gif        
/// <param name="FeeUser">被计费用户的号码,当Fee_UserType为3时该值有效,当Fee_UserType为0、1、2时该值无意义</param>
InBlock.gif        
/// <param name="LinkID">点播业务使用的LinkID,非点播类业务的MT流程不使用该字段</param>
ExpandedSubBlockEnd.gif        
/// <returns>是否发送成功</returns>

InBlock.gif        public bool Submit(string Message, string[] Destination, string Source, string ServiceID, CODING Coding, bool NeedReport, byte FeeUserType, byte FeeType, int InfoFee, string FeeUser, string LinkID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool bOK = true;
InBlock.gif            
string strError = string.Empty;
InBlock.gif            CMPP_SUBMIT submit 
= new CMPP_SUBMIT();
InBlock.gif            submit.Head 
= new CMPP_HEAD();
InBlock.gif            submit.Head.CommandID 
= CMPP30.CMD_SUBMIT;
InBlock.gif            submit.Head.SequenceID 
= CreateID();
InBlock.gif            submit.Msg_ID 
= 0;
InBlock.gif            submit.Pk_Total 
= 1;
InBlock.gif            submit.Pk_Number 
= 1;
InBlock.gif            submit.Registered_Delivery 
= Convert.ToByte(NeedReport);
InBlock.gif            submit.Msg_Level 
= 0;
InBlock.gif            submit.Service_Id 
= ServiceID;
InBlock.gif            submit.Fee_UserType 
= FeeUserType;
InBlock.gif            submit.Fee_Terminal_Id 
= FeeUser;
InBlock.gif            submit.Fee_Terminal_Type 
= 0;//真实号码
InBlock.gif
            submit.TP_Pid = 0;
InBlock.gif            submit.TP_Udhi 
= 0;
InBlock.gif            submit.Msg_Fmt 
= (Byte)Coding;
InBlock.gif            submit.Msg_Src 
= m_strSPID;
InBlock.gif            submit.FeeType 
= string.Format("{0:d2}", FeeType);
InBlock.gif            submit.FeeCode 
= InfoFee.ToString();
InBlock.gif            submit.Valid_Time 
= string.Empty;
InBlock.gif            submit.At_Time 
= string.Empty;
InBlock.gif            submit.Src_Id 
= Source;
InBlock.gif            submit.DestUsr_Tl 
= (Byte)Destination.Length;
InBlock.gif            submit.Dest_Terminal_ID 
= Destination;
InBlock.gif            submit.Dest_Terminal_Type 
= 0;//真实号码
InBlock.gif
            submit.Msg_Length = (Byte)Utility.CountLength(Message, Coding);
InBlock.gif            submit.Msg_Content 
= Message;
InBlock.gif            submit.LinkID 
= LinkID;
InBlock.gif
InBlock.gif
InBlock.gif            DATA_PACKAGE dp 
= new DATA_PACKAGE();
InBlock.gif            dp.SequenceID 
= submit.Head.SequenceID;
InBlock.gif            dp.Command 
= submit.Head.CommandID;
InBlock.gif            dp.SendCount 
= 0;
InBlock.gif            dp.Data 
= submit;
InBlock.gif            dp.Status 
= 1;
InBlock.gif
InBlock.gif            Monitor.Enter(m_MessageQueue);
InBlock.gif            m_MessageQueue.Enqueue(dp);
InBlock.gif            Monitor.Exit(m_MessageQueue);
InBlock.gif
InBlock.gif            
return bOK;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 异步发送回调函数
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public delegate bool CMPPAsyncSubmit(string Message, string[] Destination, string Source, string ServiceID, CODING Coding,
InBlock.gif                                        
bool NeedReport, byte FeeUserType, byte FeeType, int InfoFee, string FeeUser, string LinkID);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif

转载于:https://www.cnblogs.com/EndPoint/archive/2007/01/31/635506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值