Пример #1
0
        //接收头数据回调
        public void ReciveCallback(IAsyncResult ir)
        {
            SocketError err;
            StateObject state = new StateObject();
            Socket      sok   = null;

            try
            {
                if (ir.IsCompleted)
                {
                    state = ir.AsyncState as StateObject;
                    sok   = state.workSocket;

                    int bytesread = sok.EndReceive(ir, out err);
                    if (bytesread > 0 && err == SocketError.Success)
                    {
                        _isConnectionSuccessful = true;
                        byte[] b = Tools.GetSubBytes(state.buffer, 0, bytesread);
                        //处理数据
                        MemoryStream ms = new MemoryStream(b);
                        BinaryReader br = new BinaryReader(ms);
                        _isConnectionSuccessful = true;
                        Cmpp_HeaderDecode head = new Cmpp_HeaderDecode();
                        head.Decode(br);//解析包头
                        br.Close();
                        ms.Close();
                        Cmpp_Command cmd = (Cmpp_Command)head.Header.Command_Id;
                        if (!Tools.CheckCommand(head.Header.Command_Id))
                        {
                            SMSLog.Error("解析到未知Cmpp_CommandID[重新登录]:" + head.Header.Command_Id.ToString("x8"));
                            // _isLoginSuccessful = false;
                            //Login();
                            return;
                        }



                        try
                        {
                            SMSLog.Debug("接收到:" + cmd + ",cmd=0x" + head.Header.Command_Id.ToString("x8"));
                            if ((head.Header.Total_Length - GlobalModel.HeaderLength) > 0)
                            {
                                byte[] body    = new byte[head.Header.Total_Length - GlobalModel.HeaderLength];
                                int    bodyLen = sok.Receive(body);//接收包体
                                if (bodyLen != head.Header.Total_Length - GlobalModel.HeaderLength)
                                {
                                    SMSLog.Error("接收包体失败[重新登录]:" + head.Header.Command_Id);
                                    // _isLoginSuccessful = false;
                                    //Login();
                                    return;
                                }
                                ms = new MemoryStream(body);
                                br = new BinaryReader(ms);
                            }
                        }
                        catch (Exception ex)
                        {
                            SMSLog.Error("接收包体异常:" + ex.Message);
                        }

                        LastActiveTime = DateTime.Now;//最后一次交互时间
                        switch (cmd)
                        {
                        case Cmpp_Command.CMPP_CONNECT_RESP:
                            Cmpp_LoginDecode login = new Cmpp_LoginDecode();
                            login.Decode(br);
                            if (login.Status == 0)
                            {
                                _isLoginSuccessful = true;
                                this.HeartStart();
                            }
                            else
                            {
                                _isLoginSuccessful = false;
                            }
                            SMSLog.Log("【CMPP_CONNECT_RESP】login=>status=" + login.Status);
                            break;

                        case Cmpp_Command.CMPP_SUBMIT_RESP:
                            try
                            {
                                Cmpp_SubmitDecode sr = new Cmpp_SubmitDecode();
                                sr.Decode(br);
                                SMSLog.Log("【CMPP_SUBMIT_RESP】submit_resp=>result=" + sr.Result + ",msgid=" + sr.MsgId + ",seq=" + head.Header.Sequence_Id);
                                if (GlobalModel.Lparams.IsResend.Equals("1"))
                                {
                                    DelFromWaitingQueueHandler.BeginInvoke(head.Header.Sequence_Id, new AsyncCallback((IAsyncResult) => { }), null);
                                }
                                string[] submitArgs = { head.Header.Sequence_Id + "", sr.MsgId + "", sr.Result + "" };
                                GlobalModel.UpdateSubmitStateHandler.BeginInvoke(submitArgs, new AsyncCallback((IAsyncResult) => { }), null);
                            }
                            catch (Exception ex)
                            {
                                SMSLog.Error("【CMPP_SUBMIT_RESP】异常:" + ex.Message);
                            }
                            break;

                        case Cmpp_Command.CMPP_DELIVER:
                            try
                            {
                                Cmpp_DeliverDecode cd = new Cmpp_DeliverDecode();
                                cd.Decode(br);
                                CMPP_DeliverResp cdresp = new CMPP_DeliverResp(cd.Msg_Id, 0, head.Header.Sequence_Id);
                                this.SendAsync(cdresp.Encode());
                                if (cd.Registered_Delivery == 1)
                                {
                                    SMSLog.Log("【CMPP_DELIVER】CMPP_DELIVER=>stat=" + cd.ReportStat + ",msgid=" + cd.ReportMsgId + ",Dest_Id=" + cd.Dest_Id + ",mobile=" + cd.ReportDestTerminalId + ",seq=" + head.Header.Sequence_Id);
                                    string[] repportArgs = { cd.ReportMsgId + "", cd.Dest_Id, cd.ReportStat, cd.ReportDestTerminalId };
                                    GlobalModel.UpdateReportStateHandler.BeginInvoke(repportArgs, new AsyncCallback((IAsyncResult) => { }), null);
                                }
                                else
                                {
                                    SMSLog.Log("【CMPP_MO】CMPP_DELIVER=>mo=" + cd.Msg_Content + ",msgid=" + cd.ReportMsgId + ",接收Dest_Id=" + cd.Dest_Id + ",来自mobile=" + cd.Src_terminal_Id);
                                    string[] moArgs = { cd.Src_terminal_Id, cd.Dest_Id, cd.Msg_Content };
                                    GlobalModel.SaveMoHandler.BeginInvoke(moArgs, new AsyncCallback((IAsyncResult) => { }), null);
                                }
                            }
                            catch (Exception ex)
                            {
                                SMSLog.Error("【CMPP_DELIVER】异常:" + ex.Message);
                            }
                            break;

                        case Cmpp_Command.CMPP_ACTIVE_TEST:
                            try
                            {
                                CMPP_ActiveTestDecode catde = new CMPP_ActiveTestDecode();
                                catde.Decode(br);
                                CMPP_ActiveTestResp cat = new CMPP_ActiveTestResp();
                                cat.SequenceId = head.Header.Sequence_Id;
                                this.SendAsync(cat.Encode());
                            }
                            catch (Exception)
                            {
                            }
                            break;

                        case Cmpp_Command.CMPP_ACTIVE_TEST_RESP:
                            SMSLog.Log("【CMPP_ACTIVE_TEST_RESP】不处理");
                            //CMPP_ActiveTestDecode catdecode = new CMPP_ActiveTestDecode();
                            //catdecode.Decode(br);
                            //CMPP_ActiveTestResp cata = new CMPP_ActiveTestResp();
                            //cata.SequenceId = head.Header.Sequence_Id;
                            //this.SendAsync(cata.Encode());
                            break;

                        case Cmpp_Command.CMPP_TERMINATE_RESP:
                            SMSLog.Log("【CMPP_TERMINATE_RESP】不处理");
                            //this.HeartStop();
                            //CMPP_TerminateResp tr = new CMPP_TerminateResp();
                            //tr.SequenceId = head.Header.Sequence_Id;
                            //this.SendAsync(tr.Encode());
                            // this._isStop = true;//通知其他线程可以退出了
                            break;

                        case Cmpp_Command.CMPP_TERMINATE:
                            try
                            {
                                SMSLog.Log("【CMPP_TERMINATE】退出命令");
                                _isLoginSuccessful = false;
                                CMPP_TerminateDecode ctrd = new CMPP_TerminateDecode();
                                ctrd.Decode(br);
                                CMPP_TerminateResp ctr = new CMPP_TerminateResp();
                                ctr.SequenceId = head.Header.Sequence_Id;
                                this.SendAsync(ctr.Encode());
                                //this.StopMe();
                            }
                            catch (Exception ex)
                            {
                                SMSLog.Error("【CMPP_TERMINATE】异常:" + ex.Message);
                            }
                            break;

                        default:
                            break;
                        }
                        br.Close();
                        ms.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                SMSLog.Error("【" + _account + "】SocketClient=>ReciveCallback()异常:" + ex.ToString());
            }
            finally
            {
                try
                {
                    if (sok != null)
                    {
                        sok.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, out err, new AsyncCallback(ReciveCallback), state);
                    }
                }
                catch (Exception ex)
                {
                    SMSLog.Error("SocketClient=>再次启动BeginReceive()异常:" + ex.ToString());
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 同步登录
        /// </summary>
        /// <returns></returns>
        public bool Login()
        {
            lock (loginlocker)
            {
                try
                {
                    SMSLog.Debug("Login==>begin...");
                    Socket sock;
                    try
                    {
                        if (_isStop)
                        {
                            SMSLog.Debug("Login==>已停止不再登录...");
                            return(false);
                        }

                        if (_isLoginSuccessful)
                        {
                            SMSLog.Debug("Login==>已登录成功...");
                            return(true);
                        }

                        if (_isLogining)
                        {
                            SMSLog.Debug("Login==>正在登录...");
                            return(false);
                        }
                        _isLogining         = true;
                        sock                = this.Conn();
                        GlobalModel.IsLogin = false;
                        if (sock == null)
                        {
                            SMSLog.Error("SocketClient==>Login()...sock连接短信中心失败....");
                            if (sendsmstimes < 1)
                            {
                                // MyTools.SendSMS("网关[" + GlobalModel.Lparams.GatewayName + "]连接短信中心失败");
                                sendsmstimes++;
                            }
                            _isLogining = false;
                            return(false);
                        }

                        //发送登录验证包
                        //uint seq = Tools.GetSequence_Id(); //取得一个流水号
                        CmppConnectModel cc = new CmppConnectModel();
                        cc.Source_Addr = _account;
                        cc.Password    = _password;
                        cc.Version     = (uint)GlobalModel.Lparams.Version;//0x20
                        cc.Timestamp   = Convert.ToUInt32(DateTime.Now.ToString("MMddHHmmss"));
                        Cmpp_Login login   = new Cmpp_Login(cc);
                        int        sendlen = sock.Send(login.Encode());
                        SMSLog.Debug("连接成功发送登录包[" + sendlen + "]...");

                        if (!sock.Poll(1000000, SelectMode.SelectWrite))
                        {
                            SMSLog.Error("=>cmpp::Login", "短信中心超时未应答,登录失败!");
                            sock.Close();
                            return(_isLogining = false);
                        }
                    }
                    catch (SocketException se)
                    {
                        SMSLog.Error("登录异常【" + _account + "】:" + se.Message);
                        _isLogining = false;
                        return(_isLogining = false);
                    }
                    DateTime t1 = DateTime.Now;

                    byte[] rbuf = new Byte[400];
                    int    l;
                    try
                    {
                        l = sock.Receive(rbuf);
                        SMSLog.Debug("接收到数据长度:" + l);
                        if (l > 16)
                        {
                            MemoryStream      ms   = new MemoryStream(rbuf);
                            BinaryReader      br   = new BinaryReader(ms);
                            Cmpp_HeaderDecode head = new Cmpp_HeaderDecode();
                            head.Decode(br);//解析包头
                            SMSLog.Debug(head.Header.Command_Id + "");
                            if ((Cmpp_Command)head.Header.Command_Id == Cmpp_Command.CMPP_CONNECT_RESP)
                            {
                                Cmpp_LoginDecode login = new Cmpp_LoginDecode();
                                login.Decode(br);
                                if (login.Status == 0)
                                {
                                    _isLoginSuccessful = true;
                                    SMSLog.Debug("登录成功:[" + _account + "],version=0x" + login.Version.ToString("x8"));
                                    this.LastActiveTime = DateTime.Now;  //更新当前最后成功收发套接字的时间
                                }
                                else
                                {
                                    _isLoginSuccessful = false;
                                    SMSLog.Error("登录失败:[" + _account + "],result=" + login.Status);
                                }
                            }
                        }
                    }
                    catch (SocketException ex)
                    {
                        _isLoginSuccessful = false;
                        SMSLog.Error("登录接收异常:" + ex.Message);
                        if (sock != null)
                        {
                            sock.Close();
                        }
                    }



                    if (this._isLoginSuccessful)
                    {
                        sendsmstimes        = 0;
                        this.LastActiveTime = DateTime.Now;
                        _socket             = sock;
                        MyTools.StopThread(_threadReceive);
                        _threadReceive = new Thread(new ThreadStart(() => { Receive(_socket); }));
                        _threadReceive.Start();
                        //登录ok,就立即发送active_test包
                        CMPP_ActiveTest heart = new CMPP_ActiveTest();
                        this.Send(heart.Encode());
                        GlobalModel.IsLogin = true;
                        // 启动 主监视程序de线程
                        this.HeartStart();
                        _isLogining = false;
                        return(true);
                    }
                    else
                    {
                        if (sendsmstimes < 1)
                        {
                            MyTools.SendSMS("网关[" + GlobalModel.Lparams.GatewayName + "]登录失败");
                            sendsmstimes++;
                        }
                        sock.Shutdown(SocketShutdown.Both);
                        sock.Close();
                        _isLogining = false;
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    SMSLog.Error("登录异常:" + ex.Message);
                }
                finally
                {
                    _isLogining = false;
                }
                return(false);
            }
        }