Beispiel #1
0
 /// <summary>
 /// 客户端的数据处理中心
 /// </summary>
 /// <param name="session">会话</param>
 /// <param name="protocol">消息暗号</param>
 /// <param name="customer">用户消息</param>
 /// <param name="content">数据内容</param>
 internal override void DataProcessingCenter(AppSession session, int protocol, int customer, byte[] content)
 {
     if (protocol == HslProtocol.ProtocolCheckSecends)
     {
         DateTime dt = new DateTime(BitConverter.ToInt64(content, 0));
         ServerTime             = new DateTime(BitConverter.ToInt64(content, 8));
         DelayTime              = (int)(DateTime.Now - dt).TotalMilliseconds;
         this.session.HeartTime = DateTime.Now;
         // MessageAlerts?.Invoke("心跳时间:" + DateTime.Now.ToString());
     }
     else if (protocol == HslProtocol.ProtocolClientQuit)
     {
         // 申请了退出
     }
     else if (protocol == HslProtocol.ProtocolUserBytes)
     {
         // 接收到字节数据
         AcceptByte?.Invoke(this.session, customer, content);
     }
     else if (protocol == HslProtocol.ProtocolUserString)
     {
         // 接收到文本数据
         string str = Encoding.Unicode.GetString(content);
         AcceptString?.Invoke(this.session, customer, str);
     }
 }
Beispiel #2
0
 /// <summary>
 /// 数据处理中心
 /// </summary>
 /// <param name="session"></param>
 /// <param name="protocol"></param>
 /// <param name="customer"></param>
 /// <param name="content"></param>
 internal override void DataProcessingCenter(AppSession session, int protocol, int customer, byte[] content)
 {
     if (protocol == HslProtocol.ProtocolCheckSecends)
     {
         BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(content, 8);
         SendBytes(session, HslProtocol.CommandBytes(HslProtocol.ProtocolCheckSecends, customer, Token, content));
         session.HeartTime = DateTime.Now;
     }
     else if (protocol == HslProtocol.ProtocolClientQuit)
     {
         TcpStateDownLine(session, true);
     }
     else if (protocol == HslProtocol.ProtocolUserBytes)
     {
         //接收到字节数据
         AcceptByte?.Invoke(session, customer, content);
     }
     else if (protocol == HslProtocol.ProtocolUserString)
     {
         //接收到文本数据
         string str = Encoding.Unicode.GetString(content);
         AcceptString?.Invoke(session, customer, str);
     }
     else
     {
         // 其他一概不处理
     }
 }
Beispiel #3
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);
     }
 }
Beispiel #4
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 == HslCommunicationCode.Hsl_Protocol_Check_Secends)
     {
         DateTime dt = new DateTime(BitConverter.ToInt64(content, 0));
         ServerTime         = new DateTime(BitConverter.ToInt64(content, 8));
         DelayTime          = (int)(DateTime.Now - dt).TotalMilliseconds;
         stateone.HeartTime = DateTime.Now;
         // MessageAlerts?.Invoke("心跳时间:" + DateTime.Now.ToString());
     }
     else if (protocol == HslCommunicationCode.Hsl_Protocol_Client_Quit)
     {
         // 申请了退出
     }
     else if (protocol == HslCommunicationCode.Hsl_Protocol_User_Bytes)
     {
         // 接收到字节数据
         AcceptByte?.Invoke(stateone, customer, content);
     }
     else if (protocol == HslCommunicationCode.Hsl_Protocol_User_String)
     {
         // 接收到文本数据
         string str = Encoding.Unicode.GetString(content);
         AcceptString?.Invoke(stateone, customer, str);
     }
 }
        /***********************************************************************************************************
        *
        *    无法使用如下的字节头接收来确认网络传输,总是报错为最小
        *
        ***********************************************************************************************************/

        //private void ReceiveAsyncCallback(IAsyncResult ar)
        //{
        //    if (ar.AsyncState is AsyncStateOne state)
        //    {
        //        try
        //        {
        //            state.AlreadyReceivedHead += state.WorkSocket.EndReceiveFrom(ar, ref state.UdpEndPoint);
        //            if (state.AlreadyReceivedHead < state.HeadLength)
        //            {
        //                //接续接收头数据
        //                WorkSocket.BeginReceiveFrom(state.BytesHead, state.AlreadyReceivedHead, state.HeadLength - state.AlreadyReceivedHead, SocketFlags.None,
        //                    ref state.UdpEndPoint, new AsyncCallback(ReceiveAsyncCallback), state);
        //            }
        //            else
        //            {
        //                //开始接收内容
        //                int ReceiveLenght = BitConverter.ToInt32(state.BytesHead, 4);
        //                if (ReceiveLenght > 0)
        //                {
        //                    state.BytesContent = new byte[ReceiveLenght];
        //                    WorkSocket.BeginReceiveFrom(state.BytesContent, state.AlreadyReceivedContent, state.BytesContent.Length - state.AlreadyReceivedContent,
        //                        SocketFlags.None, ref state.UdpEndPoint, new AsyncCallback(ContentReceiveAsyncCallback), state);
        //                }
        //                else
        //                {
        //                    //没有内容了
        //                    ThreadDealWithReveice(state, BitConverter.ToInt32(state.BytesHead, 0), state.BytesContent);
        //                    state = null;
        //                    RefreshReceive();
        //                }
        //            }
        //        }
        //        catch(Exception ex)
        //        {
        //            LogHelper.SaveError(StringResources.Language.异步数据结束挂起发送出错, ex);
        //        }


        //    }
        //}

        //private void ContentReceiveAsyncCallback(IAsyncResult ar)
        //{
        //    if (ar.AsyncState is AsyncStateOne state)
        //    {
        //        try
        //        {
        //            state.AlreadyReceivedContent += state.WorkSocket.EndReceiveFrom(ar, ref state.UdpEndPoint);
        //            if (state.AlreadyReceivedContent < state.BytesContent.Length)
        //            {
        //                //还需要继续接收
        //                WorkSocket.BeginReceiveFrom(state.BytesContent, state.AlreadyReceivedContent, state.BytesContent.Length - state.AlreadyReceivedContent,
        //                        SocketFlags.None, ref state.UdpEndPoint, new AsyncCallback(ContentReceiveAsyncCallback), state);
        //            }
        //            else
        //            {
        //                //接收完成了
        //                ThreadDealWithReveice(state, BitConverter.ToInt32(state.BytesHead, 0), new byte[0]);
        //                state = null;
        //                RefreshReceive();
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            LogHelper.SaveError(StringResources.Language.异步数据结束挂起发送出错, ex);
        //        }


        //    }
        //}

        #region Data Process Center

        /// <summary>
        /// 数据处理中心
        /// </summary>
        /// <param name="receive"></param>
        /// <param name="protocol"></param>
        /// <param name="customer"></param>
        /// <param name="content"></param>
        internal override void DataProcessingCenter(AppSession receive, int protocol, int customer, byte[] content)
        {
            if (protocol == HslProtocol.ProtocolUserBytes)
            {
                AcceptByte?.Invoke(receive, customer, content);
            }
            else if (protocol == HslProtocol.ProtocolUserString)
            {
                // 接收到文本数据
                string str = Encoding.Unicode.GetString(content);
                AcceptString?.Invoke(receive, customer, str);
            }
        }
Beispiel #6
0
        /***********************************************************************************************************
        *
        *    无法使用如下的字节头接收来确认网络传输,总是报错为最小
        *
        ***********************************************************************************************************/

        //private void ReceiveAsyncCallback(IAsyncResult ar)
        //{
        //    if (ar.AsyncState is AsyncStateOne state)
        //    {
        //        try
        //        {
        //            state.AlreadyReceivedHead += state.WorkSocket.EndReceiveFrom(ar, ref state.UdpEndPoint);
        //            if (state.AlreadyReceivedHead < state.HeadLength)
        //            {
        //                //接续接收头数据
        //                WorkSocket.BeginReceiveFrom(state.BytesHead, state.AlreadyReceivedHead, state.HeadLength - state.AlreadyReceivedHead, SocketFlags.None,
        //                    ref state.UdpEndPoint, new AsyncCallback(ReceiveAsyncCallback), state);
        //            }
        //            else
        //            {
        //                //开始接收内容
        //                int ReceiveLenght = BitConverter.ToInt32(state.BytesHead, 4);
        //                if (ReceiveLenght > 0)
        //                {
        //                    state.BytesContent = new byte[ReceiveLenght];
        //                    WorkSocket.BeginReceiveFrom(state.BytesContent, state.AlreadyReceivedContent, state.BytesContent.Length - state.AlreadyReceivedContent,
        //                        SocketFlags.None, ref state.UdpEndPoint, new AsyncCallback(ContentReceiveAsyncCallback), state);
        //                }
        //                else
        //                {
        //                    //没有内容了
        //                    ThreadDealWithReveice(state, BitConverter.ToInt32(state.BytesHead, 0), state.BytesContent);
        //                    state = null;
        //                    RefreshReceive();
        //                }
        //            }
        //        }
        //        catch(Exception ex)
        //        {
        //            LogHelper.SaveError(StringResources.异步数据结束挂起发送出错, ex);
        //        }


        //    }
        //}

        //private void ContentReceiveAsyncCallback(IAsyncResult ar)
        //{
        //    if (ar.AsyncState is AsyncStateOne state)
        //    {
        //        try
        //        {
        //            state.AlreadyReceivedContent += state.WorkSocket.EndReceiveFrom(ar, ref state.UdpEndPoint);
        //            if (state.AlreadyReceivedContent < state.BytesContent.Length)
        //            {
        //                //还需要继续接收
        //                WorkSocket.BeginReceiveFrom(state.BytesContent, state.AlreadyReceivedContent, state.BytesContent.Length - state.AlreadyReceivedContent,
        //                        SocketFlags.None, ref state.UdpEndPoint, new AsyncCallback(ContentReceiveAsyncCallback), state);
        //            }
        //            else
        //            {
        //                //接收完成了
        //                ThreadDealWithReveice(state, BitConverter.ToInt32(state.BytesHead, 0), new byte[0]);
        //                state = null;
        //                RefreshReceive();
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            LogHelper.SaveError(StringResources.异步数据结束挂起发送出错, ex);
        //        }


        //    }
        //}

        #region 数据中心处理块

        /// <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)
        {
            LogNet?.WriteDebug(LogHeaderText, "Protocol:" + protocol + " customer:" + customer + " ip:" + receive.GetRemoteEndPoint().Address.ToString());
            if (protocol == HslCommunicationCode.Hsl_Protocol_User_Bytes)
            {
                AcceptByte?.Invoke(receive, customer, content);
            }
            else if (protocol == HslCommunicationCode.Hsl_Protocol_User_String)
            {
                //接收到文本数据
                string str = Encoding.Unicode.GetString(content);
                AcceptString?.Invoke(receive, customer, str);
            }
        }
Beispiel #7
0
        private void ContentReceiveCallBack(IAsyncResult ar)
        {
            if (ar.AsyncState is DeviceState stateone)
            {
                try
                {
                    int count = stateone.WorkSocket.EndReceive(ar);

                    if (count > 0)
                    {
                        MemoryStream ms   = new MemoryStream( );
                        byte         next = stateone.Buffer[0];

                        while (next != endByte)
                        {
                            ms.WriteByte(next);
                            byte[] buffer = new byte[1];
                            stateone.WorkSocket.Receive(buffer, 0, 1, SocketFlags.None);
                            next = buffer[0];
                        }

                        // 接收完成
                        stateone.WorkSocket.BeginReceive(stateone.Buffer, 0, stateone.Buffer.Length, SocketFlags.None,
                                                         new AsyncCallback(ContentReceiveCallBack), stateone);


                        byte[] receive = ms.ToArray( );
                        ms.Dispose( );

                        lock_list.Enter( );
                        stateone.ReceiveTime = DateTime.Now;
                        lock_list.Leave( );
                        AcceptBytes?.Invoke(stateone, receive);
                        AcceptString?.Invoke(stateone, Encoding.ASCII.GetString(receive));
                    }
                    else
                    {
                        RemoveClient(stateone);
                        LogNet?.WriteInfo(ToString( ), StringResources.Language.NetClientOffline);
                    }
                }
                catch (Exception ex)
                {
                    //登录前已经出错
                    RemoveClient(stateone);
                    LogNet?.WriteException(ToString( ), StringResources.Language.NetClientLoginFailed, ex);
                }
            }
        }
        private void ContentReceiveCallBack(IAsyncResult ar)
        {
            if (ar.AsyncState is DeviceState stateone)
            {
                try
                {
                    int count = stateone.WorkSocket.EndReceive(ar);

                    if (count > 0)
                    {
                        MemoryStream ms   = new MemoryStream( );
                        byte         next = stateone.Buffer[0];

                        while (next != endByte)
                        {
                            ms.WriteByte(next);
                            next = NetSupport.ReadBytesFromSocket(stateone.WorkSocket, 1)[0];
                        }

                        // 接收完成
                        stateone.WorkSocket.BeginReceive(stateone.Buffer, 0, stateone.Buffer.Length, SocketFlags.None,
                                                         new AsyncCallback(ContentReceiveCallBack), stateone);

                        string read = Encoding.ASCII.GetString(ms.ToArray( ));
                        ms.Dispose( );

                        lock_list.Enter( );
                        stateone.ReceiveTime = DateTime.Now;
                        lock_list.Leave( );
                        AcceptString?.Invoke(stateone, read);
                    }
                    else
                    {
                        stateone.WorkSocket.BeginReceive(stateone.Buffer, 0, stateone.Buffer.Length, SocketFlags.None,
                                                         new AsyncCallback(ContentReceiveCallBack), stateone);
                    }
                }
                catch (Exception ex)
                {
                    //登录前已经出错
                    RemoveClient(stateone);
                    LogNet?.WriteException(LogHeaderText, StringResources.NetClientLoginFailed, ex);
                }
            }
        }