Esempio n. 1
0
        private OperateResult <Socket> ConnectServer( )
        {
            OperateResult <Socket> connectResult = CreateSocketAndConnect(EndPointServer, 10000);

            if (!connectResult.IsSuccess)
            {
                return(connectResult);
            }

            // 连接成功,发送数据信息
            OperateResult sendResult = SendStringAndCheckReceive(connectResult.Content, 1, ClientAlias);

            if (!sendResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <Socket>(sendResult));
            }

            MessageAlerts?.Invoke(StringResources.Language.ConnectServerSuccess);
            return(connectResult);
        }
Esempio n. 2
0
 /// <summary>
 /// 连接服务器之前的消息提示,如果是重连的话,就提示10秒等待信息
 /// </summary>
 private void AwaitToConnect( )
 {
     if (ConnectFailedCount == 0)
     {
         MessageAlerts?.Invoke(StringResources.Language.ConnectingServer);
     }
     else
     {
         int count = 10;
         while (count > 0)
         {
             if (IsQuie)
             {
                 return;
             }
             count--;
             MessageAlerts?.Invoke(string.Format(StringResources.Language.ConnectFailedAndWait, count));
             Thread.Sleep(1000);
         }
         MessageAlerts?.Invoke(string.Format(StringResources.Language.AttemptConnectServer, ConnectFailedCount));
     }
 }
 /// <summary>
 /// 连接服务器之前的消息提示,如果是重连的话,就提示10秒等待信息
 /// </summary>
 private void AwaitToConnect( )
 {
     if (ConnectFailedCount == 0)
     {
         // English Version : Connecting Server...
         MessageAlerts?.Invoke("正在连接服务器...");
     }
     else
     {
         int count = 10;
         while (count > 0)
         {
             if (IsQuie)
             {
                 return;
             }
             count--;
             // English Version : Disconnected, wait [count] second to restart
             MessageAlerts?.Invoke("连接断开,等待" + count + "秒后重新连接");
             Thread.Sleep(1000);
         }
         MessageAlerts?.Invoke("正在尝试第" + ConnectFailedCount + "次连接服务器...");
     }
 }
Esempio n. 4
0
        private void ThreadLogin()
        {
            lock (lock_connecting)
            {
                if (Is_Client_Connecting)
                {
                    return;
                }
                Is_Client_Connecting = true;
            }


            if (Connect_Failed_Count == 0)
            {
                MessageAlerts?.Invoke("正在连接服务器...");
            }
            else
            {
                int count = 10;
                while (count > 0)
                {
                    if (IsQuie)
                    {
                        return;
                    }
                    MessageAlerts?.Invoke("连接断开,等待" + count-- + "秒后重新连接");
                    Thread.Sleep(1000);
                }
                MessageAlerts?.Invoke("正在尝试第" + Connect_Failed_Count + "次连接服务器...");
            }


            stateone.HeartTime = DateTime.Now;
            LogNet?.WriteDebug(LogHeaderText, "Begin Connect Server, Times: " + Connect_Failed_Count);


            OperateResult result = new OperateResult();

            if (!CreateSocketAndConnect(out Socket socket, EndPointServer, result))
            {
                Connect_Failed_Count++;
                Is_Client_Connecting = false;
                LoginFailed?.Invoke(Connect_Failed_Count);
                LogNet?.WriteDebug(LogHeaderText, "Connected Failed, Times: " + Connect_Failed_Count);
                // 连接失败,重新连接服务器
                ReconnectServer();
                return;
            }

            // 连接成功,发送数据信息
            if (!SendStringAndCheckReceive(
                    socket,
                    1,
                    ClientAlias,
                    result
                    ))
            {
                Connect_Failed_Count++;
                Is_Client_Connecting = false;
                LogNet?.WriteDebug(LogHeaderText, "Login Server Failed, Times: " + Connect_Failed_Count);
                LoginFailed?.Invoke(Connect_Failed_Count);
                // 连接失败,重新连接服务器
                ReconnectServer();
                return;
            }

            // 登录成功
            Connect_Failed_Count = 0;
            stateone.IpEndPoint  = (IPEndPoint)socket.RemoteEndPoint;
            stateone.LoginAlias  = ClientAlias;
            stateone.WorkSocket  = socket;
            stateone.WorkSocket.BeginReceive(stateone.BytesHead, stateone.AlreadyReceivedHead,
                                             stateone.BytesHead.Length - stateone.AlreadyReceivedHead, SocketFlags.None,
                                             new AsyncCallback(HeadReceiveCallback), stateone);

            // 发送一条验证消息
            // SendBytes(stateone, CommunicationCode.CommandBytes(CommunicationCode.Hsl_Protocol_Check_Secends));
            byte[] bytesTemp = new byte[16];
            BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(bytesTemp, 0);
            SendBytes(stateone, NetSupport.CommandBytes(HslProtocol.ProtocolCheckSecends, 0, KeyToken, bytesTemp));


            stateone.HeartTime = DateTime.Now;
            Is_Client_Start    = true;
            LoginSuccess?.Invoke();

            LogNet?.WriteDebug(LogHeaderText, "Login Server Success, Times: " + Connect_Failed_Count);

            Is_Client_Connecting = false;

            Thread.Sleep(1000);
        }
        private void ThreadLogin()
        {
            lock (lock_connecting)
            {
                if (IsClientConnecting)
                {
                    return;
                }
                IsClientConnecting = true;
            }


            if (ConnectFailedCount == 0)
            {
                // English Version : Connecting Server...
                MessageAlerts?.Invoke("正在连接服务器...");
            }
            else
            {
                int count = 10;
                while (count > 0)
                {
                    if (IsQuie)
                    {
                        return;
                    }
                    // English Version : Disconnected, wait [count--] second to restart
                    MessageAlerts?.Invoke("连接断开,等待" + count-- + "秒后重新连接");
                    Thread.Sleep(1000);
                }
                MessageAlerts?.Invoke("正在尝试第" + ConnectFailedCount + "次连接服务器...");
            }


            stateone.HeartTime = DateTime.Now;
            LogNet?.WriteDebug(ToString( ), "Begin Connect Server, Times: " + ConnectFailedCount);

            OperateResult <Socket> connectResult = CreateSocketAndConnect(EndPointServer, 10000);

            if (!connectResult.IsSuccess)
            {
                ConnectFailedCount++;
                IsClientConnecting = false;
                LoginFailed?.Invoke(ConnectFailedCount);
                LogNet?.WriteDebug(ToString( ), "Connected Failed, Times: " + ConnectFailedCount);
                // 连接失败,重新连接服务器
                ReconnectServer( );
                return;
            }



            // 连接成功,发送数据信息
            OperateResult sendResult = SendStringAndCheckReceive(connectResult.Content, 1, ClientAlias);

            if (!sendResult.IsSuccess)
            {
                ConnectFailedCount++;
                IsClientConnecting = false;
                LogNet?.WriteDebug(ToString( ), "Login Server Failed, Times: " + ConnectFailedCount);
                LoginFailed?.Invoke(ConnectFailedCount);
                // 连接失败,重新连接服务器
                ReconnectServer( );
                return;
            }

            // 登录成功
            ConnectFailedCount  = 0;
            stateone.IpEndPoint = (IPEndPoint)connectResult.Content.RemoteEndPoint;
            stateone.LoginAlias = ClientAlias;
            stateone.WorkSocket = connectResult.Content;
            stateone.WorkSocket.BeginReceive(stateone.BytesHead, stateone.AlreadyReceivedHead,
                                             stateone.BytesHead.Length - stateone.AlreadyReceivedHead, SocketFlags.None,
                                             new AsyncCallback(HeadBytesReceiveCallback), stateone);


            byte[] bytesTemp = new byte[16];
            BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(bytesTemp, 0);
            SendBytes(stateone, HslProtocol.CommandBytes(HslProtocol.ProtocolCheckSecends, 0, Token, bytesTemp));


            stateone.HeartTime = DateTime.Now;
            IsClientStart      = true;
            LoginSuccess?.Invoke( );
            LogNet?.WriteDebug(ToString( ), "Login Server Success, Times: " + ConnectFailedCount);
            IsClientConnecting = false;
            Thread.Sleep(1000);
        }