Ejemplo n.º 1
0
 /// <summary>
 /// 重新开始接收下一次的数据传递
 /// </summary>
 /// <param name="session">网络状态</param>
 /// <param name="isProcess">是否触发数据处理</param>
 internal void ReBeginReceiveHead(AppSession session, bool isProcess)
 {
     try
     {
         byte[] head = session.BytesHead, Content = session.BytesContent;
         session.Clear( );
         session.WorkSocket.BeginReceive(session.BytesHead, session.AlreadyReceivedHead, session.BytesHead.Length - session.AlreadyReceivedHead,
                                         SocketFlags.None, new AsyncCallback(HeadBytesReceiveCallback), session);
         // 检测是否需要数据处理
         if (isProcess)
         {
             // 校验令牌
             if (CheckRemoteToken(head))
             {
                 Content = HslProtocol.CommandAnalysis(head, Content);
                 int protocol = BitConverter.ToInt32(head, 0);
                 int customer = BitConverter.ToInt32(head, 4);
                 // 转移到数据中心处理
                 DataProcessingCenter(session, protocol, customer, Content);
             }
             else
             {
                 // 应该关闭网络通信
                 LogNet?.WriteWarn(ToString( ), StringResources.TokenCheckFailed);
                 AppSessionRemoteClose(session);
             }
         }
     }
     catch (Exception ex)
     {
         SocketReceiveException(session, ex);
         LogNet?.WriteException(ToString( ), ex);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 需要发送的底层数据
        /// </summary>
        /// <param name="send">需要发送的底层数据</param>
        /// <returns>带返回消息的结果对象</returns>
        private OperateResult <NetHandle, byte[]> ReadCustomerFromServerBase(byte[] send)
        {
            // 核心数据交互
            var read = ReadFromCoreServer(send);

            if (!read.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <NetHandle, byte[]>(read));
            }

            // 提炼数据信息
            byte[] headBytes    = new byte[HslProtocol.HeadByteLength];
            byte[] contentBytes = new byte[read.Content.Length - HslProtocol.HeadByteLength];

            Array.Copy(read.Content, 0, headBytes, 0, HslProtocol.HeadByteLength);
            if (contentBytes.Length > 0)
            {
                Array.Copy(read.Content, HslProtocol.HeadByteLength, contentBytes, 0, read.Content.Length - HslProtocol.HeadByteLength);
            }

            int customer = BitConverter.ToInt32(headBytes, 4);

            contentBytes = HslProtocol.CommandAnalysis(headBytes, contentBytes);
            return(OperateResult.CreateSuccessResult((NetHandle)customer, contentBytes));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// [自校验] 接收一条完整的同步数据,包含头子节和内容字节,基础的数据,如果结果异常,则结束通讯
        /// </summary>
        /// <param name="socket">套接字</param>
        /// <param name="timeout">超时时间设置,如果为负数,则不检查超时</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">result</exception>
        protected OperateResult <byte[], byte[]> ReceiveAndCheckBytes(Socket socket, int timeout)
        {
            // 30秒超时接收验证
            HslTimeOut hslTimeOut = new HslTimeOut( )
            {
                DelayTime    = timeout,
                IsSuccessful = false,
                StartTime    = DateTime.Now,
                WorkSocket   = socket,
            };

            if (timeout > 0)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolCheckTimeOut), hslTimeOut);
            }

            // 接收头指令
            OperateResult <byte[]> headResult = Receive(socket, HslProtocol.HeadByteLength);

            if (!headResult.IsSuccess)
            {
                hslTimeOut.IsSuccessful = true;
                return(OperateResult.CreateFailedResult <byte[], byte[]>(headResult));
            }
            hslTimeOut.IsSuccessful = true;

            // 检查令牌
            if (!CheckRemoteToken(headResult.Content))
            {
                socket?.Close( );
                return(new OperateResult <byte[], byte[]>( )
                {
                    Message = StringResources.TokenCheckFailed
                });
            }

            int contentLength = BitConverter.ToInt32(headResult.Content, HslProtocol.HeadByteLength - 4);
            // 接收内容
            OperateResult <byte[]> contentResult = Receive(socket, contentLength);

            if (!contentResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[], byte[]>(contentResult));
            }

            // 返回成功信息
            OperateResult checkResult = SendLong(socket, HslProtocol.HeadByteLength + contentLength);

            if (!checkResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[], byte[]>(checkResult));
            }

            byte[] head    = headResult.Content;
            byte[] content = contentResult.Content;
            content = HslProtocol.CommandAnalysis(head, content);
            return(OperateResult.CreateSuccessResult(head, content));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 需要发送的底层数据
        /// </summary>
        /// <param name="headcode">数据的指令头</param>
        /// <param name="customer">用户的指令头</param>
        /// <param name="send">需要发送的底层数据</param>
        /// <returns>带返回消息的结果对象</returns>
        private OperateResult <byte[]> ReadFromServerBase(int headcode, int customer, byte[] send)
        {
            var read = ReadFromCoreServer(HslProtocol.CommandBytes(headcode, customer, Token, send));

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

            byte[] headBytes    = new byte[HslProtocol.HeadByteLength];
            byte[] contentBytes = new byte[read.Content.Length - HslProtocol.HeadByteLength];

            Array.Copy(read.Content, 0, headBytes, 0, HslProtocol.HeadByteLength);
            if (contentBytes.Length > 0)
            {
                Array.Copy(read.Content, HslProtocol.HeadByteLength, contentBytes, 0, read.Content.Length - HslProtocol.HeadByteLength);
            }

            contentBytes = HslProtocol.CommandAnalysis(headBytes, contentBytes);
            return(OperateResult.CreateSuccessResult(contentBytes));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 需要发送的底层数据
        /// </summary>
        /// <param name="send">需要发送的底层数据</param>
        /// <returns>带返回消息的结果对象</returns>
        private OperateResult <byte[]> ReadFromServerBase(byte[] send)
        {
            // 核心数据交互
            var read = ReadFromCoreServer(send);

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

            // 提炼数据信息
            byte[] headBytes    = new byte[HslProtocol.HeadByteLength];
            byte[] contentBytes = new byte[read.Content.Length - HslProtocol.HeadByteLength];

            Array.Copy(read.Content, 0, headBytes, 0, HslProtocol.HeadByteLength);
            if (contentBytes.Length > 0)
            {
                Array.Copy(read.Content, HslProtocol.HeadByteLength, contentBytes, 0, read.Content.Length - HslProtocol.HeadByteLength);
            }

            contentBytes = HslProtocol.CommandAnalysis(headBytes, contentBytes);
            return(OperateResult.CreateSuccessResult(contentBytes));
        }
        private void AsyncCallback(IAsyncResult ar)
        {
            if (ar.AsyncState is AppSession session)
            {
                try
                {
                    int received = session.WorkSocket.EndReceiveFrom(ar, ref session.UdpEndPoint);
                    // 释放连接关联
                    session.WorkSocket = null;
                    // 马上开始重新接收,提供性能保障
                    RefreshReceive( );
                    // 处理数据
                    if (received >= HslProtocol.HeadByteLength)
                    {
                        // 检测令牌
                        if (CheckRemoteToken(session.BytesContent))
                        {
                            session.IpEndPoint = (IPEndPoint)session.UdpEndPoint;
                            int contentLength = BitConverter.ToInt32(session.BytesContent, HslProtocol.HeadByteLength - 4);
                            if (contentLength == received - HslProtocol.HeadByteLength)
                            {
                                byte[] head    = new byte[HslProtocol.HeadByteLength];
                                byte[] content = new byte[contentLength];

                                Array.Copy(session.BytesContent, 0, head, 0, HslProtocol.HeadByteLength);
                                if (contentLength > 0)
                                {
                                    Array.Copy(session.BytesContent, 32, content, 0, contentLength);
                                }

                                // 解析内容
                                content = HslProtocol.CommandAnalysis(head, content);

                                int protocol = BitConverter.ToInt32(head, 0);
                                int customer = BitConverter.ToInt32(head, 4);
                                // 丢给数据中心处理
                                DataProcessingCenter(session, protocol, customer, content);
                            }
                            else
                            {
                                // 否则记录到日志
                                LogNet?.WriteWarn(ToString(), $"Should Rece:{(BitConverter.ToInt32( session.BytesContent, 4 ) + 8)} Actual:{received}");
                            }
                        }
                        else
                        {
                            LogNet?.WriteWarn(ToString( ), StringResources.Language.TokenCheckFailed);
                        }
                    }
                    else
                    {
                        LogNet?.WriteWarn(ToString( ), $"Receive error, Actual:{received}");
                    }
                }
                catch (ObjectDisposedException)
                {
                    //主程序退出的时候触发
                }
                catch (Exception ex)
                {
                    LogNet?.WriteException(ToString( ), StringResources.Language.SocketEndReceiveException, ex);
                    //重新接收,此处已经排除掉了对象释放的异常
                    RefreshReceive( );
                }
                finally
                {
                    //state = null;
                }
            }
        }