Exemplo n.º 1
0
 /// <summary>
 /// 删除同步文件
 /// </summary>
 /// <param name="fileIdentity">服务端返回下载文件信息</param>
 private void removeCreate(DownloadFileIdentity fileIdentity)
 {
     server.Remove(Identity);
     if (fileStream != null)
     {
         fileStream.Dispose();
     }
     onCreated(fileIdentity);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 文件下载创建回调处理
        /// </summary>
        /// <param name="fileIdentity"></param>
        private void onCreated(ReturnValue <DownloadFileIdentity> fileIdentity)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (fileIdentity.Type == ReturnType.Success)
                {
                    if (fileIdentity.Value != null)
                    {
                        this.fileIdentity = fileIdentity.Value;
                        if (this.fileIdentity.Tick == 0)
                        {
                            state = this.fileIdentity.GetErrorState();
                            if (state == SynchronousState.Success)
                            {
                                state = SynchronousState.Unknown;
                                byte[] data = this.fileIdentity.Data;
                                if (data == null)
                                {
                                    if (this.fileIdentity.ListFileItem.Length == 0)
                                    {
                                        fileStream = new FileStream(fileInfo.FullName, FileMode.Create, FileAccess.Write, FileShare.None, 1);
                                        fileStream.Dispose();
                                        fileStream = null;
                                        fileInfo.LastWriteTimeUtc = this.fileIdentity.ListFileItem.LastWriteTime;
                                        state = SynchronousState.Success;
                                    }
                                    else
                                    {
                                        if (fileInfo.Exists && this.fileIdentity.ListFileItem.Check(fileInfo))
                                        {
                                            state = SynchronousState.Success;
                                        }
                                    }
                                }
                                else
                                {
                                    state = writeFrist();
                                }
                            }
                        }
                        else
                        {
                            state = writeFrist();
                        }
                    }
                }
                else
                {
                    state = SynchronousState.TcpError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 小文件读取回调处理
        /// </summary>
        /// <param name="result"></param>
        private void onReadAll(IAsyncResult result)
        {
            DownloadFileIdentity fileIdentity = null;

            try
            {
                fileIdentity = fileStream.EndRead(result) == fileStream.Length ? new DownloadFileIdentity(fileInfo, buffer) : new DownloadFileIdentity(SynchronousState.ReadError);
            }
            catch { }
            finally { removeCreate(fileIdentity); }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 开始创建文件
        /// </summary>
        internal void Start()
        {
            DownloadFileIdentity fileIdentity = null;
            bool isAsynchronous = false;

            try
            {
                int bufferSize = (int)Math.Min(server.DownloadBufferSize, fileInfo.Length);
                fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
                if (fileStream.Length == fileInfo.Length)
                {
                    if (fileStream.Length != 0 && !listFileItem.Check(fileInfo))
                    {
                        buffer = new byte[bufferSize];
                        if (fileStream.Length <= buffer.Length)
                        {
                            fileStream.BeginRead(buffer, 0, bufferSize, onReadAll, this);
                        }
                        else
                        {
                            if (listFileItem.LastWriteTime == fileInfo.LastWriteTimeUtc && listFileItem.Length < fileStream.Length)
                            {
                                fileStream.Seek(index = listFileItem.Length, SeekOrigin.Begin);
                            }
                            fileStream.BeginRead(buffer, 0, readSize = (int)Math.Min(fileStream.Length - index, bufferSize), onReadFirst, this);
                        }
                        isAsynchronous = true;
                    }
                    else
                    {
                        fileIdentity = new DownloadFileIdentity(fileInfo);
                    }
                }
            }
            finally
            {
                if (!isAsynchronous)
                {
                    removeCreate(fileIdentity);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 第一次读取文件操作
        /// </summary>
        /// <param name="result"></param>
        private void onReadFirst(IAsyncResult result)
        {
            DownloadFileIdentity fileIdentity = null;

            try
            {
                if (fileStream.EndRead(result) == readSize)
                {
                    if ((index += readSize) == fileStream.Length)
                    {
                        fileIdentity = new DownloadFileIdentity(fileInfo, buffer, index - readSize, index);
                    }
                    else
                    {
                        fileIdentity = new DownloadFileIdentity(Identity, fileInfo, buffer, index - readSize, index);
                    }
                }
                else
                {
                    fileIdentity = new DownloadFileIdentity(SynchronousState.ReadError);
                }
            }
            catch { }
            finally
            {
                if (fileIdentity != null && fileIdentity.Tick != 0)
                {
                    checkTimeoutSeconds = AutoCSer.Date.NowTime.CurrentSeconds;
                    onCreated(fileIdentity);
                }
                else
                {
                    removeCreate(fileIdentity);
                }
            }
        }