Esempio n. 1
0
 /// <summary>
 /// 异步连接回调,登录
 /// </summary>
 /// <param name="ir"></param>
 private void ConnectCallBack(IAsyncResult ir)
 {
     try
     {
         if (ir.IsCompleted)
         {
             Socket sok = ir.AsyncState as Socket;
             if (sok != null && sok.Connected)
             {
                 sok.EndConnect(ir);
                 _isConnectionSuccessful = true;
                 CmppConnectModel cc = new CmppConnectModel();
                 cc.Source_Addr = _account;
                 cc.Password    = _password;
                 cc.Version     = 0x20;
                 cc.Timestamp   = Convert.ToUInt32(DateTime.Now.ToString("MMddHHmmss"));
                 Cmpp_Login login = new Cmpp_Login(cc);
                 SendAsync(login.Encode());
                 Receive(sok);
             }
             else
             {
                 SMSLog.Error("【" + _account + "】====>ConnectCallBack(),连接失败");
             }
         }
     }
     catch (Exception ex)
     {
         _isConnectionSuccessful = false;
         SMSLog.Error("【" + _account + "】====>ConnectCallBack()Exception:" + ex.ToString());
     }
 }
Esempio n. 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);
            }
        }