Exemple #1
0
        /// <summary>
        /// 连接到 ISMG。
        /// </summary>
        public bool Connect()
        {
            _connecting = true;
            _connect    = false;

            Login connect = new Login(_setting.SPID, _setting.Password, 2, DateTime.Now, 0x30, Sequence.Instance.CreateID());

            byte[] bytes = connect.ToBytes();
            _client.Close();

            while (_client.IsConnected)
            {
                Thread.Sleep(100);
            }
            //最后一次连接跟本次连接是否在20秒之内
            if (DateTime.Now.Ticks - _lastConnectTime < 10000000 * 20)
            {
                _connectCount++;
            }
            //重连10次后重新建立客户端对象
            if (_connectCount == 10)
            {
                InitClient();
                _connectCount = 0;
            }
            _lastConnectTime = DateTime.Now.Ticks;
            if (_client.Connect())
            {
                Thread.Sleep(1000);
            }
            else
            {
                _connecting = false;
                return(false);
            }

            try
            {
                _client.Send(bytes);
            }
            catch
            {
                _connecting = false;
                return(false);
            }

            autoConnectEvent.WaitOne(_setting.TimeOut * 100, false);

            if (!_connect)
            {
                OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "等待 CONNECT_RESP 超时"));
                _connecting = false;
                return(false);
            }
            _connecting = false;
            return(_connect);
        }
Exemple #2
0
        public bool Connect()
        {
            _connecting = true;
            _connect    = false;

            SGIP_BIND connect = new SGIP_BIND((uint)LoginTypes.SpToSmg, Setting.LoginName, Setting.Password, Sequence.Instance.CreateID());

            byte[] bytes = connect.ToBytes();

            _client.Close();
            while (_client.IsConnected)
            {
                Thread.Sleep(100);
            }

            if (_client.Connect())
            {
                Thread.Sleep(1000);
            }
            else
            {
                _connecting = false;
                return(false);
            }

            try
            {
                Thread.Sleep(Setting.SendSpan);
                _client.Send(bytes);
                DebugLog.Instance.Write <SGIP_BIND>("Client Send -> SGIP_BIND", connect);
            }
            catch
            {
                _connecting = false;
                return(false);
            }
            autoConnectEvent.WaitOne(Setting.TimeOut * 1000, false);

            if (!_connect)
            {
                _client.Close();
                OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "等待 CONNECT_RESP 超时"));
                _connecting = false;
                return(false);
            }
            _connecting = false;
            return(_connect);
        }
Exemple #3
0
        /// <summary>
        /// 连接到 ISMG。
        /// </summary>
        public bool Connect()
        {
            _connecting = true;
            _connect    = false;

            DateTime     dt   = DateTime.Now;
            CMPP_CONNECT conn = new CMPP_CONNECT();

            conn.Head                = new CMPP_HEAD();
            conn.Head.CommandID      = (uint)CMPP_COMMAND.CMD_CONNECT;
            conn.Head.SequenceID     = Sequence.Instance.CreateID();
            conn.SourceAddress       = _setting.SPID;
            conn.TimeStamp           = System.Convert.ToUInt32(string.Format("{0:MMddhhmmss}", dt));
            conn.AuthenticatorSource = CreateDigest(dt);
            conn.Version             = CMPP_VERSION_30;

            byte[] bytes = conn.GetBytes();

            _client.Close();
            while (_client.IsConnected)
            {
                Thread.Sleep(100);
            }

            if (_client.Connect())
            {
                Thread.Sleep(1000);
            }
            else
            {
                _connecting = false;
                return(false);
            }

            try
            {
                Thread.Sleep(_setting.SendSpan);
                _client.Send(bytes);
                //  DebugLog.Instance.Write<CMPP_CONNECT>("Client Send -> CMPP_CONNECT", conn);
            }
            catch (Exception ex)
            {
                //  DebugLog.Instance.Write("Client Send Error -> CMPP_CONNECT", ex.ToString());
                MessageTools.MessageHelper.Instance.WirteError("Client Send Error -> CMPP_CONNECT", ex);

                _connecting = false;
                return(false);
            }

            autoConnectEvent.WaitOne(_setting.TimeOut * 1000, false);

            if (!_connect)
            {
                _client.Close();
                OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "连接服务器超时"));
                _connecting = false;
                return(false);
            }
            _connecting = false;
            return(_connect);
        }