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

📄 cmppclient.cs

📁 cmpp3.0网关开发
💻 CS
📖 第 1 页 / 共 5 页
字号:
                {
                    if (this.isPingTime())
                    {
                        this.ping();  //发送一个ping包 
                    }
                    if (t_count > 50)  // 500*100=50000=50秒
                    {
                        t_count = 0;
                        checkReSend(); //检查需要重新发送的消息
                        //触发一个事件,让系统自动检查消息队列,存储消息队列中的消息状态
                    }
                }
                else
                {
                    EventArgs e = new EventArgs();
                    if (this.onSocketClosedHandler != null)
                    {
                        onSocketClosedHandler(this, e);
                    }
                    else
                    {
                    }
                    this.isStop = true;  //通知其他线程退出
                }
                Thread.Sleep(1000);
            }
        }

        private void SendSPMsgThread()
        {
            while (!this.isStop)
            {
                Thread.Sleep(10);
                if (this.isLogin)
                {
                    ArrayList lists = this.getTop16Queue();  //取出16条最顶的消息     
                    if (lists != null && lists.Count > 0)
                    {
                        int count = lists.Count;
                        ArrayList pks = new ArrayList(count); //定义容量
                        for (int i = 0; i < lists.Count; i++)
                        {
                            QueueItem outitem = (QueueItem)lists[i]; //取出每一个消息对象
                            if (outitem != null)
                            {
                                try
                                {
                                    sendQueueItem(outitem);    //发送每一个消息
                                }
                                catch (SocketException se)
                                {
                                    //发送失败
                                    outitem.FailedCount++;
                                }
                            }
                        }
                    }
                }
                Thread.Sleep(100);
            }
        }

        private void _StopMe()
        {
            lock (this)
            {
                this.isStop = true;
            }
        }

        private void _forcedSubThread(Thread t)   //强制停止线程
        {
            try
            {
                t.Abort();
                t.Join();
            }
            catch (Exception)
            { }
        }

        //private 函数区域//////////////////////////////////////////////////////////////////


        //公用函数 属性区域//////////////////////////////////////// 
        public bool Init(string CMPPServer, int CMPPPort)
        {
            return (this._init(CMPPServer, CMPPPort));
        }

        public bool Init(string CMPPServer, int CMPPPort, int recvtimeout, int sendtimeout)
        {
            this.RecvTimeOut = recvtimeout;
            this.SendTimeout = sendtimeout;
            return (this._init(CMPPServer, CMPPPort));
        }

        public bool Init(string CMPPServer, int CMPPPort, int recvtimeout)
        {
            this.RecvTimeOut = recvtimeout;
            this.SendTimeout = recvtimeout;
            return (this._init(CMPPServer, CMPPPort));
        }

        public bool Login(string SystemID, string UserName, string Password)
        {
            try
            {
                SendLogin(SystemID, UserName, Password);
                this.LogLastOkTime(DateTime.Now);    //最后一次正确的发送 
            }
            catch (SocketException se)
            {
                //发送出错
                this.ErrorInfo = this.ErrorInfo + "\r\n" + se.ToString();
                return (false);
            }
            DateTime t1 = DateTime.Now;
            while (!this.isLogin)
            {
                byte[] rbuf = new Byte[400];
                int l;
                try
                {
                    l = tcp.Receive(rbuf);
                    if (l > 16)
                    {
                        if (BIConvert.Bytes2UInt(rbuf, 4) == (uint)MSG.CMPP_COMMAND_ID.CMPP_CONNECT_RESP)
                        {
                            MSG.CMPP_MSG_CONNECT_RESP resp = new MSG.CMPP_MSG_CONNECT_RESP(rbuf);
                            if (resp.isOk)
                            {
                                EventArgs e = new EventArgs();
                                if (onLogonSuccEventHandler != null)
                                {
                                    onLogonSuccEventHandler(this, e);
                                }
                                else
                                {
                                    this.defaultConnectRespEventHandler();
                                }
                                this.isLogin = true;
                            }
                            else
                            {
                            }
                            break;
                        }
                    }
                    this._lastOkTime = DateTime.Now;  //更新当前最后成功收发套接字的时间
                }
                catch (SocketException)
                {
                }
                System.TimeSpan t = DateTime.Now - t1;
                if (t.TotalSeconds > 10)
                {
                    break;
                }
            }
            if (this.isLogin)
            { //登录ok,就立即发送active_test包
                this.ErrorInfo = this.ErrorInfo + "\r\n" + " Logon succ! ";
                startThreads();  // 启动 主监视程序de线程
                return (true);
            }
            else
            {
                return (false);
            }
        }

        public uint SubmitSMS(string to_user, string fee_code, string svc_code, string fee_user, string spnum, string content, int fee_usertype)
        {
            MSG.CMPP_MSG_SUBMIT sndmsg;
            uint seq = this.getNextSequence();   //取得下一个sequence 
            sndmsg = new MSG.CMPP_MSG_SUBMIT(seq);
            sndmsg.FeeCode = fee_code;
            sndmsg.FeeTerminalId = to_user;
            sndmsg.FeeType = MSG.FeeType.FEE_TERMINAL_PERITEM; //按条收取
            sndmsg.FeeUserType = fee_usertype;
            sndmsg.Msg_Level = 0;
            sndmsg.MSGFormat = (uint)MSG.Msg_Format.UCS2;
            sndmsg.SMS_Content = content;
            sndmsg.SrcID = spnum;         //长号码
            sndmsg.SPID = this.systemID;
            sndmsg.Svc_Code = svc_code;
            sndmsg.UDHI = 0;
            sndmsg.ValIdTime = getValIdTime(DateTime.Now);        //存活时间
            sndmsg.addTerminalID(to_user);
            QueueItem q = new QueueItem(seq, (uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT, 0, 0);
            q.setmsgObj(sndmsg);
            this.addToOutQueue(q);
            return (seq);
        }

        public uint SendMsg(string to_user, string fee_user, string fee, string svccode, string content, string spnum)
        {
            uint seq = this.getNextSequence();
            MSG.CMPP_MSG_SUBMIT sndmsg = new MSG.CMPP_MSG_SUBMIT(seq);
            sndmsg.FeeCode = fee;
            //sndmsg.FeeType = MSG.FeeType.FEE_TERMINAL_PERITEM;  //对计费用户按条收费
            sndmsg.FeeType = MSG.FeeType.FEE_TERMINAL_FREE;     //免计费用户
            sndmsg.FeeTerminalId = fee_user;
            sndmsg.FeeUserType = (int)MSG.FeeUserType.FEE_NULL;    //计费 按照计费号码计费
            sndmsg.SPID = this.systemID;         //企业代码
            sndmsg.UDHI = 0;             //
            sndmsg.MSGFormat = (uint)MSG.Msg_Format.GB2312;
            sndmsg.SMS_Content = content;
            sndmsg.SrcID = spnum;
            sndmsg.Svc_Code = svccode;
            sndmsg.addTerminalID(to_user);
            QueueItem q = new QueueItem(seq, (uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT, 0, 0);
            q.setmsgObj(sndmsg);
            this.addToOutQueue(q);
            return (seq);
        }

        public uint SendSMC(string fee_user, string feecode, string svccode)  //向计费用户发送一条包月计费信息
        {
            uint seq = this.getNextSequence();
            MSG.CMPP_MSG_SUBMIT sndmsg = new MSG.CMPP_MSG_SUBMIT(seq);
            sndmsg.SMS_Delivery_Type = 2;         //产生包月SMC 
            sndmsg.FeeCode = feecode;
            sndmsg.FeeType = MSG.FeeType.FEE_TERMINAL_MONTH;   //包月计费
            sndmsg.FeeTerminalId = fee_user;
            sndmsg.FeeUserType = (int)MSG.FeeUserType.FEE_TERMINAL_ID;    //计费 按照计费号码计费
            sndmsg.SPID = this.systemID;         //企业代码
            sndmsg.UDHI = 0;             //
            sndmsg.MSGFormat = (uint)MSG.Msg_Format.UCS2;
            sndmsg.SMS_Content = "SMC";
            sndmsg.SrcID = this.userName;         //sp的特符号码 
            sndmsg.Svc_Code = svccode;
            sndmsg.addTerminalID(fee_user);
            QueueItem q = new QueueItem(seq, (uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT, 0, 0);
            q.setmsgObj(sndmsg);
            this.addToOutQueue(q);
            return (seq);
        }

        public uint SendSMT(string to_user, string feecode, string svccode, string spnum, string content)
        {
            uint seq = this.getNextSequence();
            MSG.CMPP_MSG_SUBMIT sndmsg = new MSG.CMPP_MSG_SUBMIT(seq);
            sndmsg.SMS_Delivery_Type = 1;         //产生包月SMC 
            sndmsg.FeeCode = feecode;          //包月计费代码
            sndmsg.FeeType = MSG.FeeType.FEE_TERMINAL_MONTH;   //包月计费
            sndmsg.FeeTerminalId = to_user;
            sndmsg.FeeUserType = (int)MSG.FeeUserType.FEE_TERMINAL_ID;    //计费 按照计费号码计费
            sndmsg.SPID = this.systemID;         //企业代码
            sndmsg.UDHI = 0;             //
            sndmsg.MSGFormat = (uint)MSG.Msg_Format.UCS2;
            sndmsg.SMS_Content = content;
            sndmsg.SrcID = spnum;         //sp的特符号码 
            sndmsg.Svc_Code = svccode;
            sndmsg.addTerminalID(to_user);
            QueueItem q = new QueueItem(seq, (uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT, 0, 0);
            q.setmsgObj(sndmsg);
            this.addToOutQueue(q);
            return (seq);
        }

        public uint SendQuery(string svccode, string whichday) //查询某个业务的总计数
        {
            string wd = whichday.Trim();
            int query_type = 0;
            if (svccode == null || svccode.CompareTo("") == 0)
            {//查询全部页数量
            }
            else
            {//查询某项业务
                query_type = 1;
            }
            if (wd == null || wd.CompareTo("") == 0)
            {
                DateTime d = DateTime.Now;
                wd = d.Year.ToString() + d.Month.ToString().PadLeft(2, '0') + d.Day.ToString().PadLeft(2, '0');
            }
            uint seq = this.getNextSequence();
            MSG.CMPP_MSG_QUERY query = new MSG.CMPP_MSG_QUERY(seq);
            query.Query_Type = query_type;
            query.Query_Code = svccode;
            query.Time = wd;     //设定那一天
            QueueItem q = new QueueItem(seq, (uint)MSG.CMPP_COMMAND_ID.CMPP_QUERY, 0, 0);
            q.setmsgObj(query);
            this.addToOutQueue(q);
            return (seq);   //返回消息的内部编号
        }

        public uint StopCMPPConnection()   //停止CMPP协议的socket连接
        {
            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);

⌨️ 快捷键说明

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