namespace AsiaINFO.SMS.APPCMPP2.MyThread.CMPPThread { using AsiaINFO.SMS.APPCMPP2; using AsiaINFO.SMS.APPCMPP2.MyThread; using AsiaINFO.SMS.CMPP2; using AsiaINFO.SMS.Entity; using System; using System.Collections; using System.Collections.Generic; using System.Threading; using System.IO; public class ReceiveThread : CMPPThreadBase { private Queue _cmppReceiveQueue; private SyncEvents _moinfoEvents; private Queue _moinfoQueue; private SyncEvents _reportEvents; private Queue _reportQueue; private SyncEvents _submitRespEvents; private Queue _submitRespQueue; public ReceiveThread(CMPPClient cmppClient, SyncEvents syncEvents, Queue cmppReceiveQueue, Queue moinfoQueue, SyncEvents moinfoEvents, Queue reportQueue, SyncEvents reportEvents, Queue submitRespQueue, SyncEvents submitRespEvents) : base(cmppClient, syncEvents) { if ((((cmppReceiveQueue == null) || (moinfoQueue == null)) || ((moinfoEvents == null) || (reportQueue == null))) || (((reportEvents == null) || (submitRespQueue == null)) || (submitRespEvents == null))) { throw new Exception("ReceiveThread参数不能为空!"); } this._cmppReceiveQueue = cmppReceiveQueue; this._moinfoQueue = moinfoQueue; this._moinfoEvents = moinfoEvents; this._reportQueue = reportQueue; this._reportEvents = reportEvents; this._submitRespQueue = submitRespQueue; this._submitRespEvents = submitRespEvents; } private void DealActive(uint seqId) { base._cmppClient.ActiveResp(seqId); //base.OnMsg("收到CMPP_ACTIVE_TEST 并返回 CMPP_ACTIVE_TEST_RESP", MsgLevel.Debug); } private void DealActiveResp() { ShareData.ActiveSeq = 0; //base.OnMsg("收到CMPP_ACTIVE_TEST_RESP", MsgLevel.Debug); } private void DealConnResp(byte[] data) { CMPP_CONNECT_RESP cmpp_connect_resp = new CMPP_CONNECT_RESP(data); base._cmppClient.IsConnected = cmpp_connect_resp.Status == 0; base.OnMsg(StateDictionary.ConnResDictionary(cmpp_connect_resp.Status), MsgLevel.Msg); //base.OnMsg(string.Format("收到CMPP_CONNECT_RESP 返回值={0}", cmpp_connect_resp.Status), MsgLevel.Debug); } private void DealDeliver(byte[] data) { CMPP_DELIVER cmpp_deliver = new CMPP_DELIVER(data); base.OnMsg("收到状态报告或收到MO,正在处理... 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), MsgLevel.Debug); base._cmppClient.DeliverResp(cmpp_deliver.Msg_Id, 0, cmpp_deliver.Header.Sequence_Id); try { base.OnMsg("RECEIVE:" + cmpp_deliver.Registered_Delivery, MsgLevel.Msg); if (cmpp_deliver.Registered_Delivery == 0) { MOInfo info = new MOInfo(Guid.NewGuid().ToString(), cmpp_deliver.Msg_Id, DateTime.Now, cmpp_deliver.Dest_Id, cmpp_deliver.Service_Id, cmpp_deliver.Src_terminal_Id, ShareData.CurrentISMG, cmpp_deliver.Msg_Content, "", SysConf.Instance.ProgramID); this._moinfoData = info; this._moinfoEvents.NewItemEvent.Set(); string msg = string.Format("收到MO信息 手机号码=[{0}] 目的号码=[{1}] 内容=[{2}] 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), info.MOBILE_NO, info.LONG_SERV_NO, info.SMS_CONTENT); base.OnMsg(msg, MsgLevel.Msg); } else { CMPP_Msg_Content content = new CMPP_Msg_Content(cmpp_deliver.Report); bool success = false; if (content.Stat == "DELIVRD") { success = true; } ReportInfo info2 = new ReportInfo(content.Msg_Id, content.Stat, success, content.Done_time, DateTime.Now, content.Dest_terminal_Id); this._reportData = info2; this._reportEvents.NewItemEvent.Set(); string str2 = string.Format("收到Report信息 MsgID={0} State={1} mobile={2} 时间:", info2.MSGID, info2.STAT, info2.MOBILE_NO); base.OnMsg(str2, MsgLevel.Debug); } } catch (Exception exception) { string.Format("收到DELIVER数据错误", new object[0]); try { string.Format("收到DELIVER数据错误 DELIVER: {0}", cmpp_deliver.ToString()); } catch { } base.OnMsg("[收到DELIVER数据错误]:" + exception.Message, MsgLevel.Err); } } private void DealSubmitResp(byte[] data) { this._submitRespData = data; this._submitRespEvents.NewItemEvent.Set(); } private void DealTerminate() { base._cmppClient.IsConnected = false; base.OnMsg("服务器主动断开连接", MsgLevel.Msg); } private void DistributeData(byte[] data) { try { base.OnMsg("收到消息了!", MsgLevel.Debug); MessageEventArgs args = new MessageEventArgs(data); MessageHeader header = args.Header; byte[] array = new byte[header.Total_Length]; header.ToBytes().CopyTo(array, 0); args.BodyData.CopyTo(array, 12); if (header.Command_Id != CMPP_Command_Id.CMPP_ACTIVE_TEST_RESP || header.Command_Id != CMPP_Command_Id.CMPP_ACTIVE_TEST) { base.OnMsg("返回的commandId:" + header.Command_Id + " 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), MsgLevel.Debug); } switch (header.Command_Id) { case CMPP_Command_Id.CMPP_CONNECT_RESP: this.DealConnResp(data); return; //case ((CMPP_Command_Id)(-2147483647)): // this.DealConnResp(data); // return; //case ((CMPP_Command_Id) (-2147483646)): //case ((CMPP_Command_Id) (-2147483645)): //case ((CMPP_Command_Id) (-2147483643)): //case ((CMPP_Command_Id) (-2147483642)): //case ((CMPP_Command_Id) (-2147483641)): // return; case CMPP_Command_Id.CMPP_SUBMIT_RESP: this.DealSubmitResp(data); return; //case ((CMPP_Command_Id) (-2147483644)): // this.DealSubmitResp(data); // return; case CMPP_Command_Id.CMPP_ACTIVE_TEST_RESP: this.DealActiveResp(); return; //case ((CMPP_Command_Id) (-2147483640)): // this.DealActiveResp(); // return; case CMPP_Command_Id.CMPP_ACTIVE_TEST: this.DealActive(header.Sequence_Id); return; case CMPP_Command_Id.CMPP_TERMINATE: break; case CMPP_Command_Id.CMPP_DELIVER: this.DealDeliver(data); return; default: this.DealConnResp(data); return; } this.DealTerminate(); } catch (Exception exception) { base.OnMsg(exception.Message, MsgLevel.Err); } } protected override void Run() { byte[] data = null; while (WaitHandle.WaitAny(base._syncEvents.EventArray) != 1) { Label_0004: data = this._cmppReceiveData; if (data != null) { this.DistributeData(data); goto Label_0004; } } base.Stop(); } public override void Stop() { if ((base._thread != null) && (base._thread.ThreadState == ThreadState.Suspended)) { base.StartRun(); } base._syncEvents.ExitThreadEvent.Set(); } private byte[] _cmppReceiveData { get { lock (((ICollection)this._cmppReceiveQueue).SyncRoot) { if (this._cmppReceiveQueue.Count > 0) { return this._cmppReceiveQueue.Dequeue(); } } return null; } } private MOInfo _moinfoData { get { lock (((ICollection)this._moinfoQueue).SyncRoot) { if (this._moinfoQueue.Count > 0) { return this._moinfoQueue.Dequeue(); } } return null; } set { lock (((ICollection)this._moinfoQueue).SyncRoot) { this._moinfoQueue.Enqueue(value); } } } private ReportInfo _reportData { get { lock (((ICollection)this._reportQueue).SyncRoot) { if (this._reportQueue.Count > 0) { return this._reportQueue.Dequeue(); } } return null; } set { lock (((ICollection)this._reportQueue).SyncRoot) { this._reportQueue.Enqueue(value); } } } private byte[] _submitRespData { get { lock (((ICollection)this._submitRespQueue).SyncRoot) { if (this._submitRespQueue.Count > 0) { return this._submitRespQueue.Dequeue(); } } return null; } set { lock (((ICollection)this._submitRespQueue).SyncRoot) { this._submitRespQueue.Enqueue(value); } } } } }