예제 #1
0
 void SendFile(String fileName, String root)
 {
     FileFormat entity = new FileFormat(fileName, root);
     MemoryStream format = new MemoryStream();
     entity.Write(format);
     format.Position = 0;
     Client.Send(format);
     Client.Send(entity.Stream);
 }
예제 #2
0
 /// <summary>
 /// 从流中加载一个文件格式对象
 /// </summary>
 /// <param name="stream"></param>
 /// <returns></returns>
 public static FileFormat Load(Stream stream)
 {
     FileFormat ff = new FileFormat();
     ff.Read(stream);
     return ff;
 }
예제 #3
0
        /// <summary>处理收到的数据</summary>
        /// <param name="e"></param>
        protected override void OnReceive(ReceivedEventArgs e)
        {
            //base.OnReceive(e);

            var stream = e.Packet.GetStream();

            // 第一个数据包解析头部
            if (e.Packet.Total > 0 && Inf == null)
            {
                var fi = new FileFormat();
                try
                {
                    fi.Read(stream);

                    if (fi.Checksum != fi.Crc)
                    {
                        throw new XException("文件{0}校验和错误{1:X8}!={2:X8}!", fi.Name, fi.Checksum, fi.Crc);
                    }
                }
                catch (Exception ex)
                {
                    WriteError("无法解析文件头!{0}", ex.Message);
                    // 如果加载失败,则关闭会话
                    Dispose();
                    return;
                }
                Inf    = fi;
                Length = 0;
                if (StartTime == DateTime.MinValue)
                {
                    StartTime = Session.StartTime;
                }

                // 加大网络缓冲区
                Session.Client.ReceiveBufferSize = 8 * 1024 * 1024;

                var file = Host.SavedPath.CombinePath(Inf.Name).EnsureDirectory();
                Stream = file.AsFile().OpenWrite();
                WriteLog("接收{0},{1:n0}kb", Inf.Name, Inf.Length / 1024);

                if (stream.Position >= stream.Length)
                {
                    return;
                }
            }

            var len = stream.Length - stream.Position;

            if (len > 0)
            {
                Length += len;
                var ms    = (DateTime.Now - StartTime).TotalMilliseconds;
                var speed = Length / 1024 / ms * 1000;
                WriteLog("收到{0:n0}字节,{1:n0}kb/s,完成{2:p}", len, (Int32)speed, (Double)Length / Inf.Length);
                if (Stream != null && Stream.CanWrite)
                {
                    Stream.Write(stream);
                }
                //}
                //else
                //{
                if (Length >= Inf.Length)
                {
                    //var ms = (DateTime.Now - StartTime).TotalMilliseconds;
                    //var speed = Length / 1024 / ms * 1000;
                    WriteLog("{0}接收完成,{1:n0}ms,{2:n0}kb/s", Inf.Name, (Int32)ms, (Int32)speed);
                    //Dispose();

                    // 清空,方便接收下一个文件
                    Inf = null;
                    CloseStream();
                    StartTime = DateTime.MinValue;
                }
            }
        }