Ejemplo n.º 1
0
        /// <summary>
        /// 获取 CMPP_SUBMIT 的状态报告(只有在 CMPP_SUBMIT 中的 RegisteredDelivery 被设置为1时,ISMG才会向SP发送状态报告)。
        /// </summary>
        /// <returns></returns>
        public CMPP_REPORT GetReport()
        {
            CMPP_REPORT Report = new CMPP_REPORT();

            if (RegisteredDelivery == 1)//是状态报告
            {
                Byte[] bytes = System.Convert.FromBase64String(MsgContent);
                if ((bytes != null) && (bytes.Length > 0))
                {
                    Report.Init(bytes);
                }
            }
            return(Report);
        }
Ejemplo n.º 2
0
        void _sc_DataReceived(byte[] data)
        {
            int headSize = 0;

            Byte[]    buffer = null;
            CMPP_HEAD head;

            try
            {
                if (data.Length > 0)
                {
                    buffer = new byte[12];
                    Buffer.BlockCopy(data, 0, buffer, 0, buffer.Length);
                    head.TotalLength = Convert.ToUInt32(buffer, 0);
                    head.CommandID   = Convert.ToUInt32(buffer, 4);
                    head.SequenceID  = Convert.ToUInt32(buffer, 8);
                }
                else
                {
                    OnNetworkError(new Exception("Head data error."));
                    return;
                }
            }
            catch (Exception ex)
            {
                OnNetworkError(ex);
                return;
            }

            switch ((CMPP_COMMAND)head.CommandID)
            {
            case CMPP_COMMAND.CMD_SUBMIT_RESP:
                CMPP_SUBMIT_RESP submit_resp = new CMPP_SUBMIT_RESP();
                submit_resp.Head = head;

                try
                {
                    headSize = Marshal.SizeOf(head);
                    buffer   = new byte[head.TotalLength - headSize];
                    Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length);
                    submit_resp.MsgID  = Convert.ToUInt64(buffer, 0);
                    submit_resp.Result = Convert.ToUInt32(buffer, 8);
                }
                catch (Exception ex)
                {
                    OnNetworkError(ex);
                    return;
                }
                // DebugLog.Instance.Write<CMPP_SUBMIT_RESP>("Client Received -> CMPP_SUBMIT_RESP", submit_resp);
                MessageTools.MessageHelper.Instance.WirteInfo("Client Received -> CMPP_SUBMIT_RESP" + submit_resp.ToString());
                SlidingWindow send = SWGet(head.SequenceID);
                if (send.Status == WindowStatus.Idle)
                {
                    return;
                }

                //流控
                if (submit_resp.Result == 8)
                {
                    SWClr(head.SequenceID);
                    send.SendTime       = DateTime.Now;
                    send.MSG.SequenceID = Sequence.Instance.CreateID();
                    Send(send.MSG);
                    return;
                }

                if (SendEvent != null)
                {
                    send.Report.Succeed = false;
                    switch (submit_resp.Result)
                    {
                    case 0:
                        send.Report.Succeed  = true;
                        send.Report.Describe = "等待报告";
                        break;

                    case 1:
                        send.Report.Describe = "消息结构错";
                        break;

                    case 2:
                        send.Report.Describe = "命令字错";
                        break;

                    case 3:
                        send.Report.Describe = "消息序号重复";
                        break;

                    case 4:
                        send.Report.Describe = "消息长度错";
                        break;

                    case 5:
                        send.Report.Describe = "资费代码错";
                        break;

                    case 6:
                        send.Report.Describe = "超过最大信息长";
                        break;

                    case 7:
                        send.Report.Describe = "业务代码错";
                        break;

                    case 8:
                        send.Report.Describe = "流量控制错";
                        break;

                    case 9:
                        send.Report.Describe = "本网关不负责服务此计费号码";
                        break;

                    case 10:
                        send.Report.Describe = "源号码错误";
                        break;

                    case 11:
                        send.Report.Describe = "短信内容错误";
                        break;

                    case 12:
                        send.Report.Describe = "被计费号码错误";
                        break;

                    case 13:
                        send.Report.Describe = "终端号码错误";
                        break;
                    }
                    send.Report.Serial = submit_resp.MsgID.ToString();
                    SendEvent(this, send.Report);
                }
                //清除窗口
                SWClr(head.SequenceID);
                break;

            case CMPP_COMMAND.CMD_CANCEL_RESP:
                CMPP_CANCEL_RESP cancel_resp = new CMPP_CANCEL_RESP();
                cancel_resp.Head = head;
                try
                {
                    headSize = Marshal.SizeOf(head);
                    buffer   = new byte[head.TotalLength - headSize];
                    Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length);
                    cancel_resp.SuccessID = Convert.ToUInt32(buffer, 0);
                }
                catch (Exception ex)
                {
                    OnNetworkError(ex);
                    return;
                }
                // DebugLog.Instance.Write<CMPP_CANCEL_RESP>("Client Received -> CMPP_CANCEL_RESP", cancel_resp);
                break;

            case CMPP_COMMAND.CMD_CONNECT_RESP:
                CMPP_CONNECT_RESP connect_resp = new CMPP_CONNECT_RESP();
                connect_resp.Head = head;
                try
                {
                    headSize = Marshal.SizeOf(head);
                    buffer   = new byte[head.TotalLength - headSize];
                    Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length);
                    connect_resp.Status            = Convert.ToUInt32(buffer, 0);
                    connect_resp.AuthenticatorISMG = new Byte[16];
                    Array.Copy(buffer, 4, connect_resp.AuthenticatorISMG, 0, 16);
                    connect_resp.Version = buffer[buffer.Length - 1];
                }
                catch (Exception ex)
                {
                    OnNetworkError(ex);
                    return;
                }
                // DebugLog.Instance.Write<CMPP_CONNECT_RESP>("Client Received -> CMPP_CONNECT_RESP", connect_resp);
                switch (connect_resp.Status)
                {
                case 0:
                    _connect = true;
                    autoConnectEvent.Set();
                    OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT, "登录成功"));
                    break;

                default:
                    switch (connect_resp.Status)
                    {
                    case 1:
                        OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "消息结构错"));
                        break;

                    case 2:
                        OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "非法源地址"));
                        break;

                    case 3:
                        OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "认证错"));
                        break;

                    case 4:
                        OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "版本太高"));
                        break;

                    default:
                        OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, string.Format("其他错误(错误码:{0})", connect_resp.Status)));
                        break;
                    }
                    OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "错误代码 " + connect_resp.Status));
                    break;
                }
                break;

            case CMPP_COMMAND.CMD_ACTIVE_TEST_RESP:
                CMPP_ACTIVE_TEST_RESP resp = new CMPP_ACTIVE_TEST_RESP();
                resp.Head = head;

                try
                {
                    headSize = Marshal.SizeOf(head);
                    buffer   = new byte[head.TotalLength - headSize];
                    Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length);
                    resp.Reserved = buffer[0];
                }
                catch (Exception ex)
                {
                    OnNetworkError(ex);
                    return;
                }
                //     DebugLog.Instance.Write<CMPP_ACTIVE_TEST_RESP>("Client Received -> CMPP_ACTIVE_TEST_RESP", resp);
                //SWClr(head.SequenceID);
                break;

            case CMPP_COMMAND.CMD_ACTIVE_TEST:
                // DebugLog.Instance.Write("Client Received -> CMD_ACTIVE_TEST");
                MessageTools.MessageHelper.Instance.WirteTest("Client Received -> CMD_ACTIVE_TEST");
                CMPP_ACTIVE_TEST_RESP active_test_resp = new CMPP_ACTIVE_TEST_RESP();
                active_test_resp.Head            = new CMPP_HEAD();
                active_test_resp.Head.CommandID  = (uint)CMPP_COMMAND.CMD_ACTIVE_TEST_RESP;
                active_test_resp.Head.SequenceID = head.SequenceID;
                active_test_resp.Reserved        = 0;
                Send(active_test_resp);
                MessageTools.MessageHelper.Instance.WirteTest("Client Send -> CMPP_ACTIVE_TEST_RESP");
                //     DebugLog.Instance.Write<CMPP_ACTIVE_TEST_RESP>("Client Send -> CMPP_ACTIVE_TEST_RESP", active_test_resp);
                break;

            case CMPP_COMMAND.CMD_DELIVER:
                CMPP_DELIVER deliver = new CMPP_DELIVER();
                deliver.Head = head;
                try
                {
                    headSize = Marshal.SizeOf(head);
                    buffer   = new byte[head.TotalLength - headSize];
                    Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length);
                    deliver.Init(buffer);
                }
                catch (Exception ex)
                {
                    OnNetworkError(ex);
                    return;
                }
                //     DebugLog.Instance.Write<CMPP_DELIVER>("Client Received -> CMPP_DELIVER", deliver);
                CMPP_DELIVER_RESP deliver_resp = new CMPP_DELIVER_RESP();
                deliver_resp.Head            = new CMPP_HEAD();
                deliver_resp.Head.CommandID  = (uint)CMPP_COMMAND.CMD_DELIVER_RESP;
                deliver_resp.Head.SequenceID = deliver.Head.SequenceID;
                deliver_resp.MsgID           = deliver.MsgID;
                deliver_resp.Result          = 0;
                Send(deliver_resp);
                //   DebugLog.Instance.Write<CMPP_DELIVER_RESP>("Client Send -> CMPP_DELIVER_RESP", deliver_resp);

                if (deliver.RegisteredDelivery == 1)
                {
                    ////状态报告
                    if (ReportEvent != null)
                    {
                        CMPP_REPORT report        = deliver.GetReport();
                        bool        result        = false;
                        string      resultMessage = "";
                        ushort      resultCode    = ((ushort)PlatformCode.CMPP + (ushort)SystemCode.ReportBack);

                        DateTime time;
                        try
                        {
                            time = new DateTime(DateTime.Now.Year, System.Convert.ToInt32(report.DoneTime.Substring(2, 2)), System.Convert.ToInt32(report.DoneTime.Substring(4, 2)), System.Convert.ToInt32(report.DoneTime.Substring(6, 2)), System.Convert.ToInt32(report.DoneTime.Substring(8, 2)), 0);
                        }
                        catch
                        {
                            time = DateTime.Now;
                        }
                        switch (report.Stat.ToUpper())
                        {
                        case "DELIVRD":
                        case "RETRIEV":
                            result = true;
                            break;

                        case "EXPIRED":
                        case "DELETED":
                        case "UNDELIV":
                        case "ACCEPTD":
                        case "UNKNOWN":
                        case "REJECTD":
                        default:
                            resultMessage = report.Stat;
                            break;
                        }
                        ReportEventArgs args = new ReportEventArgs(report.MsgID.ToString(), result, resultCode, resultMessage, time);
                        ReportEvent(this, args);
                    }
                }
                else
                {
                    if (DeliverEvent != null)
                    {
                        DeliverEvent(this, new DeliverEventArgs(deliver.MsgID.ToString(), DateTime.Now, deliver.MsgContent, deliver.SrcTerminalID, deliver.DestID, deliver.ServiceID.ToString()));
                    }
                }
                break;

            case CMPP_COMMAND.CMPP_QUERY_RESP:
                CMPP_QUERY_RESP query_resp = new CMPP_QUERY_RESP();
                query_resp.Head = head;
                try
                {
                    headSize = Marshal.SizeOf(head);
                    buffer   = new byte[head.TotalLength - headSize];
                    Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length);
                    query_resp.Init(buffer);
                }
                catch (Exception ex)
                {
                    OnNetworkError(ex);
                    return;
                }
                //   DebugLog.Instance.Write<CMPP_QUERY_RESP>("Client Received -> CMPP_QUERY_RESP", query_resp);
                break;

            case CMPP_COMMAND.CMD_TERMINATE:
                //      DebugLog.Instance.Write("Client Received -> CMD_TERMINATE");
                _connect = false;
                Thread.Sleep(100);
                break;

            case CMPP_COMMAND.CMD_ERROR:
                //      DebugLog.Instance.Write("Client Received -> CMD_ERROR");
                _connect = false;
                Thread.Sleep(100);
                break;
            }
        }