⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmppclient.cs

📁 cmpp3.0网关开发
💻 CS
📖 第 1 页 / 共 5 页
字号:
            this.addToOutQueue(q);
            return (seq); //返回终止消息,便于等待
        }


        public uint CancelMsg(string msgid)
        {
            uint seq = this.getNextSequence();
            MSG.CMPP_MSG_CANCEL cancel = new MSG.CMPP_MSG_CANCEL(seq);
            cancel.MsgID = msgid;
            QueueItem q = new QueueItem(seq, (uint)MSG.CMPP_COMMAND_ID.CMPP_CANCEL, 0, 0);
            q.setmsgObj(cancel);
            this.addToOutQueue(q);
            return (seq);   //返回消息的内部编号
        }

        public void StopMe()
        {
            if (!this.isStop)
            {
                if (this.tcpIsCanUse())//发送一条对服务器的通告
                {
                    uint seq = this.getNextSequence();
                    MSG.CMPP_MSG_TERMINATE t = new MSG.CMPP_MSG_TERMINATE(seq);
                    QueueItem q = new QueueItem(seq, (uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE, 0, 0);
                    q.setmsgObj(t);
                    this.addToOutQueue(q);
                }
                Thread.Sleep(500);   //等待1000ms,告知服务器
                this._StopMe();
                tcp.Close();

                if (this.onClientSvcStopedHandler != null)
                {
                    ClientQueueStateArgs arg = new ClientQueueStateArgs(this._outSeqQueue, this._waitingSeqQueue);
                    onClientSvcStopedHandler(this, arg);
                }
                else
                {
                    this.defaultClientStopEventHandler();
                }
                Thread.Sleep(500);   //再次主动等待线程结束          
                //此处报告 2个队列中的信息    
            }
            //准备强行结束
            _forcedSubThread(this.Send_Thread);
            Thread.Sleep(500);   //等待1000ms,告知服务器
            _forcedSubThread(this.Recv_Thread);
            Thread.Sleep(500);   //等待1000ms,告知服务器
            _forcedSubThread(this.Deamo_Thread);
            Thread.Sleep(500);   //等待1000ms,告知服务器
        }

        public string getLogInfo()
        {
            string t = this.ErrorInfo;
            this.ErrorInfo = "";
            return (t);
        }

        public int getQueueItemState(uint seq)  //根据seq寻找发送内部队列的消息对象的状态
        {
            int status = 0;       //状态未知
            if (this._outSeqQueue.ContainsKey(seq)) //存在于outSeqQueue中
            {
                if (this._waitingSeqQueue.Contains(seq))
                {
                    //正在发送等待返回,状态未定
                }
                else
                {
                    //还没有发送
                }
            }
            else
            {
                if (this._waitingSeqQueue.ContainsKey(seq))
                {
                    //正等待回应
                }
                else
                {
                    //已经发送结束了
                }
            }
            return (status);
        }

        public void TestSubmit(string[] nums, int topI, int topJ)  //发送测试包
        {
            int count = 0;
            int total = 0;
            ArrayList pks = new ArrayList();
            //准备100个包
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    uint seq = this.getNextSequence(); //准备流水号 
                    MSG.CMPP_MSG_SUBMIT sndmsg = new MSG.CMPP_MSG_SUBMIT(seq);
                    sndmsg.FeeCode = "000001";
                    sndmsg.FeeTerminalId = nums[i];
                    sndmsg.FeeType = MSG.FeeType.FEE_TERMINAL_PERITEM; //按条收取
                    sndmsg.FeeUserType = 0;  //终端用户计费
                    sndmsg.Msg_Level = 0;
                    sndmsg.MSGFormat = (uint)MSG.Msg_Format.UCS2;
                    sndmsg.SMS_Content = "test";
                    sndmsg.SrcID = "09880";         //长号码
                    sndmsg.SPID = this.systemID;
                    sndmsg.Svc_Code = "cmcctest";
                    sndmsg.UDHI = 0;
                    sndmsg.ValIdTime = getValIdTime(DateTime.Now);        //存活时间
                    sndmsg.addTerminalID(nums[i]);
                    pks.Add(sndmsg.toBytes());   //存入数组
                }
            }

            DateTime t1 = DateTime.Now;
            for (int i = 0; i < topI; i++)
            {
                for (int j = 0; j < topJ; j++)
                {
                    try
                    {
                        tcp.Send((byte[])pks[i * 10 + j]);
                        count++;
                        total++;
                    }
                    catch (SocketException se)
                    {
                        this.ErrorInfo = this.ErrorInfo + "\r\n" + "发送错误: " + se.ToString();
                    }
                    if (count >= 16)
                    {
                        count = 0;  //复位
                        Thread.Sleep(50);  //暂停20ms 
                    }
                }
            }
            DateTime t2 = DateTime.Now;
            TimeSpan t = t2 - t1;
            this.ErrorInfo = this.ErrorInfo + "\r\n" + "发送: " + total + " 条消息, 总计花费时间:" + t.TotalMilliseconds + "毫秒";
        }  //测试函数////////////////////////////////////////////////供测试移动网络测试

        //测试翰数区域/////////////////////////////////////////// 
        //公用函数 属性区域//////////////////////////////////////// 
    }
    //*************工具类结束***********************************
    enum MSG_STATE     //CMPP消息在队列中的状态枚举值
    {
        NEW = 0,      //加入到队列等待发送出去
        SENDING = 1,     //正被某个线程锁定
        SENDED_WAITTING = 2,   //发送出去,现在等待resp消息返回
        SENDING_FINISHED = 3   //得到回应,一般等待被清理出队列
    }

    public class QueueItem   //代表一个存储在缓存队列中的消息,序列号由CMPPClient产生
    {
        uint _sequence;    //消息索引 就是流水号
        uint _msgType;    //消息的类别就是COMMAND_ID,根据此值决定 Object _msgObj的原类型
        int _failedCount = 0;   //失败计数,如果失败次数超过3此需要进行清理
        int _msgState;    //当前消息状态,具体为 MSG_STATE的枚举类型
        object _msgObj;    //存放消息对象,具体类型参考 _msgType
        DateTime _inQueueTime;  //消息进入队列的时间,上次消息的发送时间

        public int FailedCount
        {
            set
            {
                this._failedCount = value;
            }
            get
            {
                return (this._failedCount);
            }

        }

        public object getMsgObj()
        {
            return (this._msgObj);
        }
        public void setmsgObj(object inmsg)
        {
            this._msgObj = inmsg;
        }
        public DateTime inQueueTime
        {
            set
            {
                this._inQueueTime = value;
            }
            get
            {
                return (this._inQueueTime);
            }
        }
        public uint MsgType
        {
            get
            {
                return (this._msgType);
            }
        }
        public int MsgState
        {
            get
            {
                return (this._msgState);
            }
            set
            {
                this._msgState = value;
            }
        }
        public uint Sequence
        {
            get
            {
                return (this._sequence);
            }
            set
            {
                this._sequence = value;
            }
        }
        public QueueItem(uint sequence, uint msgtype, int faildedcount, int msgstate)
        {
            this._failedCount = faildedcount;
            this._msgState = msgstate;
            this._msgType = msgtype;
            this._sequence = sequence;
        }
        public QueueItem(uint sequence, uint msgtype, int faildedcount, int msgstate, object msgobj)
        {
            this._failedCount = faildedcount;
            this._msgState = msgstate;
            this._msgType = msgtype;
            this._sequence = sequence;
            this.setmsgObj(msgobj);
        }
    }

    public class BIConvert  //字节 整形 转换类 网络格式转换为内存格式
    {
        public static byte[] Int2Bytes(uint i)  //转换整形数据网络次序的字节数组
        {
            byte[] t = BitConverter.GetBytes(i);
            byte b = t[0];
            t[0] = t[3];
            t[3] = b;
            b = t[1];
            t[1] = t[2];
            t[2] = b;
            return (t);
        }

        public static uint Bytes2UInt(byte[] bs, int startIndex) //返回字节数组代表的整数数字,4个数组
        {
            byte[] t = new byte[4];
            for (int i = 0; i < 4 && i < bs.Length - startIndex; i++)
            {
                t[i] = bs[startIndex + i];
            }
            byte b = t[0];
            t[0] = t[3];
            t[3] = b;
            b = t[1];
            t[1] = t[2];
            t[2] = b;
            return (BitConverter.ToUInt32(t, 0));
        }

        public static uint Bytes2UInt(byte[] bs)  //没有指定起始索引
        {
            return (Bytes2UInt(bs, 0));
        }

        public static void DumpBytes(byte[] bs, string txt)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(txt);
            for (int i = 0; i < bs.Length; i++)
            {
                byte b = bs[i];
                sw.WriteLine(b.ToString("X") + " ");
            }
            sw.WriteLine("-----" + DateTime.Now.ToLocalTime());
            sw.Close();
        }

        public static void DebugString(string bs, string txt)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(txt)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -