コード例 #1
0
 /// <summary>
 /// 心跳线程的方法
 /// </summary>
 private void ThreadHeartCheck()
 {
     Thread.Sleep(2000);
     while (true)
     {
         Thread.Sleep(1000);
         if (!IsQuie)
         {
             byte[] send = new byte[16];
             BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(send, 0);
             SendBytes(stateone, NetSupport.CommandBytes(HslProtocol.ProtocolCheckSecends, 0, KeyToken, send));
             double timeSpan = (DateTime.Now - stateone.HeartTime).TotalSeconds;
             if (timeSpan > 1 * 8)//8次没有收到失去联系
             {
                 LogNet?.WriteDebug(LogHeaderText, $"Heart Check Failed int {timeSpan} Seconds.");
                 ReconnectServer();
                 Thread.Sleep(1000);
             }
         }
         else
         {
             break;
         }
     }
 }
コード例 #2
0
 /// <summary>
 /// 数据处理中心
 /// </summary>
 /// <param name="receive"></param>
 /// <param name="protocol"></param>
 /// <param name="customer"></param>
 /// <param name="content"></param>
 internal override void DataProcessingCenter(AsyncStateOne receive, int protocol, int customer, byte[] content)
 {
     if (protocol == HslProtocol.ProtocolCheckSecends)
     {
         BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(content, 8);
         SendBytes(receive, NetSupport.CommandBytes(HslProtocol.ProtocolCheckSecends, customer, KeyToken, content));
         receive.HeartTime = DateTime.Now;
     }
     else if (protocol == HslProtocol.ProtocolClientQuit)
     {
         TcpStateDownLine(receive, true);
     }
     else if (protocol == HslProtocol.ProtocolUserBytes)
     {
         //接收到字节数据
         AcceptByte?.Invoke(receive, customer, content);
         // LogNet?.WriteDebug(LogHeaderText, "Protocol:" + protocol + " customer:" + customer + " name:" + receive.LoginAlias);
     }
     else if (protocol == HslProtocol.ProtocolUserString)
     {
         //接收到文本数据
         string str = Encoding.Unicode.GetString(content);
         AcceptString?.Invoke(receive, customer, str);
         // LogNet?.WriteDebug(LogHeaderText, "Protocol:" + protocol + " customer:" + customer + " name:" + receive.LoginAlias);
     }
 }
コード例 #3
0
 /// <summary>
 /// 服务器端用于发送字节的方法
 /// </summary>
 /// <param name="customer">用户自定义的命令头</param>
 /// <param name="bytes">实际发送的数据</param>
 public void Send(NetHandle customer, byte[] bytes)
 {
     if (Is_Client_Start)
     {
         SendBytes(stateone, NetSupport.CommandBytes(customer, KeyToken, bytes));
     }
 }
コード例 #4
0
        /// <summary>
        /// 关闭该客户端引擎
        /// </summary>
        public void ClientClose()
        {
            IsQuie = true;
            if (Is_Client_Start)
            {
                SendBytes(stateone, NetSupport.CommandBytes(HslProtocol.ProtocolClientQuit, 0, KeyToken, null));
            }

            thread_heart_check?.Abort();
            Is_Client_Start = false;
            Thread.Sleep(5);
            LoginSuccess  = null;
            LoginFailed   = null;
            MessageAlerts = null;
            AcceptByte    = null;
            AcceptString  = null;
            stateone.WorkSocket?.Close();
            LogNet?.WriteDebug(LogHeaderText, "Client Close.");
        }
コード例 #5
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);
        }
コード例 #6
0
 /// <summary>
 /// 服务器端用于发送字节的方法
 /// </summary>
 /// <param name="stateone">数据发送对象</param>
 /// <param name="customer">用户自定义的数据对象,如不需要,赋值为0</param>
 /// <param name="bytes">实际发送的数据</param>
 public void Send(AsyncStateOne stateone, NetHandle customer, byte[] bytes)
 {
     SendBytes(stateone, NetSupport.CommandBytes(customer, KeyToken, bytes));
 }
コード例 #7
0
 /// <summary>
 /// 服务器端用于数据发送文本的方法
 /// </summary>
 /// <param name="stateone">数据发送对象</param>
 /// <param name="customer">用户自定义的数据对象,如不需要,赋值为0</param>
 /// <param name="str">发送的文本</param>
 public void Send(AsyncStateOne stateone, NetHandle customer, string str)
 {
     SendBytes(stateone, NetSupport.CommandBytes(customer, KeyToken, str));
 }
コード例 #8
0
 /// <summary>
 /// 发送字符串数据到服务器
 /// </summary>
 /// <param name="customer">用户自定义的标记数据</param>
 /// <param name="data">字符串数据</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="SocketException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 public void SendMessage(NetHandle customer, string data)
 {
     WorkSocket.SendTo(NetSupport.CommandBytes(customer, KeyToken, data), ServerEndPoint);
 }