Example #1
0
 /// <summary>
 /// TCP协议使用的数据第二层分配中心;把数据归类;
 /// </summary>
 /// <param name="stateOne"></param>
 /// <param name="statecode"></param>
 internal void TcpCodeManage(TcpState stateOne, StateCode statecode)
 {
     if (statecode == null || stateOne == null)
     {
         return;
     }
     if (statecode.State == PasswordCode._verificationCode)//说明是暗号;抛给暗号处理中心
     {
         byte haveDate = EncryptionDecryptVerification.DecryptVerification(statecode.DateByte);
         VerificationCodeManage(stateOne, haveDate);
     }
     else
     {
         codeManage(stateOne, statecode);
     }
 }
Example #2
0
        /// <summary> 关闭相连的scoket以及关联的TcpState,释放所有的资源 </summary>
        private void socketRemove(TcpState stateOne, string str)
        {
            if (stateOne == null)
            {
                return;
            }

            stateOne.WorkSocket.Close();

            if (state.Remove(stateOne))                                                      //当没有登录的时候断掉,不触发下面的事件
            {
                EngineTool.EventInvoket(() => { Disconnection(stateOne.IpEndPoint, str); }); //当客户端断掉的时候触发此事件
                FileOperate(stateOne.IpEndPoint.ToString() + "已经断开");                        //记录
                FileStart.FileStopITxBase(stateOne);
            }

            stateOne = null;
        }
Example #3
0
        /// <summary> 当连接服务器之后的回调函数 </summary>
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket socket = (Socket)ar.AsyncState;

            stateOne = new TcpState(socket, BufferSize);
            try
            {
                socket.EndConnect(ar);
                socket.BeginReceive(stateOne.Buffer, 0, stateOne.Buffer.Length, 0, new AsyncCallback(ReadCallback), stateOne);
            }
            catch (Exception Ex)
            {
                if (outtimebool == true)
                {
                    return;               //说明已经超时了,已经触发登录失败了
                }
                loginFailure(Ex.Message); //登录失败触发
            }
        }
Example #4
0
        /// <summary> 登录以及心跳代码进行处理;由基类在操作 P2=代码</summary>
        override internal 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._clientToServer:    //客户端和服务端暗号正确;可以登录;
                loginSuccess(stateOne);
                break;
            }
        }
Example #5
0
 /// <summary> 重连模块 </summary>
 private void reconnect()
 {
     if (_reconnectMax == 0)
     {
         return;    //不重连直接返回
     }
     reconnectCi++; //每重连一次重连的次数加1
     if (stateOne != null)
     {
         stateOne.WorkSocket.Close();
         stateOne = null;
     }
     if (reconnectOn == false)
     {
         reconnectOn = true;
         EngineTool.EventInvoket(() => { ReconnectionStart(); });
     }
     _engineStart = false;
     StartEngine();
 }
Example #6
0
        /// <summary> 当客户端连接之后要处理的一个线程,会验证客户端的身份。成功才允许登陆 </summary>
        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;
                }
            }
        }
Example #7
0
        /// <summary> 服务器向客户端发送图片数据 </summary>
        public void SendMessage(IPEndPoint ipEndPoint, byte[] data)
        {
            TcpState stateOne = IPEndPointToState(ipEndPoint);

            sendMessage(stateOne, data);
        }
Example #8
0
        /// <summary> 服务器强制关闭一个客户端 </summary>
        public void ClientClose(IPEndPoint ipEndPoint)
        {
            TcpState stateOne = IPEndPointToState(ipEndPoint);

            clientClose(stateOne);
        }
Example #9
0
 /// <summary>
 /// 要被TCP主类重写的;关于暗号怎么处理的类;
 /// </summary>
 /// <param name="stateOne">TcpState</param>
 /// <param name="haveDate">字节</param>
 virtual internal void VerificationCodeManage(TcpState stateOne, byte haveDate)
 {
 }