예제 #1
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="data"></param>
        /// <param name="onUploaded"></param>
        internal void Upload(ref SubArray <byte> data, ref Func <ReturnValue <SynchronousState>, bool> onUploaded)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                this.onUploaded     = onUploaded;
                this.data           = data;
                checkTimeoutSeconds = AutoCSer.Date.NowTime.CurrentSeconds;
                onUploaded          = null;

                if (onWriteHandle == null)
                {
                    onWriteHandle = onWrite;
                }
                fileStream.BeginWrite(data.Array, data.Start, data.Length, onWriteHandle, this);
                state = SynchronousState.Asynchronous;
            }
            finally
            {
                if (state != SynchronousState.Asynchronous)
                {
                    remove(state);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <returns>文件同步状态</returns>
        internal SynchronousState Download()
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (fileInfo.Exists)
                {
                    client.IClient.CreateDownload(path, new ListFileItem(name, fileInfo.LastWriteTimeUtc, fileStream.Length), onCreated);
                }
                else
                {
                    client.IClient.CreateDownload(path, new ListFileItem(name, default(DateTime), 0), onCreated);
                }
                state = SynchronousState.Asynchronous;
            }
            finally
            {
                if (state != SynchronousState.Asynchronous)
                {
                    client.Remove(ref FileNameKey);
                }
            }
            return(state);
        }
예제 #3
0
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="tick">时钟周期标识</param>
        /// <param name="identity">文件编号</param>
        /// <param name="onDownload">下载回调处理</param>
        public void Download(long tick, long identity, Func <ReturnValue <DownloadData>, bool> onDownload)
        {
            SynchronousState state = SynchronousState.IdentityError;

            try
            {
                if (tick == AutoCSer.Pub.StartTime.Ticks)
                {
                    ServerFile serverFile;
                    Monitor.Enter(fileLock);
                    if (files.TryGetValue(identity, out serverFile))
                    {
                        Monitor.Exit(fileLock);
                        state = SynchronousState.ServerException;
                        new UnionType {
                            Value = serverFile
                        }.ServerDownloadFile.Download(ref onDownload);
                    }
                    else
                    {
                        Monitor.Exit(fileLock);
                    }
                }
            }
            finally
            {
                if (onDownload != null)
                {
                    onDownload(new DownloadData(state));
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="onDownload">下载回调处理</param>
        internal void Download(ref Func <ReturnValue <DownloadData>, bool> onDownload)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                this.onDownload     = onDownload;
                checkTimeoutSeconds = AutoCSer.Date.NowTime.CurrentSeconds;
                onDownload          = null;

                if (onReadHandle == null)
                {
                    onReadHandle = onRead;
                }
                fileStream.BeginRead(buffer, 0, readSize = (int)Math.Min(fileStream.Length - index, buffer.Length), onReadHandle, this);
                state = SynchronousState.Asynchronous;
            }
            finally
            {
                if (state != SynchronousState.Asynchronous)
                {
                    remove(new DownloadData(state));
                }
            }
        }
예제 #5
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="fileIdentity">服务端返回文件信息</param>
        /// <param name="data">文件数据</param>
        /// <param name="onUploaded">上传回调处理</param>
        public void Upload(ref UploadFileIdentity fileIdentity, ref SubArray <byte> data, Func <ReturnValue <SynchronousState>, bool> onUploaded)
        {
            SynchronousState state = SynchronousState.IdentityError;

            try
            {
                if (fileIdentity.Tick == AutoCSer.Pub.StartTime.Ticks)
                {
                    ServerFile serverFile;
                    Monitor.Enter(fileLock);
                    if (files.TryGetValue(fileIdentity.Identity, out serverFile))
                    {
                        Monitor.Exit(fileLock);
                        state = SynchronousState.ServerException;
                        new UnionType {
                            Value = serverFile
                        }.ServerUploadFile.Upload(ref data, ref onUploaded);
                    }
                    else
                    {
                        Monitor.Exit(fileLock);
                    }
                }
            }
            finally
            {
                if (onUploaded != null)
                {
                    onUploaded(state);
                }
            }
        }
예제 #6
0
        /// <summary>
        /// 文件上传创建回调处理
        /// </summary>
        /// <param name="fileIdentity"></param>
        private void onCreated(ReturnValue <UploadFileIdentity> fileIdentity)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (fileIdentity.Type == ReturnType.Success)
                {
                    this.fileIdentity = fileIdentity.Value;
                    if (this.fileIdentity.Tick != 0)
                    {
                        if (index != fileStream.Length)
                        {
                            read();
                            state = SynchronousState.Asynchronous;
                        }
                    }
                    else
                    {
                        state = this.fileIdentity.GetErrorState();
                    }
                }
                else
                {
                    state = SynchronousState.TcpError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #7
0
        /// <summary>
        /// 第一次读取文件操作
        /// </summary>
        /// <param name="result"></param>
        private void onReadFirst(IAsyncResult result)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                int size = fileStream.EndRead(result);
                if (size == readSize)
                {
                    if ((index += size) == fileStream.Length)
                    {
                        fileStream.Dispose();
                        fileStream = null;
                    }
                    client.IClient.CreateUpload(path, new ListFileItem(name, fileInfo.LastWriteTimeUtc, fileStream.Length), index - size, new SubArray <byte>(buffer, 0, size), onCreated);
                    state = SynchronousState.Asynchronous;
                }
                else
                {
                    state = SynchronousState.ReadError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #8
0
        /// <summary>
        /// 文件上传回调处理
        /// </summary>
        /// <param name="synchronousState"></param>
        private void onUploaded(ReturnValue <SynchronousState> synchronousState)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (synchronousState.Type == ReturnType.Success)
                {
                    if (synchronousState.Value == SynchronousState.Success)
                    {
                        if (index == fileStream.Length)
                        {
                            state = SynchronousState.Success;
                        }
                        else
                        {
                            read();
                            state = SynchronousState.Asynchronous;
                        }
                    }
                    else
                    {
                        state = synchronousState.Value;
                    }
                }
                else
                {
                    state = SynchronousState.TcpError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #9
0
        /// <summary>
        /// 文件读取回调处理
        /// </summary>
        /// <param name="result"></param>
        private void onReaded(IAsyncResult result)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                int size = fileStream.EndRead(result);
                if (size == readSize)
                {
                    if ((index += size) == fileStream.Length)
                    {
                        fileStream.Dispose();
                        fileStream = null;
                    }
                    client.IClient.Upload(fileIdentity, new SubArray <byte>(buffer, 0, size), onUploaded);
                    state = SynchronousState.Asynchronous;
                }
                else
                {
                    state = SynchronousState.ReadError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #10
0
        /// <summary>
        /// 开始创建文件
        /// </summary>
        internal void Start()
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (index == 0)
                {
                    fileStream = new FileStream(fileInfo.FullName, FileMode.Create, FileAccess.Write, FileShare.None, data.Length, FileOptions.Asynchronous);
                    fileStream.BeginWrite(data.Array, data.Start, data.Length, onWriteFirst, this);
                    state = SynchronousState.Asynchronous;
                }
                else if (fileInfo.Exists && index == fileInfo.Length)
                {
                    fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Write, FileShare.None, data.Length, FileOptions.Asynchronous);
                    if (index == fileStream.Length && fileInfo.LastWriteTimeUtc == listFileItem.LastWriteTime)
                    {
                        fileStream.Seek(0, SeekOrigin.End);
                        fileStream.BeginWrite(data.Array, data.Start, data.Length, onWriteFirst, this);
                        state = SynchronousState.Asynchronous;
                    }
                }
            }
            finally
            {
                if (state != SynchronousState.Asynchronous)
                {
                    removeCreate(state);
                }
            }
        }
예제 #11
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns>文件同步状态</returns>
        internal SynchronousState Upload()
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (fileInfo.Exists)
                {
                    client.IClient.Get(Path.Combine(path, name), onGet);
                    state = SynchronousState.Asynchronous;
                }
                else
                {
                    state = SynchronousState.NotExists;
                }
            }
            finally
            {
                if (state != SynchronousState.Asynchronous)
                {
                    client.Remove(ref FileNameKey);
                }
            }
            return(state);
        }
예제 #12
0
        /// <summary>
        /// 文件下载回调处理
        /// </summary>
        /// <param name="downloadData"></param>
        private void onDownload(ReturnValue <DownloadData> downloadData)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (downloadData.Type == ReturnType.Success)
                {
                    if (downloadData.Value.State == SynchronousState.Success)
                    {
                        SubArray <byte> data = downloadData.Value.Data;
                        fileStream.BeginWrite(data.Array, data.Start, data.Length, onWriteHandle, this);
                        state = SynchronousState.Asynchronous;
                    }
                    else
                    {
                        state = downloadData.Value.State;
                    }
                }
                else
                {
                    state = SynchronousState.TcpError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #13
0
        /// <summary>
        /// 文件首次写入回调处理
        /// </summary>
        /// <param name="result"></param>
        private void onWrite(IAsyncResult result)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                fileStream.EndWrite(result);
                fileInfo.LastWriteTimeUtc = fileIdentity.ListFileItem.LastWriteTime;
                if (fileStream.Length == fileIdentity.ListFileItem.Length)
                {
                    state = SynchronousState.Success;
                }
                else
                {
                    if (onDownloadHandle == null)
                    {
                        onDownloadHandle = onDownload;
                    }
                    client.IClient.Download(fileIdentity.Tick, fileIdentity.Identity, onDownload);
                    state = SynchronousState.Asynchronous;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #14
0
 /// <summary>
 /// 删除同步文件
 /// </summary>
 /// <param name="state">文件同步状态</param>
 private void removeCreate(SynchronousState state)
 {
     server.Remove(Identity);
     if (fileStream != null)
     {
         fileStream.Dispose();
     }
     onCreated(new UploadFileIdentity(state));
 }
예제 #15
0
 /// <summary>
 /// 删除同步文件
 /// </summary>
 /// <param name="state">文件同步状态</param>
 private void remove(SynchronousState state)
 {
     server.Remove(Identity);
     if (fileStream != null)
     {
         fileStream.Dispose();
     }
     onUploaded(state);
 }
예제 #16
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); }
        }
예제 #17
0
        /// <summary>
        /// 获取服务端文件信息回调处理
        /// </summary>
        /// <param name="serverFile"></param>
        private void onGet(ReturnValue <ListFileItem> serverFile)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                if (serverFile.Type == ReturnType.Success)
                {
                    if (serverFile.Value.Check(fileInfo))
                    {
                        state = SynchronousState.Success;
                    }
                    else if (fileInfo.Length != 0)
                    {
                        int bufferSize = (int)Math.Min(client.UploadDBufferSize, 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)
                            {
                                buffer = new byte[bufferSize];
                                if (fileStream.Length <= buffer.Length)
                                {
                                    fileStream.BeginRead(buffer, 0, bufferSize, onReadAll, this);
                                }
                                else
                                {
                                    if (serverFile.Value.LastWriteTime == fileInfo.LastWriteTimeUtc && serverFile.Value.Length < fileStream.Length)
                                    {
                                        fileStream.Seek(index = serverFile.Value.Length, SeekOrigin.Begin);
                                    }
                                    fileStream.BeginRead(buffer, 0, readSize = (int)Math.Min(fileStream.Length - index, bufferSize), onReadFirst, this);
                                }
                            }
                            else
                            {
                                client.IClient.UploadAll(Path.Combine(path, name), fileInfo.LastWriteTimeUtc, new SubArray <byte>(), onUploadedAll);
                            }
                            state = SynchronousState.Asynchronous;
                        }
                    }
                    else
                    {
                        client.IClient.UploadAll(Path.Combine(path, name), fileInfo.LastWriteTimeUtc, new SubArray <byte>(), onUploadedAll);
                        state = SynchronousState.Asynchronous;
                    }
                }
                else
                {
                    state = SynchronousState.TcpError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #18
0
 /// <summary>
 /// 删除同步文件
 /// </summary>
 /// <param name="state"></param>
 internal void Remove(SynchronousState state)
 {
     if (state != SynchronousState.Asynchronous)
     {
         client.Remove(ref FileNameKey);
         if (fileStream != null)
         {
             fileStream.Dispose();
         }
         if (onCompleted != null)
         {
             onCompleted(this, state);
         }
     }
 }
예제 #19
0
        /// <summary>
        /// 小文件读取回调处理
        /// </summary>
        /// <param name="result"></param>
        private void onReadAll(IAsyncResult result)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                int size = fileStream.EndRead(result);
                if (size == fileStream.Length)
                {
                    fileStream.Dispose();
                    fileStream = null;
                    client.IClient.UploadAll(Path.Combine(path, name), fileInfo.LastWriteTimeUtc, new SubArray <byte>(buffer, 0, size), onUploadedAll);
                    state = SynchronousState.Asynchronous;
                }
                else
                {
                    state = SynchronousState.ReadError;
                }
            }
            catch { }
            finally { Remove(state); }
        }
예제 #20
0
        /// <summary>
        /// 文件写入回调处理
        /// </summary>
        /// <param name="result"></param>
        private void onWrite(IAsyncResult result)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                fileStream.EndWrite(result);
                if ((index += data.Length) == listFileItem.Length)
                {
                    fileStream.Dispose();
                    fileStream = null;
                }
                if (fileInfo.LastWriteTimeUtc != listFileItem.LastWriteTime)
                {
                    fileInfo.LastWriteTimeUtc = listFileItem.LastWriteTime;
                }
                state = SynchronousState.Success;
            }
            catch { }
            finally
            {
                if (state == SynchronousState.Success && index != listFileItem.Length)
                {
                    checkTimeoutSeconds = AutoCSer.Date.NowTime.CurrentSeconds;
                    if (!onUploaded(state))
                    {
                        server.Remove(Identity);
                        fileStream.Dispose();
                    }
                }
                else
                {
                    remove(state);
                }
            }
        }
예제 #21
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="name">文件名称</param>
        /// <param name="onCompleted">文件同步完成处理委托</param>
        /// <returns>文件同步状态</returns>
        public SynchronousState Upload(string path, string name, Action <ClientFile, SynchronousState> onCompleted)
        {
            SynchronousState state = SynchronousState.Unknown;

            try
            {
                FileNameKey      fileNameKey = new FileNameKey(path.FileNameToLower(), name.FileNameToLower());
                ClientUploadFile uploadFile  = new ClientUploadFile(this, path, name, ref fileNameKey, onCompleted);
                state = synchronous(uploadFile, ref fileNameKey);
                if (state == SynchronousState.Success)
                {
                    onCompleted = null;
                    return(uploadFile.Upload());
                }
                return(state);
            }
            finally
            {
                if (onCompleted != null)
                {
                    onCompleted(null, state);
                }
            }
        }
예제 #22
0
        /// <summary>
        /// 创建文件下载
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="listFileItem">列表文件数据</param>
        /// <param name="onCreated">下载回调处理</param>
        public void CreateDownload(string path, ref ListFileItem listFileItem, Func <ReturnValue <DownloadFileIdentity>, bool> onCreated)
        {
            ServerDownloadFile downloadFile = null;
            SynchronousState   state        = SynchronousState.ServerException;

            try
            {
                FileInfo fileInfo = new FileInfo(System.IO.Path.Combine(System.IO.Path.Combine(Path, path), listFileItem.Name));
                if (fileInfo.Exists)
                {
                    downloadFile = new ServerDownloadFile(this, path, ref listFileItem, fileInfo, onCreated);
                    Monitor.Enter(fileLock);
                    try
                    {
                        files.Add(downloadFile.Identity, downloadFile);
                    }
                    finally { Monitor.Exit(fileLock); }
                    onCreated = null;
                }
                else
                {
                    state = SynchronousState.NotExists;
                }
            }
            finally
            {
                if (onCreated == null)
                {
                    downloadFile.Start();
                }
                else
                {
                    onCreated(new DownloadFileIdentity(state));
                }
            }
        }
예제 #23
0
 /// <summary>
 /// 设置下载数据
 /// </summary>
 /// <param name="data">下载数据</param>
 /// <param name="size"></param>
 internal void Set(byte[] data, int size)
 {
     State = SynchronousState.Success;
     Data.Set(data, 0, size);
 }
예제 #24
0
 /// <summary>
 /// 下载数据
 /// </summary>
 /// <param name="state">文件同步状态</param>
 internal DownloadData(SynchronousState state)
 {
     State = state;
     Data  = new SubArray <byte>();
 }
예제 #25
0
 /// <summary>
 /// 服务端返回下载文件信息
 /// </summary>
 /// <param name="state"></param>
 internal DownloadFileIdentity(SynchronousState state)
 {
     Identity = (byte)state;
 }
예제 #26
0
 /// <summary>
 /// 下载数据
 /// </summary>
 /// <param name="state">文件同步状态</param>
 internal DownloadData(SynchronousState state)
 {
     State = state;
     Data  = default(SubArray <byte>);
 }
예제 #27
0
 /// <summary>
 /// 服务端返回文件信息
 /// </summary>
 /// <param name="state"></param>
 internal UploadFileIdentity(SynchronousState state)
 {
     Tick     = 0;
     Identity = (byte)state;
 }