Beispiel #1
0
 /// <summary>
 /// 心跳线程
 /// </summary>
 private void heartThread()
 {
     while (true)
     {
         Thread.Sleep(HeartTime * 1000);
         int i = 0;
         while (i < state.Count)
         {
             if (state[i] == null)
             {
                 state.RemoveAt(i);
                 continue;
             }
             else if ((int)(DateTime.Now - state[i].HeartTime).TotalSeconds > HeartTime * 4)//4次没有收到失去联系
             {
                 socketRemove(state[i], "客户端长期连接不上,将断开此客户端");
                 continue;
             }
             else
             {
                 Send(state[i], EncryptionDecryptVerification.EncryptionVerification(PasswordCode._heartbeatCode));
             }
             i++;
         }
         if (_engineStart == false || state.Count > 100)//后面的大于100用于限制商业版的
         {
             break;
         }
     }
 }
Beispiel #2
0
 /// <summary>
 /// 心跳线程
 /// </summary>
 private void heartThread()
 {
     while (true)
     {
         Thread.Sleep(HeartTime * 1000);
         if (stateOne == null)
         {
             continue;
         }
         else if ((int)(DateTime.Now - stateOne.HeartTime).TotalSeconds > HeartTime * 4) //4次没有收到失去联系
         {
             lostClient("客户端长期连接不上服务器根据Reconnection值判断是否重连");                            //当突然断开的时候
             continue;
         }
         else
         {
             Send(stateOne, EncryptionDecryptVerification.EncryptionVerification(PasswordCode._heartbeatCode));
         }
         if (_engineStart == false)
         {
             break;
         }
     }
     HeartThread = null;
 }
Beispiel #3
0
        /// <summary>
        ///     登录以及心跳代码进行处理;由基类在操作
        /// </summary>
        /// <param name="stateOne">TcpState</param>
        /// <param name="haveDate">byte</param>
        internal override void VerificationCodeManage(TcpState stateOne, byte haveDate)
        {
            if (stateOne == null)
            {
                return;
            }
            switch (haveDate)
            {
            case 0:     //不是需要的数据
                break;

            case PasswordCode._heartbeatCode:     //是心跳信息
                //stateOne.HeartTime = DateTime.Now;
                break;

            case PasswordCode._serverToClient:     //客户端和服务端暗号正确;已登录;
                Send(stateOne, EncryptionDecryptVerification.EncryptionVerification(PasswordCode._clientToServer));
                loginSuccess();
                break;

            case PasswordCode._clientCloseCode:     //服务器要求客户端关掉;
                CloseEngine();
                break;
            }
        }
Beispiel #4
0
        /// <summary>
        /// 当客户端连接之后要处理的一个线程,会验证客户端的身份。成功才允许登陆;
        /// </summary>
        /// <param name="stateOne1">TcpState</param>
        private void loginInitialization(object stateOne1)
        {
            TcpState stateOne = (TcpState)stateOne1;

            if (ClientNumber >= _clientMax)
            {
                FileOperate("客户端数量已达到上限,交流QQ群:426414437");    //记录
                socketRemove(stateOne, "客户端数量已达到上限");
                return;
            }
            Send(stateOne, EncryptionDecryptVerification.EncryptionVerification(PasswordCode._serverToClient));//发送登录成功的代码
            try
            {
                stateOne.WorkSocket.BeginReceive(stateOne.Buffer, 0, stateOne.Buffer.Length, 0, new AsyncCallback(ReadCallback), stateOne);
            }
            catch
            {
            }
            DateTime dateOne = DateTime.Now;

            while (true)//如果2秒中之内客户端没有根据登录信息回复,将自动关闭这个客户端
            {
                Thread.Sleep(100);
                if (stateOne == null || stateOne.ConnectOk == true)
                {
                    break;
                }
                if ((int)(DateTime.Now - dateOne).TotalSeconds > 2)
                {
                    clientClose(stateOne); FileOperate(stateOne.IpEndPoint.ToString() + "无法收到客户端登录信息");//记录;
                    break;
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// 发送代码的形式服务器强制关闭一个客户端
 /// </summary>
 /// <param name="stateOne">TcpState</param>
 private void clientClose(TcpState stateOne)
 {
     if (stateOne == null || ClientNumber == 0)
     {
         return;
     }
     state.Remove(stateOne);                                                                              //先把这个移除,万一对方没有收到下面的信息;对方一定时候也会自动关闭这个连接
     Send(stateOne, EncryptionDecryptVerification.EncryptionVerification(PasswordCode._clientCloseCode)); //发送一个强制关闭的代码过去
 }