Ejemplo n.º 1
0
 /// <summary>
 /// 解析
 /// </summary>
 /// <param name="header"></param>
 /// <param name="br"></param>
 /// <param name="srcEndPoint"></param>
 public ResourceRequestMessage(CommonHeader header, BinaryReader br, System.Net.IPEndPoint srcEndPoint)
     : base(header, srcEndPoint)
 {
     this.ContextId = br.ReadInt32();
     this.Position  = br.ReadInt64();
     this.Size      = br.ReadInt32();
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 解析
 /// </summary>
 /// <param name="header"></param>
 /// <param name="br"></param>
 /// <param name="srcEndPoint"></param>
 public ResourceResponseMessage(CommonHeader header, BinaryReader br, System.Net.IPEndPoint srcEndPoint)
     : base(header, srcEndPoint)
 {
     this.ContextId = br.ReadInt32();
     this.Position  = br.ReadInt64();
     this.Size      = br.ReadInt32();
     //此处不再拷贝数据,在写数据时指定位置写,减少拷贝次数
     //if (this.Size > 0)
     //{
     //    this.Data = new byte[this.Size];
     //    br.Read(this.Data, 0, this.Data.Length);
     //}
 }
        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="header"></param>
        /// <param name="br"></param>
        /// <param name="srcEndPoint"></param>
        public SendDataNotifyMessage(CommonHeader header, BinaryReader br, System.Net.IPEndPoint srcEndPoint)
            : base(header, srcEndPoint)
        {
            this.Priority     = br.ReadInt16();
            this.ResourceType = br.ReadByte();
            this.Timeout      = br.ReadInt32();
            Int16 fileNameLen = br.ReadInt16();

            if (fileNameLen > 0)
            {
                byte[] fileNameBuf = new byte[fileNameLen];
                br.Read(fileNameBuf, 0, fileNameBuf.Length);
                this.FileName = Encoding.UTF8.GetString(fileNameBuf);
            }

            this.Size = br.ReadInt64();
            if (this.ResourceType == ResourceTypeConstant.Message)
            {
                this.Data = new byte[this.Size];
                br.Read(this.Data, 0, this.Data.Length);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 解析
 /// </summary>
 /// <param name="header"></param>
 /// <param name="br"></param>
 /// <param name="srcEndPoint"></param>
 public TransferCompletedAckMessage(CommonHeader header, BinaryReader br, System.Net.IPEndPoint srcEndPoint)
     : base(header, srcEndPoint)
 {
 }
Ejemplo n.º 5
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, "解析收到的数据异常");
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 解析
 /// </summary>
 /// <param name="header"></param>
 /// <param name="srcEndPoint"></param>
 public MessageBase(CommonHeader header, IPEndPoint srcEndPoint)
 {
     this.Header      = header;
     this.SrcEndPoint = srcEndPoint;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 创建
 /// </summary>
 /// <param name="header"></param>
 public MessageBase(CommonHeader header)
 {
     this.Header = header;
 }