Ejemplo n.º 1
0
 private void NetRev(ReceiveDatagramInfo receiveDatagramInfo)
 {
     this._waitParseDatas.Add(receiveDatagramInfo);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 解析收到的数据队列线程方法
        /// </summary>
        /// <param name="receiveDatagramInfo">要解析的数据</param>
        private void ProReceiveData(ReceiveDatagramInfo receiveDatagramInfo)
        {
            try
            {
                var data = receiveDatagramInfo.Data;
                if (data == null)
                {
                    return;
                }

                if (data.Length < TransferConstant.COMMON_HEADER_SIZE)
                {
                    Loger.Warn($"收到的数据长度[{data.Length}]小于公共头[{TransferConstant.COMMON_HEADER_SIZE}],忽略...");
                    return;
                }

                var srcEndPoint = receiveDatagramInfo.SrcEndPoint;
                using (var ms = new MemoryStream(data))
                {
                    var br = new BinaryReader(ms);

                    #region 数据验证
                    Int32 sync = br.ReadInt32();// 同步字
                    if (sync != TransferConstant.SYNC)
                    {
                        throw new Exception($"收到数据的同步头:{sync}与期望的同步头:{TransferConstant.SYNC}不一致,忽略");
                    }

                    Int32 packageLen = br.ReadInt32();// 本次传输数据总长度
                    if (packageLen != data.Length)
                    {
                        throw new Exception($"收到数据的长度:{data.Length}与期望的数据长度:{packageLen}不一致,忽略");
                    }

                    var   p         = br.BaseStream.Position;
                    Int32 validCode = br.ReadInt32();//校验码,以后再验证

                    //重置校验码为填充值
                    byte[] validCodeBuf = BitConverter.GetBytes(TransferConstant.VALID_CODE_FILL);
                    br.BaseStream.Seek(p, SeekOrigin.Begin);
                    br.BaseStream.Write(validCodeBuf, 0, validCodeBuf.Length);

                    //校验数据传输正确性
                    bool parseResult = ProtocolParser.ValidData(validCode, data);
                    if (!parseResult)
                    {
                        Loger.Warn("数据正确性校验不通过");
                        return;
                    }
                    #endregion

                    var header = new CommonHeader(br);
                    switch (header.Cmd)
                    {
                    case TransferCommands.SendNotify:
                        this._receiver.ProSendNotify(new SendDataNotifyMessage(header, br, srcEndPoint));
                        break;

                    case TransferCommands.ResourceResponse:
                        this._receiver.ProResourceResponse(new ResourceResponseMessage(header, br, srcEndPoint), data);
                        break;

                    case TransferCommands.TransferCompletedAck:
                        this._receiver.ProTransferCompletedAck(new TransferCompletedAckMessage(header, br, srcEndPoint));
                        break;

                    case TransferCommands.ResourceRequest:
                        this.ProResourceRequest(new ResourceRequestMessage(header, br, srcEndPoint));
                        break;

                    case TransferCommands.TransferCompleted:
                        this.ProTransferCompleted(new TransferCompletedMessage(header, br, srcEndPoint));
                        break;

                    default:
                        throw new Exception($"未知命令{header.Cmd}");
                    }
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex, "解析收到的数据异常");
            }
        }